@webpieces/core-context 0.2.11 → 0.2.13
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
package/src/RequestContext.d.ts
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* const id = Context.get('REQUEST_ID'); // Still available!
|
|
13
13
|
* ```
|
|
14
14
|
*/
|
|
15
|
-
declare class
|
|
15
|
+
declare class RequestContextImpl {
|
|
16
16
|
private storage;
|
|
17
17
|
constructor();
|
|
18
18
|
/**
|
|
@@ -60,8 +60,8 @@ declare class ContextManager {
|
|
|
60
60
|
has(key: string): boolean;
|
|
61
61
|
}
|
|
62
62
|
/**
|
|
63
|
-
* Global singleton instance of
|
|
63
|
+
* Global singleton instance of RequestContext.
|
|
64
64
|
* Use this throughout your application.
|
|
65
65
|
*/
|
|
66
|
-
export declare const RequestContext:
|
|
66
|
+
export declare const RequestContext: RequestContextImpl;
|
|
67
67
|
export {};
|
package/src/RequestContext.js
CHANGED
|
@@ -17,7 +17,7 @@ const async_hooks_1 = require("async_hooks");
|
|
|
17
17
|
* const id = Context.get('REQUEST_ID'); // Still available!
|
|
18
18
|
* ```
|
|
19
19
|
*/
|
|
20
|
-
class
|
|
20
|
+
class RequestContextImpl {
|
|
21
21
|
constructor() {
|
|
22
22
|
this.storage = new async_hooks_1.AsyncLocalStorage();
|
|
23
23
|
}
|
|
@@ -107,8 +107,8 @@ class ContextManager {
|
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
/**
|
|
110
|
-
* Global singleton instance of
|
|
110
|
+
* Global singleton instance of RequestContext.
|
|
111
111
|
* Use this throughout your application.
|
|
112
112
|
*/
|
|
113
|
-
exports.RequestContext = new
|
|
113
|
+
exports.RequestContext = new RequestContextImpl();
|
|
114
114
|
//# sourceMappingURL=RequestContext.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RequestContext.js","sourceRoot":"","sources":["../../../../../packages/core/core-context/src/RequestContext.ts"],"names":[],"mappings":";;;AAAA,6CAAgD;AAEhD,iBAAiB;AACjB;;;;;;;;;;;;;GAaG;AACH,MAAM,
|
|
1
|
+
{"version":3,"file":"RequestContext.js","sourceRoot":"","sources":["../../../../../packages/core/core-context/src/RequestContext.ts"],"names":[],"mappings":";;;AAAA,6CAAgD;AAEhD,iBAAiB;AACjB;;;;;;;;;;;;;GAaG;AACH,MAAM,kBAAkB;IAGtB;QACE,IAAI,CAAC,OAAO,GAAG,IAAI,+BAAiB,EAAoB,CAAC;IAC3D,CAAC;IAED;;;OAGG;IACH,GAAG,CAAI,EAAW;QAChB,MAAM,KAAK,GAAG,IAAI,GAAG,EAAe,CAAC;QACrC,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IACrC,CAAC;IAED;;OAEG;IACH,cAAc,CAAI,OAAyB,EAAE,EAAW;QACtD,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IACvC,CAAC;IAED;;OAEG;IACH,GAAG,CAAC,GAAW,EAAE,KAAU;QACzB,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;QACtC,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,MAAM,IAAI,KAAK,CAAC,yDAAyD,CAAC,CAAC;QAC7E,CAAC;QACD,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACxB,CAAC;IAED;;OAEG;IACH,GAAG,CAAU,GAAW;QACtB,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;QACtC,OAAO,KAAK,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;IACzB,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,GAAW;QAChB,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;QACtC,KA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U,QAAA,cAAc,GAAG,IAAI,kBAAkB,EAAE,CAAC","sourcesContent":["import { AsyncLocalStorage } from 'async_hooks';\n\n//some stuff here\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 /**\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/**\n * Global singleton instance of RequestContext.\n * Use this throughout your application.\n */\nexport const RequestContext = new RequestContextImpl();\n"]}
|