The Node 'upgrade' listener matched the raw request path without the normalization HTTP runs. A bad percent escape in a param segment threw URIError synchronously inside the listener, an uncaught exception that ends the process, and '//' silently matched a route HTTP refuses. Upgrades now normalize the path first: malformed escapes and repeated slashes get a 400, an unknown route a written 404 instead of a bare destroy, and any other throw in the listener a 500. app.upgrade (Deno/Bun/edge) closes 1008 'invalid request path' rather than 1011. On Deno the adapter's socket is still CONNECTING when an early 1008 is sent, and that close was a no-op that left the peer hanging; eventSocket now defers such a close to 'open'. Closes #104 Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
123 lines
3.8 KiB
TypeScript
123 lines
3.8 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { createApp, Route, Ws, Module, ctx, Step, needs } from '../src/index';
|
|
import { channel } from '../src/channel';
|
|
import type { WsSocket, WsRequest } from '../src/http/ws-core';
|
|
|
|
@Route('/rt')
|
|
class Rt {
|
|
@Ws('/echo')
|
|
echo(@ctx() { inbound }: any) {
|
|
return (async function* () {
|
|
for await (const msg of inbound) yield `echo:${msg}`;
|
|
})();
|
|
}
|
|
}
|
|
@Route('/room')
|
|
class Room {
|
|
@Ws('/:name')
|
|
join(@ctx() { inbound }: any) {
|
|
return (async function* () {
|
|
for await (const msg of inbound) yield `room:${msg}`;
|
|
})();
|
|
}
|
|
}
|
|
@Module({ mountpoint: '/', controllers: [Rt, Room] })
|
|
class M {}
|
|
const app = createApp({ modules: [M] });
|
|
|
|
@Step({ provides: 'tag' })
|
|
class Tagger {
|
|
run() {
|
|
return { tag: 't' };
|
|
}
|
|
}
|
|
@Route('/tagged')
|
|
class TaggedCtl {
|
|
@Ws('/echo')
|
|
echo(@needs('tag') tag: string, @ctx() { inbound }: any) {
|
|
return (async function* () {
|
|
for await (const msg of inbound) yield `${tag}:${msg}`;
|
|
})();
|
|
}
|
|
}
|
|
@Module({ mountpoint: '/', steps: [Tagger], controllers: [TaggedCtl] })
|
|
class Tagged {}
|
|
|
|
function fakeSocket() {
|
|
const inbound = channel<unknown>();
|
|
const ac = new AbortController();
|
|
const sent: string[] = [];
|
|
const socket: WsSocket = {
|
|
inbound,
|
|
abort: ac.signal,
|
|
isOpen: true,
|
|
send: (d) => sent.push(d),
|
|
close: () => ac.abort(),
|
|
terminate: () => ac.abort(),
|
|
};
|
|
return { socket, inbound, sent };
|
|
}
|
|
|
|
describe('app.upgrade', () => {
|
|
it('routes a matching ws path through the graph', async () => {
|
|
const { socket, inbound, sent } = fakeSocket();
|
|
const req: WsRequest = { url: '/rt/echo', headers: {}, protocol: 'http', ip: '' };
|
|
const run = app.upgrade(req, socket);
|
|
// let the handler's async generator start subscribing to `inbound` (channel is fan-out:
|
|
// push before subscription is lost) before pushing — a macrotask flushes every pending
|
|
// microtask in the upgrade/boot chain first, so this is deterministic, not a sleep-and-hope.
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
inbound.push('hi');
|
|
inbound.close();
|
|
await run;
|
|
expect(sent).toEqual(['echo:hi']);
|
|
});
|
|
|
|
it('emits request:step:enter/leave for ws steps, like HTTP routes do', async () => {
|
|
const seen: string[] = [];
|
|
const logger = {
|
|
name: 'ws-step-logger',
|
|
mount: (api: any) => {
|
|
api.bus.on('request:step:enter', (p: any) => seen.push(`enter:${p.name}`));
|
|
api.bus.on('request:step:leave', (p: any) => seen.push(`leave:${p.name}`));
|
|
},
|
|
};
|
|
const wsApp = createApp({ modules: [Tagged], plugins: [logger] });
|
|
const { socket, inbound, sent } = fakeSocket();
|
|
const req: WsRequest = { url: '/tagged/echo', headers: {}, protocol: 'http', ip: '' };
|
|
const run = wsApp.upgrade(req, socket);
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
inbound.push('hi');
|
|
inbound.close();
|
|
await run;
|
|
|
|
expect(sent).toEqual(['t:hi']);
|
|
expect(seen).toEqual(['enter:tag', 'leave:tag']);
|
|
});
|
|
|
|
it('closes 1008 on an unknown ws path', async () => {
|
|
const { socket } = fakeSocket();
|
|
let closedCode: number | undefined;
|
|
socket.close = (c) => {
|
|
closedCode = c;
|
|
};
|
|
const req: WsRequest = { url: '/rt/nope', headers: {}, protocol: 'http', ip: '' };
|
|
await app.upgrade(req, socket);
|
|
expect(closedCode).toBe(1008);
|
|
});
|
|
|
|
it.each(['/rt/%E0%A4%A', '/room/%E0%A4%A', '//rt//echo'])(
|
|
'closes 1008 on the malformed path %s instead of rejecting (#104)',
|
|
async (url) => {
|
|
const { socket } = fakeSocket();
|
|
let closed: [number | undefined, string | undefined] | undefined;
|
|
socket.close = (c, r) => {
|
|
closed = [c, r];
|
|
};
|
|
const req: WsRequest = { url, headers: {}, protocol: 'http', ip: '' };
|
|
await app.upgrade(req, socket);
|
|
expect(closed).toEqual([1008, 'invalid request path']);
|
|
},
|
|
);
|
|
});
|