@pristine-ts/common 2.0.2 → 2.0.3

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.
Files changed (33) hide show
  1. package/dist/lib/cjs/common.module.js +1 -0
  2. package/dist/lib/cjs/common.module.js.map +1 -1
  3. package/dist/lib/cjs/contexts/contexts.js +1 -0
  4. package/dist/lib/cjs/contexts/contexts.js.map +1 -1
  5. package/dist/lib/cjs/contexts/event-context.js +23 -0
  6. package/dist/lib/cjs/contexts/event-context.js.map +1 -0
  7. package/dist/lib/cjs/contexts/tracing.context.js +14 -0
  8. package/dist/lib/cjs/contexts/tracing.context.js.map +1 -1
  9. package/dist/lib/cjs/managers/event-context.manager.js +107 -0
  10. package/dist/lib/cjs/managers/event-context.manager.js.map +1 -0
  11. package/dist/lib/cjs/managers/managers.js +18 -0
  12. package/dist/lib/cjs/managers/managers.js.map +1 -0
  13. package/dist/lib/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  14. package/dist/lib/esm/common.module.js +1 -0
  15. package/dist/lib/esm/common.module.js.map +1 -1
  16. package/dist/lib/esm/contexts/contexts.js +1 -0
  17. package/dist/lib/esm/contexts/contexts.js.map +1 -1
  18. package/dist/lib/esm/contexts/event-context.js +19 -0
  19. package/dist/lib/esm/contexts/event-context.js.map +1 -0
  20. package/dist/lib/esm/contexts/tracing.context.js +14 -0
  21. package/dist/lib/esm/contexts/tracing.context.js.map +1 -1
  22. package/dist/lib/esm/managers/event-context.manager.js +104 -0
  23. package/dist/lib/esm/managers/event-context.manager.js.map +1 -0
  24. package/dist/lib/esm/managers/managers.js +2 -0
  25. package/dist/lib/esm/managers/managers.js.map +1 -0
  26. package/dist/lib/esm/tsconfig.tsbuildinfo +1 -1
  27. package/dist/types/common.module.d.ts +1 -0
  28. package/dist/types/contexts/contexts.d.ts +1 -0
  29. package/dist/types/contexts/event-context.d.ts +28 -0
  30. package/dist/types/contexts/tracing.context.d.ts +14 -0
  31. package/dist/types/managers/event-context.manager.d.ts +58 -0
  32. package/dist/types/managers/managers.d.ts +1 -0
  33. package/package.json +2 -2
@@ -1,4 +1,18 @@
1
1
  import "reflect-metadata";
2
+ /**
3
+ * @deprecated Read the trace id from the active `EventContext` instead:
4
+ *
5
+ * ```ts
6
+ * import {EventContextManager} from "@pristine-ts/common";
7
+ *
8
+ * const traceId = EventContextManager.traceId();
9
+ * ```
10
+ *
11
+ * `TracingContext` predates the `AsyncLocalStorage`-backed `EventContext` and survives
12
+ * only as a back-compat shim. `TracingManager.startTracing()` still mirrors the trace id
13
+ * into this class so existing consumers keep working, but new code should read from
14
+ * `EventContextManager`. This class will be removed in a future major.
15
+ */
2
16
  export declare class TracingContext {
3
17
  traceId?: string;
4
18
  }
@@ -0,0 +1,58 @@
1
+ import { DependencyContainer } from "tsyringe";
2
+ import { EventContext } from "../contexts/event-context";
3
+ /**
4
+ * Owns the `AsyncLocalStorage` instance that propagates the active `EventContext`
5
+ * across `await` boundaries, `Promise.all`, `setImmediate`, and the rest of Node's
6
+ * async machinery. Provides:
7
+ *
8
+ * - `run(ctx, fn)` — install the context for the duration of `fn`. **Framework-only**;
9
+ * normal application code never calls this. Pristine's event pipeline calls it once
10
+ * per event.
11
+ * - Read accessors — `current()`, `eventId()`, `traceId()`, `container()`. Available
12
+ * as instance methods (DI-resolved) and as static convenience methods.
13
+ * - `bind(fn)` — escape hatch for background work that breaks the natural async
14
+ * chain (raw `setImmediate`, `EventEmitter`-based code, certain native modules).
15
+ * Captures the current context and returns a wrapped function that restores it
16
+ * on call.
17
+ *
18
+ * Why static state inside an `@injectable()` class: the `AsyncLocalStorage` instance
19
+ * itself must be a single process-wide singleton — multiple instances would each track
20
+ * their own propagation chain and contexts wouldn't be visible across them. Making the
21
+ * ALS `private static readonly` while keeping the class `@injectable()` lets DI consumers
22
+ * resolve it normally (and substitute fakes in tests) while the underlying state stays
23
+ * shared and consistent.
24
+ */
25
+ export declare class EventContextManager {
26
+ private static readonly als;
27
+ /**
28
+ * Installs `ctx` as the active `EventContext` for the duration of `fn` (and every
29
+ * async chain reachable from it). Returns whatever `fn` returns. Used by Pristine's
30
+ * event pipeline; application code shouldn't need to call this directly.
31
+ */
32
+ run<T>(ctx: EventContext, fn: () => T): T;
33
+ run<T>(ctx: EventContext, fn: () => Promise<T>): Promise<T>;
34
+ /** Returns the active `EventContext`, or `undefined` if no context is installed
35
+ * (i.e. we're outside any `run()` call). */
36
+ current(): EventContext | undefined;
37
+ /** Convenience: the `eventId` of the active context, or `undefined`. */
38
+ eventId(): string | undefined;
39
+ /** Convenience: the `traceId` of the active context, or `undefined`. */
40
+ traceId(): string | undefined;
41
+ /** Convenience: the per-event DI child container, or `undefined`. */
42
+ container(): DependencyContainer | undefined;
43
+ /**
44
+ * Returns a wrapped version of `fn` that restores the current context on call. Use
45
+ * when you need to spawn work that escapes the natural async chain (e.g. a callback
46
+ * registered with a third-party `EventEmitter`, or a `setImmediate` whose callback
47
+ * runs after the parent function returned). Without `bind`, that work would see no
48
+ * context.
49
+ *
50
+ * If no context is active when `bind` is called, returns `fn` unchanged.
51
+ */
52
+ bind<F extends (...args: any[]) => any>(fn: F): F;
53
+ static current(): EventContext | undefined;
54
+ static eventId(): string | undefined;
55
+ static traceId(): string | undefined;
56
+ static container(): DependencyContainer | undefined;
57
+ static bind<F extends (...args: any[]) => any>(fn: F): F;
58
+ }
@@ -0,0 +1 @@
1
+ export * from "./event-context.manager";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pristine-ts/common",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "description": "",
5
5
  "module": "dist/lib/esm/common.module.js",
6
6
  "main": "dist/lib/cjs/common.module.js",
@@ -63,5 +63,5 @@
63
63
  "src/*.{js,ts}"
64
64
  ]
65
65
  },
66
- "gitHead": "b0899c7eb6b34a99c6df0739507b1458549a0009"
66
+ "gitHead": "c741bb430ab8f6286068dc3870fcadf4cefd0023"
67
67
  }