Serve HTTP.
Export a fetch-style handler. Porffor bundles it, connects it to uWebSockets, and emits a native server.
Prepare the build machine
Server builds add a native networking layer. They require esbuild, a C++20 compiler, Git, and Make. The first build fetches uWebSockets into ~/.cache/porffor/deps.
$ npm install --global esbuild
$ c++ --version
$ git --versionExport one handler
The default export is an object with a fetch function and a port. Return a Response.
export default {
port: 3000,
fetch(request: Request): Response {
const url = new URL(request.url);
if (url.pathname === '/health') {
return new Response('ok');
}
return new Response('not found', { status: 404 });
}
};$ porf server.ts server
$ ./server
# from another shell
$ curl http://127.0.0.1:3000/healthWork inside the boundary
Handlers can read request.url, request.method, headers, and request bodies through text(), json(), arrayBuffer(), or blob(). The server runtime also provides Headers, URL, Blob, TextEncoder, and TextDecoder.
Unlike a plain single-file build, server mode invokes esbuild first. Local files and compatible packages can therefore be imported.
Pending work needs the event loop
Enable it when the handler returns a promise that does not settle immediately, or when you use timers.
$ porf --event-loop server.ts serverDesign for today's limits
| Surface | Current boundary |
|---|---|
| Bind | 127.0.0.1; use a reverse proxy for public ingress. |
| TLS | Terminate HTTPS before the Porffor process. |
| Bodies | Requests top out at 1 MiB; larger bodies receive 413. |
| Responses | Text bodies are sent in one piece; binary and streaming responses are not ready. |
| Protocols | WebSockets are not available yet. |
| Outbound HTTP | Client-side fetch() is not implemented. |