@twin.org/context 0.0.3-next.3 → 0.0.3-next.5
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/es/models/IContextIdHandler.js +0 -2
- package/dist/es/models/IContextIdHandler.js.map +1 -1
- package/dist/es/utils/contextIdStore.js +13 -12
- package/dist/es/utils/contextIdStore.js.map +1 -1
- package/dist/types/models/IContextIdHandler.d.ts +2 -1
- package/dist/types/utils/contextIdStore.d.ts +3 -6
- package/docs/changelog.md +39 -0
- package/docs/reference/classes/ContextIdStore.md +14 -0
- package/docs/reference/interfaces/IContextIdHandler.md +4 -0
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IContextIdHandler.js","sourceRoot":"","sources":["../../../src/models/IContextIdHandler.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"IContextIdHandler.js","sourceRoot":"","sources":["../../../src/models/IContextIdHandler.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IComponent } from \"@twin.org/core\";\n\n/**\n * Interface describing a context ID handler.\n */\nexport interface IContextIdHandler extends IComponent {\n\t/**\n\t * The short form version of the context ID, should be unique enough to partition data.\n\t * @param value The full context ID value.\n\t * @returns The short form version of the context ID.\n\t */\n\tshort?(value: string): string;\n\n\t/**\n\t * Performs a runtime guard on the provided context ID value.\n\t * @param value The context ID value to guard.\n\t * @throws Guard error if the value is invalid.\n\t */\n\tguard?(value: string): void;\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BaseError, GeneralError } from "@twin.org/core";
|
|
1
|
+
import { BaseError, GeneralError, Is, SharedStore } from "@twin.org/core";
|
|
2
2
|
/**
|
|
3
3
|
* Class to maintain context ids and execute an async method.
|
|
4
4
|
*/
|
|
@@ -7,10 +7,6 @@ export class ContextIdStore {
|
|
|
7
7
|
* Runtime name for the class.
|
|
8
8
|
*/
|
|
9
9
|
static CLASS_NAME = "ContextIdStore";
|
|
10
|
-
/**
|
|
11
|
-
* The async local storage for the context ids.
|
|
12
|
-
*/
|
|
13
|
-
static _storage;
|
|
14
10
|
/**
|
|
15
11
|
* Execute the method wrapped in the context.
|
|
16
12
|
* @param contextIds The context IDs.
|
|
@@ -18,32 +14,37 @@ export class ContextIdStore {
|
|
|
18
14
|
* @returns Nothing.
|
|
19
15
|
*/
|
|
20
16
|
static async run(contextIds, asyncMethod) {
|
|
21
|
-
|
|
17
|
+
const storage = await ContextIdStore.getStorage();
|
|
18
|
+
return storage.run(contextIds, asyncMethod);
|
|
22
19
|
}
|
|
23
20
|
/**
|
|
24
21
|
* Get the context IDs.
|
|
25
22
|
* @returns The context IDs.
|
|
26
23
|
*/
|
|
27
24
|
static async getContextIds() {
|
|
28
|
-
|
|
25
|
+
const storage = await ContextIdStore.getStorage();
|
|
26
|
+
return storage.getStore();
|
|
29
27
|
}
|
|
30
28
|
/**
|
|
31
|
-
*
|
|
29
|
+
* Get the storage and create it if it doesn't exist.
|
|
32
30
|
* @returns The storage.
|
|
33
31
|
*/
|
|
34
|
-
static async
|
|
35
|
-
|
|
32
|
+
static async getStorage() {
|
|
33
|
+
let asyncHooksStore = SharedStore.get("asyncHooks");
|
|
34
|
+
if (Is.empty(asyncHooksStore?.contextIds)) {
|
|
36
35
|
try {
|
|
37
36
|
const hooks = await import("node:async_hooks");
|
|
38
|
-
|
|
37
|
+
asyncHooksStore = asyncHooksStore ?? {};
|
|
38
|
+
asyncHooksStore.contextIds = new hooks.AsyncLocalStorage({
|
|
39
39
|
name: "AsyncContextIdsStorage"
|
|
40
40
|
});
|
|
41
41
|
}
|
|
42
42
|
catch (err) {
|
|
43
43
|
throw new GeneralError(ContextIdStore.CLASS_NAME, "asyncHooksNotAvailable", undefined, BaseError.fromError(err));
|
|
44
44
|
}
|
|
45
|
+
SharedStore.set("asyncHooks", asyncHooksStore);
|
|
45
46
|
}
|
|
46
|
-
return
|
|
47
|
+
return asyncHooksStore.contextIds;
|
|
47
48
|
}
|
|
48
49
|
}
|
|
49
50
|
//# sourceMappingURL=contextIdStore.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contextIdStore.js","sourceRoot":"","sources":["../../../src/utils/contextIdStore.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"contextIdStore.js","sourceRoot":"","sources":["../../../src/utils/contextIdStore.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,EAAE,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAI1E;;GAEG;AACH,MAAM,OAAO,cAAc;IAC1B;;OAEG;IACI,MAAM,CAAU,UAAU,oBAAoC;IAErE;;;;;OAKG;IACI,MAAM,CAAC,KAAK,CAAC,GAAG,CAAc,UAAuB,EAAE,WAAoB;QACjF,MAAM,OAAO,GAAG,MAAM,cAAc,CAAC,UAAU,EAAE,CAAC;QAClD,OAAO,OAAO,CAAC,GAAG,CAAI,UAAU,EAAE,WAAW,CAAC,CAAC;IAChD,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,KAAK,CAAC,aAAa;QAChC,MAAM,OAAO,GAAG,MAAM,cAAc,CAAC,UAAU,EAAE,CAAC;QAClD,OAAO,OAAO,CAAC,QAAQ,EAAE,CAAC;IAC3B,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,KAAK,CAAC,UAAU;QAC7B,IAAI,eAAe,GAAG,WAAW,CAAC,GAAG,CACpC,YAAY,CACZ,CAAC;QAEF,IAAI,EAAE,CAAC,KAAK,CAAC,eAAe,EAAE,UAAU,CAAC,EAAE,CAAC;YAC3C,IAAI,CAAC;gBACJ,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,kBAAkB,CAAC,CAAC;gBAC/C,eAAe,GAAG,eAAe,IAAI,EAAE,CAAC;gBACxC,eAAe,CAAC,UAAU,GAAG,IAAI,KAAK,CAAC,iBAAiB,CAAc;oBACrE,IAAI,EAAE,wBAAwB;iBAC9B,CAAC,CAAC;YACJ,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACd,MAAM,IAAI,YAAY,CACrB,cAAc,CAAC,UAAU,EACzB,wBAAwB,EACxB,SAAS,EACT,SAAS,CAAC,SAAS,CAAC,GAAG,CAAC,CACxB,CAAC;YACH,CAAC;YACD,WAAW,CAAC,GAAG,CAAC,YAAY,EAAE,eAAe,CAAC,CAAC;QAChD,CAAC;QACD,OAAO,eAAe,CAAC,UAAU,CAAC;IACnC,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { AsyncLocalStorage } from \"node:async_hooks\";\nimport { BaseError, GeneralError, Is, SharedStore } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport type { IContextIds } from \"../models/IContextIds.js\";\n\n/**\n * Class to maintain context ids and execute an async method.\n */\nexport class ContextIdStore {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<ContextIdStore>();\n\n\t/**\n\t * Execute the method wrapped in the context.\n\t * @param contextIds The context IDs.\n\t * @param asyncMethod The async method to run.\n\t * @returns Nothing.\n\t */\n\tpublic static async run<T = unknown>(contextIds: IContextIds, asyncMethod: () => T): Promise<T> {\n\t\tconst storage = await ContextIdStore.getStorage();\n\t\treturn storage.run<T>(contextIds, asyncMethod);\n\t}\n\n\t/**\n\t * Get the context IDs.\n\t * @returns The context IDs.\n\t */\n\tpublic static async getContextIds(): Promise<IContextIds | undefined> {\n\t\tconst storage = await ContextIdStore.getStorage();\n\t\treturn storage.getStore();\n\t}\n\n\t/**\n\t * Get the storage and create it if it doesn't exist.\n\t * @returns The storage.\n\t */\n\tpublic static async getStorage(): Promise<AsyncLocalStorage<IContextIds>> {\n\t\tlet asyncHooksStore = SharedStore.get<{ contextIds?: AsyncLocalStorage<IContextIds> }>(\n\t\t\t\"asyncHooks\"\n\t\t);\n\n\t\tif (Is.empty(asyncHooksStore?.contextIds)) {\n\t\t\ttry {\n\t\t\t\tconst hooks = await import(\"node:async_hooks\");\n\t\t\t\tasyncHooksStore = asyncHooksStore ?? {};\n\t\t\t\tasyncHooksStore.contextIds = new hooks.AsyncLocalStorage<IContextIds>({\n\t\t\t\t\tname: \"AsyncContextIdsStorage\"\n\t\t\t\t});\n\t\t\t} catch (err) {\n\t\t\t\tthrow new GeneralError(\n\t\t\t\t\tContextIdStore.CLASS_NAME,\n\t\t\t\t\t\"asyncHooksNotAvailable\",\n\t\t\t\t\tundefined,\n\t\t\t\t\tBaseError.fromError(err)\n\t\t\t\t);\n\t\t\t}\n\t\t\tSharedStore.set(\"asyncHooks\", asyncHooksStore);\n\t\t}\n\t\treturn asyncHooksStore.contextIds;\n\t}\n}\n"]}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import type { IComponent } from "@twin.org/core";
|
|
1
2
|
/**
|
|
2
3
|
* Interface describing a context ID handler.
|
|
3
4
|
*/
|
|
4
|
-
export interface IContextIdHandler {
|
|
5
|
+
export interface IContextIdHandler extends IComponent {
|
|
5
6
|
/**
|
|
6
7
|
* The short form version of the context ID, should be unique enough to partition data.
|
|
7
8
|
* @param value The full context ID value.
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { AsyncLocalStorage } from "node:async_hooks";
|
|
1
2
|
import type { IContextIds } from "../models/IContextIds.js";
|
|
2
3
|
/**
|
|
3
4
|
* Class to maintain context ids and execute an async method.
|
|
@@ -7,10 +8,6 @@ export declare class ContextIdStore {
|
|
|
7
8
|
* Runtime name for the class.
|
|
8
9
|
*/
|
|
9
10
|
static readonly CLASS_NAME: string;
|
|
10
|
-
/**
|
|
11
|
-
* The async local storage for the context ids.
|
|
12
|
-
*/
|
|
13
|
-
private static _storage;
|
|
14
11
|
/**
|
|
15
12
|
* Execute the method wrapped in the context.
|
|
16
13
|
* @param contextIds The context IDs.
|
|
@@ -24,8 +21,8 @@ export declare class ContextIdStore {
|
|
|
24
21
|
*/
|
|
25
22
|
static getContextIds(): Promise<IContextIds | undefined>;
|
|
26
23
|
/**
|
|
27
|
-
*
|
|
24
|
+
* Get the storage and create it if it doesn't exist.
|
|
28
25
|
* @returns The storage.
|
|
29
26
|
*/
|
|
30
|
-
|
|
27
|
+
static getStorage(): Promise<AsyncLocalStorage<IContextIds>>;
|
|
31
28
|
}
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,44 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.0.3-next.5](https://github.com/twinfoundation/framework/compare/context-v0.0.3-next.4...context-v0.0.3-next.5) (2025-11-20)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* context id handler derives from component ([c868ec2](https://github.com/twinfoundation/framework/commit/c868ec21d3a576d4faa222bf130270a21936e50e))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Dependencies
|
|
12
|
+
|
|
13
|
+
* The following workspace dependencies were updated
|
|
14
|
+
* dependencies
|
|
15
|
+
* @twin.org/core bumped from 0.0.3-next.4 to 0.0.3-next.5
|
|
16
|
+
* @twin.org/nameof bumped from 0.0.3-next.4 to 0.0.3-next.5
|
|
17
|
+
* devDependencies
|
|
18
|
+
* @twin.org/nameof-transformer bumped from 0.0.3-next.4 to 0.0.3-next.5
|
|
19
|
+
* @twin.org/nameof-vitest-plugin bumped from 0.0.3-next.4 to 0.0.3-next.5
|
|
20
|
+
* @twin.org/validate-locales bumped from 0.0.3-next.4 to 0.0.3-next.5
|
|
21
|
+
|
|
22
|
+
## [0.0.3-next.4](https://github.com/twinfoundation/framework/compare/context-v0.0.3-next.3...context-v0.0.3-next.4) (2025-11-13)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Bug Fixes
|
|
26
|
+
|
|
27
|
+
* use singleton pattern for context storage ([c69f358](https://github.com/twinfoundation/framework/commit/c69f358e45361b45d4e46f19846cd5b8c99b0ccd))
|
|
28
|
+
* use singleton pattern for context storage ([5cc706a](https://github.com/twinfoundation/framework/commit/5cc706a2bbfc601fa3d00f3efd8b764052e9f91d))
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
### Dependencies
|
|
32
|
+
|
|
33
|
+
* The following workspace dependencies were updated
|
|
34
|
+
* dependencies
|
|
35
|
+
* @twin.org/core bumped from 0.0.3-next.3 to 0.0.3-next.4
|
|
36
|
+
* @twin.org/nameof bumped from 0.0.3-next.3 to 0.0.3-next.4
|
|
37
|
+
* devDependencies
|
|
38
|
+
* @twin.org/nameof-transformer bumped from 0.0.3-next.3 to 0.0.3-next.4
|
|
39
|
+
* @twin.org/nameof-vitest-plugin bumped from 0.0.3-next.3 to 0.0.3-next.4
|
|
40
|
+
* @twin.org/validate-locales bumped from 0.0.3-next.3 to 0.0.3-next.4
|
|
41
|
+
|
|
3
42
|
## [0.0.3-next.3](https://github.com/twinfoundation/framework/compare/context-v0.0.3-next.2...context-v0.0.3-next.3) (2025-11-12)
|
|
4
43
|
|
|
5
44
|
|
|
@@ -67,3 +67,17 @@ Get the context IDs.
|
|
|
67
67
|
`Promise`\<[`IContextIds`](../interfaces/IContextIds.md) \| `undefined`\>
|
|
68
68
|
|
|
69
69
|
The context IDs.
|
|
70
|
+
|
|
71
|
+
***
|
|
72
|
+
|
|
73
|
+
### getStorage()
|
|
74
|
+
|
|
75
|
+
> `static` **getStorage**(): `Promise`\<`AsyncLocalStorage`\<[`IContextIds`](../interfaces/IContextIds.md)\>\>
|
|
76
|
+
|
|
77
|
+
Get the storage and create it if it doesn't exist.
|
|
78
|
+
|
|
79
|
+
#### Returns
|
|
80
|
+
|
|
81
|
+
`Promise`\<`AsyncLocalStorage`\<[`IContextIds`](../interfaces/IContextIds.md)\>\>
|
|
82
|
+
|
|
83
|
+
The storage.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/context",
|
|
3
|
-
"version": "0.0.3-next.
|
|
3
|
+
"version": "0.0.3-next.5",
|
|
4
4
|
"description": "Helper methods/classes for context handling",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
"node": ">=20.0.0"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@twin.org/core": "0.0.3-next.
|
|
18
|
-
"@twin.org/nameof": "0.0.3-next.
|
|
17
|
+
"@twin.org/core": "0.0.3-next.5",
|
|
18
|
+
"@twin.org/nameof": "0.0.3-next.5"
|
|
19
19
|
},
|
|
20
20
|
"main": "./dist/es/index.js",
|
|
21
21
|
"types": "./dist/types/index.d.ts",
|