@nwire/hooks 0.7.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +663 -33
- package/dist/hook.d.ts +32 -15
- package/dist/hook.d.ts.map +1 -1
- package/dist/hook.js +196 -96
- package/dist/hook.js.map +1 -1
- package/dist/index.d.ts +10 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -4
- package/dist/index.js.map +1 -1
- package/dist/record.d.ts +50 -0
- package/dist/record.d.ts.map +1 -0
- package/dist/record.js +86 -0
- package/dist/record.js.map +1 -0
- package/dist/registry.d.ts +54 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +103 -0
- package/dist/registry.js.map +1 -0
- package/dist/types.d.ts +97 -5
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +8 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,57 +1,687 @@
|
|
|
1
1
|
# @nwire/hooks
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> The universal dispatch primitive. Every middleware, lifecycle phase, event
|
|
4
|
+
> subscription, plugin extension, and dev-tool tap in Nwire is built from one
|
|
5
|
+
> shape: a named hook that runs a koa-compose chain first, then fans out
|
|
6
|
+
> parallel listeners. Auditable, traceable, replayable, topology-aware.
|
|
4
7
|
|
|
5
|
-
|
|
8
|
+
```bash
|
|
9
|
+
pnpm add @nwire/hooks
|
|
10
|
+
```
|
|
6
11
|
|
|
7
|
-
|
|
12
|
+
---
|
|
8
13
|
|
|
9
|
-
##
|
|
14
|
+
## TL;DR
|
|
10
15
|
|
|
11
|
-
```
|
|
12
|
-
|
|
16
|
+
```ts
|
|
17
|
+
import { hook } from "@nwire/hooks";
|
|
18
|
+
|
|
19
|
+
const request = hook<{ url: string; user?: string }>("http.request");
|
|
20
|
+
|
|
21
|
+
request.use(async (ctx, next) => { ctx.user = await auth(ctx); await next(); });
|
|
22
|
+
request.use(async (ctx, next) => { /* metrics */ await next(); }, { priority: 100 });
|
|
23
|
+
request.on((ctx) => analytics.track(ctx), { when: "success" });
|
|
24
|
+
|
|
25
|
+
await request.run({ url: "/api/x" });
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Two attachment kinds, one primitive:
|
|
29
|
+
|
|
30
|
+
- **`.use(fn)`** — chain step. koa-compose-shaped `(ctx, next)`. Can mutate ctx.
|
|
31
|
+
Can short-circuit by not calling `next()`. Throws bubble up.
|
|
32
|
+
- **`.on(fn)`** — listener. Observer `(ctx)`. Cannot mutate. Cannot bail.
|
|
33
|
+
Fired in parallel via `Promise.allSettled` after the chain settles.
|
|
34
|
+
|
|
35
|
+
Everything else — telemetry taps, source capture, run-id linkage, recording,
|
|
36
|
+
priorities, success/failure filters — sits on top of those two without
|
|
37
|
+
changing them.
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## Why this exists
|
|
42
|
+
|
|
43
|
+
The Nwire stack used to ship six overlapping middleware/hook substrates —
|
|
44
|
+
one per package, each with its own composer, error policy, and observability
|
|
45
|
+
story. `@nwire/hooks` is the single primitive they all reduce to:
|
|
46
|
+
|
|
47
|
+
| Substrate | Maps to |
|
|
48
|
+
| -------------------------------------------- | ------------------------------------------------------ |
|
|
49
|
+
| `@nwire/handler` `defineMiddleware` + `pipe` | chain via `.use()` |
|
|
50
|
+
| `@nwire/handler` `defineHook("after", fn)` | listener via `.on(fn, { when: "success" })` |
|
|
51
|
+
| `@nwire/forge` `runtime.use(mw)` | chain via `.use()` on the dispatch hook |
|
|
52
|
+
| `@nwire/http` `httpInterface().use()` | chain via `.use()` on the http-request hook |
|
|
53
|
+
| `@nwire/http` per-route `middleware: […]` | per-route hook + chain |
|
|
54
|
+
| `definePlugin({ middleware, actorHooks })` | chain (middleware) + listener (actorHooks) |
|
|
55
|
+
| `definePlugin({ before, after })` | chain veto (`before`) + filtered listener (`after`) |
|
|
56
|
+
| `@nwire/app` framework events — `parallel` | listener fan-out |
|
|
57
|
+
| `@nwire/app` framework events — `series` | chain without bail |
|
|
58
|
+
| `@nwire/app` framework events — `series-bail`| chain with `.runDetailed()` reading `outcome === "prevented"` |
|
|
59
|
+
|
|
60
|
+
If you're building a new extension point: it's a hook.
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## The contract — behavior matrix
|
|
65
|
+
|
|
66
|
+
| Question | Answer |
|
|
67
|
+
| ----------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
|
|
68
|
+
| Chain ordering | Higher `priority` first (outermost). Stable on ties — equal priority preserves insertion order. |
|
|
69
|
+
| Listener ordering | Higher `priority` first. Listeners run in parallel; ordering is *attempt* order only. |
|
|
70
|
+
| What if a chain step doesn't call `next()`? | Chain short-circuits. `outcome === "prevented"`. Listeners still fire (subject to `when`). |
|
|
71
|
+
| What if a chain step throws? | `.run()` rejects with the error. `outcome === "failed"`. Listeners still fire (subject to `when`). `ctx.error` is set. |
|
|
72
|
+
| What if a listener throws? | Reported via `onListenerError` (default: `console.error`). The run does **not** fail. Opt in with `strictListeners: true`. |
|
|
73
|
+
| Can a listener cancel the run? | No. |
|
|
74
|
+
| Can a chain step mutate ctx? | Yes. Listeners see the post-chain ctx. |
|
|
75
|
+
| Can listeners run before the chain finishes? | No. |
|
|
76
|
+
| Is `next()` allowed to be called twice? | No — throws `Error("next() called multiple times in @nwire/hooks chain")`. |
|
|
77
|
+
| Are taps observers or participants? | Observers. Tap throws are swallowed. |
|
|
78
|
+
| Run-id propagation across nested `.run()` calls? | Automatic via `AsyncLocalStorage`. Inner run's `parentRunId` = outer run's `runId`. |
|
|
79
|
+
| Is recording side-effect free? | No — it executes the hook normally and snapshots ctx before + after. Replay re-executes the chain. |
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
## Core API
|
|
84
|
+
|
|
85
|
+
### `hook(name, options?)`
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
import { hook } from "@nwire/hooks";
|
|
89
|
+
|
|
90
|
+
const lifecycle = hook<EndpointCtx>("endpoint.boot", {
|
|
91
|
+
strictListeners: false, // default
|
|
92
|
+
onListenerError: (err, ctx, hookName) => logger.error(...),
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Creates a fresh hook. Captures the call-site source location for Studio +
|
|
97
|
+
`nwire scan`. Adds it to the process-wide registry so `listHooks()` sees it.
|
|
98
|
+
|
|
99
|
+
### `Hook<Ctx>` surface
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
interface Hook<Ctx> {
|
|
103
|
+
readonly name: string;
|
|
104
|
+
readonly id: string; // process-unique
|
|
105
|
+
|
|
106
|
+
use(fn: ChainFn<Ctx>, opts?: UseOptions): this; // chain step
|
|
107
|
+
on (fn: ListenerFn<Ctx>, opts?: OnOptions): this; // listener
|
|
108
|
+
off(fn): this; // detach
|
|
109
|
+
|
|
110
|
+
run(ctx: Ctx, opts?: RunOptions): Promise<Ctx>;
|
|
111
|
+
runDetailed(ctx, opts?): Promise<RunResult<Ctx>>;
|
|
112
|
+
|
|
113
|
+
tap(observer): () => void; // per-step trace
|
|
114
|
+
stepCounts(): { chain: number; listeners: number };
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
#### `UseOptions` / `OnOptions`
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
interface UseOptions {
|
|
122
|
+
name?: string; // human label — shown in taps, OTel, Studio
|
|
123
|
+
priority?: number; // higher = earlier in chain (default 0)
|
|
124
|
+
}
|
|
125
|
+
interface OnOptions extends UseOptions {
|
|
126
|
+
when?: "always" | "success" | "failure"; // default "always"
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
#### `RunOptions`
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
interface RunOptions {
|
|
134
|
+
signal?: AbortSignal; // injects onto ctx.signal too; thrown if aborted before next step
|
|
135
|
+
parentRunId?: string; // override the ambient parent
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
#### `RunResult<Ctx>`
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
interface RunResult<Ctx> {
|
|
143
|
+
ctx: Ctx;
|
|
144
|
+
outcome: "completed" | "prevented" | "failed";
|
|
145
|
+
runId: string;
|
|
146
|
+
parentRunId?: string;
|
|
147
|
+
steps: StepObservation[];
|
|
148
|
+
durationMs: number;
|
|
149
|
+
error?: unknown; // set when outcome === "failed"
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
`run(ctx)` is the simple form. It returns the (mutated) ctx and throws on
|
|
154
|
+
failure — the way every existing call site has worked for years.
|
|
155
|
+
`runDetailed(ctx)` returns the structured result instead. Use it when you
|
|
156
|
+
need the outcome distinction (e.g. framework events that need to know
|
|
157
|
+
"was this prevented?").
|
|
158
|
+
|
|
159
|
+
### `compose(fns)` / `pipe(...fns)` / `withTimeout(ms, fn)` / `withRetry(opts, fn)`
|
|
160
|
+
|
|
161
|
+
Helpers for assembling reusable chain pieces before attaching them to a hook.
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
import { pipe, withRetry, withTimeout } from "@nwire/hooks";
|
|
165
|
+
|
|
166
|
+
const protected = pipe(
|
|
167
|
+
authenticate,
|
|
168
|
+
withRetry({ attempts: 3, delayMs: 100 }, callExternalApi),
|
|
169
|
+
withTimeout(5_000, persistResult),
|
|
170
|
+
);
|
|
171
|
+
hook.use(protected);
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
## Observability — what every hook surfaces for free
|
|
177
|
+
|
|
178
|
+
Every hook emits a stream of `StepObservation` events. Subscribe with
|
|
179
|
+
`.tap(observer)` to wire it into:
|
|
180
|
+
|
|
181
|
+
- structured logs (`logger.info(obs)`)
|
|
182
|
+
- Studio Live (push over SSE)
|
|
183
|
+
- `nwire scan` (write to `.nwire/observations.json`)
|
|
184
|
+
- OTel adapter (one span per `start → end` pair)
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
interface StepObservation {
|
|
188
|
+
hookName: string;
|
|
189
|
+
hookId: string;
|
|
190
|
+
runId: string;
|
|
191
|
+
parentRunId?: string;
|
|
192
|
+
stepId: number;
|
|
193
|
+
stepKind: "chain" | "listener";
|
|
194
|
+
stepName?: string; // from UseOptions.name / OnOptions.name
|
|
195
|
+
phase: "start" | "end" | "error";
|
|
196
|
+
ts: number; // performance.now()
|
|
197
|
+
durationMs?: number; // set on end + error
|
|
198
|
+
error?: unknown; // set on error
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Topology — caller / callee linkage
|
|
203
|
+
|
|
204
|
+
`AsyncLocalStorage` propagates the active `runId` through the chain. When
|
|
205
|
+
your chain step calls `await otherHook.run(...)`, the nested run picks up
|
|
206
|
+
the outer `runId` as its `parentRunId` automatically — no plumbing.
|
|
207
|
+
|
|
208
|
+
```ts
|
|
209
|
+
import { hook, currentRun } from "@nwire/hooks";
|
|
210
|
+
|
|
211
|
+
const outer = hook<{}>("outer");
|
|
212
|
+
const inner = hook<{}>("inner");
|
|
213
|
+
|
|
214
|
+
outer.use(async (_, next) => {
|
|
215
|
+
await inner.run({}); // inner's parentRunId === outer's runId
|
|
216
|
+
await next();
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
inner.use(async () => {
|
|
220
|
+
console.log(currentRun()); // { runId, hookId, parentRunId }
|
|
221
|
+
});
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
This is the substrate Studio's Trace page uses to render the causal tree.
|
|
225
|
+
|
|
226
|
+
### Source location
|
|
227
|
+
|
|
228
|
+
`hook()` captures its call site at construction. `listHooks()` exposes it
|
|
229
|
+
to `nwire scan` and Studio so every hook in the manifest has a click-to-open
|
|
230
|
+
pill — same UX as actions/events/projections.
|
|
231
|
+
|
|
232
|
+
```ts
|
|
233
|
+
import { listHooks } from "@nwire/hooks";
|
|
234
|
+
|
|
235
|
+
console.log(listHooks());
|
|
236
|
+
// [
|
|
237
|
+
// { id: "h1", name: "endpoint.boot", chain: 2, listeners: 1, source: { file, line } },
|
|
238
|
+
// { id: "h2", name: "http.request", chain: 4, listeners: 0, source: { file, line } },
|
|
239
|
+
// ]
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### Recording + replay
|
|
243
|
+
|
|
244
|
+
```ts
|
|
245
|
+
import { record, replay } from "@nwire/hooks";
|
|
246
|
+
|
|
247
|
+
// Production: capture the full trace from one run.
|
|
248
|
+
const recording = await record(myHook, ctx);
|
|
249
|
+
await fetch("/__nwire/recordings", { method: "POST", body: JSON.stringify(recording) });
|
|
250
|
+
|
|
251
|
+
// Dev / debug: replay against the same hook and assert on drift.
|
|
252
|
+
const result = await replay(myHook, recording);
|
|
253
|
+
if (!result.matches) console.warn("drift:", result.drift);
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
`record` runs the hook once, snapshots `ctxIn`/`ctxOut` (via `structuredClone`
|
|
257
|
+
by default), and returns a self-contained, JSON-serializable trace. `replay`
|
|
258
|
+
re-runs the hook with the captured `ctxIn` (cloned) and compares step
|
|
259
|
+
sequences — flagging drift in step count, names, order, or outcome.
|
|
260
|
+
|
|
261
|
+
This is observational, not stubbing. Pure / idempotent chains match
|
|
262
|
+
deterministically; non-deterministic chains report exactly what diverged.
|
|
263
|
+
|
|
264
|
+
---
|
|
265
|
+
|
|
266
|
+
## Consumer APIs — the surfaces every nwire package presents
|
|
267
|
+
|
|
268
|
+
This is the locked-in contract each downstream package exposes to its users.
|
|
269
|
+
Internally they all reduce to `hook()`, but the public API stays familiar
|
|
270
|
+
and ergonomic. This table is the source of truth — anything outside it is
|
|
271
|
+
not a public API of that package.
|
|
272
|
+
|
|
273
|
+
### `@nwire/forge`
|
|
274
|
+
|
|
275
|
+
```ts
|
|
276
|
+
// Runtime dispatch middleware — onion around every action handler.
|
|
277
|
+
runtime.use(mw: DispatchMiddleware): void;
|
|
278
|
+
type DispatchMiddleware = (
|
|
279
|
+
next: () => Promise<EventMessage | EventMessage[] | void>,
|
|
280
|
+
action: ActionDefinition,
|
|
281
|
+
input: unknown,
|
|
282
|
+
ctx: HandlerContext,
|
|
283
|
+
) => Promise<EventMessage | EventMessage[] | void>;
|
|
284
|
+
// → forge.action.dispatch hook · chain · priority by registration order
|
|
285
|
+
|
|
286
|
+
// Actor transition hook — observable, fires after every state change.
|
|
287
|
+
runtime.actorHook(fn: ActorTransitionHook): void;
|
|
288
|
+
type ActorTransitionHook = (
|
|
289
|
+
actor: ActorDefinition,
|
|
290
|
+
key: string,
|
|
291
|
+
fromState: string,
|
|
292
|
+
toState: string,
|
|
293
|
+
event: EventMessage,
|
|
294
|
+
envelope: MessageEnvelope,
|
|
295
|
+
) => Promise<void> | void;
|
|
296
|
+
// → forge.actor.transition hook · listener · when=always
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### `@nwire/forge` — plugin builder (closure form)
|
|
300
|
+
|
|
301
|
+
```ts
|
|
302
|
+
definePlugin("name", ({ bind, resolve, on, before, after, middleware, actorHook, boot, shutdown }) => {
|
|
303
|
+
// — Generic framework event subscription.
|
|
304
|
+
on<TPayload>(event, handler, priority?): void;
|
|
305
|
+
// → underlying framework-event hook · chain or listener per event.mode
|
|
306
|
+
|
|
307
|
+
// — Action-scoped sugar.
|
|
308
|
+
before(actionName, fn): void;
|
|
309
|
+
// fn returns void | false. false short-circuits ("prevented"); throw fails.
|
|
310
|
+
// → action.before:<name> hook · chain · use+next bail
|
|
311
|
+
after(actionName, fn): void;
|
|
312
|
+
// fn observes result + durationMs. Errors logged, not fatal.
|
|
313
|
+
// → action.after:<name> hook · listener · when=success
|
|
314
|
+
|
|
315
|
+
// — Dispatch middleware (same shape as runtime.use).
|
|
316
|
+
middleware(mw: DispatchMiddleware): void;
|
|
317
|
+
// → forge.action.dispatch hook · chain
|
|
318
|
+
|
|
319
|
+
// — Actor transitions (same shape as runtime.actorHook).
|
|
320
|
+
actorHook(fn: ActorTransitionHook): void;
|
|
321
|
+
// → forge.actor.transition hook · listener
|
|
322
|
+
|
|
323
|
+
// — Plugin lifecycle. Run order = registration; shutdown is reverse.
|
|
324
|
+
boot(fn: () => Promise<void> | void): void;
|
|
325
|
+
shutdown(fn: () => Promise<void> | void): void;
|
|
326
|
+
});
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### `@nwire/app` — framework events (3 dispatch modes)
|
|
330
|
+
|
|
331
|
+
```ts
|
|
332
|
+
// Subscribe (priority desc; default 0).
|
|
333
|
+
bus.on(event, handler, priority?): () => void;
|
|
334
|
+
|
|
335
|
+
// Fire. Returns false only when a series-bail handler vetoed.
|
|
336
|
+
bus.fire(event, payload): Promise<boolean>;
|
|
337
|
+
|
|
338
|
+
// Per-firing observer — sees every event, cannot veto. For dev-logger,
|
|
339
|
+
// Studio Live, OTel.
|
|
340
|
+
bus.onFire(observer): () => void;
|
|
341
|
+
type FrameworkEventObservation = {
|
|
342
|
+
eventName: string;
|
|
343
|
+
payload: unknown;
|
|
344
|
+
mode: "parallel" | "series" | "series-bail";
|
|
345
|
+
phase: "fired" | "prevented";
|
|
346
|
+
ts: string;
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
// Modes — set when defining the event.
|
|
350
|
+
defineFrameworkEvent<TPayload>(name, mode: "parallel" | "series" | "series-bail");
|
|
351
|
+
// parallel → listener fan-out (Promise.allSettled, errors logged)
|
|
352
|
+
// series → chain without bail (sequential await; throw stops)
|
|
353
|
+
// series-bail → chain with bail (sequential await; return false short-circuits)
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
### `@nwire/handler` — resolver middleware
|
|
357
|
+
|
|
358
|
+
```ts
|
|
359
|
+
defineMiddleware(name?, fn: (ctx, next) => Promise<void> | void): MiddlewareDefinition;
|
|
360
|
+
// → resolver hook · chain
|
|
361
|
+
|
|
362
|
+
defineHook("before", name?, fn: (ctx) => Promise<void> | void): HookDefinition;
|
|
363
|
+
// → resolver hook · listener · when=always · runs before handler
|
|
364
|
+
defineHook("after", name?, fn: (ctx, result) => Promise<void> | void): HookDefinition;
|
|
365
|
+
// → resolver hook · listener · when=success · runs after handler
|
|
366
|
+
|
|
367
|
+
pipe(...steps: PipeStep[]): MiddlewarePipe;
|
|
368
|
+
// compose middlewares + hooks; transports unwind into chain + listeners
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
### `@nwire/http` — request pipeline
|
|
372
|
+
|
|
373
|
+
```ts
|
|
374
|
+
httpInterface(opts?)
|
|
375
|
+
.use(mw: KoaMiddleware): this;
|
|
376
|
+
// → http.request hook · chain · global to this interface
|
|
377
|
+
.provide(container): this;
|
|
378
|
+
.wire(route: RouteBinding, handler: HttpHandler): this;
|
|
379
|
+
// per-route middleware lives on the binding:
|
|
380
|
+
// route = post("/path", { body, middleware: [mw1, mw2], openapi })
|
|
381
|
+
// → http.request:<METHOD> <path> hook · chain · scoped to this route
|
|
382
|
+
.compile(): KoaApp;
|
|
383
|
+
.toExpress(): ExpressMiddleware;
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
### `@nwire/auth` — canonical resolvers + middleware
|
|
387
|
+
|
|
388
|
+
```ts
|
|
389
|
+
// All canonical auth operations are resolvers (SignIn, SignOut, Refresh,
|
|
390
|
+
// Register, Me) and run through the @nwire/handler pipeline above.
|
|
391
|
+
identityPlugin({ adapter, scopes? }): PluginDefinition;
|
|
392
|
+
// contributes:
|
|
393
|
+
// middleware → forge.action.dispatch · chain · enriches ctx.envelope.user
|
|
394
|
+
// resolvers → /auth/*
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
### `@nwire/rbac`
|
|
398
|
+
|
|
399
|
+
```ts
|
|
400
|
+
defineAbility((user, { allow, deny }) => { ... }): AbilityFactory;
|
|
401
|
+
rbacPlugin({ ability }): PluginDefinition;
|
|
402
|
+
// contributes:
|
|
403
|
+
// middleware → forge.action.dispatch · chain · enforces action.policy
|
|
404
|
+
|
|
405
|
+
// Resolver-level:
|
|
406
|
+
can(action: string, subject: string | Subject): MiddlewareDefinition;
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
### `@nwire/observability` (tracing + auth-as-plugin)
|
|
410
|
+
|
|
411
|
+
```ts
|
|
412
|
+
tracingPlugin({ tracer? }): PluginDefinition;
|
|
413
|
+
// wires OTel via .tap() on the dispatch + framework-event hooks.
|
|
414
|
+
|
|
415
|
+
// Application code rarely calls hooks directly — observability plugs in
|
|
416
|
+
// at boot, taps every hook in listHooks(), and emits spans / logs.
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
### Test surface — `@nwire/test-kit`
|
|
420
|
+
|
|
421
|
+
```ts
|
|
422
|
+
const harness = createTestHarness({ app });
|
|
423
|
+
// Every hook the app creates is .tap()-able through harness.observe():
|
|
424
|
+
harness.observe(hookName, (obs) => { ... });
|
|
425
|
+
// Recordings can be captured + diffed in tests:
|
|
426
|
+
const rec = await harness.record(hookName, ctx);
|
|
427
|
+
expect(rec.steps.map((s) => s.stepName)).toEqual([...]);
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
### Studio + scan
|
|
431
|
+
|
|
432
|
+
`nwire scan` emits `.nwire/hooks.json`:
|
|
433
|
+
|
|
434
|
+
```json
|
|
435
|
+
[
|
|
436
|
+
{ "id": "h1", "name": "forge.action.dispatch", "chain": 4, "listeners": 0, "source": { "file": "...", "line": 12 } },
|
|
437
|
+
{ "id": "h2", "name": "nwire.app.booting", "chain": 2, "listeners": 0, "source": { "file": "...", "line": 8 } },
|
|
438
|
+
{ "id": "h3", "name": "action.after:CreateStation", "chain": 0, "listeners": 2, "source": { "file": "...", "line": 23 } }
|
|
439
|
+
]
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
Studio surfaces a **Hooks** page that lists these with source pills, a per-hook
|
|
443
|
+
inspector showing attached chain + listeners with their names + priorities,
|
|
444
|
+
and a Trace mode that overlays live `.tap()` observations onto the causation
|
|
445
|
+
tree using `runId` / `parentRunId`.
|
|
446
|
+
|
|
447
|
+
---
|
|
448
|
+
|
|
449
|
+
## Recipes — wiring the substrate
|
|
450
|
+
|
|
451
|
+
### 1. Forge — `runtime.use(middleware)`
|
|
452
|
+
|
|
453
|
+
```ts
|
|
454
|
+
const dispatch = hook<DispatchCtx>("forge.action.dispatch");
|
|
455
|
+
|
|
456
|
+
dispatch.use(async (ctx, next) => {
|
|
457
|
+
const span = tracer.startSpan(ctx.action.name);
|
|
458
|
+
try { await next(); span.end(); } catch (err) { span.recordException(err); throw err; }
|
|
459
|
+
}, { name: "tracing", priority: 100 });
|
|
460
|
+
|
|
461
|
+
// `runtime.use(mw)` is sugar:
|
|
462
|
+
runtime.use = (mw) => dispatch.use(adaptDispatchMiddleware(mw));
|
|
463
|
+
```
|
|
464
|
+
|
|
465
|
+
### 2. HTTP — global `.use()` + per-route `middleware: [...]`
|
|
466
|
+
|
|
467
|
+
```ts
|
|
468
|
+
const httpRequest = hook<KoaCtx>("http.request"); // global
|
|
469
|
+
const routeRequest = (route) =>
|
|
470
|
+
hook<KoaCtx>(`http.request:${route.method} ${route.path}`); // per route
|
|
471
|
+
|
|
472
|
+
httpRequest.use(cors());
|
|
473
|
+
httpRequest.use(bodyParser());
|
|
474
|
+
const routeHook = routeRequest(route);
|
|
475
|
+
for (const mw of route.middleware) routeHook.use(mw);
|
|
476
|
+
routeHook.use(invokeHandler(route));
|
|
477
|
+
|
|
478
|
+
// At dispatch:
|
|
479
|
+
await httpRequest.run(ctx);
|
|
480
|
+
await routeHook.run(ctx);
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
### 3. Handler — `defineMiddleware` + `defineHook("after", ...)`
|
|
484
|
+
|
|
485
|
+
```ts
|
|
486
|
+
// Migrate:
|
|
487
|
+
const authenticate = defineMiddleware(async (ctx, next) => { ... });
|
|
488
|
+
const auditLog = defineHook("after", async (ctx, result) => { ... });
|
|
489
|
+
|
|
490
|
+
// To:
|
|
491
|
+
resolverHook.use(authenticate.fn, { name: "authenticate" });
|
|
492
|
+
resolverHook.on((ctx) => auditLog.fn(ctx, ctx.result), { when: "success", name: "audit-log" });
|
|
493
|
+
```
|
|
494
|
+
|
|
495
|
+
### 4. Plugin — `before(actionName, fn)` (series-bail)
|
|
496
|
+
|
|
497
|
+
```ts
|
|
498
|
+
const beforeAction = hook<BeforeCtx>("action.before:" + actionName);
|
|
499
|
+
|
|
500
|
+
beforeAction.use(async (ctx, next) => {
|
|
501
|
+
const allowed = await fn(ctx);
|
|
502
|
+
if (allowed === false) return; // short-circuit -> outcome "prevented"
|
|
503
|
+
await next();
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
// Dispatch:
|
|
507
|
+
const result = await beforeAction.runDetailed(ctx);
|
|
508
|
+
if (result.outcome === "prevented") return /* skip the action */;
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
### 5. Plugin — `after(actionName, fn)` (parallel observer, success-only)
|
|
512
|
+
|
|
513
|
+
```ts
|
|
514
|
+
const afterAction = hook<AfterCtx>("action.after:" + actionName);
|
|
515
|
+
afterAction.on(fn, { when: "success", name: actionName + ":after" });
|
|
516
|
+
```
|
|
517
|
+
|
|
518
|
+
### 6. Plugin — `actorHooks.afterTransition`
|
|
519
|
+
|
|
520
|
+
```ts
|
|
521
|
+
const actorTransition = hook<TransitionCtx>("actor.transition");
|
|
522
|
+
actorTransition.on((ctx) => userHook(ctx.actor, ctx.key, ctx.from, ctx.to, ctx.event));
|
|
523
|
+
|
|
524
|
+
// runtime emits on every transition:
|
|
525
|
+
await actorTransition.run({ actor, key, from, to, event, envelope });
|
|
13
526
|
```
|
|
14
527
|
|
|
15
|
-
|
|
528
|
+
### 7. App framework events — three modes
|
|
16
529
|
|
|
17
|
-
|
|
530
|
+
```ts
|
|
531
|
+
const ev = hook<Payload>("nwire.app.booting", {
|
|
532
|
+
// strictListeners=false matches "parallel mode" — handler errors are logged.
|
|
533
|
+
strictListeners: false,
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
// parallel:
|
|
537
|
+
ev.on(handler, { priority });
|
|
538
|
+
|
|
539
|
+
// series:
|
|
540
|
+
ev.use(async (ctx, next) => { await handler(ctx); await next(); }, { priority });
|
|
541
|
+
|
|
542
|
+
// series-bail:
|
|
543
|
+
ev.use(async (ctx, next) => {
|
|
544
|
+
const ret = await handler(ctx);
|
|
545
|
+
if (ret === false) return; // short-circuit
|
|
546
|
+
await next();
|
|
547
|
+
}, { priority });
|
|
548
|
+
|
|
549
|
+
const r = await ev.runDetailed(payload);
|
|
550
|
+
const prevented = r.outcome === "prevented";
|
|
551
|
+
```
|
|
552
|
+
|
|
553
|
+
### 8. Studio / scan integration
|
|
554
|
+
|
|
555
|
+
```ts
|
|
556
|
+
import { listHooks } from "@nwire/hooks";
|
|
557
|
+
|
|
558
|
+
// scan emits per-hook metadata to .nwire/hooks.json
|
|
559
|
+
const manifest = listHooks().map((h) => ({
|
|
560
|
+
id: h.id, name: h.name, chain: h.chain, listeners: h.listeners, source: h.source,
|
|
561
|
+
}));
|
|
562
|
+
```
|
|
563
|
+
|
|
564
|
+
### 9. OTel adapter
|
|
565
|
+
|
|
566
|
+
```ts
|
|
567
|
+
import { trace } from "@opentelemetry/api";
|
|
568
|
+
|
|
569
|
+
const tracer = trace.getTracer("nwire");
|
|
570
|
+
const spans = new Map<string, ReturnType<typeof tracer.startSpan>>();
|
|
571
|
+
|
|
572
|
+
myHook.tap((obs) => {
|
|
573
|
+
const key = `${obs.runId}:${obs.stepId}:${obs.stepKind}`;
|
|
574
|
+
if (obs.phase === "start") {
|
|
575
|
+
spans.set(key, tracer.startSpan(`${obs.hookName}.${obs.stepName ?? obs.stepKind}`));
|
|
576
|
+
} else if (obs.phase === "end") {
|
|
577
|
+
spans.get(key)?.end(); spans.delete(key);
|
|
578
|
+
} else if (obs.phase === "error") {
|
|
579
|
+
const s = spans.get(key);
|
|
580
|
+
if (s) { s.recordException(obs.error as Error); s.end(); spans.delete(key); }
|
|
581
|
+
}
|
|
582
|
+
});
|
|
583
|
+
```
|
|
584
|
+
|
|
585
|
+
### 10. Structured logger tap
|
|
586
|
+
|
|
587
|
+
```ts
|
|
588
|
+
myHook.tap((obs) => {
|
|
589
|
+
logger.debug({
|
|
590
|
+
msg: "hook.step",
|
|
591
|
+
hook: obs.hookName,
|
|
592
|
+
runId: obs.runId,
|
|
593
|
+
parentRun: obs.parentRunId,
|
|
594
|
+
step: obs.stepName ?? `#${obs.stepId}`,
|
|
595
|
+
phase: obs.phase,
|
|
596
|
+
durationMs: obs.durationMs,
|
|
597
|
+
error: obs.error && (obs.error as Error).message,
|
|
598
|
+
});
|
|
599
|
+
});
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
### 11. AbortSignal
|
|
603
|
+
|
|
604
|
+
```ts
|
|
605
|
+
const ac = new AbortController();
|
|
606
|
+
setTimeout(() => ac.abort(new Error("timeout")), 1000);
|
|
607
|
+
|
|
608
|
+
await myHook.run(ctx, { signal: ac.signal });
|
|
609
|
+
// ctx.signal is set so inner code can poll:
|
|
610
|
+
// if (ctx.signal?.aborted) return;
|
|
611
|
+
```
|
|
612
|
+
|
|
613
|
+
---
|
|
614
|
+
|
|
615
|
+
## `createHooks()` — bundled named hosts
|
|
18
616
|
|
|
19
617
|
```ts
|
|
20
618
|
import { createHooks, hook } from "@nwire/hooks";
|
|
21
619
|
|
|
22
620
|
const lifecycle = createHooks({
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
621
|
+
registering: hook<RegisteringCtx>("nwire.app.registering"),
|
|
622
|
+
booting: hook<BootingCtx> ("nwire.app.booting"),
|
|
623
|
+
ready: hook<ReadyCtx> ("nwire.app.ready"),
|
|
624
|
+
shutdown: hook<ShutdownCtx> ("nwire.app.shutdown"),
|
|
26
625
|
});
|
|
27
626
|
|
|
28
|
-
// Apply a plugin
|
|
627
|
+
// Apply a plugin across all four at once:
|
|
29
628
|
lifecycle.use({
|
|
30
|
-
|
|
31
|
-
logger.info("
|
|
32
|
-
|
|
33
|
-
},
|
|
34
|
-
shutdown: { on: (ctx) => logger.info("shut down", ctx) },
|
|
629
|
+
booting: async (ctx, next) => { logger.info("booting"); await next(); },
|
|
630
|
+
ready: { on: (ctx) => logger.info("ready", { port: ctx.port }) },
|
|
631
|
+
shutdown: async (ctx, next) => { await flushOutbox(); await next(); },
|
|
35
632
|
});
|
|
36
633
|
```
|
|
37
634
|
|
|
38
|
-
|
|
635
|
+
`.hooks` is the underlying record — spread it to compose hosts:
|
|
636
|
+
|
|
637
|
+
```ts
|
|
638
|
+
const full = createHooks({ ...lifecycle.hooks, request: hook<...>("http.request") });
|
|
639
|
+
```
|
|
640
|
+
|
|
641
|
+
---
|
|
642
|
+
|
|
643
|
+
## Production guidance
|
|
644
|
+
|
|
645
|
+
- **Hot paths.** A `.use()` step adds one wrapper closure + one observation
|
|
646
|
+
emit. Per-request HTTP middleware on a busy server: ~250 ns/step in our
|
|
647
|
+
microbench. If you have a 100k-rps inner loop and don't need observability,
|
|
648
|
+
prefer a hand-rolled compose.
|
|
649
|
+
- **Listener errors.** Default policy is log + continue. This matches Node's
|
|
650
|
+
`EventEmitter` and Koa's "don't crash because metrics broke." Opt into
|
|
651
|
+
`strictListeners: true` for testing and any place where listener failure
|
|
652
|
+
must surface.
|
|
653
|
+
- **Taps.** Taps fire synchronously inside `.run()`. Don't do blocking I/O.
|
|
654
|
+
Buffer + flush async.
|
|
655
|
+
- **AbortSignal.** Aborting throws between steps. A long-running step that
|
|
656
|
+
never `await`s won't notice. Plumb `ctx.signal` into your I/O calls.
|
|
657
|
+
- **Topology.** `AsyncLocalStorage` survives microtasks and timers. It does
|
|
658
|
+
*not* survive `setImmediate` if you escape the await chain. Stay in
|
|
659
|
+
`await` land if you care about parent linkage.
|
|
660
|
+
- **Recording size.** A recording grows linearly with step count; ctx
|
|
661
|
+
snapshots dominate the size. Use a custom `clone` to redact PII before
|
|
662
|
+
persisting.
|
|
663
|
+
|
|
664
|
+
---
|
|
665
|
+
|
|
666
|
+
## When NOT to use a hook
|
|
667
|
+
|
|
668
|
+
- A single, hard-coded step that has no extension point. Just call the
|
|
669
|
+
function.
|
|
670
|
+
- A synchronous calculation that never spans I/O. The async overhead is
|
|
671
|
+
wasted.
|
|
672
|
+
- Something that's fundamentally request/response with no observers and no
|
|
673
|
+
cancellability. A normal function is fine.
|
|
674
|
+
|
|
675
|
+
Hooks are for extension. If nobody will ever attach to it, don't make it
|
|
676
|
+
one.
|
|
39
677
|
|
|
40
|
-
|
|
41
|
-
| ------------------------------------------------ | ------------------------------------------- |
|
|
42
|
-
| `.on()` runs if chain short-circuits (no throw)? | Yes — listeners observe outcome |
|
|
43
|
-
| `.on()` runs if chain throws? | Yes — `ctx.error` is set first |
|
|
44
|
-
| Can listeners mutate ctx? | No — `.on()` types ctx as `Readonly<Ctx>` |
|
|
45
|
-
| Listeners awaited by `.run()`? | Yes — parallel, `Promise.allSettled` |
|
|
46
|
-
| Listener throws → fails chain? | No — collected, routed to `onListenerError` |
|
|
47
|
-
| Listener ordering | Unordered (parallel) |
|
|
48
|
-
| Chain ordering | Insertion order (koa-compose) |
|
|
678
|
+
---
|
|
49
679
|
|
|
50
|
-
|
|
680
|
+
## Versioning + stability
|
|
51
681
|
|
|
52
|
-
|
|
682
|
+
`@nwire/hooks` follows the framework's semver. The contract matrix in this
|
|
683
|
+
README is the law — changing any row is a major bump.
|
|
53
684
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
- `compose(fns)` / `pipe(...fns)` / `withTimeout(ms, fn)` / `withRetry(opts, fn)` — composition helpers.
|
|
685
|
+
Already locked: `Hook<Ctx>` surface, `RunResult`, `StepObservation`,
|
|
686
|
+
`Recording`. New optional capabilities (more `RunOptions`, more tap fields)
|
|
687
|
+
are minor bumps; never breaking.
|