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

68 lines 2.8 kB
// SPDX-FileCopyrightText: 2025-present Kriasoft // SPDX-License-Identifier: MIT /** * Serve a router on Bun. * * High-level convenience function that creates a WebSocket handler and starts * a Bun HTTP server. For more control, use `createBunHandler()` directly. * * @param router - The WebSocket router to serve * @param options - Server options * @returns Promise that resolves when server is running (never completes) * * @example * ```typescript * import { serve } from "@ws-kit/bun"; * import { createRouter } from "@ws-kit/zod"; * * const router = createRouter(); * serve(router, { port: 3000 }); * ``` */ export async function serve(router, options = {}) { const { createBunHandler } = await import("./handler.js"); const { BunPubSub } = await import("./pubsub.js"); // Extract the core router if it's wrapped // (in case someone passes a typed wrapper) const coreRouter = router[Symbol.for("ws-kit.core")] ?? router; // Per ADR-035: Pass only mechanical options (auth, lifecycle hooks) to createBunHandler. // Rationale: Adapter should not have behavioral concerns (context, observability). // Those belong in plugins on top of the router, not in the adapter itself. // This keeps the adapter lean and consistent with ADR-031 (plugin-adapter architecture). const handlerOptions = { authenticate: options.authenticate, onError: options.onError, onUpgrade: options.onUpgrade, onOpen: options.onOpen, onClose: options.onClose, }; if (options.clientIdHeader) { handlerOptions.clientIdHeader = options.clientIdHeader; } if (options.authRejection) { handlerOptions.authRejection = options.authRejection; } const { fetch, websocket } = createBunHandler(coreRouter, handlerOptions); // Return a promise that never resolves (server runs indefinitely) return new Promise(() => { const server = Bun.serve({ port: options.port ?? 3000, fetch, websocket, }); // Initialize BunPubSub for this server instance if not already configured // This enables router.publish() to broadcast to WebSocket connections // Respects any custom pub/sub backend already configured by the user if (!coreRouter.pubsubInstance) { // We set the private pubsubInstance field directly since the property is readonly Object.defineProperty(coreRouter, "pubsubInstance", { value: new BunPubSub(server), writable: false, enumerable: false, configurable: false, }); } console.log(`WebSocket server running on ws://localhost:${server.port}`); }); } //# sourceMappingURL=serve.js.map