fix(deno): snapshot metadata before websocket upgrade

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-08-02 21:18:40 -05:00
1 parent 74f09d2e0b
commit 1db4e01e40
3 files changed
+55 -1

No files matched your search

+2
View File
@@ -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
+3 -1
View File
@@ -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;
}
+50
View File
@@ -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<Response>) | 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),
),
);
});