@twin.org/context 0.9.1-next.7 → 0.9.1-next.8

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.
@@ -1,4 +1,4 @@
1
- import { BaseError, GeneralError, SharedStore } 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
  */
@@ -33,7 +33,7 @@ export class ContextIdStore {
33
33
  // get with the factory is invoked synchronously and stores the returned
34
34
  // Promise before yielding, so concurrent callers always await the same
35
35
  // AsyncLocalStorage instance regardless of how many module versions are loaded.
36
- return SharedStore.get("asyncHooks", async () => {
36
+ const stored = SharedStore.get("asyncHooks", async () => {
37
37
  try {
38
38
  const hooks = await import("node:async_hooks");
39
39
  return new hooks.AsyncLocalStorage({
@@ -45,6 +45,14 @@ export class ContextIdStore {
45
45
  throw new GeneralError(ContextIdStore.CLASS_NAME, "asyncHooksNotAvailable", undefined, BaseError.fromError(err));
46
46
  }
47
47
  });
48
+ // Back-compat with context@0.9.0: that version stored { contextIds: AsyncLocalStorage }
49
+ // (a plain wrapper object, not a Promise). Leave SharedStore untouched - upgrading to
50
+ // the Promise shape would cause old code to see no contextIds, create a second
51
+ // AsyncLocalStorage, and silently split context between versions.
52
+ if (!Is.promise(stored)) {
53
+ return stored.contextIds;
54
+ }
55
+ return stored;
48
56
  }
49
57
  }
50
58
  //# 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,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAItE;;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,wEAAwE;QACxE,uEAAuE;QACvE,gFAAgF;QAChF,OAAO,WAAW,CAAC,GAAG,CAA0C,YAAY,EAAE,KAAK,IAAI,EAAE;YACxF,IAAI,CAAC;gBACJ,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,kBAAkB,CAAC,CAAC;gBAC/C,OAAO,IAAI,KAAK,CAAC,iBAAiB,CAAc;oBAC/C,IAAI,EAAE,wBAAwB;iBAC9B,CAAC,CAAC;YACJ,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACd,WAAW,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;gBACjC,MAAM,IAAI,YAAY,CACrB,cAAc,CAAC,UAAU,EACzB,wBAAwB,EACxB,SAAS,EACT,SAAS,CAAC,SAAS,CAAC,GAAG,CAAC,CACxB,CAAC;YACH,CAAC;QACF,CAAC,CAAC,CAAC;IACJ,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { AsyncLocalStorage } from \"node:async_hooks\";\nimport { BaseError, GeneralError, 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 A promise that resolves with the result of the async method.\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\t// get with the factory is invoked synchronously and stores the returned\n\t\t// Promise before yielding, so concurrent callers always await the same\n\t\t// AsyncLocalStorage instance regardless of how many module versions are loaded.\n\t\treturn SharedStore.get<Promise<AsyncLocalStorage<IContextIds>>>(\"asyncHooks\", async () => {\n\t\t\ttry {\n\t\t\t\tconst hooks = await import(\"node:async_hooks\");\n\t\t\t\treturn 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\tSharedStore.remove(\"asyncHooks\");\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});\n\t}\n}\n"]}
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,cA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;IACf,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 A promise that resolves with the result of the async method.\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\t// get with the factory is invoked synchronously and stores the returned\n\t\t// Promise before yielding, so concurrent callers always await the same\n\t\t// AsyncLocalStorage instance regardless of how many module versions are loaded.\n\t\tconst stored = SharedStore.get<\n\t\t\tPromise<AsyncLocalStorage<IContextIds>> | { contextIds: AsyncLocalStorage<IContextIds> }\n\t\t>(\"asyncHooks\", async () => {\n\t\t\ttry {\n\t\t\t\tconst hooks = await import(\"node:async_hooks\");\n\t\t\t\treturn 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\tSharedStore.remove(\"asyncHooks\");\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});\n\n\t\t// Back-compat with context@0.9.0: that version stored { contextIds: AsyncLocalStorage }\n\t\t// (a plain wrapper object, not a Promise). Leave SharedStore untouched - upgrading to\n\t\t// the Promise shape would cause old code to see no contextIds, create a second\n\t\t// AsyncLocalStorage, and silently split context between versions.\n\t\tif (!Is.promise<AsyncLocalStorage<IContextIds>>(stored)) {\n\t\t\treturn stored.contextIds;\n\t\t}\n\t\treturn stored;\n\t}\n}\n"]}
package/docs/changelog.md CHANGED
@@ -1,5 +1,24 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.9.1-next.8](https://github.com/iotaledger/twin-framework/compare/context-v0.9.1-next.7...context-v0.9.1-next.8) (2026-07-20)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * context id backwards compatibility ([#406](https://github.com/iotaledger/twin-framework/issues/406)) ([afab5fa](https://github.com/iotaledger/twin-framework/commit/afab5fa2a876b01736e117e4992536627837afde))
9
+
10
+
11
+ ### Dependencies
12
+
13
+ * The following workspace dependencies were updated
14
+ * dependencies
15
+ * @twin.org/core bumped from 0.9.1-next.7 to 0.9.1-next.8
16
+ * @twin.org/nameof bumped from 0.9.1-next.7 to 0.9.1-next.8
17
+ * devDependencies
18
+ * @twin.org/nameof-transformer bumped from 0.9.1-next.7 to 0.9.1-next.8
19
+ * @twin.org/nameof-vitest-plugin bumped from 0.9.1-next.7 to 0.9.1-next.8
20
+ * @twin.org/validate-locales bumped from 0.9.1-next.7 to 0.9.1-next.8
21
+
3
22
  ## [0.9.1-next.7](https://github.com/iotaledger/twin-framework/compare/context-v0.9.1-next.6...context-v0.9.1-next.7) (2026-07-09)
4
23
 
5
24
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/context",
3
- "version": "0.9.1-next.7",
3
+ "version": "0.9.1-next.8",
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.9.1-next.7",
18
- "@twin.org/nameof": "0.9.1-next.7"
17
+ "@twin.org/core": "0.9.1-next.8",
18
+ "@twin.org/nameof": "0.9.1-next.8"
19
19
  },
20
20
  "main": "./dist/es/index.js",
21
21
  "types": "./dist/types/index.d.ts",