Skip to content

Bun

// server.ts
import { bunAdapter } from 'lacis/adapters'
const createServer = bunAdapter.createHandler('./routes')
createServer({
port: 3000,
isDev: process.env.NODE_ENV !== 'production',
})
  • Native fetch handler — requests are handled as standard Request/Response objects, wrapped in thin BunRequest/BunResponse classes
  • TransformStream for SSE — Bun’s Bun.serve() needs the Response synchronously, so initSSE() must be called before the first await in your handler
  • Faster JSON parsing — delegates directly to Bun’s native JSON parser
export async function GET(req, res) {
res.initSSE() // must be before any await
const data = await fetchSomething()
// ...
}
OptionTypeDefaultDescription
portnumber3000Port to listen on
isDevbooleanfalseEnables dev mode
defaultHeadersRecord<string, string>Headers added to every response
clusterobjectMulti-worker via reusePort
corsCorsConfigCORS policy
middlewareobjectbeforeRequest, afterRequest, onError hooks
hooksobjectonNotFound, onShutdown hooks

Lacis spawns worker processes with Bun.spawn() and uses reusePort: true so the OS distributes connections across workers.

createServer({
port: 3000,
cluster: { enabled: true, workers: 4 },
})

Workers poll the primary’s PID every 2 seconds and exit cleanly if the primary is gone.

  1. Build:

    Terminal window
    bun run build
  2. Start:

    Terminal window
    bun dist/server.js