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>
This commit is contained in:
18 files changed
+147
-38
No files matched your search
@@ -48,9 +48,9 @@ function freePort(): number {
|
||||
}
|
||||
|
||||
/** Boot a teapot on a fixed port; the handle kills it and leaves the port free again. */
|
||||
function startTeapot(port: number) {
|
||||
async function startTeapot(port: number) {
|
||||
const app = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
|
||||
const server = serveBun(app, { port });
|
||||
const server = await serveBun(app, { port });
|
||||
|
||||
return {
|
||||
stop: async () => {
|
||||
@@ -64,7 +64,7 @@ const settle = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
||||
|
||||
test('mesh on Bun: a teacup reconnects to a teapot that came back', async () => {
|
||||
const port = freePort();
|
||||
let teapot = startTeapot(port);
|
||||
let teapot = await startTeapot(port);
|
||||
|
||||
const teacup = createApp({
|
||||
modules: [TeacupModule],
|
||||
@@ -88,7 +88,7 @@ test('mesh on Bun: a teacup reconnects to a teapot that came back', async () =>
|
||||
const down = await teacup.fetch(new Request('http://x/api/local/who'));
|
||||
expect(down.status).toBe(503);
|
||||
|
||||
teapot = startTeapot(port);
|
||||
teapot = await startTeapot(port);
|
||||
await settle(400);
|
||||
|
||||
const after = await teacup.fetch(new Request('http://x/api/local/who', { headers: { 'x-token': 'xyz' } }));
|
||||
|
||||
@@ -42,14 +42,14 @@ const controlUrl = (port: number) => `ws://127.0.0.1:${port}/__mesh__/control`;
|
||||
|
||||
test('mesh on Bun: teacup resolves a Bun teapot through the graph', async () => {
|
||||
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
|
||||
const tServer = serveBun(teapot, { port: 0 });
|
||||
const tServer = await serveBun(teapot, { port: 0 });
|
||||
|
||||
const teacup = createApp({
|
||||
modules: [TeacupModule],
|
||||
experimental: true,
|
||||
mesh: { teapots: [{ url: controlUrl(tServer.port), secret: SECRET }] },
|
||||
});
|
||||
const cServer = serveBun(teacup, { port: 0 });
|
||||
const cServer = await serveBun(teacup, { port: 0 });
|
||||
|
||||
try {
|
||||
const res = await fetch(`http://127.0.0.1:${cServer.port}/api/local/who`, { headers: { 'x-token': 'abc' } });
|
||||
@@ -67,7 +67,7 @@ test('mesh on Bun: teacup resolves a Bun teapot through the graph', async () =>
|
||||
|
||||
test('mesh on Bun: a teapot refuses a bad secret over its control channel', async () => {
|
||||
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
|
||||
const tServer = serveBun(teapot, { port: 0 });
|
||||
const tServer = await serveBun(teapot, { port: 0 });
|
||||
|
||||
const teacup = createApp({
|
||||
modules: [TeacupModule],
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
// Run with: npm run test:bun (or: bun test test/bun/)
|
||||
import 'reflect-metadata';
|
||||
import { test, expect } from 'bun:test';
|
||||
import { createApp, Route, Get, Module } from '../../src/index.ts';
|
||||
import { createApp, Route, Get, Module, Provider, needs } from '../../src/index.ts';
|
||||
import { serveBun } from '../../src/bun.ts';
|
||||
|
||||
@Route('/')
|
||||
@@ -25,7 +25,7 @@ class Ctl {
|
||||
class M {}
|
||||
|
||||
test('serveBun close({ timeoutMs }) gives up on a stuck handler and resolves', async () => {
|
||||
const server = serveBun(createApp({ modules: [M] }), { port: 0 });
|
||||
const server = await serveBun(createApp({ modules: [M] }), { port: 0 });
|
||||
|
||||
fetch(`http://127.0.0.1:${server.port}/hang`).catch(() => {});
|
||||
await Bun.sleep(50); // let the request actually reach the handler
|
||||
@@ -40,7 +40,7 @@ test('serveBun close({ timeoutMs }) gives up on a stuck handler and resolves', a
|
||||
});
|
||||
|
||||
test('serveBun close() drains an in-flight request rather than cutting it', async () => {
|
||||
const server = serveBun(createApp({ modules: [M] }), { port: 0 });
|
||||
const server = await serveBun(createApp({ modules: [M] }), { port: 0 });
|
||||
|
||||
const inFlight = fetch(`http://127.0.0.1:${server.port}/slow`).then((r) => r.json());
|
||||
await Bun.sleep(50); // the request must be *in* the handler before we close, or it never got in
|
||||
@@ -65,10 +65,35 @@ test('serveBun close() runs registered teardown', async () => {
|
||||
}),
|
||||
}],
|
||||
});
|
||||
const server = serveBun(app, { port: 0 });
|
||||
const server = await serveBun(app, { port: 0 });
|
||||
|
||||
await server.close({ timeoutMs: 2000 });
|
||||
|
||||
// Reverse registration order: the plugin registered after the hook, so it tears down first.
|
||||
expect(closed).toEqual(['plugin', 'hook']);
|
||||
});
|
||||
|
||||
// The boot gate, which is why serveBun is async. Before it, a provider that threw was memoized as a
|
||||
// rejection and answered 500 to every request from the runtime, never reaching onError — so the
|
||||
// failure looked like a runtime fault at 3am instead of a deploy that refused to start.
|
||||
@Provider({ provides: 'key' })
|
||||
class BadKey {
|
||||
provide(): never {
|
||||
throw new Error('no such key file');
|
||||
}
|
||||
}
|
||||
@Route('/')
|
||||
class NeedsKey {
|
||||
@Get('/signed')
|
||||
signed(@needs('key') key: string) {
|
||||
return { key };
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/', providers: [BadKey], controllers: [NeedsKey] })
|
||||
class KeyModule {}
|
||||
|
||||
test('serveBun rejects when a provider fails, instead of binding a port that 500s', async () => {
|
||||
const app = createApp({ modules: [KeyModule] });
|
||||
|
||||
await expect(serveBun(app, { port: 0 })).rejects.toThrow(/no such key file/);
|
||||
});
|
||||
+1
-1
@@ -26,7 +26,7 @@ class M {}
|
||||
|
||||
test('serveBun: HTTP + WebSocket round-trip', async () => {
|
||||
const app = createApp({ modules: [M] });
|
||||
const server = serveBun(app, { port: 0 });
|
||||
const server = await serveBun(app, { port: 0 });
|
||||
const port = server.port;
|
||||
try {
|
||||
const httpRes = await fetch(`http://127.0.0.1:${port}/rt/hello`);
|
||||
|
||||
@@ -32,7 +32,10 @@ it('snapshots connection metadata before Deno invalidates the upgraded request',
|
||||
// `handleSignals` is a pass-through here: this test is about upgrade metadata, and the identity
|
||||
// it returns is exactly what `createApp` returns when `handleSignals` is left off.
|
||||
const handleSignals = <T,>(closer: T): T => closer;
|
||||
serveDeno({ upgrade, handleSignals } as unknown as App);
|
||||
// `serveDeno` boots before it binds, so the stub needs the gate even though this test never
|
||||
// exercises it.
|
||||
const boot = vi.fn().mockResolvedValue(undefined);
|
||||
await serveDeno({ upgrade, handleSignals, boot } as unknown as App);
|
||||
const info = {
|
||||
get remoteAddr() {
|
||||
if (upgraded) throw new TypeError('Request closed');
|
||||
|
||||
@@ -22,7 +22,7 @@ class TeapotModule {}
|
||||
async function startTeapot() {
|
||||
const app = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: 'good' } });
|
||||
let port = 0;
|
||||
const server = serveDeno(app, { port: 0, onListen: (addr) => (port = addr.port) });
|
||||
const server = await serveDeno(app, { port: 0, onListen: (addr) => (port = addr.port) });
|
||||
await new Promise((r) => setTimeout(r, 50));
|
||||
|
||||
return { port: () => port, stop: async () => { await server.shutdown(); await app.close(); } };
|
||||
|
||||
@@ -127,7 +127,7 @@ Deno.test('interop: a Node teacup consumes a Deno teapot', async () => {
|
||||
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
|
||||
let port = 0;
|
||||
const { serveDeno } = await import('../../src/deno.ts');
|
||||
const server = serveDeno(teapot, {
|
||||
const server = await serveDeno(teapot, {
|
||||
port: 0,
|
||||
onListen: (addr) => {
|
||||
port = addr.port;
|
||||
|
||||
@@ -49,7 +49,7 @@ function freePort(): number {
|
||||
/** Boot a teapot on a fixed port; the returned handle can kill it and leave the port free again. */
|
||||
async function startTeapot(port: number) {
|
||||
const app = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
|
||||
const server = serveDeno(app, { port });
|
||||
const server = await serveDeno(app, { port });
|
||||
await new Promise((r) => setTimeout(r, 50));
|
||||
|
||||
return {
|
||||
|
||||
@@ -46,7 +46,7 @@ class TeacupModule {}
|
||||
/** Starts an app on Deno and resolves once its port is known. */
|
||||
async function serve(app: ReturnType<typeof createApp>) {
|
||||
let port = 0;
|
||||
const server = serveDeno(app, {
|
||||
const server = await serveDeno(app, {
|
||||
port: 0,
|
||||
onListen: (addr) => {
|
||||
port = addr.port;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
//
|
||||
// Run with: npm run test:deno
|
||||
import 'reflect-metadata';
|
||||
import { createApp, Route, Get, Module } from '../../src/index.ts';
|
||||
import { createApp, Route, Get, Module, Provider, needs } from '../../src/index.ts';
|
||||
import { serveDeno } from '../../src/deno.ts';
|
||||
|
||||
@Route('/')
|
||||
@@ -30,7 +30,7 @@ const portOf = (server: { addr?: { port: number } }): number => server.addr!.por
|
||||
// the uncaught BadResource that aborting a draining server produces. The test runner fails on
|
||||
// that error even when the assertions pass, so a green run here is the real assertion.
|
||||
Deno.test('serveDeno close({ timeoutMs }) returns on a stuck handler without forcing', async () => {
|
||||
const server = serveDeno(createApp({ modules: [M] }), { port: 0, onListen: () => {} });
|
||||
const server = await serveDeno(createApp({ modules: [M] }), { port: 0, onListen: () => {} });
|
||||
|
||||
fetch(`http://127.0.0.1:${portOf(server)}/hang`).catch(() => {});
|
||||
await new Promise((r) => setTimeout(r, 50)); // let the request actually reach the handler
|
||||
@@ -45,7 +45,7 @@ Deno.test('serveDeno close({ timeoutMs }) returns on a stuck handler without for
|
||||
});
|
||||
|
||||
Deno.test('serveDeno close() drains an in-flight request rather than cutting it', async () => {
|
||||
const server = serveDeno(createApp({ modules: [M] }), { port: 0, onListen: () => {} });
|
||||
const server = await serveDeno(createApp({ modules: [M] }), { port: 0, onListen: () => {} });
|
||||
|
||||
const inFlight = fetch(`http://127.0.0.1:${portOf(server)}/slow`).then((r) => r.json());
|
||||
await new Promise((r) => setTimeout(r, 50)); // must be *in* the handler before we close
|
||||
@@ -58,7 +58,7 @@ Deno.test('serveDeno close() drains an in-flight request rather than cutting it'
|
||||
|
||||
Deno.test('a caller-supplied signal still aborts the server', async () => {
|
||||
const ac = new AbortController();
|
||||
const server = serveDeno(createApp({ modules: [M] }), { port: 0, signal: ac.signal, onListen: () => {} });
|
||||
const server = await serveDeno(createApp({ modules: [M] }), { port: 0, signal: ac.signal, onListen: () => {} });
|
||||
|
||||
ac.abort();
|
||||
await server.finished; // hangs here if chaining the caller's signal into ours dropped it
|
||||
@@ -80,10 +80,44 @@ Deno.test('serveDeno close() runs registered teardown', async () => {
|
||||
}),
|
||||
}],
|
||||
});
|
||||
const server = serveDeno(app, { port: 0 });
|
||||
const server = await serveDeno(app, { port: 0 });
|
||||
|
||||
await server.close({ timeoutMs: 2000 });
|
||||
|
||||
// Reverse registration order: the plugin registered after the hook, so it tears down first.
|
||||
if (closed.join(',') !== 'plugin,hook') throw new Error(`expected plugin,hook — got ${closed.join(',')}`);
|
||||
});
|
||||
|
||||
// The boot gate, which is why serveDeno is async. Before it, a provider that threw was memoized as
|
||||
// a rejection and answered 500 to every request from the runtime, never reaching onError — so the
|
||||
// failure looked like a runtime fault at 3am instead of a deploy that refused to start.
|
||||
@Provider({ provides: 'key' })
|
||||
class BadKey {
|
||||
provide(): never {
|
||||
throw new Error('no such key file');
|
||||
}
|
||||
}
|
||||
@Route('/')
|
||||
class NeedsKey {
|
||||
@Get('/signed')
|
||||
signed(@needs('key') key: string) {
|
||||
return { key };
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/', providers: [BadKey], controllers: [NeedsKey] })
|
||||
class KeyModule {}
|
||||
|
||||
Deno.test('serveDeno rejects when a provider fails, instead of binding a port that 500s', async () => {
|
||||
const app = createApp({ modules: [KeyModule] });
|
||||
let message = '';
|
||||
|
||||
try {
|
||||
await serveDeno(app, { port: 0, onListen: () => {} });
|
||||
} catch (error) {
|
||||
message = (error as Error).message;
|
||||
}
|
||||
|
||||
if (!message.includes('no such key file')) {
|
||||
throw new Error(`expected serveDeno to reject with the provider's error, got: ${message || '<resolved>'}`);
|
||||
}
|
||||
});
|
||||
@@ -37,7 +37,7 @@ Deno.test('serveDeno: HTTP + WebSocket round-trip through the graph', async () =
|
||||
const app = createApp({ modules: [M] });
|
||||
|
||||
let port = 0;
|
||||
const server = serveDeno(app, {
|
||||
const server = await serveDeno(app, {
|
||||
port: 0,
|
||||
onListen: (addr) => {
|
||||
port = addr.port;
|
||||
|
||||
@@ -23,7 +23,7 @@ const app = createApp({
|
||||
// `ready` is printed after `serveDeno` returns, not from `onListen` — Deno calls `onListen`
|
||||
// synchronously from inside `Deno.serve`, so a parent that signals on that line races the handler
|
||||
// registration that happens a few statements later and wins.
|
||||
const server = serveDeno(app, { port: 0, onListen: () => {} });
|
||||
const server = await serveDeno(app, { port: 0, onListen: () => {} });
|
||||
|
||||
console.log(JSON.stringify({ ready: true }));
|
||||
void server;
|
||||
Reference in new issue
Block a user