@collana-solutions/als-context 0.0.0-stage → 3.2.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016-Present Eddy Hernandez, Chris Burrell, Evan Sharp
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.
package/README.md CHANGED
@@ -1,3 +1,92 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
1
+ # @collana-solutions/als-context
2
+
3
+ This package is a wrapper for the `AsyncLocalStorage`.
4
+ Documentation: https://nodejs.org/api/async_context.html#async_context_class_asynclocalstorage.
5
+
6
+ https://dev.to/miketalbot/simplify-your-node-code-with-continuation-local-storage-variables-4b3k:
7
+
8
+ > We have all kinds of ways of managing application state on the front end, but when it comes to the server we can find ourselves lost is a mass of parameters or context variables that need to be forwarded to and through everything in case something needs it later.
9
+ >
10
+ > This is because we can't have global state on something which is processing many things in parallel for different users. At best we could try to create a context and associate that, but there is an easier way using Continuation Local Storage.
11
+ >
12
+ > CLS is so named because it's a bit like Thread Local Storage - data specifically owned by a thread. It's a set of data that is scope to the current execution context. So no matter how many continuations are flowing through the server, each is sure to have it's own copy.
13
+
14
+ So basically we can write and read data to the current request. For example we can write a cookie we receive in the request to the CLS and read it anywhere we want (instead of manually passing it around everywhere).
15
+
16
+ You can also pass the `ALSContext` a name to have multiple contexts, but its optional.
17
+
18
+ ## Example
19
+
20
+ The `AlsContext` is typed so you can pass it an interface to make it strongly typed.
21
+
22
+ ```typescript
23
+ export interface AlsTokenContext {
24
+ token?: string;
25
+ }
26
+ ```
27
+
28
+ Setting it (usually you want to do this in your server file)
29
+
30
+ ```typescript
31
+ const alsContext = new AlsContext<AlsTokenContext>();
32
+
33
+ await alsContext.run(
34
+ // this is the function that will be run in inside the Context
35
+ async () => {
36
+ const store = alsContext.getStore();
37
+ if (store) {
38
+ store.token = token;
39
+ }
40
+ },
41
+ {
42
+ token: "my token",
43
+ },
44
+ );
45
+ ```
46
+
47
+ Using it:
48
+
49
+ ```typescript
50
+ // eslint-disable-next-line global-require
51
+ const alsModule = require("@collana-solutions/als-context");
52
+ const alsContext: AlsContext<AlsTokenContext> = new alsModule.AlsContext();
53
+ return alsContext.getStore()?.token;
54
+ ```
55
+
56
+ ## Caveats
57
+
58
+ :warning: This package only works on the node.js environment not in the browser! So make sure you only require / import it in on the server side, for example like this:
59
+
60
+ ```typescript
61
+ if (typeof window === "undefined") {
62
+ // eslint-disable-next-line global-require
63
+ const alsModule = require("@collana-solutions/als-context");
64
+ const alsContext: AlsContext<YourInterfaceHere> = new alsModule.AlsContext();
65
+ }
66
+ ```
67
+
68
+ But we are allowed to always import the type:
69
+
70
+ ```typescript
71
+ import { AlsContext } from "@collana-solutions/als-context/dist/lib/AlsContext/AlsContext";
72
+ ```
73
+
74
+ ```typescript
75
+ if (typeof window === "undefined") {
76
+ // eslint-disable-next-line global-require
77
+ const alsModule = require("@collana-solutions/als-context");
78
+ const alsContext: AlsContext<YourInterfaceHere> = new alsModule.AlsContext();
79
+ }
80
+ ```
81
+
82
+ You also have to tell webpack to not try to bundle it like so:
83
+
84
+ ```javascript
85
+ webpack: (config, { isServer }) => {
86
+ if (!isServer) {
87
+ config.plugins.push(new IgnorePlugin(/^(\@collana-solutions\/als-context)$/));
88
+ }
89
+
90
+ return config;
91
+ },
92
+ ```
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Provides a class to access the AsyncLocalStorage from "async_hooks"
3
+ */
4
+ declare class AsyncLocalStorageContext<T> {
5
+ /**
6
+ * The name of the instance
7
+ */
8
+ name: string;
9
+ /**
10
+ * The instance of the instiated class
11
+ */
12
+ private instance;
13
+ /**
14
+ * List of all available instances
15
+ */
16
+ private static allInstances;
17
+ /**
18
+ *
19
+ * @param name The name of the instance, optional
20
+ */
21
+ constructor(name?: string);
22
+ /**
23
+ * Disables the context. All further calls to getStore will be undefined.
24
+ * Node.js doc: https://nodejs.org/api/async_context.html#asynclocalstoragedisable
25
+ */
26
+ disable(): void;
27
+ /**
28
+ * Gets the store
29
+ * Node.js doc: https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_getstore
30
+ * @returns The underlying store of type T
31
+ */
32
+ getStore(): T | undefined;
33
+ /**
34
+ * Runs the current instance
35
+ * Node.js doc: https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_run_store_callback_arg
36
+ * @param fn The function to run, the context will be available in all underlying calls
37
+ * @param initialValue The initial values, optional
38
+ */
39
+ run<R>(fn: () => Promise<R>, initialValue: T): Promise<R>;
40
+ }
41
+ export { AsyncLocalStorageContext as AlsContext };
@@ -0,0 +1 @@
1
+ export * from "./AlsContext/AlsContext";
package/dist/index.js ADDED
@@ -0,0 +1,106 @@
1
+ var AsyncLocalStorageImport = function () {
2
+ try {
3
+ return require("async_hooks").AsyncLocalStorage;
4
+ } catch (_unused) {
5
+ return undefined;
6
+ }
7
+ }();
8
+ /**
9
+ * The default instance name
10
+ */
11
+
12
+
13
+ var DEFAULT_CONTEXT_NAME = "_ASYNC_LOCAL_STORAGE_CONTEXT_";
14
+ /**
15
+ * Provides a class to access the AsyncLocalStorage from "async_hooks"
16
+ */
17
+
18
+ var AsyncLocalStorageContext = /*#__PURE__*/function () {
19
+ /**
20
+ * The name of the instance
21
+ */
22
+
23
+ /**
24
+ * The instance of the instiated class
25
+ */
26
+
27
+ /**
28
+ * List of all available instances
29
+ */
30
+
31
+ /**
32
+ *
33
+ * @param name The name of the instance, optional
34
+ */
35
+ function AsyncLocalStorageContext(name) {
36
+ if (name === void 0) {
37
+ name = DEFAULT_CONTEXT_NAME;
38
+ }
39
+
40
+ this.name = void 0;
41
+ this.instance = void 0;
42
+
43
+ // if AsyncLocalStorage doesn't exist do nothing...
44
+ if (AsyncLocalStorageImport === undefined) {
45
+ return;
46
+ }
47
+
48
+ var staticInstance = AsyncLocalStorageContext.allInstances.get(name); // We already have an instance, we won't recreate it...
49
+
50
+ if (staticInstance) {
51
+ this.instance = staticInstance;
52
+ } // We are missing the instance so we create a new one...
53
+ else {
54
+ var newInstance = new AsyncLocalStorageImport();
55
+ this.instance = newInstance;
56
+ AsyncLocalStorageContext.allInstances.set(name, newInstance);
57
+ }
58
+
59
+ this.name = name;
60
+ }
61
+ /**
62
+ * Disables the context. All further calls to getStore will be undefined.
63
+ * Node.js doc: https://nodejs.org/api/async_context.html#asynclocalstoragedisable
64
+ */
65
+
66
+
67
+ var _proto = AsyncLocalStorageContext.prototype;
68
+
69
+ _proto.disable = function disable() {
70
+ this.instance.disable();
71
+ }
72
+ /**
73
+ * Gets the store
74
+ * Node.js doc: https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_getstore
75
+ * @returns The underlying store of type T
76
+ */
77
+ ;
78
+
79
+ _proto.getStore = function getStore() {
80
+ return this.instance.getStore();
81
+ }
82
+ /**
83
+ * Runs the current instance
84
+ * Node.js doc: https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_run_store_callback_arg
85
+ * @param fn The function to run, the context will be available in all underlying calls
86
+ * @param initialValue The initial values, optional
87
+ */
88
+ ;
89
+
90
+ _proto.run = function run(fn, initialValue) {
91
+ try {
92
+ var _this2 = this;
93
+
94
+ return Promise.resolve(_this2.instance.run(initialValue, fn));
95
+ } catch (e) {
96
+ return Promise.reject(e);
97
+ }
98
+ };
99
+
100
+ return AsyncLocalStorageContext;
101
+ }();
102
+
103
+ AsyncLocalStorageContext.allInstances = new Map();
104
+
105
+ exports.AlsContext = AsyncLocalStorageContext;
106
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/AlsContext/AlsContext.ts"],"sourcesContent":["import type { AsyncLocalStorage as AsyncLocalStorageType } from \"async_hooks\";\r\n\r\nconst AsyncLocalStorageImport = (() => {\r\n try {\r\n return require(\"async_hooks\").AsyncLocalStorage;\r\n } catch {\r\n return undefined;\r\n }\r\n})();\r\n\r\n/**\r\n * The default instance name\r\n */\r\nconst DEFAULT_CONTEXT_NAME = \"_ASYNC_LOCAL_STORAGE_CONTEXT_\";\r\n\r\n/**\r\n * Provides a class to access the AsyncLocalStorage from \"async_hooks\"\r\n */\r\nclass AsyncLocalStorageContext<T> {\r\n /**\r\n * The name of the instance\r\n */\r\n public name: string;\r\n\r\n /**\r\n * The instance of the instiated class\r\n */\r\n private instance: AsyncLocalStorageType<T>;\r\n\r\n /**\r\n * List of all available instances\r\n */\r\n private static allInstances: Map<string, AsyncLocalStorageType<any>> = new Map();\r\n\r\n /**\r\n *\r\n * @param name The name of the instance, optional\r\n */\r\n constructor(name = DEFAULT_CONTEXT_NAME) {\r\n // if AsyncLocalStorage doesn't exist do nothing...\r\n if (AsyncLocalStorageImport === undefined) {\r\n return;\r\n }\r\n\r\n const staticInstance = AsyncLocalStorageContext.allInstances.get(name);\r\n // We already have an instance, we won't recreate it...\r\n if (staticInstance) {\r\n this.instance = staticInstance;\r\n }\r\n // We are missing the instance so we create a new one...\r\n else {\r\n const newInstance = new AsyncLocalStorageImport();\r\n this.instance = newInstance;\r\n AsyncLocalStorageContext.allInstances.set(name, newInstance);\r\n }\r\n\r\n this.name = name;\r\n }\r\n\r\n /**\r\n * Disables the context. All further calls to getStore will be undefined.\r\n * Node.js doc: https://nodejs.org/api/async_context.html#asynclocalstoragedisable\r\n */\r\n disable() {\r\n this.instance.disable();\r\n }\r\n\r\n /**\r\n * Gets the store\r\n * Node.js doc: https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_getstore\r\n * @returns The underlying store of type T\r\n */\r\n getStore(): T | undefined {\r\n return this.instance.getStore();\r\n }\r\n\r\n /**\r\n * Runs the current instance\r\n * Node.js doc: https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_run_store_callback_arg\r\n * @param fn The function to run, the context will be available in all underlying calls\r\n * @param initialValue The initial values, optional\r\n */\r\n async run<R>(fn: () => Promise<R>, initialValue: T) {\r\n return this.instance.run(initialValue, fn);\r\n }\r\n}\r\n\r\nexport { AsyncLocalStorageContext as AlsContext };\r\n"],"names":["AsyncLocalStorageImport","require","AsyncLocalStorage","undefined","DEFAULT_CONTEXT_NAME","AsyncLocalStorageContext","name","instance","staticInstance","allInstances","get","newInstance","set","disable","getStore","run","fn","initialValue","Map"],"mappings":"AAEA,IAAMA,uBAAuB,GAAI;AAC/B,MAAI;AACF,WAAOC,OAAO,CAAC,aAAD,CAAP,CAAuBC,iBAA9B;AACD,GAFD,CAEE,gBAAM;AACN,WAAOC,SAAP;AACD;AACF,CAN+B,EAAhC;AAQA;;;;;AAGA,IAAMC,oBAAoB,GAAG,+BAA7B;AAEA;;;;IAGMC;AACJ;;;;AAKA;;;;AAKA;;;;AAKA;;;;AAIA,oCAAYC,IAAZ;QAAYA;AAAAA,MAAAA,OAAOF;;;SAhBZE;SAKCC;;AAYN;AACA,QAAIP,uBAAuB,KAAKG,SAAhC,EAA2C;AACzC;AACD;;AAED,QAAMK,cAAc,GAAGH,wBAAwB,CAACI,YAAzB,CAAsCC,GAAtC,CAA0CJ,IAA1C,CAAvB;;AAEA,QAAIE,cAAJ,EAAoB;AAClB,WAAKD,QAAL,GAAgBC,cAAhB;AACD,KAFD;AAAA,SAIK;AACH,UAAMG,WAAW,GAAG,IAAIX,uBAAJ,EAApB;AACA,WAAKO,QAAL,GAAgBI,WAAhB;AACAN,MAAAA,wBAAwB,CAACI,YAAzB,CAAsCG,GAAtC,CAA0CN,IAA1C,EAAgDK,WAAhD;AACD;;AAED,SAAKL,IAAL,GAAYA,IAAZ;AACD;AAED;;;;;;;;SAIAO,UAAA;AACE,SAAKN,QAAL,CAAcM,OAAd;AACD;AAED;;;;;;;SAKAC,WAAA;AACE,WAAO,KAAKP,QAAL,CAAcO,QAAd,EAAP;AACD;AAED;;;;;;;;SAMMC,mBAAOC,IAAsBC;;mBAC1B;;AAAP,6BAAO,OAAKV,QAAL,CAAcQ,GAAd,CAAkBE,YAAlB,EAAgCD,EAAhC,CAAP;AACD;;;;;;;;AAlEGX,yBAcWI,eAAwD,IAAIS,GAAJ;;;;"}
@@ -0,0 +1,93 @@
1
+ const AsyncLocalStorageImport = (() => {
2
+ try {
3
+ return require("async_hooks").AsyncLocalStorage;
4
+ } catch (_unused) {
5
+ return undefined;
6
+ }
7
+ })();
8
+ /**
9
+ * The default instance name
10
+ */
11
+
12
+
13
+ const DEFAULT_CONTEXT_NAME = "_ASYNC_LOCAL_STORAGE_CONTEXT_";
14
+ /**
15
+ * Provides a class to access the AsyncLocalStorage from "async_hooks"
16
+ */
17
+
18
+ class AsyncLocalStorageContext {
19
+ /**
20
+ * The name of the instance
21
+ */
22
+
23
+ /**
24
+ * The instance of the instiated class
25
+ */
26
+
27
+ /**
28
+ * List of all available instances
29
+ */
30
+
31
+ /**
32
+ *
33
+ * @param name The name of the instance, optional
34
+ */
35
+ constructor(name = DEFAULT_CONTEXT_NAME) {
36
+ this.name = void 0;
37
+ this.instance = void 0;
38
+
39
+ // if AsyncLocalStorage doesn't exist do nothing...
40
+ if (AsyncLocalStorageImport === undefined) {
41
+ return;
42
+ }
43
+
44
+ const staticInstance = AsyncLocalStorageContext.allInstances.get(name); // We already have an instance, we won't recreate it...
45
+
46
+ if (staticInstance) {
47
+ this.instance = staticInstance;
48
+ } // We are missing the instance so we create a new one...
49
+ else {
50
+ const newInstance = new AsyncLocalStorageImport();
51
+ this.instance = newInstance;
52
+ AsyncLocalStorageContext.allInstances.set(name, newInstance);
53
+ }
54
+
55
+ this.name = name;
56
+ }
57
+ /**
58
+ * Disables the context. All further calls to getStore will be undefined.
59
+ * Node.js doc: https://nodejs.org/api/async_context.html#asynclocalstoragedisable
60
+ */
61
+
62
+
63
+ disable() {
64
+ this.instance.disable();
65
+ }
66
+ /**
67
+ * Gets the store
68
+ * Node.js doc: https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_getstore
69
+ * @returns The underlying store of type T
70
+ */
71
+
72
+
73
+ getStore() {
74
+ return this.instance.getStore();
75
+ }
76
+ /**
77
+ * Runs the current instance
78
+ * Node.js doc: https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_run_store_callback_arg
79
+ * @param fn The function to run, the context will be available in all underlying calls
80
+ * @param initialValue The initial values, optional
81
+ */
82
+
83
+
84
+ async run(fn, initialValue) {
85
+ return this.instance.run(initialValue, fn);
86
+ }
87
+
88
+ }
89
+
90
+ AsyncLocalStorageContext.allInstances = new Map();
91
+
92
+ export { AsyncLocalStorageContext as AlsContext };
93
+ //# sourceMappingURL=index.modern.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.modern.js","sources":["../src/AlsContext/AlsContext.ts"],"sourcesContent":["import type { AsyncLocalStorage as AsyncLocalStorageType } from \"async_hooks\";\r\n\r\nconst AsyncLocalStorageImport = (() => {\r\n try {\r\n return require(\"async_hooks\").AsyncLocalStorage;\r\n } catch {\r\n return undefined;\r\n }\r\n})();\r\n\r\n/**\r\n * The default instance name\r\n */\r\nconst DEFAULT_CONTEXT_NAME = \"_ASYNC_LOCAL_STORAGE_CONTEXT_\";\r\n\r\n/**\r\n * Provides a class to access the AsyncLocalStorage from \"async_hooks\"\r\n */\r\nclass AsyncLocalStorageContext<T> {\r\n /**\r\n * The name of the instance\r\n */\r\n public name: string;\r\n\r\n /**\r\n * The instance of the instiated class\r\n */\r\n private instance: AsyncLocalStorageType<T>;\r\n\r\n /**\r\n * List of all available instances\r\n */\r\n private static allInstances: Map<string, AsyncLocalStorageType<any>> = new Map();\r\n\r\n /**\r\n *\r\n * @param name The name of the instance, optional\r\n */\r\n constructor(name = DEFAULT_CONTEXT_NAME) {\r\n // if AsyncLocalStorage doesn't exist do nothing...\r\n if (AsyncLocalStorageImport === undefined) {\r\n return;\r\n }\r\n\r\n const staticInstance = AsyncLocalStorageContext.allInstances.get(name);\r\n // We already have an instance, we won't recreate it...\r\n if (staticInstance) {\r\n this.instance = staticInstance;\r\n }\r\n // We are missing the instance so we create a new one...\r\n else {\r\n const newInstance = new AsyncLocalStorageImport();\r\n this.instance = newInstance;\r\n AsyncLocalStorageContext.allInstances.set(name, newInstance);\r\n }\r\n\r\n this.name = name;\r\n }\r\n\r\n /**\r\n * Disables the context. All further calls to getStore will be undefined.\r\n * Node.js doc: https://nodejs.org/api/async_context.html#asynclocalstoragedisable\r\n */\r\n disable() {\r\n this.instance.disable();\r\n }\r\n\r\n /**\r\n * Gets the store\r\n * Node.js doc: https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_getstore\r\n * @returns The underlying store of type T\r\n */\r\n getStore(): T | undefined {\r\n return this.instance.getStore();\r\n }\r\n\r\n /**\r\n * Runs the current instance\r\n * Node.js doc: https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_run_store_callback_arg\r\n * @param fn The function to run, the context will be available in all underlying calls\r\n * @param initialValue The initial values, optional\r\n */\r\n async run<R>(fn: () => Promise<R>, initialValue: T) {\r\n return this.instance.run(initialValue, fn);\r\n }\r\n}\r\n\r\nexport { AsyncLocalStorageContext as AlsContext };\r\n"],"names":["AsyncLocalStorageImport","require","AsyncLocalStorage","undefined","DEFAULT_CONTEXT_NAME","AsyncLocalStorageContext","constructor","name","instance","staticInstance","allInstances","get","newInstance","set","disable","getStore","run","fn","initialValue","Map"],"mappings":"AAEA,MAAMA,uBAAuB,GAAG,CAAC;AAC/B,MAAI;AACF,WAAOC,OAAO,CAAC,aAAD,CAAP,CAAuBC,iBAA9B;AACD,GAFD,CAEE,gBAAM;AACN,WAAOC,SAAP;AACD;AACF,CAN+B,GAAhC;AAQA;;;;;AAGA,MAAMC,oBAAoB,GAAG,+BAA7B;AAEA;;;;AAGA,MAAMC,wBAAN;AACE;;;;AAKA;;;;AAKA;;;;AAKA;;;;AAIAC,EAAAA,YAAYC,IAAI,GAAGH;SAhBZG;SAKCC;;AAYN;AACA,QAAIR,uBAAuB,KAAKG,SAAhC,EAA2C;AACzC;AACD;;AAED,UAAMM,cAAc,GAAGJ,wBAAwB,CAACK,YAAzB,CAAsCC,GAAtC,CAA0CJ,IAA1C,CAAvB;;AAEA,QAAIE,cAAJ,EAAoB;AAClB,WAAKD,QAAL,GAAgBC,cAAhB;AACD,KAFD;AAAA,SAIK;AACH,YAAMG,WAAW,GAAG,IAAIZ,uBAAJ,EAApB;AACA,WAAKQ,QAAL,GAAgBI,WAAhB;AACAP,MAAAA,wBAAwB,CAACK,YAAzB,CAAsCG,GAAtC,CAA0CN,IAA1C,EAAgDK,WAAhD;AACD;;AAED,SAAKL,IAAL,GAAYA,IAAZ;AACD;AAED;;;;;;AAIAO,EAAAA,OAAO;AACL,SAAKN,QAAL,CAAcM,OAAd;AACD;AAED;;;;;;;AAKAC,EAAAA,QAAQ;AACN,WAAO,KAAKP,QAAL,CAAcO,QAAd,EAAP;AACD;AAED;;;;;;;;AAMS,QAAHC,GAAG,CAAIC,EAAJ,EAA0BC,YAA1B;AACP,WAAO,KAAKV,QAAL,CAAcQ,GAAd,CAAkBE,YAAlB,EAAgCD,EAAhC,CAAP;AACD;;;;AAlEGZ,yBAcWK,eAAwD,IAAIS,GAAJ;;;;"}
@@ -0,0 +1,106 @@
1
+ var AsyncLocalStorageImport = function () {
2
+ try {
3
+ return require("async_hooks").AsyncLocalStorage;
4
+ } catch (_unused) {
5
+ return undefined;
6
+ }
7
+ }();
8
+ /**
9
+ * The default instance name
10
+ */
11
+
12
+
13
+ var DEFAULT_CONTEXT_NAME = "_ASYNC_LOCAL_STORAGE_CONTEXT_";
14
+ /**
15
+ * Provides a class to access the AsyncLocalStorage from "async_hooks"
16
+ */
17
+
18
+ var AsyncLocalStorageContext = /*#__PURE__*/function () {
19
+ /**
20
+ * The name of the instance
21
+ */
22
+
23
+ /**
24
+ * The instance of the instiated class
25
+ */
26
+
27
+ /**
28
+ * List of all available instances
29
+ */
30
+
31
+ /**
32
+ *
33
+ * @param name The name of the instance, optional
34
+ */
35
+ function AsyncLocalStorageContext(name) {
36
+ if (name === void 0) {
37
+ name = DEFAULT_CONTEXT_NAME;
38
+ }
39
+
40
+ this.name = void 0;
41
+ this.instance = void 0;
42
+
43
+ // if AsyncLocalStorage doesn't exist do nothing...
44
+ if (AsyncLocalStorageImport === undefined) {
45
+ return;
46
+ }
47
+
48
+ var staticInstance = AsyncLocalStorageContext.allInstances.get(name); // We already have an instance, we won't recreate it...
49
+
50
+ if (staticInstance) {
51
+ this.instance = staticInstance;
52
+ } // We are missing the instance so we create a new one...
53
+ else {
54
+ var newInstance = new AsyncLocalStorageImport();
55
+ this.instance = newInstance;
56
+ AsyncLocalStorageContext.allInstances.set(name, newInstance);
57
+ }
58
+
59
+ this.name = name;
60
+ }
61
+ /**
62
+ * Disables the context. All further calls to getStore will be undefined.
63
+ * Node.js doc: https://nodejs.org/api/async_context.html#asynclocalstoragedisable
64
+ */
65
+
66
+
67
+ var _proto = AsyncLocalStorageContext.prototype;
68
+
69
+ _proto.disable = function disable() {
70
+ this.instance.disable();
71
+ }
72
+ /**
73
+ * Gets the store
74
+ * Node.js doc: https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_getstore
75
+ * @returns The underlying store of type T
76
+ */
77
+ ;
78
+
79
+ _proto.getStore = function getStore() {
80
+ return this.instance.getStore();
81
+ }
82
+ /**
83
+ * Runs the current instance
84
+ * Node.js doc: https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_run_store_callback_arg
85
+ * @param fn The function to run, the context will be available in all underlying calls
86
+ * @param initialValue The initial values, optional
87
+ */
88
+ ;
89
+
90
+ _proto.run = function run(fn, initialValue) {
91
+ try {
92
+ var _this2 = this;
93
+
94
+ return Promise.resolve(_this2.instance.run(initialValue, fn));
95
+ } catch (e) {
96
+ return Promise.reject(e);
97
+ }
98
+ };
99
+
100
+ return AsyncLocalStorageContext;
101
+ }();
102
+
103
+ AsyncLocalStorageContext.allInstances = new Map();
104
+
105
+ export { AsyncLocalStorageContext as AlsContext };
106
+ //# sourceMappingURL=index.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.module.js","sources":["../src/AlsContext/AlsContext.ts"],"sourcesContent":["import type { AsyncLocalStorage as AsyncLocalStorageType } from \"async_hooks\";\r\n\r\nconst AsyncLocalStorageImport = (() => {\r\n try {\r\n return require(\"async_hooks\").AsyncLocalStorage;\r\n } catch {\r\n return undefined;\r\n }\r\n})();\r\n\r\n/**\r\n * The default instance name\r\n */\r\nconst DEFAULT_CONTEXT_NAME = \"_ASYNC_LOCAL_STORAGE_CONTEXT_\";\r\n\r\n/**\r\n * Provides a class to access the AsyncLocalStorage from \"async_hooks\"\r\n */\r\nclass AsyncLocalStorageContext<T> {\r\n /**\r\n * The name of the instance\r\n */\r\n public name: string;\r\n\r\n /**\r\n * The instance of the instiated class\r\n */\r\n private instance: AsyncLocalStorageType<T>;\r\n\r\n /**\r\n * List of all available instances\r\n */\r\n private static allInstances: Map<string, AsyncLocalStorageType<any>> = new Map();\r\n\r\n /**\r\n *\r\n * @param name The name of the instance, optional\r\n */\r\n constructor(name = DEFAULT_CONTEXT_NAME) {\r\n // if AsyncLocalStorage doesn't exist do nothing...\r\n if (AsyncLocalStorageImport === undefined) {\r\n return;\r\n }\r\n\r\n const staticInstance = AsyncLocalStorageContext.allInstances.get(name);\r\n // We already have an instance, we won't recreate it...\r\n if (staticInstance) {\r\n this.instance = staticInstance;\r\n }\r\n // We are missing the instance so we create a new one...\r\n else {\r\n const newInstance = new AsyncLocalStorageImport();\r\n this.instance = newInstance;\r\n AsyncLocalStorageContext.allInstances.set(name, newInstance);\r\n }\r\n\r\n this.name = name;\r\n }\r\n\r\n /**\r\n * Disables the context. All further calls to getStore will be undefined.\r\n * Node.js doc: https://nodejs.org/api/async_context.html#asynclocalstoragedisable\r\n */\r\n disable() {\r\n this.instance.disable();\r\n }\r\n\r\n /**\r\n * Gets the store\r\n * Node.js doc: https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_getstore\r\n * @returns The underlying store of type T\r\n */\r\n getStore(): T | undefined {\r\n return this.instance.getStore();\r\n }\r\n\r\n /**\r\n * Runs the current instance\r\n * Node.js doc: https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_run_store_callback_arg\r\n * @param fn The function to run, the context will be available in all underlying calls\r\n * @param initialValue The initial values, optional\r\n */\r\n async run<R>(fn: () => Promise<R>, initialValue: T) {\r\n return this.instance.run(initialValue, fn);\r\n }\r\n}\r\n\r\nexport { AsyncLocalStorageContext as AlsContext };\r\n"],"names":["AsyncLocalStorageImport","require","AsyncLocalStorage","undefined","DEFAULT_CONTEXT_NAME","AsyncLocalStorageContext","name","instance","staticInstance","allInstances","get","newInstance","set","disable","getStore","run","fn","initialValue","Map"],"mappings":"AAEA,IAAMA,uBAAuB,GAAI;AAC/B,MAAI;AACF,WAAOC,OAAO,CAAC,aAAD,CAAP,CAAuBC,iBAA9B;AACD,GAFD,CAEE,gBAAM;AACN,WAAOC,SAAP;AACD;AACF,CAN+B,EAAhC;AAQA;;;;;AAGA,IAAMC,oBAAoB,GAAG,+BAA7B;AAEA;;;;IAGMC;AACJ;;;;AAKA;;;;AAKA;;;;AAKA;;;;AAIA,oCAAYC,IAAZ;QAAYA;AAAAA,MAAAA,OAAOF;;;SAhBZE;SAKCC;;AAYN;AACA,QAAIP,uBAAuB,KAAKG,SAAhC,EAA2C;AACzC;AACD;;AAED,QAAMK,cAAc,GAAGH,wBAAwB,CAACI,YAAzB,CAAsCC,GAAtC,CAA0CJ,IAA1C,CAAvB;;AAEA,QAAIE,cAAJ,EAAoB;AAClB,WAAKD,QAAL,GAAgBC,cAAhB;AACD,KAFD;AAAA,SAIK;AACH,UAAMG,WAAW,GAAG,IAAIX,uBAAJ,EAApB;AACA,WAAKO,QAAL,GAAgBI,WAAhB;AACAN,MAAAA,wBAAwB,CAACI,YAAzB,CAAsCG,GAAtC,CAA0CN,IAA1C,EAAgDK,WAAhD;AACD;;AAED,SAAKL,IAAL,GAAYA,IAAZ;AACD;AAED;;;;;;;;SAIAO,UAAA;AACE,SAAKN,QAAL,CAAcM,OAAd;AACD;AAED;;;;;;;SAKAC,WAAA;AACE,WAAO,KAAKP,QAAL,CAAcO,QAAd,EAAP;AACD;AAED;;;;;;;;SAMMC,mBAAOC,IAAsBC;;mBAC1B;;AAAP,6BAAO,OAAKV,QAAL,CAAcQ,GAAd,CAAkBE,YAAlB,EAAgCD,EAAhC,CAAP;AACD;;;;;;;;AAlEGX,yBAcWI,eAAwD,IAAIS,GAAJ;;;;"}
@@ -0,0 +1,113 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
+ (global = global || self, factory(global.alsContext = {}));
5
+ })(this, (function (exports) {
6
+ var AsyncLocalStorageImport = function () {
7
+ try {
8
+ return require("async_hooks").AsyncLocalStorage;
9
+ } catch (_unused) {
10
+ return undefined;
11
+ }
12
+ }();
13
+ /**
14
+ * The default instance name
15
+ */
16
+
17
+
18
+ var DEFAULT_CONTEXT_NAME = "_ASYNC_LOCAL_STORAGE_CONTEXT_";
19
+ /**
20
+ * Provides a class to access the AsyncLocalStorage from "async_hooks"
21
+ */
22
+
23
+ var AsyncLocalStorageContext = /*#__PURE__*/function () {
24
+ /**
25
+ * The name of the instance
26
+ */
27
+
28
+ /**
29
+ * The instance of the instiated class
30
+ */
31
+
32
+ /**
33
+ * List of all available instances
34
+ */
35
+
36
+ /**
37
+ *
38
+ * @param name The name of the instance, optional
39
+ */
40
+ function AsyncLocalStorageContext(name) {
41
+ if (name === void 0) {
42
+ name = DEFAULT_CONTEXT_NAME;
43
+ }
44
+
45
+ this.name = void 0;
46
+ this.instance = void 0;
47
+
48
+ // if AsyncLocalStorage doesn't exist do nothing...
49
+ if (AsyncLocalStorageImport === undefined) {
50
+ return;
51
+ }
52
+
53
+ var staticInstance = AsyncLocalStorageContext.allInstances.get(name); // We already have an instance, we won't recreate it...
54
+
55
+ if (staticInstance) {
56
+ this.instance = staticInstance;
57
+ } // We are missing the instance so we create a new one...
58
+ else {
59
+ var newInstance = new AsyncLocalStorageImport();
60
+ this.instance = newInstance;
61
+ AsyncLocalStorageContext.allInstances.set(name, newInstance);
62
+ }
63
+
64
+ this.name = name;
65
+ }
66
+ /**
67
+ * Disables the context. All further calls to getStore will be undefined.
68
+ * Node.js doc: https://nodejs.org/api/async_context.html#asynclocalstoragedisable
69
+ */
70
+
71
+
72
+ var _proto = AsyncLocalStorageContext.prototype;
73
+
74
+ _proto.disable = function disable() {
75
+ this.instance.disable();
76
+ }
77
+ /**
78
+ * Gets the store
79
+ * Node.js doc: https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_getstore
80
+ * @returns The underlying store of type T
81
+ */
82
+ ;
83
+
84
+ _proto.getStore = function getStore() {
85
+ return this.instance.getStore();
86
+ }
87
+ /**
88
+ * Runs the current instance
89
+ * Node.js doc: https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_run_store_callback_arg
90
+ * @param fn The function to run, the context will be available in all underlying calls
91
+ * @param initialValue The initial values, optional
92
+ */
93
+ ;
94
+
95
+ _proto.run = function run(fn, initialValue) {
96
+ try {
97
+ var _this2 = this;
98
+
99
+ return Promise.resolve(_this2.instance.run(initialValue, fn));
100
+ } catch (e) {
101
+ return Promise.reject(e);
102
+ }
103
+ };
104
+
105
+ return AsyncLocalStorageContext;
106
+ }();
107
+
108
+ AsyncLocalStorageContext.allInstances = new Map();
109
+
110
+ exports.AlsContext = AsyncLocalStorageContext;
111
+
112
+ }));
113
+ //# sourceMappingURL=index.umd.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.umd.js","sources":["../src/AlsContext/AlsContext.ts"],"sourcesContent":["import type { AsyncLocalStorage as AsyncLocalStorageType } from \"async_hooks\";\r\n\r\nconst AsyncLocalStorageImport = (() => {\r\n try {\r\n return require(\"async_hooks\").AsyncLocalStorage;\r\n } catch {\r\n return undefined;\r\n }\r\n})();\r\n\r\n/**\r\n * The default instance name\r\n */\r\nconst DEFAULT_CONTEXT_NAME = \"_ASYNC_LOCAL_STORAGE_CONTEXT_\";\r\n\r\n/**\r\n * Provides a class to access the AsyncLocalStorage from \"async_hooks\"\r\n */\r\nclass AsyncLocalStorageContext<T> {\r\n /**\r\n * The name of the instance\r\n */\r\n public name: string;\r\n\r\n /**\r\n * The instance of the instiated class\r\n */\r\n private instance: AsyncLocalStorageType<T>;\r\n\r\n /**\r\n * List of all available instances\r\n */\r\n private static allInstances: Map<string, AsyncLocalStorageType<any>> = new Map();\r\n\r\n /**\r\n *\r\n * @param name The name of the instance, optional\r\n */\r\n constructor(name = DEFAULT_CONTEXT_NAME) {\r\n // if AsyncLocalStorage doesn't exist do nothing...\r\n if (AsyncLocalStorageImport === undefined) {\r\n return;\r\n }\r\n\r\n const staticInstance = AsyncLocalStorageContext.allInstances.get(name);\r\n // We already have an instance, we won't recreate it...\r\n if (staticInstance) {\r\n this.instance = staticInstance;\r\n }\r\n // We are missing the instance so we create a new one...\r\n else {\r\n const newInstance = new AsyncLocalStorageImport();\r\n this.instance = newInstance;\r\n AsyncLocalStorageContext.allInstances.set(name, newInstance);\r\n }\r\n\r\n this.name = name;\r\n }\r\n\r\n /**\r\n * Disables the context. All further calls to getStore will be undefined.\r\n * Node.js doc: https://nodejs.org/api/async_context.html#asynclocalstoragedisable\r\n */\r\n disable() {\r\n this.instance.disable();\r\n }\r\n\r\n /**\r\n * Gets the store\r\n * Node.js doc: https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_getstore\r\n * @returns The underlying store of type T\r\n */\r\n getStore(): T | undefined {\r\n return this.instance.getStore();\r\n }\r\n\r\n /**\r\n * Runs the current instance\r\n * Node.js doc: https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_run_store_callback_arg\r\n * @param fn The function to run, the context will be available in all underlying calls\r\n * @param initialValue The initial values, optional\r\n */\r\n async run<R>(fn: () => Promise<R>, initialValue: T) {\r\n return this.instance.run(initialValue, fn);\r\n }\r\n}\r\n\r\nexport { AsyncLocalStorageContext as AlsContext };\r\n"],"names":["AsyncLocalStorageImport","require","AsyncLocalStorage","undefined","DEFAULT_CONTEXT_NAME","AsyncLocalStorageContext","name","instance","staticInstance","allInstances","get","newInstance","set","disable","getStore","run","fn","initialValue","Map"],"mappings":";;;;;EAEA,IAAMA,uBAAuB,GAAI;EAC/B,MAAI;EACF,WAAOC,OAAO,CAAC,aAAD,CAAP,CAAuBC,iBAA9B;EACD,GAFD,CAEE,gBAAM;EACN,WAAOC,SAAP;EACD;EACF,CAN+B,EAAhC;EAQA;;;;;EAGA,IAAMC,oBAAoB,GAAG,+BAA7B;EAEA;;;;MAGMC;EACJ;;;;EAKA;;;;EAKA;;;;EAKA;;;;EAIA,oCAAYC,IAAZ;UAAYA;EAAAA,MAAAA,OAAOF;;;WAhBZE;WAKCC;;EAYN;EACA,QAAIP,uBAAuB,KAAKG,SAAhC,EAA2C;EACzC;EACD;;EAED,QAAMK,cAAc,GAAGH,wBAAwB,CAACI,YAAzB,CAAsCC,GAAtC,CAA0CJ,IAA1C,CAAvB;;EAEA,QAAIE,cAAJ,EAAoB;EAClB,WAAKD,QAAL,GAAgBC,cAAhB;EACD,KAFD;EAAA,SAIK;EACH,UAAMG,WAAW,GAAG,IAAIX,uBAAJ,EAApB;EACA,WAAKO,QAAL,GAAgBI,WAAhB;EACAN,MAAAA,wBAAwB,CAACI,YAAzB,CAAsCG,GAAtC,CAA0CN,IAA1C,EAAgDK,WAAhD;EACD;;EAED,SAAKL,IAAL,GAAYA,IAAZ;EACD;EAED;;;;;;;;WAIAO,UAAA;EACE,SAAKN,QAAL,CAAcM,OAAd;EACD;EAED;;;;;;;WAKAC,WAAA;EACE,WAAO,KAAKP,QAAL,CAAcO,QAAd,EAAP;EACD;EAED;;;;;;;;WAMMC,mBAAOC,IAAsBC;;qBAC1B;;EAAP,6BAAO,OAAKV,QAAL,CAAcQ,GAAd,CAAkBE,YAAlB,EAAgCD,EAAhC,CAAP;EACD;;;;;;;;EAlEGX,yBAcWI,eAAwD,IAAIS,GAAJ;;;;;;;;"}
package/package.json CHANGED
@@ -1,6 +1,64 @@
1
- {
2
- "name": "@collana-solutions/als-context",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
1
+ {
2
+ "name": "@collana-solutions/als-context",
3
+ "version": "3.2.0",
4
+ "description": "Provides an easy to use class for using AsyncLocalStorage from 'async_hooks' by nodejs",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/collana-solutions/als-context"
8
+ },
9
+ "license": "MIT",
10
+ "author": "collana solutions AG",
11
+ "main": "./dist/index.js",
12
+ "unpkg": "./dist/index.umd.js",
13
+ "module": "./dist/index.module.js",
14
+ "source": "src/index.ts",
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "shx rm -rf dist && microbundle --no-compress --tsconfig ./tsconfig.microbundle.json",
20
+ "lint": "eslint \"**/*.{ts,tsx}\" --cache --max-warnings 0",
21
+ "prepack": "pnpm run build",
22
+ "prettier-check": "prettier --check .",
23
+ "start": "microbundle watch --no-compress --tsconfig ./tsconfig.microbundle.json",
24
+ "start-all": "concurrently \"pnpm run start\" \"pnpm run start-yalc\"",
25
+ "start-yalc": "nodemon --watch dist -x \"yalc push\"",
26
+ "test": "cross-env DEBUG_PRINT_LIMIT=100 jest",
27
+ "tsc": "tsc",
28
+ "changeset": "changeset",
29
+ "version-packages": "changeset version",
30
+ "release": "changeset publish"
31
+ },
32
+ "devDependencies": {
33
+ "@changesets/changelog-github": "^1.0.1",
34
+ "@changesets/cli": "^3.0.3",
35
+ "@faker-js/faker": "^6.0.0-alpha.7",
36
+ "@types/jest": "^27.4.1",
37
+ "@types/node": "^14.14.37",
38
+ "@typescript-eslint/eslint-plugin": "^5.12.1",
39
+ "@typescript-eslint/parser": "^5.12.1",
40
+ "concurrently": "^7.0.0",
41
+ "cross-env": "^7.0.3",
42
+ "eslint": "^8.9.0",
43
+ "eslint-config-prettier": "^8.4.0",
44
+ "eslint-import-resolver-typescript": "^2.5.0",
45
+ "eslint-plugin-import": "^2.25.4",
46
+ "jest": "^27.5.1",
47
+ "microbundle": "^0.14.2",
48
+ "nodemon": "^2.0.15",
49
+ "pkg-pr-new": "^0.0.93",
50
+ "prettier": "^2.5.1",
51
+ "shx": "^0.3.3",
52
+ "ts-jest": "^27.1.3",
53
+ "typescript": "^4.2.3",
54
+ "yalc": "^1.0.0-pre.53"
55
+ },
56
+ "engines": {
57
+ "node": ">=13.10.0"
58
+ },
59
+ "publishConfig": {
60
+ "access": "public",
61
+ "registry": "https://registry.npmjs.org"
62
+ },
63
+ "packageManager": "pnpm@11.22.0"
64
+ }