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 <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-09-16 22:26:28 -05:00
1 parent 80f3f532f2
commit 3fc761ce16
3 files changed
+75

No files matched your search

+1
View File
@@ -392,6 +392,7 @@ export function createApp(opts: {
close,
handleSignals,
ready,
boot: bootApp,
fetch: fetchFn,
upgrade: upgradeFn,
inspect,
+15
View File
@@ -59,6 +59,21 @@ export interface App {
* shares this same memoized step — calling both never resolves the graph twice.
*/
ready(): Promise<void>;
/**
* 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<void>;
/** 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<Response>;
/** Run a WebSocket upgrade through the graph using an adapter-provided socket (Deno/Bun/edge). Node uses its own listener path. */
+59
View File
@@ -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<Record<string, unknown>>): 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);
});
});