@webpieces/core-context 0.2.17 → 0.2.22
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 +4 -1
- package/src/RequestContext.d.ts +12 -0
- package/src/RequestContext.js +21 -1
- package/src/RequestContext.js.map +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webpieces/core-context",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.22",
|
|
4
4
|
"description": "AsyncLocalStorage-based context management for request-scoped data",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -20,5 +20,8 @@
|
|
|
20
20
|
],
|
|
21
21
|
"publishConfig": {
|
|
22
22
|
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@webpieces/core-util": "0.2.22"
|
|
23
26
|
}
|
|
24
27
|
}
|
package/src/RequestContext.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Header } from '@webpieces/core-util';
|
|
1
2
|
/**
|
|
2
3
|
* Context management using AsyncLocalStorage.
|
|
3
4
|
* Similar to Java WebPieces Context class that uses ThreadLocal.
|
|
@@ -24,6 +25,10 @@ declare class RequestContextImpl {
|
|
|
24
25
|
* Run a function with a specific context.
|
|
25
26
|
*/
|
|
26
27
|
runWithContext<T>(context: Map<string, any>, fn: () => T): T;
|
|
28
|
+
getHeader(header: Header): any;
|
|
29
|
+
putHeader(header: Header, value: any): void;
|
|
30
|
+
hasHeader(header: Header): boolean;
|
|
31
|
+
getHeaders(headers: Header[]): any[];
|
|
27
32
|
/**
|
|
28
33
|
* Store a value in the current context.
|
|
29
34
|
*/
|
|
@@ -58,6 +63,13 @@ declare class RequestContextImpl {
|
|
|
58
63
|
* Check if a key exists in the context.
|
|
59
64
|
*/
|
|
60
65
|
has(key: string): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Check if RequestContext is currently active.
|
|
68
|
+
* Returns true if we're inside a RequestContext.run() block, false otherwise.
|
|
69
|
+
*
|
|
70
|
+
* Useful for tests to verify context is set up before making API calls.
|
|
71
|
+
*/
|
|
72
|
+
isActive(): boolean;
|
|
61
73
|
}
|
|
62
74
|
/**
|
|
63
75
|
* Global singleton instance of RequestContext.
|
package/src/RequestContext.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RequestContext = void 0;
|
|
4
4
|
const async_hooks_1 = require("async_hooks");
|
|
5
|
-
//some stuff here
|
|
6
5
|
/**
|
|
7
6
|
* Context management using AsyncLocalStorage.
|
|
8
7
|
* Similar to Java WebPieces Context class that uses ThreadLocal.
|
|
@@ -35,6 +34,18 @@ class RequestContextImpl {
|
|
|
35
34
|
runWithContext(context, fn) {
|
|
36
35
|
return this.storage.run(context, fn);
|
|
37
36
|
}
|
|
37
|
+
getHeader(header) {
|
|
38
|
+
return this.get(header.getHeaderName());
|
|
39
|
+
}
|
|
40
|
+
putHeader(header, value) {
|
|
41
|
+
this.put(header.getHeaderName(), value);
|
|
42
|
+
}
|
|
43
|
+
hasHeader(header) {
|
|
44
|
+
return this.has(header.getHeaderName());
|
|
45
|
+
}
|
|
46
|
+
getHeaders(headers) {
|
|
47
|
+
return headers.map(header => this.getHeader(header));
|
|
48
|
+
}
|
|
38
49
|
/**
|
|
39
50
|
* Store a value in the current context.
|
|
40
51
|
*/
|
|
@@ -105,6 +116,15 @@ class RequestContextImpl {
|
|
|
105
116
|
const store = this.storage.getStore();
|
|
106
117
|
return store?.has(key) ?? false;
|
|
107
118
|
}
|
|
119
|
+
/**
|
|
120
|
+
* Check if RequestContext is currently active.
|
|
121
|
+
* Returns true if we're inside a RequestContext.run() block, false otherwise.
|
|
122
|
+
*
|
|
123
|
+
* Useful for tests to verify context is set up before making API calls.
|
|
124
|
+
*/
|
|
125
|
+
isActive() {
|
|
126
|
+
return this.storage.getStore() !== undefined;
|
|
127
|
+
}
|
|
108
128
|
}
|
|
109
129
|
/**
|
|
110
130
|
* Global singleton instance of RequestContext.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RequestContext.js","sourceRoot":"","sources":["../../../../../packages/core/core-context/src/RequestContext.ts"],"names":[],"mappings":";;;AAAA,6CAAgD;
|
|
1
|
+
{"version":3,"file":"RequestContext.js","sourceRoot":"","sources":["../../../../../packages/core/core-context/src/RequestContext.ts"],"names":[],"mappings":";;;AAAA,6CAAgD;AAGhD;;;;;;;;;;;;;GAaG;AACH,MAAM,kBAAkB;IAGpB;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,CAAC,MAAc;QACpB,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,aAAa,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED,SAAS,CAAC,MAAc,EAAE,KAAU;QAChC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,aAAa,EAAE,EAAE,KAAK,CAAC,CAAC;IAC5C,CAAC;IAED,SAAS,CAAC,MAAc;QACpB,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,aAAa,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED,UAAU,CAAC,OAAiB;QACxB,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,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AAK,CAAC,KAAK,EAAE,CAAC;QACd,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;YAC3B,KAAK,CAAC,GAAG,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 { Header } 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 getHeader(header: Header) {\n return this.get(header.getHeaderName());\n }\n\n putHeader(header: Header, value: any) {\n this.put(header.getHeaderName(), value);\n }\n\n hasHeader(header: Header) {\n return this.has(header.getHeaderName());\n }\n\n getHeaders(headers: Header[]): any[] {\n return headers.map(header => this.getHeader(header));\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"]}
|