fix(bus): emit step events for ws and mesh routes

Steps were executed by three separate hand-rolled loops: runPipeline (HTTP),
buildWsRoutes (ws) and resolveScope (mesh). Only the HTTP one emitted
request:step:enter/leave, so a logging plugin silently observed nothing on ws
routes — a gap in a documented public API, not just redundancy.

Route every transport through a single runSteps() so observability is uniform
by construction rather than by remembering to copy the emit calls.

Also fixes the plugins guide, which documented a request:step:exit event that
has never existed (the bus emits request:step:leave).

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-07-15 14:37:03 -05:00
1 parent 44cf547f1b
commit 922700674c
4 files changed
+81 -34

No files matched your search

+38 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { createApp, Route, Ws, Module, ctx } from '../src/index';
import { createApp, Route, Ws, Module, ctx, Step, needs } from '../src/index';
import { channel } from '../src/channel';
import type { WsSocket, WsRequest } from '../src/http/ws-core';
@@ -16,6 +16,24 @@ class Rt {
class M {}
const app = createApp({ modules: [M] });
@Step({ provides: 'tag' })
class Tagger {
run() {
return { tag: 't' };
}
}
@Route('/tagged')
class TaggedCtl {
@Ws('/echo')
echo(@needs('tag') tag: string, @ctx() { inbound }: any) {
return (async function* () {
for await (const msg of inbound) yield `${tag}:${msg}`;
})();
}
}
@Module({ mountpoint: '/', steps: [Tagger], controllers: [TaggedCtl] })
class Tagged {}
function fakeSocket() {
const inbound = channel<unknown>();
const ac = new AbortController();
@@ -46,6 +64,25 @@ describe('app.upgrade', () => {
expect(sent).toEqual(['echo:hi']);
});
it('emits request:step:enter/leave for ws steps, like HTTP routes do', async () => {
const seen: string[] = [];
const logger = (api: any) => {
api.bus.on('request:step:enter', (p: any) => seen.push(`enter:${p.name}`));
api.bus.on('request:step:leave', (p: any) => seen.push(`leave:${p.name}`));
};
const wsApp = createApp({ modules: [Tagged], plugins: [logger] });
const { socket, inbound, sent } = fakeSocket();
const req: WsRequest = { url: '/tagged/echo', headers: {}, protocol: 'http', ip: '' };
const run = wsApp.upgrade(req, socket);
await new Promise((resolve) => setTimeout(resolve, 0));
inbound.push('hi');
inbound.close();
await run;
expect(sent).toEqual(['t:hi']);
expect(seen).toEqual(['enter:tag', 'leave:tag']);
});
it('closes 1008 on an unknown ws path', async () => {
const { socket } = fakeSocket();
let closedCode: number | undefined;