@alanszp/shared-context 6.0.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/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ node_modules
2
+ *.log
3
+ dist
package/.npmignore ADDED
@@ -0,0 +1,3 @@
1
+ node_modules
2
+ src
3
+ *.log
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Alan Szpigiel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,4 @@
1
+ import { BaseError } from "@alanszp/errors";
2
+ export declare class SharedContextNotInitializedException extends BaseError {
3
+ constructor();
4
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SharedContextNotInitializedException = void 0;
4
+ const errors_1 = require("@alanszp/errors");
5
+ class SharedContextNotInitializedException extends errors_1.BaseError {
6
+ constructor() {
7
+ super("Shared context wasn't initialized correctly.");
8
+ }
9
+ }
10
+ exports.SharedContextNotInitializedException = SharedContextNotInitializedException;
11
+ //# sourceMappingURL=SharedContextNotInitializedException.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SharedContextNotInitializedException.js","sourceRoot":"","sources":["../src/SharedContextNotInitializedException.ts"],"names":[],"mappings":";;;AAAA,4CAA4C;AAE5C,MAAa,oCAAqC,SAAQ,kBAAS;IACjE;QACE,KAAK,CAAC,8CAA8C,CAAC,CAAC;IACxD,CAAC;CACF;AAJD,oFAIC"}
@@ -0,0 +1,2 @@
1
+ export * from "./sharedContext";
2
+ export * from "./SharedContextNotInitializedException";
package/dist/index.js ADDED
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./sharedContext"), exports);
14
+ __exportStar(require("./SharedContextNotInitializedException"), exports);
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,kDAAgC;AAChC,yEAAuD"}
@@ -0,0 +1,19 @@
1
+ import { ILogger } from "@alanszp/logger";
2
+ import { AuditWithState } from "@alanszp/audit";
3
+ export interface SharedInternalContext {
4
+ audit: AuditWithState;
5
+ logger: ILogger;
6
+ lifecycleId: string;
7
+ lifecycleChain: string;
8
+ contextId: string;
9
+ }
10
+ export declare class SharedContext {
11
+ private context;
12
+ run<R>(executable: () => R, internalContext: SharedInternalContext): R;
13
+ getLogger(): ILogger | undefined;
14
+ getAudit(): AuditWithState | undefined;
15
+ getLifecycleId(): string | undefined;
16
+ getLifecycleChain(): string | undefined;
17
+ getContextId(): string | undefined;
18
+ private getFromContext;
19
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SharedContext = void 0;
4
+ const async_hooks_1 = require("async_hooks");
5
+ class SharedContext {
6
+ constructor() {
7
+ this.context = new async_hooks_1.AsyncLocalStorage();
8
+ }
9
+ run(executable, internalContext) {
10
+ const { audit, logger, contextId, lifecycleId, lifecycleChain } = internalContext;
11
+ return this.context.run(Object.assign(Object.assign({}, internalContext), { logger: logger.child({
12
+ lifecycleId,
13
+ lifecycleChain,
14
+ }) }), executable);
15
+ }
16
+ getLogger() {
17
+ return this.getFromContext("logger");
18
+ }
19
+ getAudit() {
20
+ return this.getFromContext("audit");
21
+ }
22
+ getLifecycleId() {
23
+ return this.getFromContext("lifecycleId");
24
+ }
25
+ getLifecycleChain() {
26
+ return this.getFromContext("lifecycleChain");
27
+ }
28
+ getContextId() {
29
+ return this.getFromContext("getContextId");
30
+ }
31
+ getFromContext(propertyName) {
32
+ const context = this.context.getStore();
33
+ return context ? context[propertyName] : undefined;
34
+ }
35
+ }
36
+ exports.SharedContext = SharedContext;
37
+ //# sourceMappingURL=sharedContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sharedContext.js","sourceRoot":"","sources":["../src/sharedContext.ts"],"names":[],"mappings":";;;AAEA,6CAAgD;AAShD,MAAa,aAAa;IAA1B;QACU,YAAO,GAAG,IAAI,+BAAiB,EAAyB,CAAC;IA4CnE,CAAC;IA1CQ,GAAG,CACR,UAAmB,EACnB,eAAsC;QAEtC,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,cAAc,EAAE,GAC7D,eAAe,CAAC;QAClB,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,iCAEhB,eAAe,KAClB,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC;gBACnB,WAAW;gBACX,cAAc;aACf,CAAC,KAEJ,UAAU,CACX,CAAC;IACJ,CAAC;IAEM,SAAS;QACd,OAAO,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;IACvC,CAAC;IAEM,QAAQ;QACb,OAAO,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;IACtC,CAAC;IAEM,cAAc;QACnB,OAAO,IAAI,CAAC,cAAc,CAAC,aAAa,CAAC,CAAC;IAC5C,CAAC;IAEM,iBAAiB;QACtB,OAAO,IAAI,CAAC,cAAc,CAAC,gBAAgB,CAAC,CAAC;IAC/C,CAAC;IAEM,YAAY;QACjB,OAAO,IAAI,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC;IAC7C,CAAC;IAEO,cAAc,CAAO,YAAoB;QAC/C,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;QACxC,OAAO,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IACrD,CAAC;CACF;AA7CD,sCA6CC"}
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@alanszp/shared-context",
3
+ "version": "6.0.0",
4
+ "description": "Alan's shared context util.",
5
+ "main": "dist/index.js",
6
+ "typings": "dist/index.d.ts",
7
+ "license": "MIT",
8
+ "files": [
9
+ "**/*"
10
+ ],
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "scripts": {
15
+ "compile": "rm -rf ./dist && tsc --declaration",
16
+ "compile-watch": "tsc -w",
17
+ "build": "yarn run compile",
18
+ "prepack": "yarn run build",
19
+ "yalc-publish": "yarn run yalc publish"
20
+ },
21
+ "dependencies": {
22
+ "@alanszp/audit": "^6.0.0",
23
+ "@alanszp/errors": "^6.0.0",
24
+ "@alanszp/logger": "^6.0.0",
25
+ "lodash": "^4.17.21"
26
+ },
27
+ "gitHead": "75b34d763277366fdf93146cd1d20084ff469273"
28
+ }
@@ -0,0 +1,7 @@
1
+ import { BaseError } from "@alanszp/errors";
2
+
3
+ export class SharedContextNotInitializedException extends BaseError {
4
+ constructor() {
5
+ super("Shared context wasn't initialized correctly.");
6
+ }
7
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./sharedContext";
2
+ export * from "./SharedContextNotInitializedException";
@@ -0,0 +1,57 @@
1
+ import { ILogger } from "@alanszp/logger";
2
+ import { Audit, AuditWithState } from "@alanszp/audit";
3
+ import { AsyncLocalStorage } from "async_hooks";
4
+
5
+ export interface SharedInternalContext {
6
+ audit: AuditWithState;
7
+ logger: ILogger;
8
+ lifecycleId: string;
9
+ lifecycleChain: string;
10
+ contextId: string;
11
+ }
12
+ export class SharedContext {
13
+ private context = new AsyncLocalStorage<SharedInternalContext>();
14
+
15
+ public run<R>(
16
+ executable: () => R,
17
+ internalContext: SharedInternalContext
18
+ ): R {
19
+ const { audit, logger, contextId, lifecycleId, lifecycleChain } =
20
+ internalContext;
21
+ return this.context.run(
22
+ {
23
+ ...internalContext,
24
+ logger: logger.child({
25
+ lifecycleId,
26
+ lifecycleChain,
27
+ }),
28
+ },
29
+ executable
30
+ );
31
+ }
32
+
33
+ public getLogger(): ILogger | undefined {
34
+ return this.getFromContext("logger");
35
+ }
36
+
37
+ public getAudit(): AuditWithState | undefined {
38
+ return this.getFromContext("audit");
39
+ }
40
+
41
+ public getLifecycleId(): string | undefined {
42
+ return this.getFromContext("lifecycleId");
43
+ }
44
+
45
+ public getLifecycleChain(): string | undefined {
46
+ return this.getFromContext("lifecycleChain");
47
+ }
48
+
49
+ public getContextId(): string | undefined {
50
+ return this.getFromContext("getContextId");
51
+ }
52
+
53
+ private getFromContext<Type>(propertyName: string): Type | undefined {
54
+ const context = this.context.getStore();
55
+ return context ? context[propertyName] : undefined;
56
+ }
57
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "compilerOptions": {
3
+ "rootDir": "src",
4
+ "outDir": "dist",
5
+ "module": "commonjs",
6
+ "target": "es6",
7
+ "types": ["node"],
8
+ "esModuleInterop": true,
9
+ "sourceMap": true,
10
+
11
+ "alwaysStrict": true,
12
+ "strictNullChecks": true
13
+ },
14
+ "exclude": ["node_modules"]
15
+ }