@wooksjs/event-core 0.7.1 → 0.7.2
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/dist/index.cjs
CHANGED
|
@@ -251,9 +251,9 @@ var ContextInjector = class {
|
|
|
251
251
|
*/
|
|
252
252
|
hook(_method, _name, _route) {}
|
|
253
253
|
};
|
|
254
|
-
let ci =
|
|
254
|
+
let ci = null;
|
|
255
255
|
/**
|
|
256
|
-
* Returns the current `ContextInjector` instance
|
|
256
|
+
* Returns the current `ContextInjector` instance, or `null` if none has been installed.
|
|
257
257
|
* Used internally by adapters to wrap lifecycle events.
|
|
258
258
|
*/
|
|
259
259
|
function getContextInjector() {
|
|
@@ -281,12 +281,19 @@ function getContextInjector() {
|
|
|
281
281
|
function replaceContextInjector(newCi) {
|
|
282
282
|
ci = newCi;
|
|
283
283
|
}
|
|
284
|
+
/**
|
|
285
|
+
* Resets the global `ContextInjector` back to `null` (no-op default).
|
|
286
|
+
* Useful for tests or when disabling instrumentation.
|
|
287
|
+
*/
|
|
288
|
+
function resetContextInjector() {
|
|
289
|
+
ci = null;
|
|
290
|
+
}
|
|
284
291
|
|
|
285
292
|
//#endregion
|
|
286
293
|
//#region packages/event-core/src/storage.ts
|
|
287
294
|
const STORAGE_KEY = Symbol.for("wooks.core.asyncStorage");
|
|
288
295
|
const VERSION_KEY = Symbol.for("wooks.core.asyncStorage.version");
|
|
289
|
-
const CURRENT_VERSION = "0.7.
|
|
296
|
+
const CURRENT_VERSION = "0.7.1";
|
|
290
297
|
const _g = globalThis;
|
|
291
298
|
if (_g[STORAGE_KEY]) {
|
|
292
299
|
if (_g[VERSION_KEY] !== CURRENT_VERSION) throw new Error(`[wooks] Incompatible versions of @wooksjs/event-core detected: existing v${_g[VERSION_KEY]}, loading v${CURRENT_VERSION}. All packages must use the same @wooksjs/event-core version.`);
|
|
@@ -355,7 +362,8 @@ function createEventContext(options, kindOrFn, seedsOrUndefined, maybeFn) {
|
|
|
355
362
|
if (typeof kindOrFn === "function") return run(ctx, kindOrFn);
|
|
356
363
|
return run(ctx, () => {
|
|
357
364
|
ctx.seed(kindOrFn, seedsOrUndefined);
|
|
358
|
-
|
|
365
|
+
const ci$1 = getContextInjector();
|
|
366
|
+
return ci$1 ? ci$1.with("Event:start", { eventType: kindOrFn.name }, maybeFn) : maybeFn();
|
|
359
367
|
});
|
|
360
368
|
}
|
|
361
369
|
|
|
@@ -530,6 +538,7 @@ exports.eventTypeKey = eventTypeKey;
|
|
|
530
538
|
exports.getContextInjector = getContextInjector;
|
|
531
539
|
exports.key = key;
|
|
532
540
|
exports.replaceContextInjector = replaceContextInjector;
|
|
541
|
+
exports.resetContextInjector = resetContextInjector;
|
|
533
542
|
exports.routeParamsKey = routeParamsKey;
|
|
534
543
|
exports.run = run;
|
|
535
544
|
exports.slot = slot;
|
package/dist/index.d.ts
CHANGED
|
@@ -279,10 +279,10 @@ declare class ContextInjector<N> {
|
|
|
279
279
|
hook(_method: string, _name: 'Handler:not_found' | 'Handler:routed', _route?: string): void;
|
|
280
280
|
}
|
|
281
281
|
/**
|
|
282
|
-
* Returns the current `ContextInjector` instance
|
|
282
|
+
* Returns the current `ContextInjector` instance, or `null` if none has been installed.
|
|
283
283
|
* Used internally by adapters to wrap lifecycle events.
|
|
284
284
|
*/
|
|
285
|
-
declare function getContextInjector<N = TContextInjectorHooks>(): ContextInjector<N
|
|
285
|
+
declare function getContextInjector<N = TContextInjectorHooks>(): ContextInjector<N> | null;
|
|
286
286
|
/**
|
|
287
287
|
* Replaces the global `ContextInjector` with a custom implementation.
|
|
288
288
|
* Use this to integrate OpenTelemetry or other observability tools.
|
|
@@ -303,6 +303,11 @@ declare function getContextInjector<N = TContextInjectorHooks>(): ContextInjecto
|
|
|
303
303
|
* ```
|
|
304
304
|
*/
|
|
305
305
|
declare function replaceContextInjector(newCi: ContextInjector<string>): void;
|
|
306
|
+
/**
|
|
307
|
+
* Resets the global `ContextInjector` back to `null` (no-op default).
|
|
308
|
+
* Useful for tests or when disabling instrumentation.
|
|
309
|
+
*/
|
|
310
|
+
declare function resetContextInjector(): void;
|
|
306
311
|
/** Built-in hook names used by the framework. */
|
|
307
312
|
type TContextInjectorHooks = 'Event:start';
|
|
308
313
|
|
|
@@ -399,6 +404,9 @@ declare function useLogger(ctx?: EventContext): Logger;
|
|
|
399
404
|
* @param fn - Callback to execute within the new context
|
|
400
405
|
* @returns The return value of `fn`
|
|
401
406
|
*
|
|
407
|
+
* The kindless overload is a convenience for tests that need a context scope
|
|
408
|
+
* without declaring an event kind. Production code should always provide a kind.
|
|
409
|
+
*
|
|
402
410
|
* @example
|
|
403
411
|
* ```ts
|
|
404
412
|
* createEventContext({ logger }, () => {
|
|
@@ -428,5 +436,5 @@ declare function createEventContext<R>(options: EventContextOptions, fn: () => R
|
|
|
428
436
|
*/
|
|
429
437
|
declare function createEventContext<S extends Record<string, any>, R>(options: EventContextOptions, kind: EventKind<S>, seeds: EventKindSeeds<EventKind<S>>, fn: () => R): R;
|
|
430
438
|
|
|
431
|
-
export { ContextInjector, EventContext, cached, cachedBy, createEventContext, current, defineEventKind, defineWook, eventTypeKey, getContextInjector, key, replaceContextInjector, routeParamsKey, run, slot, tryGetCurrent, useEventId, useLogger, useRouteParams };
|
|
439
|
+
export { ContextInjector, EventContext, cached, cachedBy, createEventContext, current, defineEventKind, defineWook, eventTypeKey, getContextInjector, key, replaceContextInjector, resetContextInjector, routeParamsKey, run, slot, tryGetCurrent, useEventId, useLogger, useRouteParams };
|
|
432
440
|
export type { Accessor, Cached, EventContextOptions, EventKind, EventKindSeeds, Key, Logger, SlotMarker, TContextInjectorHooks };
|
package/dist/index.mjs
CHANGED
|
@@ -228,9 +228,9 @@ var ContextInjector = class {
|
|
|
228
228
|
*/
|
|
229
229
|
hook(_method, _name, _route) {}
|
|
230
230
|
};
|
|
231
|
-
let ci =
|
|
231
|
+
let ci = null;
|
|
232
232
|
/**
|
|
233
|
-
* Returns the current `ContextInjector` instance
|
|
233
|
+
* Returns the current `ContextInjector` instance, or `null` if none has been installed.
|
|
234
234
|
* Used internally by adapters to wrap lifecycle events.
|
|
235
235
|
*/
|
|
236
236
|
function getContextInjector() {
|
|
@@ -258,12 +258,19 @@ function getContextInjector() {
|
|
|
258
258
|
function replaceContextInjector(newCi) {
|
|
259
259
|
ci = newCi;
|
|
260
260
|
}
|
|
261
|
+
/**
|
|
262
|
+
* Resets the global `ContextInjector` back to `null` (no-op default).
|
|
263
|
+
* Useful for tests or when disabling instrumentation.
|
|
264
|
+
*/
|
|
265
|
+
function resetContextInjector() {
|
|
266
|
+
ci = null;
|
|
267
|
+
}
|
|
261
268
|
|
|
262
269
|
//#endregion
|
|
263
270
|
//#region packages/event-core/src/storage.ts
|
|
264
271
|
const STORAGE_KEY = Symbol.for("wooks.core.asyncStorage");
|
|
265
272
|
const VERSION_KEY = Symbol.for("wooks.core.asyncStorage.version");
|
|
266
|
-
const CURRENT_VERSION = "0.7.
|
|
273
|
+
const CURRENT_VERSION = "0.7.1";
|
|
267
274
|
const _g = globalThis;
|
|
268
275
|
if (_g[STORAGE_KEY]) {
|
|
269
276
|
if (_g[VERSION_KEY] !== CURRENT_VERSION) throw new Error(`[wooks] Incompatible versions of @wooksjs/event-core detected: existing v${_g[VERSION_KEY]}, loading v${CURRENT_VERSION}. All packages must use the same @wooksjs/event-core version.`);
|
|
@@ -332,7 +339,8 @@ function createEventContext(options, kindOrFn, seedsOrUndefined, maybeFn) {
|
|
|
332
339
|
if (typeof kindOrFn === "function") return run(ctx, kindOrFn);
|
|
333
340
|
return run(ctx, () => {
|
|
334
341
|
ctx.seed(kindOrFn, seedsOrUndefined);
|
|
335
|
-
|
|
342
|
+
const ci$1 = getContextInjector();
|
|
343
|
+
return ci$1 ? ci$1.with("Event:start", { eventType: kindOrFn.name }, maybeFn) : maybeFn();
|
|
336
344
|
});
|
|
337
345
|
}
|
|
338
346
|
|
|
@@ -495,4 +503,4 @@ function useEventId(ctx) {
|
|
|
495
503
|
}
|
|
496
504
|
|
|
497
505
|
//#endregion
|
|
498
|
-
export { ContextInjector, EventContext, cached, cachedBy, createEventContext, current, defineEventKind, defineWook, eventTypeKey, getContextInjector, key, replaceContextInjector, routeParamsKey, run, slot, tryGetCurrent, useEventId, useLogger, useRouteParams };
|
|
506
|
+
export { ContextInjector, EventContext, cached, cachedBy, createEventContext, current, defineEventKind, defineWook, eventTypeKey, getContextInjector, key, replaceContextInjector, resetContextInjector, routeParamsKey, run, slot, tryGetCurrent, useEventId, useLogger, useRouteParams };
|
package/package.json
CHANGED
|
@@ -234,10 +234,10 @@ const log = ctx.logger
|
|
|
234
234
|
|
|
235
235
|
## ContextInjector (observability)
|
|
236
236
|
|
|
237
|
-
`ContextInjector` is a hook point for observability tools (OpenTelemetry, etc.).
|
|
237
|
+
`ContextInjector` is a hook point for observability tools (OpenTelemetry, etc.). No injector is installed by default — `getContextInjector()` returns `null`, so all adapters run with zero overhead until one is installed.
|
|
238
238
|
|
|
239
239
|
```ts
|
|
240
|
-
import { ContextInjector, replaceContextInjector } from '@wooksjs/event-core'
|
|
240
|
+
import { ContextInjector, replaceContextInjector, resetContextInjector } from '@wooksjs/event-core'
|
|
241
241
|
|
|
242
242
|
class OtelInjector extends ContextInjector<string> {
|
|
243
243
|
with<T>(name: string, attributes: Record<string, any>, cb: () => T): T {
|
|
@@ -246,9 +246,12 @@ class OtelInjector extends ContextInjector<string> {
|
|
|
246
246
|
}
|
|
247
247
|
|
|
248
248
|
replaceContextInjector(new OtelInjector())
|
|
249
|
+
|
|
250
|
+
// To disable instrumentation later:
|
|
251
|
+
resetContextInjector()
|
|
249
252
|
```
|
|
250
253
|
|
|
251
|
-
The injector's `with()` method wraps `createEventContext` callbacks
|
|
254
|
+
The injector's `with()` method wraps `createEventContext` callbacks. All adapters (HTTP, CLI, WS, WF) route through `createEventContext`, so installing an injector automatically instruments every event type.
|
|
252
255
|
|
|
253
256
|
## Best Practices
|
|
254
257
|
|
|
@@ -63,8 +63,9 @@ Every event (HTTP request, CLI invocation, workflow step) gets its own `EventCon
|
|
|
63
63
|
| `routeParamsKey` | key | Standard key for route params |
|
|
64
64
|
| `eventTypeKey` | key | Standard key for event type name |
|
|
65
65
|
| `ContextInjector` | class | Observability hook point (OpenTelemetry etc.) |
|
|
66
|
-
| `getContextInjector()` | function | Get current injector
|
|
67
|
-
| `replaceContextInjector(ci)` | function |
|
|
66
|
+
| `getContextInjector()` | function | Get current injector (returns `null` when none installed) |
|
|
67
|
+
| `replaceContextInjector(ci)` | function | Install a custom injector (e.g. with OTel spans) |
|
|
68
|
+
| `resetContextInjector()` | function | Reset injector back to `null` (disables instrumentation) |
|
|
68
69
|
|
|
69
70
|
## Types
|
|
70
71
|
|