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
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user