Skip to main content

Class: BunRouter

Defined in: service-core/src/http/bun/BunRouter.ts:153

Bun-native HTTP/WebSocket router built on Bun.serve(). Exposes the same public surface as the uWS-backed HttpRouter (use/get/post/put/delete/patch/head/options/ws/listen/close/isListening/ listenPort) so RouteUtils.registerRoute() and Server.ts work against either implementation unchanged.

Unlike uWS, Bun.serve() has no built-in dynamic router, so this class maintains its own route table matched inside a single fetch() callback, with static-segment > :param > /* wildcard precedence — replicating uWS's specificity-based matching.

Implements​

Indexable​

[key: string]: any

Constructors​

Constructor​

new BunRouter(maxBodySize?, sslConfig?): BunRouter

Defined in: service-core/src/http/bun/BunRouter.ts:171

Parameters​

maxBodySize?​

number = DEFAULT_MAX_BODY_SIZE

sslConfig?​

any

Returns​

BunRouter

Properties​

listenPort​

listenPort: number = 0

Defined in: service-core/src/http/bun/BunRouter.ts:169

The port the server is currently listening on (set after a successful listen() call).

Implementation of​

IHttpRouter.listenPort

Accessors​

inFlightRequests​

Get Signature​

get inFlightRequests(): number

Defined in: service-core/src/http/bun/BunRouter.ts:626

The number of HTTP requests currently being handled.

Returns​

number


isListening​

Get Signature​

get isListening(): boolean

Defined in: service-core/src/http/bun/BunRouter.ts:177

Returns true if the server is currently listening.

Returns​

boolean

Implementation of​

IHttpRouter.isListening

Methods​

close()​

close(): void

Defined in: service-core/src/http/bun/BunRouter.ts:618

Stops the server, closing the listen socket.

Returns​

void

Implementation of​

IHttpRouter.close


delete()​

delete(routePath, ...handlersOrOptions): this

Defined in: service-core/src/http/bun/BunRouter.ts:253

Parameters​

routePath​

string

handlersOrOptions​

...(HttpRouteOptions | RequestHandler)[]

Returns​

this

Implementation of​

IHttpRouter.delete


get()​

get(routePath, ...handlersOrOptions): this

Defined in: service-core/src/http/bun/BunRouter.ts:238

Parameters​

routePath​

string

handlersOrOptions​

...(HttpRouteOptions | RequestHandler)[]

Returns​

this

Implementation of​

IHttpRouter.get


hasExplicitOptionsRoute()​

hasExplicitOptionsRoute(path): boolean

Defined in: service-core/src/http/bun/BunRouter.ts:282

Returns true if the application has registered its own literal OPTIONS route at path (not the framework's own /* CORS-preflight fallback) - see HttpRouter's identical method doc comment for the full rationale. path is normalized the same way registered routes are, so a trailing-slash mismatch between the two doesn't cause a false miss.

Parameters​

path​

string

Returns​

boolean

Implementation of​

IHttpRouter.hasExplicitOptionsRoute


head(routePath, ...handlersOrOptions): this

Defined in: service-core/src/http/bun/BunRouter.ts:263

Parameters​

routePath​

string

handlersOrOptions​

...(HttpRouteOptions | RequestHandler)[]

Returns​

this

Implementation of​

IHttpRouter.head


listen()​

listen(host, port): Promise<void>

Defined in: service-core/src/http/bun/BunRouter.ts:561

Starts listening on the given host and port. Resolves when the server is ready; rejects if the port cannot be bound.

Parameters​

host​

string

port​

number

Returns​

Promise<void>

Implementation of​

IHttpRouter.listen


options()​

options(routePath, ...handlersOrOptions): this

Defined in: service-core/src/http/bun/BunRouter.ts:268

Parameters​

routePath​

string

handlersOrOptions​

...(HttpRouteOptions | RequestHandler)[]

Returns​

this

Implementation of​

IHttpRouter.options


patch()​

patch(routePath, ...handlersOrOptions): this

Defined in: service-core/src/http/bun/BunRouter.ts:258

Parameters​

routePath​

string

handlersOrOptions​

...(HttpRouteOptions | RequestHandler)[]

Returns​

this

Implementation of​

IHttpRouter.patch


post()​

post(routePath, ...handlersOrOptions): this

Defined in: service-core/src/http/bun/BunRouter.ts:243

Parameters​

routePath​

string

handlersOrOptions​

...(HttpRouteOptions | RequestHandler)[]

Returns​

this

Implementation of​

IHttpRouter.post


put()​

put(routePath, ...handlersOrOptions): this

Defined in: service-core/src/http/bun/BunRouter.ts:248

Parameters​

routePath​

string

handlersOrOptions​

...(HttpRouteOptions | RequestHandler)[]

Returns​

this

Implementation of​

IHttpRouter.put


shutdown()​

shutdown(timeoutMs?): Promise<void>

Defined in: service-core/src/http/bun/BunRouter.ts:636

Stops accepting new connections, waits up to timeoutMs for in-flight HTTP requests to finish, then closes every remaining connection (keep-alive and WebSocket connections included).

Parameters​

timeoutMs?​

number = 10000

The maximum time to wait for in-flight requests, in milliseconds.

Returns​

Promise<void>

Implementation of​

IHttpRouter.shutdown


use()​

use(...handlers): this

Defined in: service-core/src/http/bun/BunRouter.ts:182

Register global middleware that runs before every route handler.

Parameters​

handlers​

...RequestHandler[]

Returns​

this

Implementation of​

IHttpRouter.use


ws()​

ws(routePath, handlers, wsOptions?, upgradeAuth?): this

Defined in: service-core/src/http/bun/BunRouter.ts:301

Registers a WebSocket route. Handlers follow the same (req, res, next) pattern as HTTP routes; they receive req.websocket containing the Bun WebSocket shim.

upgradeAuth is an optional pre-upgrade auth function, run synchronously inside fetch() before the handshake. If it returns { reject: true }, an HTTP 401 is sent and the upgrade is aborted. If it returns { user, ... }, those credentials are attached to the request so downstream middleware sees an authenticated user. If it returns {}, auth falls through to the post-upgrade message-based LOGIN flow.

wsOptions uses the uWS option names (maxPayloadLength, idleTimeout, maxBackpressure) over DEFAULT_WS_OPTIONS. Bun.serve() only accepts one WebSocket configuration for the whole server, so maxPayloadLength is additionally enforced per route (a larger message closes the socket with 1009), while idleTimeout and maxBackpressure are applied server-wide using the largest value any route registered.

Parameters​

routePath​

string

handlers​

RequestHandler[]

wsOptions?​

WebSocketOptions

upgradeAuth?​

WsUpgradeAuth

Returns​

this

Implementation of​

IHttpRouter.ws