@pristine-ts/common 2.0.1 → 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.
- package/dist/lib/cjs/common.module.js +1 -0
- package/dist/lib/cjs/common.module.js.map +1 -1
- package/dist/lib/cjs/contexts/contexts.js +1 -0
- package/dist/lib/cjs/contexts/contexts.js.map +1 -1
- package/dist/lib/cjs/contexts/event-context.js +23 -0
- package/dist/lib/cjs/contexts/event-context.js.map +1 -0
- package/dist/lib/cjs/contexts/tracing.context.js +14 -0
- package/dist/lib/cjs/contexts/tracing.context.js.map +1 -1
- package/dist/lib/cjs/managers/event-context.manager.js +107 -0
- package/dist/lib/cjs/managers/event-context.manager.js.map +1 -0
- package/dist/lib/cjs/managers/managers.js +18 -0
- package/dist/lib/cjs/managers/managers.js.map +1 -0
- package/dist/lib/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/lib/esm/common.module.js +1 -0
- package/dist/lib/esm/common.module.js.map +1 -1
- package/dist/lib/esm/contexts/contexts.js +1 -0
- package/dist/lib/esm/contexts/contexts.js.map +1 -1
- package/dist/lib/esm/contexts/event-context.js +19 -0
- package/dist/lib/esm/contexts/event-context.js.map +1 -0
- package/dist/lib/esm/contexts/tracing.context.js +14 -0
- package/dist/lib/esm/contexts/tracing.context.js.map +1 -1
- package/dist/lib/esm/managers/event-context.manager.js +104 -0
- package/dist/lib/esm/managers/event-context.manager.js.map +1 -0
- package/dist/lib/esm/managers/managers.js +2 -0
- package/dist/lib/esm/managers/managers.js.map +1 -0
- package/dist/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/dist/types/common.module.d.ts +1 -0
- package/dist/types/contexts/contexts.d.ts +1 -0
- package/dist/types/contexts/event-context.d.ts +28 -0
- package/dist/types/contexts/tracing.context.d.ts +14 -0
- package/dist/types/managers/event-context.manager.d.ts +58 -0
- package/dist/types/managers/managers.d.ts +1 -0
- 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.
|
|
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": "
|
|
66
|
+
"gitHead": "c741bb430ab8f6286068dc3870fcadf4cefd0023"
|
|
67
67
|
}
|