Files
core/test/deno/ws.test.ts
T
chrnx 9634ec73e8
CI / dco (pull_request) Successful in 6s
CI / build (pull_request) Successful in 30s
CI / runtimes (bun) (pull_request) Successful in 16s
CI / runtimes (deno) (pull_request) Successful in 20s
CI / runtimes (edge) (pull_request) Successful in 13s
feat(runtime)!: serveDeno and serveBun boot before they bind
Both were synchronous and left the boot to the first request, with the fix
documented as something the caller should remember: `await app.boot()` before
`Deno.serve` / `Bun.serve`. That made the correct use of a core helper depend
on reading a README, in a framework whose whole argument is that order should
not be something you have to get right.

The failure it leaves is the bad kind. `app.fetch` boots lazily and the boot
memo keeps the *rejection*, so a missing signing key does not fail the deploy:
the port binds, every request is answered 500 by the runtime, and none of them
reach `onError`. Anything that health-checks by asking whether the process is
listening sees a healthy server.

`listen()` has always awaited `bootApp()` before binding its socket, with a
comment saying it exists so Node and Deno/Bun serve an identical graph. They
did not. Now the three helpers agree, and the two adapters return promises.

`edgeHandler` is deliberately unchanged: on workerd there is no startup outside
a request, so there is no earlier moment to move the failure to.

Both new tests were confirmed to fail with the gate commented out — without it
`serveBun` resolves instead of rejecting, and serveDeno binds a port that 500s.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-09-19 12:38:50 -05:00

72 lines
2.5 KiB
TypeScript

// 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}`;
})();
}
}
@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<string>((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();
}
});