@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
|
@@ -20,6 +20,7 @@ __exportStar(require("./contexts/contexts"), exports);
|
|
|
20
20
|
__exportStar(require("./decorators/decorators"), exports);
|
|
21
21
|
__exportStar(require("./enums/enums"), exports);
|
|
22
22
|
__exportStar(require("./errors/errors"), exports);
|
|
23
|
+
__exportStar(require("./managers/managers"), exports);
|
|
23
24
|
__exportStar(require("./models/models"), exports);
|
|
24
25
|
__exportStar(require("./interfaces/interfaces"), exports);
|
|
25
26
|
__exportStar(require("./types/types"), exports);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common.module.js","sourceRoot":"","sources":["../../../src/common.module.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,mEAA4D;AAG5D,sDAAoC;AACpC,0DAAwC;AACxC,gDAA8B;AAC9B,kDAAgC;AAChC,kDAAgC;AAChC,0DAAwC;AACxC,gDAA8B;AAC9B,gDAA8B;AAC9B,0DAAwC;AAE3B,QAAA,YAAY,GAAoB;IAC3C,OAAO,EAAE,2CAAmB;IAC5B,aAAa,EAAE,EAAE;IACjB,qBAAqB,EAAE,EAAE;CAC1B,CAAA"}
|
|
1
|
+
{"version":3,"file":"common.module.js","sourceRoot":"","sources":["../../../src/common.module.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,mEAA4D;AAG5D,sDAAoC;AACpC,0DAAwC;AACxC,gDAA8B;AAC9B,kDAAgC;AAChC,sDAAoC;AACpC,kDAAgC;AAChC,0DAAwC;AACxC,gDAA8B;AAC9B,gDAA8B;AAC9B,0DAAwC;AAE3B,QAAA,YAAY,GAAoB;IAC3C,OAAO,EAAE,2CAAmB;IAC5B,aAAa,EAAE,EAAE;IACjB,qBAAqB,EAAE,EAAE;CAC1B,CAAA"}
|
|
@@ -14,5 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./event-context"), exports);
|
|
17
18
|
__exportStar(require("./tracing.context"), exports);
|
|
18
19
|
//# sourceMappingURL=contexts.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contexts.js","sourceRoot":"","sources":["../../../../src/contexts/contexts.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC"}
|
|
1
|
+
{"version":3,"file":"contexts.js","sourceRoot":"","sources":["../../../../src/contexts/contexts.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAAgC;AAChC,oDAAkC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EventContext = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Per-event runtime state — the framework's correlation primitives plus a handle to
|
|
6
|
+
* the per-event DI child container. Propagated implicitly via `AsyncLocalStorage`
|
|
7
|
+
* (managed by `EventContextManager`) so downstream code doesn't have to thread these
|
|
8
|
+
* fields through every method signature.
|
|
9
|
+
*
|
|
10
|
+
* **Not to be confused with `ExecutionContextInterface` (in `@pristine-ts/core`):**
|
|
11
|
+
* - `ExecutionContext` describes *where* the framework is running (AWS Lambda, CLI,
|
|
12
|
+
* Express, etc.) — set once per `kernel.handle()` call, immutable, host-shaped.
|
|
13
|
+
* - `EventContext` describes *which specific event* is being handled — set per event
|
|
14
|
+
* in the pipeline, mutable (`traceId` fills in once tracing starts), correlation-
|
|
15
|
+
* shaped.
|
|
16
|
+
*
|
|
17
|
+
* One `kernel.handle()` call can produce multiple events (each event mapper can return
|
|
18
|
+
* a list); each event gets its own `EventContext` instance.
|
|
19
|
+
*/
|
|
20
|
+
class EventContext {
|
|
21
|
+
}
|
|
22
|
+
exports.EventContext = EventContext;
|
|
23
|
+
//# sourceMappingURL=event-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event-context.js","sourceRoot":"","sources":["../../../../src/contexts/event-context.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;;;;;;GAeG;AACH,MAAa,YAAY;CAYxB;AAZD,oCAYC"}
|
|
@@ -9,6 +9,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
9
9
|
exports.TracingContext = void 0;
|
|
10
10
|
require("reflect-metadata");
|
|
11
11
|
const tsyringe_1 = require("tsyringe");
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated Read the trace id from the active `EventContext` instead:
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import {EventContextManager} from "@pristine-ts/common";
|
|
17
|
+
*
|
|
18
|
+
* const traceId = EventContextManager.traceId();
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* `TracingContext` predates the `AsyncLocalStorage`-backed `EventContext` and survives
|
|
22
|
+
* only as a back-compat shim. `TracingManager.startTracing()` still mirrors the trace id
|
|
23
|
+
* into this class so existing consumers keep working, but new code should read from
|
|
24
|
+
* `EventContextManager`. This class will be removed in a future major.
|
|
25
|
+
*/
|
|
12
26
|
let TracingContext = class TracingContext {
|
|
13
27
|
};
|
|
14
28
|
exports.TracingContext = TracingContext;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tracing.context.js","sourceRoot":"","sources":["../../../../src/contexts/tracing.context.ts"],"names":[],"mappings":";;;;;;;;;AAAA,4BAA0B;AAC1B,uCAAuD;
|
|
1
|
+
{"version":3,"file":"tracing.context.js","sourceRoot":"","sources":["../../../../src/contexts/tracing.context.ts"],"names":[],"mappings":";;;;;;;;;AAAA,4BAA0B;AAC1B,uCAAuD;AAEvD;;;;;;;;;;;;;GAaG;AAGI,IAAM,cAAc,GAApB,MAAM,cAAc;CAE1B,CAAA;AAFY,wCAAc;yBAAd,cAAc;IAF1B,IAAA,qBAAU,GAAE;IACZ,IAAA,iBAAM,EAAC,oBAAS,CAAC,eAAe,CAAC;GACrB,cAAc,CAE1B"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var EventContextManager_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.EventContextManager = void 0;
|
|
11
|
+
const async_hooks_1 = require("async_hooks");
|
|
12
|
+
const tsyringe_1 = require("tsyringe");
|
|
13
|
+
/**
|
|
14
|
+
* Owns the `AsyncLocalStorage` instance that propagates the active `EventContext`
|
|
15
|
+
* across `await` boundaries, `Promise.all`, `setImmediate`, and the rest of Node's
|
|
16
|
+
* async machinery. Provides:
|
|
17
|
+
*
|
|
18
|
+
* - `run(ctx, fn)` — install the context for the duration of `fn`. **Framework-only**;
|
|
19
|
+
* normal application code never calls this. Pristine's event pipeline calls it once
|
|
20
|
+
* per event.
|
|
21
|
+
* - Read accessors — `current()`, `eventId()`, `traceId()`, `container()`. Available
|
|
22
|
+
* as instance methods (DI-resolved) and as static convenience methods.
|
|
23
|
+
* - `bind(fn)` — escape hatch for background work that breaks the natural async
|
|
24
|
+
* chain (raw `setImmediate`, `EventEmitter`-based code, certain native modules).
|
|
25
|
+
* Captures the current context and returns a wrapped function that restores it
|
|
26
|
+
* on call.
|
|
27
|
+
*
|
|
28
|
+
* Why static state inside an `@injectable()` class: the `AsyncLocalStorage` instance
|
|
29
|
+
* itself must be a single process-wide singleton — multiple instances would each track
|
|
30
|
+
* their own propagation chain and contexts wouldn't be visible across them. Making the
|
|
31
|
+
* ALS `private static readonly` while keeping the class `@injectable()` lets DI consumers
|
|
32
|
+
* resolve it normally (and substitute fakes in tests) while the underlying state stays
|
|
33
|
+
* shared and consistent.
|
|
34
|
+
*/
|
|
35
|
+
let EventContextManager = EventContextManager_1 = class EventContextManager {
|
|
36
|
+
run(ctx, fn) {
|
|
37
|
+
return EventContextManager_1.als.run(ctx, fn);
|
|
38
|
+
}
|
|
39
|
+
/** Returns the active `EventContext`, or `undefined` if no context is installed
|
|
40
|
+
* (i.e. we're outside any `run()` call). */
|
|
41
|
+
current() {
|
|
42
|
+
return EventContextManager_1.als.getStore();
|
|
43
|
+
}
|
|
44
|
+
/** Convenience: the `eventId` of the active context, or `undefined`. */
|
|
45
|
+
eventId() {
|
|
46
|
+
var _a;
|
|
47
|
+
return (_a = this.current()) === null || _a === void 0 ? void 0 : _a.eventId;
|
|
48
|
+
}
|
|
49
|
+
/** Convenience: the `traceId` of the active context, or `undefined`. */
|
|
50
|
+
traceId() {
|
|
51
|
+
var _a;
|
|
52
|
+
return (_a = this.current()) === null || _a === void 0 ? void 0 : _a.traceId;
|
|
53
|
+
}
|
|
54
|
+
/** Convenience: the per-event DI child container, or `undefined`. */
|
|
55
|
+
container() {
|
|
56
|
+
var _a;
|
|
57
|
+
return (_a = this.current()) === null || _a === void 0 ? void 0 : _a.container;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Returns a wrapped version of `fn` that restores the current context on call. Use
|
|
61
|
+
* when you need to spawn work that escapes the natural async chain (e.g. a callback
|
|
62
|
+
* registered with a third-party `EventEmitter`, or a `setImmediate` whose callback
|
|
63
|
+
* runs after the parent function returned). Without `bind`, that work would see no
|
|
64
|
+
* context.
|
|
65
|
+
*
|
|
66
|
+
* If no context is active when `bind` is called, returns `fn` unchanged.
|
|
67
|
+
*/
|
|
68
|
+
bind(fn) {
|
|
69
|
+
const ctx = this.current();
|
|
70
|
+
if (ctx === undefined)
|
|
71
|
+
return fn;
|
|
72
|
+
const als = EventContextManager_1.als;
|
|
73
|
+
return ((...args) => als.run(ctx, () => fn(...args)));
|
|
74
|
+
}
|
|
75
|
+
// ── Static convenience mirrors. ────────────────────────────────────────────────
|
|
76
|
+
// For callers that don't want DI noise (e.g. utility code, decorators, helpers
|
|
77
|
+
// that don't have a constructor to inject through). All forwarding to the same
|
|
78
|
+
// shared ALS instance.
|
|
79
|
+
static current() {
|
|
80
|
+
return EventContextManager_1.als.getStore();
|
|
81
|
+
}
|
|
82
|
+
static eventId() {
|
|
83
|
+
var _a;
|
|
84
|
+
return (_a = EventContextManager_1.current()) === null || _a === void 0 ? void 0 : _a.eventId;
|
|
85
|
+
}
|
|
86
|
+
static traceId() {
|
|
87
|
+
var _a;
|
|
88
|
+
return (_a = EventContextManager_1.current()) === null || _a === void 0 ? void 0 : _a.traceId;
|
|
89
|
+
}
|
|
90
|
+
static container() {
|
|
91
|
+
var _a;
|
|
92
|
+
return (_a = EventContextManager_1.current()) === null || _a === void 0 ? void 0 : _a.container;
|
|
93
|
+
}
|
|
94
|
+
static bind(fn) {
|
|
95
|
+
const ctx = EventContextManager_1.current();
|
|
96
|
+
if (ctx === undefined)
|
|
97
|
+
return fn;
|
|
98
|
+
const als = EventContextManager_1.als;
|
|
99
|
+
return ((...args) => als.run(ctx, () => fn(...args)));
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
exports.EventContextManager = EventContextManager;
|
|
103
|
+
EventContextManager.als = new async_hooks_1.AsyncLocalStorage();
|
|
104
|
+
exports.EventContextManager = EventContextManager = EventContextManager_1 = __decorate([
|
|
105
|
+
(0, tsyringe_1.injectable)()
|
|
106
|
+
], EventContextManager);
|
|
107
|
+
//# sourceMappingURL=event-context.manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event-context.manager.js","sourceRoot":"","sources":["../../../../src/managers/event-context.manager.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,6CAA8C;AAC9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C;IAChD,CAAC;IAED,MAAM,CAAC,OAAO;;QACZ,OAAO,MAAA,qBAAmB,CAAC,OAAO,EAAE,0CAAE,OAAO,CAAC;IAChD,CAAC;IAED,MAAM,CAAC,SAAS;;QACd,OAAO,MAAA,qBAAmB,CAAC,OAAO,EAAE,0CAAE,SAAS,CAAC;IAClD,CAAC;IAED,MAAM,CAAC,IAAI,CAAoC,EAAK;QAClD,MAAM,GAAG,GAAG,qBAAmB,CAAC,OAAO,EAAE,CAAC;QAC1C,IAAI,GAAG,KAAK,SAAS;YAAE,OAAO,EAAE,CAAC;QACjC,MAAM,GAAG,GAAG,qBAAmB,CAAC,GAAG,CAAC;QACpC,OAAO,CAAC,CAAC,GAAG,IAAW,EAAE,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC,CAAM,CAAC;IACpE,CAAC;;AA7EU,kDAAmB;AACN,uBAAG,GAAG,IAAI,+BAAiB,EAAgB,AAAxC,CAAyC;8BADzD,mBAAmB;IAD/B,IAAA,qBAAU,GAAE;GACA,mBAAmB,CA8E/B"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./event-context.manager"), exports);
|
|
18
|
+
//# sourceMappingURL=managers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"managers.js","sourceRoot":"","sources":["../../../../src/managers/managers.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0DAAwC"}
|