bkr

tutorial

Your first HTTP server

Install bkr, write a small TypeScript server, and run it with scoped network and file permissions.

Install bkr

Install a prebuilt binary:

curl https://boomkat.run/install | sh

Building from source instead? See Building from source. Substitute ./out/bkr for bkr in the commands below.

1. Write a server

Create server.ts:

import { serve } from "http";

serve({
  hostname: "127.0.0.1",
  port: 8080,
  routes: {
    "/health": new Response("OK"),
    "/hello/:name": (req) => Response.json({ hello: req.params.name }),
  },
});

console.log("Listening on http://127.0.0.1:8080");

"/health" passes a static Response directly. bkr serializes it once at startup and serves it from native code without entering JS on each request.

2. Check required permissions

bkr check scans static imports and tells you which flags a script will need:

bkr check server.ts
1 modules scanned
  http -> net
try: --allow-net

3. Start the server

Nothing is allowed by default. Scope the network grant to 127.0.0.1:8080 so the script can only bind that address:

bkr --allow-net=127.0.0.1:8080 server.ts

In another terminal:

curl http://127.0.0.1:8080/health
curl http://127.0.0.1:8080/hello/margay
OK
{"hello":"margay"}

4. Stream a file from disk

Create a text file to serve:

printf "hello from disk\n" > note.txt

Add a /note route to server.ts using body() from the file module. It streams the file in 64 KiB chunks with socket backpressure instead of buffering the whole file in memory:

import { serve } from "http";
import { body } from "file";

serve({
  hostname: "127.0.0.1",
  port: 8080,
  routes: {
    "/health": new Response("OK"),
    "/hello/:name": (req) => Response.json({ hello: req.params.name }),
    "/note": () => new Response(body("./note.txt")),
  },
});

Restart the server with read access to note.txt:

bkr --allow-net=127.0.0.1:8080 --allow-read=./note.txt server.ts
curl http://127.0.0.1:8080/note
hello from disk

Next