From 23a31f9264d56cf9f8109d1ffda79a2471e389d7 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Fri, 26 Jun 2026 00:47:55 -0500 Subject: [PATCH] feat: request pipeline with ctx accumulation, throw handling, lifecycle events --- src/pipeline.ts | 31 +++++++++++++++++++++++++++++++ test/pipeline.test.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 src/pipeline.ts create mode 100644 test/pipeline.test.ts diff --git a/src/pipeline.ts b/src/pipeline.ts new file mode 100644 index 0000000..02ebf28 --- /dev/null +++ b/src/pipeline.ts @@ -0,0 +1,31 @@ +import { Bus } from './bus'; +import { errorToResponse } from './transformers'; +import type { TransformerFn } from './metadata'; + +export interface PipelineStep { name: string; origin: string; run: (ctx: any) => any } +export interface ResponseShape { status: number; headers: Record; body: string } + +export async function runPipeline(args: { + steps: PipelineStep[]; + handler: (ctx: any) => unknown; + transformer: TransformerFn; + seed: Record; + bus: Bus; +}): Promise { + const { steps, handler, transformer, seed, bus } = args; + let ctx: any = { ...seed }; + try { + for (const s of steps) { + bus.emit('request:step:enter', { name: s.name, scope: s.origin }); + const out = await s.run(ctx); + ctx = { ...ctx, ...out }; + bus.emit('request:step:leave', { name: s.name, scope: s.origin }); + } + const result = await handler(ctx); + const r = transformer(result); + return { status: r.status ?? 200, headers: r.headers ?? {}, body: r.body }; + } catch (error) { + bus.emit('request:step:error', { name: 'pipeline', error }); + return errorToResponse(error); + } +} diff --git a/test/pipeline.test.ts b/test/pipeline.test.ts new file mode 100644 index 0000000..83985e8 --- /dev/null +++ b/test/pipeline.test.ts @@ -0,0 +1,42 @@ +import { expect, test, vi } from 'vitest'; +import { runPipeline } from '../src/pipeline'; +import { Bus } from '../src/bus'; +import { JsonTransformer } from '../src/transformers'; +import { Unauthorized } from '../src/signals'; + +const base = { transformer: JsonTransformer, seed: { req: { token: 'abc' } } }; + +test('runs steps, accumulates ctx, transforms handler return', async () => { + const bus = new Bus(); + const res = await runPipeline({ + ...base, bus, + steps: [{ name: 'auth', origin: 'mod', run: (ctx) => ({ user: ctx.req.token }) }], + handler: (ctx) => ({ user: ctx.user }), + }); + expect(res.status).toBe(200); + expect(JSON.parse(res.body)).toEqual({ user: 'abc' }); +}); + +test('a thrown HttpError short-circuits to an error response', async () => { + const bus = new Bus(); + const res = await runPipeline({ + ...base, bus, + steps: [{ name: 'auth', origin: 'mod', run: () => { throw new Unauthorized('no'); } }], + handler: () => ({ never: true }), + }); + expect(res.status).toBe(401); +}); + +test('emits enter/leave/error lifecycle events', async () => { + const bus = new Bus(); + const enter = vi.fn(); const error = vi.fn(); + bus.on('request:step:enter', enter); + bus.on('request:step:error', error); + await runPipeline({ + ...base, bus, + steps: [{ name: 'boom', origin: 'mod', run: () => { throw new Unauthorized(); } }], + handler: () => ({}), + }); + expect(enter).toHaveBeenCalledWith(expect.objectContaining({ name: 'boom' })); + expect(error).toHaveBeenCalled(); +});