Bun Runtime

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

Basic Usage

import { serve } from "sevo";
import { BunRuntimeAdapter } from "sevo/bun";

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

Runtime-Specific Server Options

Use ServerOptions.bun to forward Bun-native options through BunRuntimeAdapter.

BunServerOptions is defined as:

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

fetch, routes, and unix are excluded because Server owns request dispatch and sevo currently binds through the generic hostname / port options instead.

Pass Bun-native options on the bun field:

serve({
  adapter: new BunRuntimeAdapter(),
  bun: {
    idleTimeout: 10,
  },
  fetch() {
    return new Response("Hello from Bun");
  },
});

Commonly Used Options

  • idleTimeout: close idle connections after a number of seconds
  • hostname: override the listening hostname
  • port: override the listening port
  • tls: pass Bun-specific TLS options
  • error: customize Bun's runtime-level error handling

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

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

The adapter exposes Bun's resolved server.url.href as server.url.

CLI Usage

When using the CLI under Bun:

bunx --bun sevo

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

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