architecture
Runtime architecture
How bkr wires the boomkat JS engine to uWebSockets, the native thread pool, and OS resources.
Single binary, embedded JS layer
bkr compiles the boomkat engine from source alongside its C3 runtime code into one executable. At startup, it creates a JS runtime and context, attaches native callbacks to an internal bridge object, evaluates the embedded web-standard prelude (Fetch, Streams, WebSocket, Worker, timers, events), and registers the built-in modules.
Async calls cross from JS into C3 by allocating a 32-bit operation ID paired with a rooted promise capability. When the native side finishes on the event loop, it resolves or rejects that ID, drains microtasks, and checks for unhandled rejections.
Event loop, thread pool, and resource table
Each execution thread owns its own runtime state and uSockets event loop (kqueue on macOS, epoll on Linux). Non-blocking TCP, TLS, HTTP, and timer events run directly on that loop.
Blocking work—filesystem calls, getaddrinfo DNS lookups, waitpid, Argon2id hashing, and crypto.subtle operations—runs on a shared 16-thread native pool and posts completions back to the originating loop.
Streamable handles (stdio, open files, sockets, listeners, child pipes) live in a per-runtime resource table. Each handle ID packs a 12-bit generation counter above a 20-bit slot index so a stale ID after close() never aliases a recycled slot.
HTTP fast paths
The HTTP server wraps uWebSockets through a thin C++ shim in shim/. Common cases skip JS allocations where possible:
- Static routes: Buffered
Responseobjects passed toserve({ routes, static })are serialized into wire headers and body bytes once at startup and served from C3 without entering the VM. - Lazy requests and plain 200s: Incoming
Requestobjects allocate a pre-shaped object; URL and header strings are only copied out of the uWebSockets buffer if accessed or if the request outlives the callback. Simple 200 responses with string, byte, orfile.body()payloads write straight from C3. - Stream batching and file bodies: Untouched
ReadableStreamresponses pull through a native sink in 64 KiB batches; streams that finish within the first batch send in one call with a known content length.file.body(path)reads 64 KiB chunks on the thread pool and pauses under socket backpressure.
Concurrency
Boomkat VMs are single-threaded and share no mutable JS heap across threads:
- Module workers (
new Worker): Each worker runs on its own OS thread with a fresh VM and uSockets loop. Messages serialize value graphs as JSON metadata plus copied byte buffers. - HTTP workers (
--http-workers=N): SpawnsN - 1sibling threads that re-evaluate the entry module and join the main server's acceptor pool; connections go round-robin. - HTTP processes (
--http-processes=N): A native supervisor binds the TCP port before any VM starts, forksNworker processes, and passes accepted sockets round-robin over Unix domain sockets viaSCM_RIGHTS.
Optional dependencies and non-goals
On macOS, OpenSSL 3 is weakly linked and SQLite 3.31+ is loaded with dlopen. Plain HTTP and scripts that don't use TLS or SQLite run without either library installed; calling serve({ tls }), https:/wss:, or crypto.subtle without OpenSSL 3 throws with an install hint. On Linux, libssl.so.3 is required at startup.
bkr does not include a Node.js compatibility layer, CommonJS require, node_modules resolution, or remote HTTP imports. Every import specifier is a relative path, an absolute path, or a built-in module name.
Source layout
| Path | Role |
|---|---|
runtime/main.c3, state.c3 | CLI entry point, argument parsing, bootstrap, thread-local runtime state |
runtime/bridge.c3, guard.c3 | C3/JS marshalling, async op table, rejection tracker, --time-limit watchdog |
runtime/loop.c3, timers.c3, immediates.c3 | uSockets loop glue, nanosecond timer heap, setImmediate queue, 16-thread pool |
runtime/http.c3, ws.c3, file_body.c3, stream_sink.c3 | HTTP server, native route table, WebSocket upgrades, streaming file and sink paths |
runtime/net.c3, tls.c3, res.c3 | TCP/TLS sockets, DNS resolution, OpenSSL verification, generation-tagged resource table |
runtime/fs.c3, proc.c3, sqlite.c3 | Filesystem tasks, environment and child processes, SQLite 3.31+ loader and authorizer |
runtime/crypto.c3, subtle.c3, transpile.c3 | Argon2id pool tasks, OpenSSL Web Crypto natives, TypeScript/JSX transpiler and source maps |
runtime/perms.c3, sandbox.c3, toml.c3 | Permission matching, path canonicalization, interactive prompt, macOS Seatbelt profile, boomkat.toml |
runtime/js/*.js | Web-standard prelude and built-in ES module wrappers |
shim/ | C/C++ shim over uWebSockets, OpenSSL EVP, and SCM_RIGHTS socket passing |
Building from source
Requires a sibling checkout of ../boomkat, c3c (v0.8.5+), a C/C++ compiler, and OpenSSL 3:
bash scripts/build_uws.sh
c3c build bkr
This writes the release binary to out/bkr. project.json also defines bkr-opprof and bkr-opprof-th for builds with OP_PROFILE enabled (switch or threaded dispatch).