Bun Runtime
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 secondshostname: override the listening hostnameport: override the listening portunix: listen on a Unix domain sockettls: pass Bun-specific TLS optionserror: customize Bun's runtime-level error handlingroutes,websocket: available on Bun's native server API if you need them outsidefest
Official References
- Bun HTTP server docs: https://bun.sh/docs/runtime/http/server
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
hostnameandport - applying
reusePort - wiring the runtime
errorhandler - merging TLS options into Bun's
tlsfield - 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