UNPKG

@ws-kit/bun

Version:

Bun platform adapter for WS-Kit leveraging native WebSocket API with built-in pub/sub and low-latency message routing

35 lines 1.64 kB
import type { ConnectionData, Router } from "@ws-kit/core"; import type { BunHandlerOptions, BunServerHandlers } from "./types.js"; /** * Create Bun WebSocket handlers for use with Bun.serve. * * Returns a `{ fetch, websocket }` object that can be passed directly to Bun.serve. * Accepts both typed routers and core routers. Per ADR-035, this adapter is a mechanical * bridge between Bun's WebSocket API and the router's internal protocol. * * **Usage**: * ```typescript * import { createRouter } from "@ws-kit/zod"; * import { createBunHandler } from "@ws-kit/bun"; * * const router = createRouter<TContext>(); * const { fetch, websocket } = createBunHandler(router); * * Bun.serve({ fetch, websocket, port: 3000 }); * ``` * * **Connection Flow**: * 1. HTTP request arrives at Bun.serve fetch handler * 2. Your code calls `router.upgrade(req, { server })` or similar * 3. Authentication is performed (if configured) * 4. Bun upgrades the connection to WebSocket * 5. `websocket.open(ws)` → router handles the connection * 6. `websocket.message(ws, msg)` → router routes the message * 7. `websocket.close(ws, code, reason)` → router handles cleanup * * @param router - TypedRouter or WebSocketRouter instance * @param options - Optional handler configuration * @returns Object with `fetch` and `websocket` handlers for Bun.serve */ export declare function createBunHandler<TContext extends ConnectionData = ConnectionData, TExtensions extends object = any>(router: Router<TContext, TExtensions>, options?: BunHandlerOptions<TContext>): BunServerHandlers<TContext>; //# sourceMappingURL=handler.d.ts.map