From fd6446f5146a67373e8dc90009b2a21c2d828d62 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Thu, 9 Jul 2026 21:44:46 -0500 Subject: [PATCH] =?UTF-8?q?feat(bun):=20serveBun=20adapter=20=E2=80=94=20H?= =?UTF-8?q?TTP+SSE+WS=20on=20Bun=20via=20/bun=20subpath?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Diego Resendez --- package.json | 11 +++++ src/bun.ts | 131 +++++++++++++++++++++++++++++++++++++++++++++++++ tsup.config.ts | 2 +- 3 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 src/bun.ts diff --git a/package.json b/package.json index 68ffafa..7f29f35 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,16 @@ "types": "./dist/deno.d.ts", "default": "./dist/deno.js" } + }, + "./bun": { + "import": { + "types": "./dist/bun.d.mts", + "default": "./dist/bun.mjs" + }, + "require": { + "types": "./dist/bun.d.ts", + "default": "./dist/bun.js" + } } }, "files": [ @@ -60,6 +70,7 @@ "test": "vitest run", "test:dev": "vitest", "test:deno": "deno test --allow-net --allow-read --no-check test/deno/", + "test:bun": "bun test test/bun/", "typecheck": "tsc --noEmit -p tsconfig.json", "typecheck:bench": "tsc --noEmit -p bench/tsconfig.json", "lint": "eslint src", diff --git a/src/bun.ts b/src/bun.ts new file mode 100644 index 0000000..4da5579 --- /dev/null +++ b/src/bun.ts @@ -0,0 +1,131 @@ +import { channel } from './channel'; +import type { Channel } from './channel'; +import type { App } from './app/types'; +import type { WsSocket, WsRequest } from './http/ws-core'; + +// Minimal ambient for the Bun globals we use — avoids a @types/bun devDep. +// Mirrors the real Bun.serve / server.upgrade / ServerWebSocket signatures we call. +interface BunServerWebSocket { + data: unknown; + readyState: number; + send(data: string): number; + close(code?: number, reason?: string): void; + terminate?(): void; +} +interface BunWebSocketHandler { + open?(ws: BunServerWebSocket): void; + message(ws: BunServerWebSocket, message: string | Uint8Array): void; + close?(ws: BunServerWebSocket, code: number, reason: string): void; +} +interface BunServer { + port: number; + stop(closeActiveConnections?: boolean): void; + upgrade(request: Request, options?: { data?: unknown; headers?: Record }): boolean; + requestIP(request: Request): { address: string; family: string; port: number } | null; +} +interface BunServeOptions { + port?: number; + hostname?: string; + fetch(request: Request, server: BunServer): Response | Promise | undefined; + websocket?: BunWebSocketHandler; +} +type BunServeShortOptions = { port?: number; hostname?: string }; +declare const Bun: { serve(options: BunServeOptions): BunServer }; + +/** Per-connection state stashed on ws.data between fetch (upgrade) and the open/message/close callbacks. */ +interface BunConnData { + wsReq: WsRequest; + inbound: Channel; + ac: AbortController; +} + +/** Builds a neutral WsRequest from a Bun request + server (for the client IP). */ +function toWsRequest(request: Request, server: BunServer): WsRequest { + const url = new URL(request.url); + const headers: Record = {}; + request.headers.forEach((v, k) => { + headers[k] = v; + }); + return { + url: url.pathname + url.search, + headers, + protocol: url.protocol === 'https:' ? 'https' : 'http', + ip: server.requestIP(request)?.address ?? '', + }; +} + +/** Wraps a Bun ServerWebSocket (+ its stashed channel/abort) as a neutral WsSocket. */ +function bunSocket(ws: BunServerWebSocket): WsSocket { + const { inbound, ac } = ws.data as BunConnData; + return { + inbound, + abort: ac.signal, + get isOpen() { + return ws.readyState === 1; // 1 = OPEN + }, + send: (data) => { + ws.send(data); + }, + close: (code, reason) => { + try { + ws.close(code, reason); + } catch { + /* already closed */ + } + }, + terminate: () => { + try { + (ws.terminate ?? ws.close).call(ws); + } catch { + /* already closed */ + } + }, + }; +} + +/** + * Serve a Green Tea app on Bun: HTTP + SSE via app.fetch, WebSocket via Bun's server-level + * websocket handler. The ServerWebSocket only exists in open(), so per-connection state + * (inbound channel, abort, WsRequest) is stashed on ws.data during the upgrade. + */ +export function serveBun(app: App, options?: BunServeShortOptions): BunServer { + return Bun.serve({ + ...options, + fetch(request, server) { + if (request.headers.get('upgrade')?.toLowerCase() === 'websocket') { + const inbound = channel(); + const ac = new AbortController(); + const data: BunConnData = { wsReq: toWsRequest(request, server), inbound, ac }; + if (server.upgrade(request, { data })) return undefined; + return new Response('WebSocket upgrade failed', { status: 400 }); + } + + return app.fetch(request); + }, + websocket: { + open(ws) { + const { wsReq } = ws.data as BunConnData; + // fire-and-forget: app.upgrade can reject before runWsConnection's try/catch + // (mesh-before-listen, provider boot failure); guard it or an uncaught rejection + // could crash the Bun process. + void app.upgrade(wsReq, bunSocket(ws)).catch(() => { + try { + ws.close(1011, 'internal error'); + } catch { + /* already closed */ + } + }); + }, + message(ws, message) { + const { inbound } = ws.data as BunConnData; + // decode binary to UTF-8 to match Node's Buffer.toString() (cross-adapter parity) + inbound.push(typeof message === 'string' ? message : new TextDecoder().decode(message)); + }, + close(ws) { + const { inbound, ac } = ws.data as BunConnData; + inbound.close(); + ac.abort(); + }, + }, + }); +} diff --git a/tsup.config.ts b/tsup.config.ts index ac3aeff..bfad144 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -4,7 +4,7 @@ import { defineConfig } from 'tsup'; // tsup injects a createRequire shim into the ESM output so the lazy require('ws') / require('busboy') // (optional peer deps) keep working there. reflect-metadata and the peer deps stay external. export default defineConfig({ - entry: ['src/index.ts', 'src/deno.ts'], + entry: ['src/index.ts', 'src/deno.ts', 'src/bun.ts'], format: ['cjs', 'esm'], dts: true, clean: true,