From 3fc761ce162319b03929a90f986f70a619bb49c3 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Wed, 16 Sep 2026 22:26:28 -0500 Subject: [PATCH] feat(app): expose boot() so providers can fail before serving Only listen() booted before accepting traffic. Deno, Bun and edge all boot on the first request, and the memo keeps the rejection, so a bad key answered 500 for the life of the process while the app looked healthy. Signed-off-by: Diego Resendez --- src/app/index.ts | 1 + src/app/types.ts | 15 +++++++++++ test/app-boot.test.ts | 59 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 test/app-boot.test.ts diff --git a/src/app/index.ts b/src/app/index.ts index 5659b44..1a0107b 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -392,6 +392,7 @@ export function createApp(opts: { close, handleSignals, ready, + boot: bootApp, fetch: fetchFn, upgrade: upgradeFn, inspect, diff --git a/src/app/types.ts b/src/app/types.ts index e597582..2d0bb45 100644 --- a/src/app/types.ts +++ b/src/app/types.ts @@ -59,6 +59,21 @@ export interface App { * shares this same memoized step — calling both never resolves the graph twice. */ ready(): Promise; + /** + * Boots the providers now, instead of on the first request. + * + * `listen()` already does this before it accepts a connection. Every other path — `app.fetch`, + * `app.upgrade`, `serveDeno`, `serveBun`, `edgeHandler` — boots lazily on the first request, and a + * provider that throws there fails *every* request, answered by the runtime with a 500 that never + * reaches `onError`. Calling this first turns that into a startup failure, which is where a bad + * key or an unreachable database belongs. + * + * Idempotent, and shares its memo with `listen()` and `fetch()`: calling both boots once. Unlike + * `ready()`, it does run provider factories. + * + * On workerd there is no startup outside a request, so calling it moves nothing. + */ + boot(): Promise; /** Web-Standards handler: run a Fetch API Request through the graph and return a Response (Node/Deno/Bun/edge). WS not included. */ fetch(request: Request): Promise; /** Run a WebSocket upgrade through the graph using an adapter-provided socket (Deno/Bun/edge). Node uses its own listener path. */ diff --git a/test/app-boot.test.ts b/test/app-boot.test.ts new file mode 100644 index 0000000..786f20a --- /dev/null +++ b/test/app-boot.test.ts @@ -0,0 +1,59 @@ +// test/app-boot.test.ts +import { describe, expect, it, vi } from 'vitest'; + +import { createApp, Get, Module, Route, needs } from '../src/index'; +import type { Plugin } from '../src/index'; + +@Route('/') +class Ctl { + @Get('/x') + x(@needs('thing') thing: string) { + return { thing }; + } +} + +@Module({ mountpoint: '/', controllers: [Ctl] }) +class M {} + +const plugin = (run: () => Promise>): Plugin => ({ + name: 'thing', + mount({ scope }) { + scope.add({ kind: 'provider', name: 'thing', needs: [], provides: ['thing'], run }); + }, +}); + +describe('app.boot()', () => { + it('surfaces a failing provider instead of leaving it for the first request', async () => { + const app = createApp({ modules: [M], plugins: [plugin(async () => { throw new Error('bad key'); })] }); + + await expect(app.boot()).rejects.toThrow(/bad key/); + }); + + it('runs provider factories exactly once, and serves afterwards', async () => { + const run = vi.fn(async () => ({ thing: 'ok' })); + const app = createApp({ modules: [M], plugins: [plugin(run)] }); + + await app.boot(); + await app.boot(); + const response = await app.fetch(new Request('http://x/x')); + + expect(run).toHaveBeenCalledTimes(1); + expect(await response.json()).toEqual({ thing: 'ok' }); + }); + + // The memo keeps the rejection, which is exactly why a failed boot answers 500 for the life of + // the process instead of retrying. It is the documented behaviour, so it gets a test rather than + // a sentence: if it ever starts retrying, the guide in Task 7 becomes wrong. + it('keeps a failed boot failed', async () => { + const run = vi.fn(async () => { + throw new Error('bad key'); + }); + const app = createApp({ modules: [M], plugins: [plugin(run)] }); + + await expect(app.boot()).rejects.toThrow(/bad key/); + await expect(app.boot()).rejects.toThrow(/bad key/); + await expect(app.fetch(new Request('http://x/x'))).rejects.toThrow(/bad key/); + + expect(run).toHaveBeenCalledTimes(1); + }); +});