Bun Runtime

BunRuntimeAdapter uses Bun.serve() to host a fest server.

Basic Usage

import { serve } from "@hornjs/fest";
import { BunRuntimeAdapter } from "@hornjs/fest/bun";

serve({
  adapter: new BunRuntimeAdapter(),
  routes: {
    "/": () => new Response("Hello from Bun"),
  },
  fetch: () => new Response("Not Found", { status: 404 }),
});

Constructor Options

new BunRuntimeAdapter(options) accepts BunServerOptions, which is defined as:

type BunServerOptions = Omit<Bun.Serve.Options<any>, "fetch">;

fetch is owned by Server, so every other Bun server option can be forwarded through the adapter constructor.

Commonly Used Options

  • idleTimeout: close idle connections after a number of seconds
  • hostname: override the listening hostname
  • port: override the listening port
  • unix: listen on a Unix domain socket
  • tls: pass Bun-specific TLS options
  • error: customize Bun's runtime-level error handling
  • routes, websocket: available on Bun's native server API if you need them outside fest

Official References

What the Adapter Does

BunRuntimeAdapter translates generic ServerOptions into Bun's native listener options and delegates request handling through server.fetch().

It is responsible for:

  • resolving hostname and port
  • applying reusePort
  • wiring the runtime error handler
  • merging TLS options into Bun's tls field
  • reporting the final listening URL

Bun-Specific Options

Pass Bun native options directly to the adapter constructor:

import { BunRuntimeAdapter } from "@hornjs/fest/bun";

const adapter = new BunRuntimeAdapter({
  idleTimeout: 10,
});

These options are merged with generic server options during setup().

TLS

When TLS is configured on the server, BunRuntimeAdapter resolves certificate and key values from either inline PEM strings or filesystem paths.

serve({
  adapter: new BunRuntimeAdapter(),
  protocol: "https",
  tls: {
    cert: "./certs/dev-cert.pem",
    key: "./certs/dev-key.pem",
  },
  routes: {
    "/": () => new Response("secure"),
  },
  fetch: () => new Response("Not Found", { status: 404 }),
});

URL Resolution

Bun's high-level server.url is not always the most accurate source for the bound address, so the adapter prefers server.address when available.

CLI Usage

When using the CLI under Bun:

bunx --bun fest

You can also preload modules with Bun's --import support through the CLI:

fest --entry ./server.tsx --import jiti/register