diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e3ee56..0e62d6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,8 @@ npm treats versions as semver and semver forbids leading zeros. pings — the platform `WebSocket` on Deno/Bun does not expose `ws.ping()`. ### Fixed +- The Deno WebSocket adapter snapshots request and connection metadata before accepting an upgrade; + Deno 2.9 invalidates that metadata once upgraded, which previously broke WebSocket and mesh boots. - Repeated slashes and malformed path encoding now return `400` consistently across Node and Fetch adapters, retaining configured security/CORS headers. `/path` and `/path/` remain equivalent. - Ambiguous same-method route shapes now fail at boot with both declarations named. Effective-shape diff --git a/src/deno.ts b/src/deno.ts index f73e80a..73771c6 100644 --- a/src/deno.ts +++ b/src/deno.ts @@ -35,10 +35,12 @@ declare const Deno: { export function serveDeno(app: App, options?: DenoServeOptions): DenoHttpServer { return Deno.serve(options ?? {}, (request, info) => { if (request.headers.get('upgrade')?.toLowerCase() === 'websocket') { + // Deno invalidates request connection metadata as soon as the upgrade is accepted. + const wsRequest = toWsRequest(request, info.remoteAddr.hostname); 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)); + void upgradeSafely(app, wsRequest, eventSocket(socket)); return response; } diff --git a/test/deno-adapter.test.ts b/test/deno-adapter.test.ts new file mode 100644 index 0000000..568218d --- /dev/null +++ b/test/deno-adapter.test.ts @@ -0,0 +1,50 @@ +import { afterEach, expect, it, vi } from 'vitest'; +import type { App } from '../src/app/types'; +import { serveDeno } from '../src/deno'; + +afterEach(() => { + vi.unstubAllGlobals(); +}); + +it('snapshots connection metadata before Deno invalidates the upgraded request', async () => { + let upgraded = false; + let handler: ((request: Request, info: unknown) => Response | Promise) | undefined; + const socket = { + readyState: 1, + binaryType: '', + send: vi.fn(), + close: vi.fn(), + addEventListener: vi.fn(), + }; + + vi.stubGlobal('Deno', { + serve: (_options: unknown, next: typeof handler) => { + handler = next; + return { finished: Promise.resolve(), shutdown: vi.fn() }; + }, + upgradeWebSocket: () => { + upgraded = true; + return { socket, response: new Response() }; + }, + }); + + const upgrade = vi.fn().mockResolvedValue(undefined); + serveDeno({ upgrade } as unknown as App); + const info = { + get remoteAddr() { + if (upgraded) throw new TypeError('Request closed'); + return { hostname: '127.0.0.1', port: 1234, transport: 'tcp' }; + }, + }; + const request = new Request('http://localhost/socket?token=x', { + headers: { upgrade: 'websocket', 'x-test': 'yes' }, + }); + + expect(() => handler!(request, info)).not.toThrow(); + await vi.waitFor(() => + expect(upgrade).toHaveBeenCalledWith( + expect.objectContaining({ url: '/socket?token=x', ip: '127.0.0.1' }), + expect.any(Object), + ), + ); +});