toWsRequest, the neutral-socket wrapper and the upgrade error guard were copy-pasted across all three runtime adapters, comments included. A change to the upgrade close code meant remembering to touch three files. Extract the shared parts to http/ws-adapter (kept separate from ws-core: that module runs a connection, this one builds one from a host runtime's socket). Bun keeps its own socket wrapper — its events land on a server-level handler rather than the socket, so the lifecycle genuinely differs. Verified against real runtimes: deno test, bun test, workerd via miniflare. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
48 lines
2.0 KiB
TypeScript
48 lines
2.0 KiB
TypeScript
import type { App } from './app/types';
|
|
import { eventSocket, toWsRequest, upgradeSafely, type EventfulSocket } from './http/ws-adapter';
|
|
|
|
// The project tsconfig's `lib` doesn't include `DOM`, and the installed `@types/node` (18.x)
|
|
// doesn't declare a global `WebSocket` either — so we ambient-declare just the shape we use.
|
|
interface WebSocket extends EventfulSocket {
|
|
binaryType: string;
|
|
}
|
|
|
|
// Minimal ambient for the Deno globals we use — avoids a @types/deno devDep.
|
|
// Mirrors the real Deno.serve / Deno.upgradeWebSocket signatures we call.
|
|
interface DenoServeOptions {
|
|
port?: number;
|
|
hostname?: string;
|
|
signal?: AbortSignal;
|
|
onListen?: (addr: { hostname: string; port: number }) => void;
|
|
}
|
|
interface DenoServeHandlerInfo {
|
|
remoteAddr: { hostname: string; port: number; transport: string };
|
|
}
|
|
interface DenoHttpServer {
|
|
finished: Promise<void>;
|
|
shutdown(): Promise<void>;
|
|
}
|
|
type DenoServeHandler = (request: Request, info: DenoServeHandlerInfo) => Response | Promise<Response>;
|
|
declare const Deno: {
|
|
serve(options: DenoServeOptions, handler: DenoServeHandler): DenoHttpServer;
|
|
upgradeWebSocket(request: Request): { socket: WebSocket; response: Response };
|
|
};
|
|
|
|
/**
|
|
* Serve a Green Tea app on Deno: HTTP + SSE via app.fetch, WebSocket via Deno.upgradeWebSocket.
|
|
* WebSocket messages flow through app.upgrade over the shared neutral core.
|
|
*/
|
|
export function serveDeno(app: App, options?: DenoServeOptions): DenoHttpServer {
|
|
return Deno.serve(options ?? {}, (request, info) => {
|
|
if (request.headers.get('upgrade')?.toLowerCase() === 'websocket') {
|
|
const { socket, response } = Deno.upgradeWebSocket(request);
|
|
socket.binaryType = 'arraybuffer'; // deliver binary frames as ArrayBuffer, which decodeMessage expects
|
|
// fire-and-forget: the 101 response must be returned synchronously while the graph runs
|
|
void upgradeSafely(app, toWsRequest(request, info.remoteAddr.hostname), eventSocket(socket));
|
|
return response;
|
|
}
|
|
|
|
return app.fetch(request);
|
|
});
|
|
}
|