feat: pipeline returns StreamResult for async-iterable handlers

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-06-27 15:15:05 -05:00
1 parent a3dad32384
commit a94a987a35
2 files changed
+40 -6

No files matched your search

+31 -5
View File
@@ -1,5 +1,5 @@
import { expect, test, vi } from 'vitest';
import { runPipeline } from '../src/pipeline';
import { describe, expect, it, test, vi } from 'vitest';
import { runPipeline, isStreamResult } from '../src/pipeline';
import { Bus } from '../src/bus';
import { JsonTransformer } from '../src/transformers';
import { Unauthorized } from '../src/signals';
@@ -13,8 +13,11 @@ test('runs steps, accumulates ctx, transforms handler return', async () => {
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' });
expect(isStreamResult(res)).toBe(false);
if (!isStreamResult(res)) {
expect(res.status).toBe(200);
expect(JSON.parse(res.body)).toEqual({ user: 'abc' });
}
});
test('a thrown HttpError short-circuits to an error response', async () => {
@@ -24,7 +27,8 @@ test('a thrown HttpError short-circuits to an error response', async () => {
steps: [{ name: 'auth', origin: 'mod', run: () => { throw new Unauthorized('no'); } }],
handler: () => ({ never: true }),
});
expect(res.status).toBe(401);
expect(isStreamResult(res)).toBe(false);
if (!isStreamResult(res)) expect(res.status).toBe(401);
});
test('emits enter/leave/error lifecycle events', async () => {
@@ -40,3 +44,25 @@ test('emits enter/leave/error lifecycle events', async () => {
expect(enter).toHaveBeenCalledWith(expect.objectContaining({ name: 'boom' }));
expect(error).toHaveBeenCalled();
});
describe('runPipeline streaming', () => {
const bus = new Bus();
const noTransform = (v: unknown) => ({ body: String(v) });
it('returns a StreamResult when the handler returns an AsyncIterable (transformer bypassed)', async () => {
async function* gen() { yield 1; yield 2; }
const res = await runPipeline({ steps: [], handler: () => gen(), transformer: noTransform, seed: {}, bus });
expect(isStreamResult(res)).toBe(true);
if (isStreamResult(res)) {
const out: unknown[] = [];
for await (const v of res.stream) out.push(v);
expect(out).toEqual([1, 2]);
}
});
it('still buffers a normal return through the transformer', async () => {
const res = await runPipeline({ steps: [], handler: () => 'hi', transformer: () => ({ body: 'HI' }), seed: {}, bus });
expect(isStreamResult(res)).toBe(false);
if (!isStreamResult(res)) expect(res.body).toBe('HI');
});
});