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.

terminal
$ npm install --global esbuild
$ c++ --version
$ git --version

Export one handler

The default export is an object with a fetch function and a port. Return a Response.

server.ts
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 });
  }
};
terminal
$ porf server.ts server
$ ./server
# from another shell
$ curl http://127.0.0.1:3000/health

Work 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.

Imports

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.

terminal
$ porf --event-loop server.ts server

Design for today's limits

SurfaceCurrent boundary
Bind127.0.0.1; use a reverse proxy for public ingress.
TLSTerminate HTTPS before the Porffor process.
BodiesRequests top out at 1 MiB; larger bodies receive 413.
ResponsesText bodies are sent in one piece; binary and streaming responses are not ready.
ProtocolsWebSockets are not available yet.
Outbound HTTPClient-side fetch() is not implemented.