@webpieces/core-context 0.3.285 → 0.3.287
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webpieces/core-context",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.287",
|
|
4
4
|
"description": "AsyncLocalStorage-based context management for request-scoped data",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"access": "public"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@webpieces/core-util": "0.3.
|
|
25
|
+
"@webpieces/core-util": "0.3.287",
|
|
26
26
|
"@inversifyjs/binding-decorators": "1.1.5",
|
|
27
27
|
"inversify": "7.10.4",
|
|
28
28
|
"reflect-metadata": "0.2.2"
|
package/src/RequestContext.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ContextKey } from '@webpieces/core-util';
|
|
2
2
|
/**
|
|
3
3
|
* Context management using AsyncLocalStorage.
|
|
4
4
|
* Similar to Java WebPieces Context class that uses ThreadLocal.
|
|
@@ -25,10 +25,10 @@ declare class RequestContextImpl {
|
|
|
25
25
|
* Run a function with a specific context.
|
|
26
26
|
*/
|
|
27
27
|
runWithContext<T>(context: Map<string, any>, fn: () => T): T;
|
|
28
|
-
getHeader(
|
|
29
|
-
putHeader(
|
|
30
|
-
hasHeader(
|
|
31
|
-
getHeaders(
|
|
28
|
+
getHeader<T = unknown>(key: ContextKey): T | undefined;
|
|
29
|
+
putHeader(key: ContextKey, value: unknown): void;
|
|
30
|
+
hasHeader(key: ContextKey): boolean;
|
|
31
|
+
getHeaders(keys: ContextKey[]): unknown[];
|
|
32
32
|
/**
|
|
33
33
|
* Store a value in the current context.
|
|
34
34
|
*/
|
package/src/RequestContext.js
CHANGED
|
@@ -35,17 +35,20 @@ class RequestContextImpl {
|
|
|
35
35
|
runWithContext(context, fn) {
|
|
36
36
|
return this.storage.run(context, fn);
|
|
37
37
|
}
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
// webpieces-disable no-any-unknown -- context values are heterogeneous (strings, recorder, meta objects)
|
|
39
|
+
getHeader(key) {
|
|
40
|
+
return this.get(key.name);
|
|
40
41
|
}
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
// webpieces-disable no-any-unknown -- context values are heterogeneous (strings, recorder, meta objects)
|
|
43
|
+
putHeader(key, value) {
|
|
44
|
+
this.put(key.name, value);
|
|
43
45
|
}
|
|
44
|
-
hasHeader(
|
|
45
|
-
return this.has(
|
|
46
|
+
hasHeader(key) {
|
|
47
|
+
return this.has(key.name);
|
|
46
48
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
+
// webpieces-disable no-any-unknown -- context values are heterogeneous (strings, recorder, meta objects)
|
|
50
|
+
getHeaders(keys) {
|
|
51
|
+
return keys.map(key => this.getHeader(key));
|
|
49
52
|
}
|
|
50
53
|
/**
|
|
51
54
|
* Store a value in the current context.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RequestContext.js","sourceRoot":"","sources":["../../../../../packages/core/core-context/src/RequestContext.ts"],"names":[],"mappings":";;;AAAA,6CAAgD;AAGhD;;;;;;;;;;;;;GAaG;AACH,MAAM,kBAAkB;IACZ,OAAO,CAAsC;IAErD;QACI,IAAI,CAAC,OAAO,GAAG,IAAI,+BAAiB,EAAoB,CAAC;IAC7D,CAAC;IAED;;;OAGG;IACH,GAAG,CAAI,EAAW;QACd,MAAM,KAAK,GAAG,IAAI,GAAG,EAAe,CAAC;QACrC,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IACvC,CAAC;IAED;;OAEG;IACH,cAAc,CAAI,OAAyB,EAAE,EAAW;QACpD,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IACzC,CAAC;IAED,SAAS,
|
|
1
|
+
{"version":3,"file":"RequestContext.js","sourceRoot":"","sources":["../../../../../packages/core/core-context/src/RequestContext.ts"],"names":[],"mappings":";;;AAAA,6CAAgD;AAGhD;;;;;;;;;;;;;GAaG;AACH,MAAM,kBAAkB;IACZ,OAAO,CAAsC;IAErD;QACI,IAAI,CAAC,OAAO,GAAG,IAAI,+BAAiB,EAAoB,CAAC;IAC7D,CAAC;IAED;;;OAGG;IACH,GAAG,CAAI,EAAW;QACd,MAAM,KAAK,GAAG,IAAI,GAAG,EAAe,CAAC;QACrC,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IACvC,CAAC;IAED;;OAEG;IACH,cAAc,CAAI,OAAyB,EAAE,EAAW;QACpD,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IACzC,CAAC;IAED,yGAAyG;IACzG,SAAS,CAAc,GAAe;QAClC,OAAO,IAAI,CAAC,GAAG,CAAI,GAAG,CAAC,IAAI,CAAC,CAAC;IACjC,CAAC;IAED,yGAAyG;IACzG,SAAS,CAAC,GAAe,EAAE,KAAc;QACrC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAC9B,CAAC;IAED,SAAS,CAAC,GAAe;QACrB,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC9B,CAAC;IAED,yGAAyG;IACzG,UAAU,CAAC,IAAkB;QACzB,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC;IAChD,CAAC;IAED;;OAEG;IACH,GAAG,CAAC,GAAW,EAAE,KAAU;QACvB,MAAM,KAAK,GAAG,IAAI,CAAC,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CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAC1B,CAAC,CAAC,CAAC;IACP,CAAC;IAED;;OAEG;IACH,MAAM;QACF,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;QACtC,OAAO,KAAK,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,EAAE,CAAC;IAC9C,CAAC;IAED;;OAEG;IACH,GAAG,CAAC,GAAW;QACX,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;QACtC,OAAO,KAAK,EAAE,GAAG,CAAC,GAAG,CAAC,IAAI,KAAK,CAAC;IACpC,CAAC;IAED;;;;;OAKG;IACH,QAAQ;QACJ,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,KAAK,SAAS,CAAC;IACjD,CAAC;CAEJ;AAID;;;GAGG;AACU,QAAA,cAAc,GAAG,IAAI,kBAAkB,EAAE,CAAC","sourcesContent":["import { AsyncLocalStorage } from 'async_hooks';\nimport { ContextKey } from '@webpieces/core-util';\n\n/**\n * Context management using AsyncLocalStorage.\n * Similar to Java WebPieces Context class that uses ThreadLocal.\n *\n * This allows storing request-scoped data that is automatically available\n * throughout the async call chain, similar to MDC (Mapped Diagnostic Context).\n *\n * Example usage:\n * ```typescript\n * Context.put('REQUEST_ID', '12345');\n * await someAsyncOperation();\n * const id = Context.get('REQUEST_ID'); // Still available!\n * ```\n */\nclass RequestContextImpl {\n private storage: AsyncLocalStorage<Map<string, any>>;\n\n constructor() {\n this.storage = new AsyncLocalStorage<Map<string, any>>();\n }\n\n /**\n * Run a function with a new context.\n * This is typically called at the beginning of a request.\n */\n run<T>(fn: () => T): T {\n const store = new Map<string, any>();\n return this.storage.run(store, fn);\n }\n\n /**\n * Run a function with a specific context.\n */\n runWithContext<T>(context: Map<string, any>, fn: () => T): T {\n return this.storage.run(context, fn);\n }\n\n // webpieces-disable no-any-unknown -- context values are heterogeneous (strings, recorder, meta objects)\n getHeader<T = unknown>(key: ContextKey): T | undefined {\n return this.get<T>(key.name);\n }\n\n // webpieces-disable no-any-unknown -- context values are heterogeneous (strings, recorder, meta objects)\n putHeader(key: ContextKey, value: unknown): void {\n this.put(key.name, value);\n }\n\n hasHeader(key: ContextKey): boolean {\n return this.has(key.name);\n }\n\n // webpieces-disable no-any-unknown -- context values are heterogeneous (strings, recorder, meta objects)\n getHeaders(keys: ContextKey[]): unknown[] {\n return keys.map(key => this.getHeader(key));\n }\n\n /**\n * Store a value in the current context.\n */\n put(key: string, value: any): void {\n const store = this.storage.getStore();\n if (!store) {\n throw new Error('No context available. Did you call Context.run() first?');\n }\n store.set(key, value);\n }\n\n /**\n * Retrieve a value from the current context.\n */\n get<T = any>(key: string): T | undefined {\n const store = this.storage.getStore();\n return store?.get(key);\n }\n\n /**\n * Remove a value from the current context.\n */\n remove(key: string): void {\n const store = this.storage.getStore();\n store?.delete(key);\n }\n\n /**\n * Clear all values from the current context.\n */\n clear(): void {\n const store = this.storage.getStore();\n store?.clear();\n }\n\n /**\n * Copy the current context to a new Map.\n * Used by XPromise to preserve context across async boundaries.\n */\n copyContext(): Map<string, any> {\n const store = this.storage.getStore();\n if (!store) {\n return new Map();\n }\n return new Map(store);\n }\n\n /**\n * Set the entire context from a Map.\n * Used by XPromise to restore context.\n */\n setContext(context: Map<string, any>): void {\n const store = this.storage.getStore();\n if (!store) {\n throw new Error('No context available. Did you call Context.run() first?');\n }\n store.clear();\n context.forEach((value, key) => {\n store.set(key, value);\n });\n }\n\n /**\n * Get all context entries.\n */\n getAll(): Map<string, any> {\n const store = this.storage.getStore();\n return store ? new Map(store) : new Map();\n }\n\n /**\n * Check if a key exists in the context.\n */\n has(key: string): boolean {\n const store = this.storage.getStore();\n return store?.has(key) ?? false;\n }\n\n /**\n * Check if RequestContext is currently active.\n * Returns true if we're inside a RequestContext.run() block, false otherwise.\n *\n * Useful for tests to verify context is set up before making API calls.\n */\n isActive(): boolean {\n return this.storage.getStore() !== undefined;\n }\n\n}\n\n\n\n/**\n * Global singleton instance of RequestContext.\n * Use this throughout your application.\n */\nexport const RequestContext = new RequestContextImpl();\n"]}
|
|
@@ -1,27 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ContextKey } from '@webpieces/core-util';
|
|
1
|
+
import { ContextKey, ContextReader } from '@webpieces/core-util';
|
|
3
2
|
/**
|
|
4
|
-
* RequestContextReader -
|
|
3
|
+
* RequestContextReader - the NODE/server ContextReader. Reads context-key values
|
|
4
|
+
* from the AsyncLocalStorage-backed RequestContext.
|
|
5
5
|
*
|
|
6
|
-
* Only works in Node.js with active
|
|
7
|
-
*
|
|
6
|
+
* Only works in Node.js with an active RequestContext. Lives in
|
|
7
|
+
* @webpieces/core-context (Node-only) alongside the RequestContext it reads from,
|
|
8
|
+
* so libraries can build a context-propagating ContextMgr without pulling in
|
|
9
|
+
* @webpieces/http-routing.
|
|
8
10
|
*
|
|
9
|
-
*
|
|
10
|
-
* (AsyncLocalStorage) it reads from, so libraries can build a context-propagating
|
|
11
|
-
* ContextMgr without pulling in @webpieces/http-routing.
|
|
12
|
-
*
|
|
13
|
-
* For browser environments, use StaticContextReader from @webpieces/http-client.
|
|
11
|
+
* For browsers, use MutableContextStore from @webpieces/http-client.
|
|
14
12
|
*/
|
|
15
13
|
export declare class RequestContextReader implements ContextReader {
|
|
14
|
+
/** Read a string context-key value from the active RequestContext. */
|
|
15
|
+
read(key: ContextKey): string | undefined;
|
|
16
16
|
/**
|
|
17
|
-
* Read a
|
|
18
|
-
*
|
|
19
|
-
* @param header - The platform header to read
|
|
20
|
-
* @returns The header value, or undefined if not in context
|
|
21
|
-
*/
|
|
22
|
-
read(header: PlatformHeader): string | undefined;
|
|
23
|
-
/**
|
|
24
|
-
* Read a non-header context value (e.g. the active TestCaseRecorder under
|
|
17
|
+
* Read a non-string context value (e.g. the active TestCaseRecorder under
|
|
25
18
|
* RecorderKeys.RECORDER). Lets the isomorphic http-client find server-side
|
|
26
19
|
* context without importing core-context itself.
|
|
27
20
|
*/
|
|
@@ -3,30 +3,23 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.RequestContextReader = void 0;
|
|
4
4
|
const RequestContext_1 = require("./RequestContext");
|
|
5
5
|
/**
|
|
6
|
-
* RequestContextReader -
|
|
6
|
+
* RequestContextReader - the NODE/server ContextReader. Reads context-key values
|
|
7
|
+
* from the AsyncLocalStorage-backed RequestContext.
|
|
7
8
|
*
|
|
8
|
-
* Only works in Node.js with active
|
|
9
|
-
*
|
|
9
|
+
* Only works in Node.js with an active RequestContext. Lives in
|
|
10
|
+
* @webpieces/core-context (Node-only) alongside the RequestContext it reads from,
|
|
11
|
+
* so libraries can build a context-propagating ContextMgr without pulling in
|
|
12
|
+
* @webpieces/http-routing.
|
|
10
13
|
*
|
|
11
|
-
*
|
|
12
|
-
* (AsyncLocalStorage) it reads from, so libraries can build a context-propagating
|
|
13
|
-
* ContextMgr without pulling in @webpieces/http-routing.
|
|
14
|
-
*
|
|
15
|
-
* For browser environments, use StaticContextReader from @webpieces/http-client.
|
|
14
|
+
* For browsers, use MutableContextStore from @webpieces/http-client.
|
|
16
15
|
*/
|
|
17
16
|
class RequestContextReader {
|
|
18
|
-
/**
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
* @param header - The platform header to read
|
|
22
|
-
* @returns The header value, or undefined if not in context
|
|
23
|
-
*/
|
|
24
|
-
read(header) {
|
|
25
|
-
// Use RequestContext.getHeader() which calls header.getHeaderName()
|
|
26
|
-
return RequestContext_1.RequestContext.getHeader(header);
|
|
17
|
+
/** Read a string context-key value from the active RequestContext. */
|
|
18
|
+
read(key) {
|
|
19
|
+
return RequestContext_1.RequestContext.getHeader(key);
|
|
27
20
|
}
|
|
28
21
|
/**
|
|
29
|
-
* Read a non-
|
|
22
|
+
* Read a non-string context value (e.g. the active TestCaseRecorder under
|
|
30
23
|
* RecorderKeys.RECORDER). Lets the isomorphic http-client find server-side
|
|
31
24
|
* context without importing core-context itself.
|
|
32
25
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RequestContextReader.js","sourceRoot":"","sources":["../../../../../packages/core/core-context/src/RequestContextReader.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"RequestContextReader.js","sourceRoot":"","sources":["../../../../../packages/core/core-context/src/RequestContextReader.ts"],"names":[],"mappings":";;;AACA,qDAAkD;AAElD;;;;;;;;;;GAUG;AACH,MAAa,oBAAoB;IAC7B,sEAAsE;IACtE,IAAI,CAAC,GAAe;QAChB,OAAO,+BAAc,CAAC,SAAS,CAAS,GAAG,CAAC,CAAC;IACjD,CAAC;IAED;;;;OAIG;IACH,gGAAgG;IAChG,SAAS,CAAC,GAAe;QACrB,OAAO,+BAAc,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IACzC,CAAC;CACJ;AAfD,oDAeC","sourcesContent":["import { ContextKey, ContextReader } from '@webpieces/core-util';\nimport { RequestContext } from './RequestContext';\n\n/**\n * RequestContextReader - the NODE/server ContextReader. Reads context-key values\n * from the AsyncLocalStorage-backed RequestContext.\n *\n * Only works in Node.js with an active RequestContext. Lives in\n * @webpieces/core-context (Node-only) alongside the RequestContext it reads from,\n * so libraries can build a context-propagating ContextMgr without pulling in\n * @webpieces/http-routing.\n *\n * For browsers, use MutableContextStore from @webpieces/http-client.\n */\nexport class RequestContextReader implements ContextReader {\n /** Read a string context-key value from the active RequestContext. */\n read(key: ContextKey): string | undefined {\n return RequestContext.getHeader<string>(key);\n }\n\n /**\n * Read a non-string context value (e.g. the active TestCaseRecorder under\n * RecorderKeys.RECORDER). Lets the isomorphic http-client find server-side\n * context without importing core-context itself.\n */\n // webpieces-disable no-any-unknown -- context values are heterogeneous (recorder, meta objects)\n readValue(key: ContextKey): unknown {\n return RequestContext.getHeader(key);\n }\n}\n"]}
|