fix: guard handler errors, dedupe runner names, export flow core, test optional provider
This commit is contained in:
4 files changed
+32
-4
No files matched your search
+7
-3
@@ -37,6 +37,10 @@ export function createApp(opts: { modules: Ctor[]; plugins?: Plugin[] }): App {
|
|||||||
const providerNodes: GraphNode[] = [];
|
const providerNodes: GraphNode[] = [];
|
||||||
const stepNodes: GraphNode[] = [];
|
const stepNodes: GraphNode[] = [];
|
||||||
const runners = new Map<string, (ctx: any) => any>(); // node name -> runtime fn
|
const runners = new Map<string, (ctx: any) => any>(); // node name -> runtime fn
|
||||||
|
const setRunner = (name: string, fn: (ctx: any) => any) => {
|
||||||
|
if (runners.has(name)) throw new Error(`duplicate provider/step name '${name}' — names must be unique across modules and plugins`);
|
||||||
|
runners.set(name, fn);
|
||||||
|
};
|
||||||
const providerMeta = new Map<string, { optional: boolean }>();
|
const providerMeta = new Map<string, { optional: boolean }>();
|
||||||
const routePlans: RoutePlan[] = [];
|
const routePlans: RoutePlan[] = [];
|
||||||
|
|
||||||
@@ -50,13 +54,13 @@ export function createApp(opts: { modules: Ctor[]; plugins?: Plugin[] }): App {
|
|||||||
providerNodes.push({ name: meta.provides, needs: meta.needs, provides: [meta.provides], origin });
|
providerNodes.push({ name: meta.provides, needs: meta.needs, provides: [meta.provides], origin });
|
||||||
providerMeta.set(meta.provides, { optional: meta.optional });
|
providerMeta.set(meta.provides, { optional: meta.optional });
|
||||||
const inst: any = new P();
|
const inst: any = new P();
|
||||||
runners.set(meta.provides, (ctx) => inst.provide(ctx));
|
setRunner(meta.provides, (ctx) => inst.provide(ctx));
|
||||||
}
|
}
|
||||||
for (const S of m.steps ?? []) {
|
for (const S of m.steps ?? []) {
|
||||||
const meta = getStepMeta(S)!;
|
const meta = getStepMeta(S)!;
|
||||||
stepNodes.push({ name: meta.provides, needs: meta.needs, provides: [meta.provides], origin });
|
stepNodes.push({ name: meta.provides, needs: meta.needs, provides: [meta.provides], origin });
|
||||||
const inst: any = new S();
|
const inst: any = new S();
|
||||||
runners.set(meta.provides, (ctx) => inst.run(ctx));
|
setRunner(meta.provides, (ctx) => inst.run(ctx));
|
||||||
}
|
}
|
||||||
for (const C of m.controllers ?? []) {
|
for (const C of m.controllers ?? []) {
|
||||||
for (const route of getRoutes(C)) {
|
for (const route of getRoutes(C)) {
|
||||||
@@ -79,7 +83,7 @@ export function createApp(opts: { modules: Ctor[]; plugins?: Plugin[] }): App {
|
|||||||
for (const n of extraSteps) {
|
for (const n of extraSteps) {
|
||||||
const node = { name: n.name, needs: n.needs, provides: n.provides, origin: 'plugin' };
|
const node = { name: n.name, needs: n.needs, provides: n.provides, origin: 'plugin' };
|
||||||
if (n.kind === 'provider') providerNodes.push(node); else stepNodes.push(node);
|
if (n.kind === 'provider') providerNodes.push(node); else stepNodes.push(node);
|
||||||
runners.set(n.name, n.run);
|
setRunner(n.name, n.run);
|
||||||
}
|
}
|
||||||
|
|
||||||
// For each route, resolve which providers/steps feed it via topo order.
|
// For each route, resolve which providers/steps feed it via topo order.
|
||||||
|
|||||||
+7
-1
@@ -1,5 +1,6 @@
|
|||||||
import http from 'http';
|
import http from 'http';
|
||||||
import type { ResponseShape } from './pipeline';
|
import type { ResponseShape } from './pipeline';
|
||||||
|
import { errorToResponse } from './transformers';
|
||||||
|
|
||||||
export type RouteHandler = (req: {
|
export type RouteHandler = (req: {
|
||||||
method: string; url: string;
|
method: string; url: string;
|
||||||
@@ -36,9 +37,14 @@ export function createHttpServer(routes: RouteDef[]): http.Server {
|
|||||||
res.end(JSON.stringify({ error: 'Not Found' }));
|
res.end(JSON.stringify({ error: 'Not Found' }));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const result = await matched.handler({
|
let result: ResponseShape;
|
||||||
|
try {
|
||||||
|
result = await matched.handler({
|
||||||
method: req.method ?? 'GET', url: req.url ?? '/', headers: req.headers, params: matched.params,
|
method: req.method ?? 'GET', url: req.url ?? '/', headers: req.headers, params: matched.params,
|
||||||
});
|
});
|
||||||
|
} catch (error) {
|
||||||
|
result = errorToResponse(error);
|
||||||
|
}
|
||||||
res.writeHead(result.status, result.headers);
|
res.writeHead(result.status, result.headers);
|
||||||
res.end(result.body);
|
res.end(result.body);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -8,3 +8,5 @@ export type { Plugin } from './plugin';
|
|||||||
export { JsonTransformer } from './transformers';
|
export { JsonTransformer } from './transformers';
|
||||||
export { HttpError, Unauthorized, NotFound, Redirect, NotModified } from './signals';
|
export { HttpError, Unauthorized, NotFound, Redirect, NotModified } from './signals';
|
||||||
export { Bus } from './bus';
|
export { Bus } from './bus';
|
||||||
|
export { flow, Flow } from './flow';
|
||||||
|
export type { StepFn, CompiledFlow } from './flow';
|
||||||
@@ -60,3 +60,19 @@ test('required provider failure aborts boot', () => {
|
|||||||
const app = createApp({ modules: [BrokenModule] });
|
const app = createApp({ modules: [BrokenModule] });
|
||||||
return expect(app.listen(0)).rejects.toThrow(/provider 'broken' failed/);
|
return expect(app.listen(0)).rejects.toThrow(/provider 'broken' failed/);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('optional provider failure warns but does not abort boot', async () => {
|
||||||
|
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||||
|
@Provider({ provides: 'cache', optional: true })
|
||||||
|
class FlakyCache { provide() { throw new Error('cache down'); } }
|
||||||
|
@Route('/ping')
|
||||||
|
class PingCtl { @Get('/') ping() { return { ok: true }; } }
|
||||||
|
@Module({ mountpoint: '/sys', providers: [FlakyCache], steps: [], controllers: [PingCtl] })
|
||||||
|
class SysModule {}
|
||||||
|
|
||||||
|
const app = createApp({ modules: [SysModule] });
|
||||||
|
const server = await app.listen(0); // must NOT throw
|
||||||
|
expect(warn).toHaveBeenCalledWith(expect.stringContaining("optional provider 'cache' failed"));
|
||||||
|
server.close();
|
||||||
|
warn.mockRestore();
|
||||||
|
});
|
||||||
Reference in new issue
Block a user