// Real Deno integration test for serveDeno: boots an app under the actual Deno runtime // and round-trips both HTTP (app.fetch) and WebSocket (Deno.upgradeWebSocket -> app.upgrade). // // Run with: npm run test:deno (or: deno test --allow-net --allow-read --no-check test/deno/) // // `--no-check`: the barrel (`src/index.ts`) re-exports types from Node-only modules // (e.g. `src/http/*`) that reference ambient Node globals like `Buffer`. Those modules // aren't executed under Deno (this test only exercises `serveDeno`'s fetch/WebSocket // path), but Deno's checker still type-checks the whole reachable graph and has no // ambient `Buffer` type without a full `@types/node` setup. Node-side static typing for // those modules is already covered by `npm run typecheck` (tsc); this test's job is to // prove Deno *runtime* behavior of the adapter, not re-typecheck Node-only internals. import 'npm:reflect-metadata'; import { assertEquals } from 'jsr:@std/assert'; import { createApp, Route, Ws, Get, Module, ctx } from '../../src/index.ts'; import { serveDeno } from '../../src/deno.ts'; @Route('/rt') class Rt { @Get('/hello') hello() { return { ok: true }; } @Ws('/echo') echo(@ctx() { inbound }: any) { return (async function* () { for await (const msg of inbound) yield `echo:${msg}`; })(); } // Never reads `inbound`, so every frame the client sends stays queued (#107). @Ws('/stall') stall() { return (async function* () { await new Promise(() => {}); yield ''; })(); } @Ws('/room/:name') room(@ctx() { inbound }: any) { return (async function* () { for await (const msg of inbound) yield `room:${msg}`; })(); } } @Module({ mountpoint: '/', controllers: [Rt] }) class M {} Deno.test('serveDeno: HTTP + WebSocket round-trip through the graph', async () => { const app = createApp({ modules: [M] }); let port = 0; const server = await serveDeno(app, { port: 0, onListen: (addr) => { port = addr.port; }, }); // Let onListen fire before we try to connect. await new Promise((resolve) => setTimeout(resolve, 50)); try { const httpRes = await fetch(`http://127.0.0.1:${port}/rt/hello`); assertEquals(httpRes.status, 200); assertEquals((await httpRes.json()).ok, true); const ws = new WebSocket(`ws://127.0.0.1:${port}/rt/echo`); try { const got = await new Promise((resolve, reject) => { ws.onopen = () => ws.send('hi'); ws.onmessage = (e) => resolve(String(e.data)); ws.onerror = () => reject(new Error('ws error')); }); assertEquals(got, 'echo:hi'); } finally { ws.close(); // Give the close frame a moment to complete the handshake so no socket is leaked. await new Promise((resolve) => setTimeout(resolve, 20)); } } finally { await server.shutdown(); } }); // #104: a malformed escape in a param segment used to reject out of the matcher and close 1011. Deno.test('serveDeno: a malformed upgrade path closes 1008 and the server keeps serving', async () => { const app = createApp({ modules: [M] }); let port = 0; const server = await serveDeno(app, { port: 0, onListen: (addr) => { port = addr.port; }, }); await new Promise((resolve) => setTimeout(resolve, 50)); try { const ws = new WebSocket(`ws://127.0.0.1:${port}/rt/room/%E0%A4%A`); const closed = await new Promise<{ code: number; reason: string }>((resolve) => { ws.onclose = (e) => resolve({ code: e.code, reason: e.reason }); }); assertEquals(closed, { code: 1008, reason: 'invalid request path' }); const httpRes = await fetch(`http://127.0.0.1:${port}/rt/hello`); assertEquals(httpRes.status, 200); await httpRes.body?.cancel(); } finally { await server.shutdown(); } }); /** Opens `path`, sends `frames` once open, and resolves with every reply and the close. */ function exchange(port: number, path: string, frames: string[], until = 2000) { const ws = new WebSocket(`ws://127.0.0.1:${port}${path}`); const replies: string[] = []; return new Promise<{ replies: string[]; close?: { code: number; reason: string } }>((resolve) => { const timer = setTimeout(() => { ws.close(); resolve({ replies }); }, until); ws.onopen = () => frames.forEach((f) => ws.send(f)); ws.onmessage = (e) => replies.push(String(e.data)); ws.onclose = (e) => { clearTimeout(timer); resolve({ replies, close: { code: e.code, reason: e.reason } }); }; }); } // #107: on Deno the runtime receives the whole message before user code sees it, so the size // bound is checked after receipt — the close code is the same as on Node and Bun. Deno.test('serveDeno: ws message size and inbound queue limits', async () => { const app = createApp({ modules: [M], limits: { maxWsMessageBytes: 1024, maxWsQueue: 4 } }); let port = 0; const server = await serveDeno(app, { port: 0, onListen: (addr) => { port = addr.port; }, }); await new Promise((resolve) => setTimeout(resolve, 50)); try { const atLimit = await exchange(port, '/rt/echo', ['a'.repeat(1024)], 300); assertEquals(atLimit.replies, [`echo:${'a'.repeat(1024)}`]); const over = await exchange(port, '/rt/echo', ['a'.repeat(1025)]); assertEquals(over.close, { code: 1009, reason: 'message too large' }); const full = await exchange(port, '/rt/stall', ['1', '2', '3', '4', '5']); assertEquals(full.close, { code: 1013, reason: 'inbound queue full' }); } finally { await server.shutdown(); } }); Deno.test('serveDeno: ws message size default is 1 MiB', async () => { const app = createApp({ modules: [M] }); let port = 0; const server = await serveDeno(app, { port: 0, onListen: (addr) => { port = addr.port; }, }); await new Promise((resolve) => setTimeout(resolve, 50)); try { const atLimit = await exchange(port, '/rt/echo', ['a'.repeat(1_048_576)], 500); assertEquals(atLimit.replies.length, 1); const over = await exchange(port, '/rt/echo', ['a'.repeat(1_048_577)]); assertEquals(over.close?.code, 1009); } finally { await server.shutdown(); } }); // CARD-21 (D17): the reserved mesh path is refused the way the Node listener refuses it, and a route // that claims it fails createApp here too. Deno.test('serveDeno: /__mesh__/control without a control channel closes 1008; a route there fails createApp', async () => { @Route('/') class Claims { @Ws('/__mesh__/control') control() { return (async function* () {})(); } } @Module({ mountpoint: '/', controllers: [Claims] }) class ClaimsModule {} let message = ''; try { createApp({ modules: [ClaimsModule] }); } catch (error) { message = (error as Error).message; } assertEquals(message.startsWith('route @Ws /__mesh__/control at Claims.control uses the reserved path'), true); const app = createApp({ modules: [M] }); let port = 0; const server = await serveDeno(app, { port: 0, onListen: (addr) => { port = addr.port; }, }); await new Promise((resolve) => setTimeout(resolve, 50)); try { assertEquals((await exchange(port, '/__mesh__/control', [])).close, { code: 1008, reason: 'mesh control channel not enabled: this app exports nothing, or has no mesh.secret', }); } finally { await server.shutdown(); } });