Skip to content

Bun (N4) ​

Ready

The hub proposes this hook for the containers it fits, and nothing changes until a player approves the proposal (Hook proposals). The cheat sheet has the steps by hand.

For services running on Bun: Bun.serve applications (plain, Hono, Elysia, …) and Node-style services (Express and friends) running on Bun's node:http. A preload loads vulnboxd's core through bun:ffi, installs the N1 hooks for Bun's Node-compatible modules and wraps Bun's own APIs. It gets in config-only (a mounted bunfig.toml) or compose-only (--preload in the command, or BUN_OPTIONS on Bun ≥ 1.3.7).

Covers ​

Versions
Bun1.x with the bunfig or command-line preload; BUN_OPTIONS only on ≥ 1.3.7 (samples: 1.3.11)
C librarymusl and glibc builds of Bun (samples: the official musl release binary on Alpine)
ServersBun.serve (fetch handler, routes, WebSocket handlers); node:http on Bun (Express etc.)
APIsBun.spawn, Bun.spawnSync, Bun.connect; through N1: node:child_process, node:fs (sync), node:net

What the kernel already links without a hook: nothing — Bun serves every request on its JavaScript thread.

How it gets in ​

From a registry (local development, or a build with network; the same release as the hook directory): bun add vbxtrace@1.0.0, then bun --preload vbxtrace/bun app.ts; see Local development.

Three ways, least invasive first; the hub picks one per service.

Config-only (template hooks/node/n4/proposals/config-bunfig.toml), when the service has nobunfig.toml of its own (the mount would hide it):

diff
 services:
   <service>:
+    volumes:
+      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
+      - /usr/local/lib/vulnboxd/trace/bun/bunfig.toml:<workdir>/bunfig.toml:ro

The mounted file is preload = ["/opt/vbxtrace/bun/register.ts"]; <workdir> is the image's WORKDIR (or the compose working_dir). Bun reads bunfig.toml from the working directory on every 1.x version.

Compose-only, command (template compose-preload-command.toml), any Bun 1.x — --preload right after bun in the service's command:

diff
 services:
   <service>:
+    volumes:
+      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
-    command: ["bun", "run", "server.ts"]
+    command: ["bun", "--preload", "/opt/vbxtrace/bun/register.ts", "run", "server.ts"]

(When the image's CMD starts the service, the hub copies it into command with the flag added.)

Compose-only, environment (template compose-bun-options.toml) — only Bun ≥ 1.3.7:

diff
 services:
   <service>:
+    volumes:
+      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
+    environment:
+      BUN_OPTIONS: --preload /opt/vbxtrace/bun/register.ts

BUN_OPTIONS on older Bun stops the service

On Bun up to 1.3.6 (verified: 1.2.23, 1.3.0, 1.3.5, 1.3.6) BUN_OPTIONS="--preload …" runs the preload and then exits 0 without running the service. Verified working on 1.3.7, 1.3.8, 1.3.10 and 1.3.11. The hub only proposes this way when the image tag shows ≥ 1.3.7; otherwise it uses one of the two above. This is a Bun behaviour, not the hook's: the same happens with an empty preload file.

An existing BUN_OPTIONS is appended to with a space. The hub proposes N4 when it detects bun.lockb or bun.lock, an oven/bun image, or a command starting with bun.

What register.ts does: VBXTRACE=0 → nothing. Otherwise it loads /opt/vbxtrace/libvbxtrace-<arch>.so with bun:ffi (falling back to N-API), hands it to N1's binding (/opt/vbxtrace/node/vbxtrace.js), runs N1's register.js (Bun implements node:http, child_process, fs and net in JavaScript, so N1's wrappers apply) and wraps Bun.serve, Bun.spawn, Bun.spawnSync and Bun.connect.

What it records ​

EventEmitted fromJoin key
request_begina Bun.serve fetch handler or routes handler, before it runs; protocol websocket when the request asks for an upgrade, else http1local port (server.port), peer address and port (server.requestIP(req)), request index unknown (see below)
request_endwhen the handler's Response (or its promise) is ready, with response.status; for an upgraded WebSocket, when the socket closes (status 0)context id
request_begin / request_endExpress etc. on Bun's node:http: N1's Server.prototype.emit('request') path; Bun keeps one req.socket per connection, so the index is exactas N1
spawnedBun.spawn (after it returns, proc.pid); node:child_process async children through N1child pid
context_enter / context_exitaround Bun.spawnSync, N1's sync child_process and fs calls (nested ones — Bun's execFileSync calls Bun.spawnSync — collapse into one bracket)thread
connectedBun.connect (once the socket is open: socket.localPort)local port
openednode:fs openSync through N1pid + fd

Request index on Bun.serve: unknown. Bun.serve exposes no connection identity, so the index of a request on a keep-alive connection can't be known exactly; the binding passes VBX_REQUEST_INDEX_UNKNOWN (never a guess). The sensor then links such requests only through other exact facts (its own accept events), or not at all. Services on Bun's node:http get exact indexes.

Context following ​

AsyncLocalStorage (Bun implements node:async_hooks' AsyncLocalStorage). Each fetch/route handler runs inside its request's context; every promise, timer and callback it starts carries it. Bun.serve options are copied (same prototype and properties) with the handlers wrapped — the service's own object is never modified; server.reload(options) is wrapped the same way (not covered by a sample). Entry files with export default { fetch } work too: Bun starts them through the global Bun.serve (checked by hand on Bun 1.3.11).

WebSockets: when server.upgrade(req, { data }) is given a data object, the WebSocket handlers (open, message, drain, close, …) run in the upgrade request's context and the request ends at close. Without a data object the session can't be followed: the request ends at the upgrade (status 101) and the handlers run without context.

Known gaps ​

  • Async node:fs on Bun (fs.writeFile, fs.readFile, fs.promises.*): Bun implements them natively off the JavaScript thread and never hands the fd to JavaScript — not reported. Sync calls are (openSync → opened; writeFileSync etc. → bracketed).
  • Outgoing HTTP from Bun: fetch and Bun's node:http client are native — no connected. Raw Bun.connect is reported.
  • Bun.write, Bun.file().writer(), Bun.$: native, not reported.
  • Static Response values in routes run no handler: no request events for them (they do nothing worth linking).
  • Bun.serve request indexes are unknown (above).

Safety ​

  • Wrapped: Bun.serve (copies options; wraps fetch, route and WebSocket handlers), the server's upgrade and reload (on its prototype), Bun.spawn, Bun.spawnSync, Bun.connect; plus N1's wrappers. Handlers are called with the same this and arguments and their result (Response or promise) is returned unchanged; exceptions propagate. Bun.connect returns an equivalent promise (same socket or rejection, a constant few microtask ticks later) — only while a tracer is attached and a request is current.
  • Same failure behaviour as N1: every core call guarded, off after 5 internal errors, VBXTRACE=0 switches it off, nothing but one enabled() call while no tracer is attached; VBXTRACE_CHAOS=1 makes every core event throw inside the guards.
  • Overhead: not measured yet; it will be measured in the performance phase.

Verified (hooks/run-samples.sh node --package n4, Bun 1.3.11 musl on Alpine):

SampleWay ineventschaosbaseline
servebunfig.toml26 events match: Bun.serve fetch + routes (function and method map), Bun.spawn, Bun.spawnSync, Bun.connect, node:fs sync, a streamed body, a JSON body, a WebSocket sessionbyte-identicalbyte-identical
expressBUN_OPTIONS20 events match: Express 4 on Bun's node:http (exact keep-alive indexes), execFile, execFileSync, openSync; fs.writeFile and http.get produce no events (gaps above)byte-identicalbyte-identical

The command-line --preload way was checked by hand on Bun 1.3.11 with a test service (Bun.serve, Bun.spawn, Bun.spawnSync, Bun.connect, a WebSocket); it is the same preload as the bunfig way. Bun sends a Date header regardless of res.sendDate; the sample drivers drop that one header before comparing.

Turning it off ​

Set VBXTRACE=0 in the service's environment and restart it, or revert the change that added the hook (the bunfig mount, the --preload arguments or BUN_OPTIONS, together with the hook directory mount); vulnboxd's rollback covers a hook that breaks a service.

Troubleshooting ​

  • Are the probes there? readelf -n /usr/local/lib/vulnboxd/trace/libvbxtrace-x86_64.so lists them; sudo bpftrace -l 'usdt:/usr/local/lib/vulnboxd/trace/libvbxtrace-x86_64.so:*' too.
  • Is the hook loaded? grep vbxtrace /proc/<pid>/maps; from inside the service globalThis[Symbol.for('vbxtrace.node.v1')]?.status() → { loaded: true, core: 'bun:ffi', … }.
  • Which links did a flow get? The hub shows each link's method; this package's are hook:N4.
  • The service exits right after starting, with status 0: BUN_OPTIONS on Bun ≤ 1.3.6. Switch to the bunfig or command-line way.
  • No requests recorded: does the service have its own bunfig.toml that the mount replaced, or that lacks the preload? Is the preload path readable in the container (the mount)?