@warlock.js/context 5.52.0 → 5.53.0
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/CHANGELOG.md +11 -0
- package/cjs/index.cjs +80 -6
- package/cjs/index.cjs.map +1 -1
- package/esm/base-context.d.mts +12 -2
- package/esm/base-context.d.mts.map +1 -1
- package/esm/base-context.mjs +8 -2
- package/esm/base-context.mjs.map +1 -1
- package/esm/context-manager.d.mts +5 -2
- package/esm/context-manager.d.mts.map +1 -1
- package/esm/context-manager.mjs +28 -4
- package/esm/context-manager.mjs.map +1 -1
- package/esm/shared-storage.d.mts +24 -0
- package/esm/shared-storage.d.mts.map +1 -0
- package/esm/shared-storage.mjs +51 -0
- package/esm/shared-storage.mjs.map +1 -0
- package/llms-full.txt +2 -2
- package/package.json +1 -1
- package/skills/orchestrate-contexts/SKILL.md +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,17 @@ All notable changes to `@warlock.js/context` are documented in this file.
|
|
|
4
4
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). `@warlock.js/*` packages are released in lockstep — every package shares the same version number, so a version below may list only the changes that affected this package.
|
|
6
6
|
|
|
7
|
+
## 5.53.0 - 2026-11-11
|
|
8
|
+
|
|
9
|
+
### Upgrading
|
|
10
|
+
|
|
11
|
+
- The protected `storage` member of `Context` is a `ContextStorage` (`run`, `enterWith`, `getStore`, scoped to that context) rather than an `AsyncLocalStorage`. Subclasses that call those three methods keep working; other `AsyncLocalStorage` methods are not available on it.
|
|
12
|
+
- An `enter()` of one context made inside another context's `run()` no longer stays active after that `run()` returns in the same synchronous step (the behaviour Node 24 and later already have).
|
|
13
|
+
|
|
14
|
+
### Changed
|
|
15
|
+
|
|
16
|
+
- Every context keeps its store in one `AsyncLocalStorage` shared by all contexts (one slot per context) instead of one `AsyncLocalStorage` each. On Node versions where each enabled `AsyncLocalStorage` adds work to every promise, a request with the four framework contexts pays that cost once instead of four times, and `contextManager.runAll()` enters every context in one step. `run()`, `enter()`, `update()`, `clear()` and contexts used outside the manager behave as before; a context class that overrides `run()` is still run through it.
|
|
17
|
+
|
|
7
18
|
## 5.34.3 - 2026-10-20
|
|
8
19
|
|
|
9
20
|
### Changed
|
package/cjs/index.cjs
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
let
|
|
2
|
+
let node_async_hooks = require("node:async_hooks");
|
|
3
3
|
|
|
4
|
+
//#region ../../packages/context/src/shared-storage.ts
|
|
5
|
+
/**
|
|
6
|
+
* The one `AsyncLocalStorage` behind every context. Each enabled
|
|
7
|
+
* `AsyncLocalStorage` adds work to every promise and async resource the
|
|
8
|
+
* process creates, so all contexts share this one and keep their stores in
|
|
9
|
+
* a per-scope frame.
|
|
10
|
+
*
|
|
11
|
+
* A frame is never changed once entered: `run()` and `enterWith()` copy it
|
|
12
|
+
* and replace one slot, so a scope never sees a change made in another one.
|
|
13
|
+
*/
|
|
14
|
+
const sharedStorage = new node_async_hooks.AsyncLocalStorage();
|
|
15
|
+
/**
|
|
16
|
+
* One context's place in the shared frame.
|
|
17
|
+
*
|
|
18
|
+
* @internal Created by `Context`; used by `ContextManager.runAll()` to enter
|
|
19
|
+
* several contexts with a single run.
|
|
20
|
+
*/
|
|
21
|
+
var ContextSlot = class {
|
|
22
|
+
run(store, callback) {
|
|
23
|
+
return sharedStorage.run(withSlots([[this, store]]), callback);
|
|
24
|
+
}
|
|
25
|
+
enterWith(store) {
|
|
26
|
+
sharedStorage.enterWith(withSlots([[this, store]]));
|
|
27
|
+
}
|
|
28
|
+
getStore() {
|
|
29
|
+
return sharedStorage.getStore()?.get(this);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Run `callback` with several slots set at once: one shared-storage run,
|
|
34
|
+
* however many contexts.
|
|
35
|
+
*
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
38
|
+
function runWithSlots(entries, callback) {
|
|
39
|
+
return sharedStorage.run(withSlots(entries), callback);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* A copy of the current frame with the given slots replaced.
|
|
43
|
+
*/
|
|
44
|
+
function withSlots(entries) {
|
|
45
|
+
const frame = new Map(sharedStorage.getStore());
|
|
46
|
+
for (const [slot, store] of entries) frame.set(slot, store);
|
|
47
|
+
return frame;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
4
51
|
//#region ../../packages/context/src/base-context.ts
|
|
5
52
|
/**
|
|
6
53
|
* Keys that reach `Object.prototype` instead of the store when assigned.
|
|
@@ -43,6 +90,12 @@ function withoutDangerousKeys(updates) {
|
|
|
43
90
|
* Provides a consistent API for managing context across async operations.
|
|
44
91
|
* All framework contexts (request, storage, database) extend this class.
|
|
45
92
|
*
|
|
93
|
+
* Every context keeps its store in one `AsyncLocalStorage` shared by all
|
|
94
|
+
* contexts (each has its own slot in it), so the per-promise cost of async
|
|
95
|
+
* context tracking is paid once, not once per context. Each context still
|
|
96
|
+
* behaves as if it had its own storage: running or entering one never
|
|
97
|
+
* changes another's store.
|
|
98
|
+
*
|
|
46
99
|
* @template TStore - The type of data stored in context
|
|
47
100
|
*
|
|
48
101
|
* @example
|
|
@@ -63,7 +116,7 @@ function withoutDangerousKeys(updates) {
|
|
|
63
116
|
*/
|
|
64
117
|
var Context = class {
|
|
65
118
|
constructor() {
|
|
66
|
-
this.storage = new
|
|
119
|
+
this.storage = new ContextSlot();
|
|
67
120
|
}
|
|
68
121
|
/**
|
|
69
122
|
* Run a callback within a new context
|
|
@@ -197,17 +250,29 @@ var ContextManager = class {
|
|
|
197
250
|
/**
|
|
198
251
|
* Run all registered contexts together
|
|
199
252
|
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
253
|
+
* Activates every registered context for the duration of the callback,
|
|
254
|
+
* each with `stores[name]` (or `{}`). Contexts that use the built-in
|
|
255
|
+
* `run()` are entered together in one step of the shared async storage;
|
|
256
|
+
* a context that overrides `run()` is still run through its own `run()`,
|
|
257
|
+
* inside, in registration order.
|
|
202
258
|
*
|
|
203
259
|
* @param stores - Context stores keyed by context name
|
|
204
260
|
* @param callback - Async function to execute
|
|
205
261
|
* @returns Result of the callback
|
|
206
262
|
*/
|
|
207
263
|
async runAll(stores, callback) {
|
|
208
|
-
|
|
264
|
+
const slots = [];
|
|
265
|
+
const custom = [];
|
|
266
|
+
for (const [name, context] of this.contexts.entries()) {
|
|
267
|
+
const slot = sharedSlotOf(context);
|
|
268
|
+
if (slot) slots.push([slot, stores[name] || {}]);
|
|
269
|
+
else custom.push([name, context]);
|
|
270
|
+
}
|
|
271
|
+
const runner = custom.reduceRight((next, [name, context]) => {
|
|
209
272
|
return () => context.run(stores[name] || {}, next);
|
|
210
|
-
}, callback)
|
|
273
|
+
}, callback);
|
|
274
|
+
if (slots.length === 0) return runner();
|
|
275
|
+
return runWithSlots(slots, runner);
|
|
211
276
|
}
|
|
212
277
|
/**
|
|
213
278
|
* Enter all contexts at once (for middleware)
|
|
@@ -272,6 +337,15 @@ var ContextManager = class {
|
|
|
272
337
|
}
|
|
273
338
|
};
|
|
274
339
|
/**
|
|
340
|
+
* The shared-storage slot of a context that uses the built-in `run()`, or
|
|
341
|
+
* `undefined` when it overrides `run()` or its storage.
|
|
342
|
+
*/
|
|
343
|
+
function sharedSlotOf(context) {
|
|
344
|
+
if (context.run !== Context.prototype.run) return void 0;
|
|
345
|
+
const storage = context.storage;
|
|
346
|
+
return storage instanceof ContextSlot ? storage : void 0;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
275
349
|
* Global context manager instance
|
|
276
350
|
*
|
|
277
351
|
* Use this singleton to register and manage all framework contexts.
|
package/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["AsyncLocalStorage"],"sources":["../../../../../../../packages/context/src/base-context.ts","../../../../../../../packages/context/src/context-manager.ts"],"sourcesContent":["import { AsyncLocalStorage } from \"async_hooks\";\n\n/**\n * Keys that reach `Object.prototype` instead of the store when assigned.\n *\n * `Object.assign(store, { __proto__: {...} })` does not create a property — it\n * invokes the inherited `__proto__` setter and reparents the store, which for a\n * shared context store means every later property lookup in the process can\n * resolve through attacker-supplied data. `constructor` / `prototype` are the\n * neighbouring rungs of the same ladder.\n */\nconst DANGEROUS_KEYS = [\"__proto__\", \"constructor\", \"prototype\"] as const;\n\n/**\n * Drop prototype-poisoning keys from a merge payload.\n *\n * Contexts are frequently fed request-shaped data (`context.update(req.body)`,\n * `context.set(key, value)` with a caller-supplied key), so the merge path is\n * treated as an untrusted boundary: dangerous keys are dropped rather than\n * throwing, since a request that trips this must not be able to take the\n * process down, and the store simply never carries them.\n *\n * The common case allocates nothing — a copy is made only when a dangerous key\n * is actually present. The copy is built by spread (which defines properties\n * rather than assigning them, so it cannot trigger the setter itself) and\n * preserves symbol keys, which `Object.assign` would have copied.\n */\nfunction withoutDangerousKeys<T>(updates: T): T {\n if (!updates || typeof updates !== \"object\") return updates;\n\n const hasDangerousKey = DANGEROUS_KEYS.some((key) =>\n Object.prototype.hasOwnProperty.call(updates, key),\n );\n\n if (!hasDangerousKey) return updates;\n\n const safe: Record<string, any> = { ...(updates as Record<string, any>) };\n\n for (const key of DANGEROUS_KEYS) {\n delete safe[key];\n }\n\n return safe as T;\n}\n\n/**\n * Base class for all AsyncLocalStorage-based contexts\n *\n * Provides a consistent API for managing context across async operations.\n * All framework contexts (request, storage, database) extend this class.\n *\n * @template TStore - The type of data stored in context\n *\n * @example\n * ```typescript\n * interface MyContextStore {\n * userId: string;\n * tenant: string;\n * }\n *\n * class MyContext extends Context<MyContextStore> {}\n * const myContext = new MyContext();\n *\n * // Use it\n * await myContext.run({ userId: '123', tenant: 'acme' }, async () => {\n * const userId = myContext.get('userId'); // '123'\n * });\n * ```\n */\nexport abstract class Context<TStore extends Record<string, any>> {\n protected readonly storage: AsyncLocalStorage<TStore> = new AsyncLocalStorage<TStore>();\n\n /**\n * Run a callback within a new context\n *\n * Creates a new async context with the provided store data.\n * All operations within the callback will have access to this context.\n *\n * @param store - Initial context data\n * @param callback - Async function to execute\n * @returns Result of the callback\n */\n public run<T>(store: TStore, callback: () => Promise<T>): Promise<T> {\n return this.storage.run(store, callback);\n }\n\n /**\n * Enter a new context without a callback\n *\n * Useful for middleware where you want to set context for the rest of the request.\n * Unlike `run()`, this doesn't require a callback.\n *\n * @param store - Context data to set\n */\n public enter(store: TStore): void {\n this.storage.enterWith(store);\n }\n\n /**\n * Update the current context\n *\n * Merges new data into existing context, or enters new context if none exists.\n *\n * Keys named `__proto__`, `constructor`, or `prototype` are dropped: the\n * merge target is a store shared by everything running in this async chain,\n * so letting those through would let one caller's payload change property\n * resolution for the whole process. Passing unvalidated input here is still\n * discouraged — this guard closes the prototype vector, not the \"an attacker\n * chose what your context says\" one.\n *\n * @param updates - Partial context data to merge\n */\n public update(updates: Partial<TStore>): void {\n const safeUpdates = withoutDangerousKeys(updates);\n\n const current = this.storage.getStore();\n\n if (current) {\n Object.assign(current, safeUpdates);\n } else {\n this.enter(safeUpdates as TStore);\n }\n }\n\n /**\n * Get the current context store\n *\n * @returns Current context or undefined if not in context\n */\n public getStore(): TStore | undefined {\n return this.storage.getStore();\n }\n\n /**\n * Get a specific value from context\n *\n * @param key - Key to retrieve\n * @returns Value or undefined\n */\n public get<K extends keyof TStore>(key: K): TStore[K] | undefined {\n return this.storage.getStore()?.[key];\n }\n\n /**\n * Set a specific value in context\n *\n * Goes through `update()`, so a caller-supplied `key` of `__proto__` /\n * `constructor` / `prototype` is dropped rather than reparenting the store.\n *\n * @param key - Key to set\n * @param value - Value to store\n */\n public set<K extends keyof TStore>(key: K, value: TStore[K]): void {\n this.update({ [key]: value } as any);\n }\n\n /**\n * Clear the context\n */\n public clear(): void {\n this.storage.enterWith({} as TStore);\n }\n\n /**\n * Check if currently in a context\n */\n public hasContext(): boolean {\n return this.storage.getStore() !== undefined;\n }\n\n /**\n * Build the initial store for this context\n *\n * Override this method to provide custom initialization logic.\n * Called by ContextManager.buildStores() for each registered context.\n *\n * @param payload - Generic payload (e.g., { request, response } for HTTP contexts)\n * @returns Initial store data\n */\n public abstract buildStore(payload?: Record<string, any>): TStore;\n}\n","import type { Context } from \"./base-context\";\n\n/**\n * Context Manager - Orchestrates multiple contexts together\n *\n * Allows running multiple AsyncLocalStorage contexts in a single operation,\n * making it easy to link request, storage, database, and other contexts.\n *\n * @example\n * ```typescript\n * // Register contexts\n * contextManager\n * .register('request', requestContext)\n * .register('storage', storageDriverContext)\n * .register('database', databaseDataSourceContext);\n *\n * // Run all contexts together\n * await contextManager.runAll({\n * request: { request, response, user },\n * storage: { driver, metadata: { tenantId: '123' } },\n * database: { dataSource: 'primary' },\n * }, async () => {\n * // All contexts active!\n * await handleRequest();\n * });\n * ```\n */\nexport class ContextManager {\n private contexts = new Map<string, Context<any>>();\n\n /**\n * Register a context\n *\n * @param name - Unique context name\n * @param context - Context instance\n * @returns This instance for chaining\n */\n public register(name: string, context: Context<any>): this {\n this.contexts.set(name, context);\n return this;\n }\n\n /**\n * Run all registered contexts together\n *\n * Nests all context.run() calls, ensuring all contexts are active\n * for the duration of the callback.\n *\n * @param stores - Context stores keyed by context name\n * @param callback - Async function to execute\n * @returns Result of the callback\n */\n public async runAll<T>(stores: Record<string, any>, callback: () => Promise<T>): Promise<T> {\n const entries = Array.from(this.contexts.entries());\n\n // Build nested context runners\n const runner = entries.reduceRight((next, [name, context]) => {\n return () => context.run(stores[name] || {}, next);\n }, callback);\n\n return runner();\n }\n\n /**\n * Enter all contexts at once (for middleware)\n *\n * @param stores - Context stores keyed by context name\n */\n public enterAll(stores: Record<string, any>): void {\n for (const [name, context] of this.contexts.entries()) {\n if (stores[name]) {\n context.enter(stores[name]);\n }\n }\n }\n\n /**\n * Clear all contexts\n */\n public clearAll(): void {\n for (const context of this.contexts.values()) {\n context.clear();\n }\n }\n\n /**\n * Get a specific registered context\n *\n * @param name - Context name\n * @returns Context instance or undefined\n */\n public getContext<T extends Context<any>>(name: string): T | undefined {\n return this.contexts.get(name) as T | undefined;\n }\n\n /**\n * Check if a context is registered\n *\n * @param name - Context name\n * @returns True if context is registered\n */\n public hasContext(name: string): boolean {\n return this.contexts.has(name);\n }\n\n /**\n * Build all context stores by calling each context's buildStore() method\n *\n * This is the immutable pattern - returns a new record of stores.\n * Each context defines its own initialization logic.\n *\n * @param payload - Payload passed to each buildStore() (e.g., { request, response })\n * @returns Record of context name -> store data\n *\n * @example\n * ```typescript\n * const httpContextStore = contextManager.buildStores({ request, response });\n * await contextManager.runAll(httpContextStore, async () => { ... });\n * ```\n */\n public buildStores(payload?: Record<string, any>): Record<string, any> {\n const stores: Record<string, any> = {};\n\n for (const [name, context] of this.contexts.entries()) {\n stores[name] = context.buildStore(payload) ?? {};\n }\n\n return stores;\n }\n\n /**\n * Unregister a context\n *\n * @param name - Context name to remove\n * @returns True if context was removed\n */\n public unregister(name: string): boolean {\n return this.contexts.delete(name);\n }\n}\n\n/**\n * Global context manager instance\n *\n * Use this singleton to register and manage all framework contexts.\n */\nexport const contextManager = new ContextManager();\n"],"mappings":";;;;;;;;;;;;;AAWA,MAAM,iBAAiB;CAAC;CAAa;CAAe;AAAW;;;;;;;;;;;;;;;AAgB/D,SAAS,qBAAwB,SAAe;CAC9C,IAAI,CAAC,WAAW,OAAO,YAAY,UAAU,OAAO;CAMpD,IAAI,CAJoB,eAAe,MAAM,QAC3C,OAAO,UAAU,eAAe,KAAK,SAAS,GAAG,CAGhC,GAAG,OAAO;CAE7B,MAAM,OAA4B,EAAE,GAAI,QAAgC;CAExE,KAAK,MAAM,OAAO,gBAChB,OAAO,KAAK;CAGd,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,IAAsB,UAAtB,MAAkE;;iBACR,IAAIA,8BAA0B;;;;;;;;;;;;CAYtF,AAAO,IAAO,OAAe,UAAwC;EACnE,OAAO,KAAK,QAAQ,IAAI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AiB;EACtB,KAAK,MAAM,WAAW,KAAK,SAAS,OAAO,GACzC,QAAQ,MAAM;CAElB;;;;;;;CAQA,AAAO,WAAmC,MAA6B;EACrE,OAAO,KAAK,SAAS,IAAI,IAAI;CAC/B;;;;;;;CAQA,AAAO,WAAW,MAAuB;EACvC,OAAO,KAAK,SAAS,IAAI,IAAI;CAC/B;;;;;;;;;;;;;;;;CAiBA,AAAO,YAAY,SAAoD;EACrE,MAAM,SAA8B,CAAC;EAErC,KAAK,MAAM,CAAC,MAAM,YAAY,KAAK,SAAS,QAAQ,GAClD,OAAO,QAAQ,QAAQ,WAAW,OAAO,KAAK,CAAC;EAGjD,OAAO;CACT;;;;;;;CAQA,AAAO,WAAW,MAAuB;EACvC,OAAO,KAAK,SAAS,OAAO,IAAI;CAClC;AACF;;;;;;AAOA,MAAa,iBAAiB,IAAI,eAAe"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["AsyncLocalStorage"],"sources":["../../../../../../../packages/context/src/shared-storage.ts","../../../../../../../packages/context/src/base-context.ts","../../../../../../../packages/context/src/context-manager.ts"],"mappings":";;;;;;;;;;;;;AAiBA,MAAM,gBAAgB,IAAIA,mCAAgC;;;;;;;AA8B1D,IAAa,cAAb,MAAmE;CACjE,AAAO,IAAO,OAAe,UAAsB;EACjD,OAAO,cAAc,IAAI,UAAU,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,GAAG,QAAQ;CAC/D;CAEA,AAAO,UAAU,OAAqB;EACpC,cAAc,UAAU,UAAU,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC;CACpD;CAEA,AAAO,WAA+B;EACpC,OAAO,cAAc,SAAS,CAAC,EAAE,IAAI,IAAI;CAC3C;AACF;;;;;;;AAQA,SAAgB,aACd,SACA,UACG;CACH,OAAO,cAAc,IAAI,UAAU,OAAO,GAAG,QAAQ;AACvD;;;;AAKA,SAAS,UAAU,SAA4E;CAC7F,MAAM,QAAQ,IAAI,IAAI,cAAc,SAAS,CAAC;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,QAAQ,GAClD,OAAO,QAAQ,QAAQ,WAAW,OAAO,KAAK,CAAC;EAGjD,OAAO;CACT;;;;;;;CAQA,AAAO,WAAW,MAAuB;EACvC,OAAO,KAAK,SAAS,OAAO,IAAI;CAClC;AACF;;;;;AAMA,SAAS,aAAa,SAAqD;CACzE,IAAI,QAAQ,QAAQ,QAAQ,UAAU,KAAK,OAAO;CAElD,MAAM,UAAW,QAA6C;CAE9D,OAAO,mBAAmB,cAAc,UAAU;AACpD;;;;;;AAOA,MAAa,iBAAiB,IAAI,eAAe"}
|
package/esm/base-context.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ContextStorage } from "./shared-storage.mjs";
|
|
2
2
|
//#region ../../packages/context/src/base-context.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* Base class for all AsyncLocalStorage-based contexts
|
|
@@ -6,6 +6,12 @@ import { AsyncLocalStorage } from "async_hooks";
|
|
|
6
6
|
* Provides a consistent API for managing context across async operations.
|
|
7
7
|
* All framework contexts (request, storage, database) extend this class.
|
|
8
8
|
*
|
|
9
|
+
* Every context keeps its store in one `AsyncLocalStorage` shared by all
|
|
10
|
+
* contexts (each has its own slot in it), so the per-promise cost of async
|
|
11
|
+
* context tracking is paid once, not once per context. Each context still
|
|
12
|
+
* behaves as if it had its own storage: running or entering one never
|
|
13
|
+
* changes another's store.
|
|
14
|
+
*
|
|
9
15
|
* @template TStore - The type of data stored in context
|
|
10
16
|
*
|
|
11
17
|
* @example
|
|
@@ -25,7 +31,11 @@ import { AsyncLocalStorage } from "async_hooks";
|
|
|
25
31
|
* ```
|
|
26
32
|
*/
|
|
27
33
|
export declare abstract class Context<TStore extends Record<string, any>> {
|
|
28
|
-
|
|
34
|
+
/**
|
|
35
|
+
* This context's slot in the shared async storage: `run`, `enterWith` and
|
|
36
|
+
* `getStore` as on `AsyncLocalStorage`, scoped to this context.
|
|
37
|
+
*/
|
|
38
|
+
protected readonly storage: ContextStorage<TStore>;
|
|
29
39
|
/**
|
|
30
40
|
* Run a callback within a new context
|
|
31
41
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base-context.d.mts","names":[],"sources":["../../../../../../../packages/context/src/base-context.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"base-context.d.mts","names":[],"sources":["../../../../../../../packages/context/src/base-context.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;8BA2EsB,QAAQ,eAAe;;;;;qBAKxB,SAAS,eAAe;;;;;;;;;;;EAYpC,IAAI,GAAG,OAAO,QAAQ,gBAAgB,QAAQ,KAAK,QAAQ;;;;;;;;;EAY3D,MAAM,OAAO;;;;;;;;;;;;;;;EAkBb,OAAO,SAAS,QAAQ;;;;;;EAiBxB,YAAY;;;;;;;EAUZ,IAAI,gBAAgB,QAAQ,KAAK,IAAI,OAAO;;;;;;;;;;EAa5C,IAAI,gBAAgB,QAAQ,KAAK,GAAG,OAAO,OAAO;;;;EAOlD;;;;EAOA;;;;;;;;;;WAaS,WAAW,UAAU,sBAAsB"}
|
package/esm/base-context.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ContextSlot } from "./shared-storage.mjs";
|
|
2
2
|
|
|
3
3
|
//#region ../../packages/context/src/base-context.ts
|
|
4
4
|
/**
|
|
@@ -42,6 +42,12 @@ function withoutDangerousKeys(updates) {
|
|
|
42
42
|
* Provides a consistent API for managing context across async operations.
|
|
43
43
|
* All framework contexts (request, storage, database) extend this class.
|
|
44
44
|
*
|
|
45
|
+
* Every context keeps its store in one `AsyncLocalStorage` shared by all
|
|
46
|
+
* contexts (each has its own slot in it), so the per-promise cost of async
|
|
47
|
+
* context tracking is paid once, not once per context. Each context still
|
|
48
|
+
* behaves as if it had its own storage: running or entering one never
|
|
49
|
+
* changes another's store.
|
|
50
|
+
*
|
|
45
51
|
* @template TStore - The type of data stored in context
|
|
46
52
|
*
|
|
47
53
|
* @example
|
|
@@ -62,7 +68,7 @@ function withoutDangerousKeys(updates) {
|
|
|
62
68
|
*/
|
|
63
69
|
var Context = class {
|
|
64
70
|
constructor() {
|
|
65
|
-
this.storage = new
|
|
71
|
+
this.storage = new ContextSlot();
|
|
66
72
|
}
|
|
67
73
|
/**
|
|
68
74
|
* Run a callback within a new context
|
package/esm/base-context.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base-context.mjs","names":[],"sources":["../../../../../../../packages/context/src/base-context.ts"],"
|
|
1
|
+
{"version":3,"file":"base-context.mjs","names":[],"sources":["../../../../../../../packages/context/src/base-context.ts"],"mappings":";;;;;;;;;;;;AAWA,MAAM,iBAAiB;CAAC;CAAa;CAAe;AAAW;;;;;;;;;;;;;;;AAgB/D,SAAS,qBAAwB,SAAe;CAC9C,IAAI,CAAC,WAAW,OAAO,YAAY,UAAU,OAAO;CAMpD,IAAI,CAJoB,eAAe,MAAM,QAC3C,OAAO,UAAU,eAAe,KAAK,SAAS,GAAG,CAGhC,GAAG,OAAO;CAE7B,MAAM,OAA4B,EAAE,GAAI,QAAgC;CAExE,KAAK,MAAM,OAAO,gBAChB,OAAO,KAAK;CAGd,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCA,IAAsB,UAAtB,MAAkE;;iBAKX,IAAI,YAAoB;;;;;;;;;;;;CAY7E,AAAO,IAAO,OAAe,UAAwC;EACnE,OAAO,KAAK,QAAQ,IAAI,OAAO,QAAQ;CACzC;;;;;;;;;CAUA,AAAO,MAAM,OAAqB;EAChC,KAAK,QAAQ,UAAU,KAAK;CAC9B;;;;;;;;;;;;;;;CAgBA,AAAO,OAAO,SAAgC;EAC5C,MAAM,cAAc,qBAAqB,OAAO;EAEhD,MAAM,UAAU,KAAK,QAAQ,SAAS;EAEtC,IAAI,SACF,OAAO,OAAO,SAAS,WAAW;OAElC,KAAK,MAAM,WAAqB;CAEpC;;;;;;CAOA,AAAO,WAA+B;EACpC,OAAO,KAAK,QAAQ,SAAS;CAC/B;;;;;;;CAQA,AAAO,IAA4B,KAA+B;EAChE,OAAO,KAAK,QAAQ,SAAS,CAAC,GAAG;CACnC;;;;;;;;;;CAWA,AAAO,IAA4B,KAAQ,OAAwB;EACjE,KAAK,OAAO,GAAG,MAAM,MAAM,CAAQ;CACrC;;;;CAKA,AAAO,QAAc;EACnB,KAAK,QAAQ,UAAU,CAAC,CAAW;CACrC;;;;CAKA,AAAO,aAAsB;EAC3B,OAAO,KAAK,QAAQ,SAAS,MAAM;CACrC;AAYF"}
|
|
@@ -38,8 +38,11 @@ export declare class ContextManager {
|
|
|
38
38
|
/**
|
|
39
39
|
* Run all registered contexts together
|
|
40
40
|
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
41
|
+
* Activates every registered context for the duration of the callback,
|
|
42
|
+
* each with `stores[name]` (or `{}`). Contexts that use the built-in
|
|
43
|
+
* `run()` are entered together in one step of the shared async storage;
|
|
44
|
+
* a context that overrides `run()` is still run through its own `run()`,
|
|
45
|
+
* inside, in registration order.
|
|
43
46
|
*
|
|
44
47
|
* @param stores - Context stores keyed by context name
|
|
45
48
|
* @param callback - Async function to execute
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context-manager.d.mts","names":[],"sources":["../../../../../../../packages/context/src/context-manager.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"context-manager.d.mts","names":[],"sources":["../../../../../../../packages/context/src/context-manager.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;qBA4Ba;UACH;;;;;;;;EASD,SAAS,cAAc,SAAS;;;;;;;;;;;;;;EAkB1B,OAAO,GAAG,QAAQ,qBAAqB,gBAAgB,QAAQ,KAAK,QAAQ;;;;;;EA+BlF,SAAS,QAAQ;;;;EAWjB;;;;;;;EAYA,WAAW,UAAU,cAAc,eAAe;;;;;;;EAUlD,WAAW;;;;;;;;;;;;;;;;EAmBX,YAAY,UAAU,sBAAsB;;;;;;;EAgB5C,WAAW;;;;;;;qBAsBP,gBAAc"}
|
package/esm/context-manager.mjs
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { ContextSlot, runWithSlots } from "./shared-storage.mjs";
|
|
2
|
+
import { Context } from "./base-context.mjs";
|
|
3
|
+
|
|
1
4
|
//#region ../../packages/context/src/context-manager.ts
|
|
2
5
|
/**
|
|
3
6
|
* Context Manager - Orchestrates multiple contexts together
|
|
@@ -42,17 +45,29 @@ var ContextManager = class {
|
|
|
42
45
|
/**
|
|
43
46
|
* Run all registered contexts together
|
|
44
47
|
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
48
|
+
* Activates every registered context for the duration of the callback,
|
|
49
|
+
* each with `stores[name]` (or `{}`). Contexts that use the built-in
|
|
50
|
+
* `run()` are entered together in one step of the shared async storage;
|
|
51
|
+
* a context that overrides `run()` is still run through its own `run()`,
|
|
52
|
+
* inside, in registration order.
|
|
47
53
|
*
|
|
48
54
|
* @param stores - Context stores keyed by context name
|
|
49
55
|
* @param callback - Async function to execute
|
|
50
56
|
* @returns Result of the callback
|
|
51
57
|
*/
|
|
52
58
|
async runAll(stores, callback) {
|
|
53
|
-
|
|
59
|
+
const slots = [];
|
|
60
|
+
const custom = [];
|
|
61
|
+
for (const [name, context] of this.contexts.entries()) {
|
|
62
|
+
const slot = sharedSlotOf(context);
|
|
63
|
+
if (slot) slots.push([slot, stores[name] || {}]);
|
|
64
|
+
else custom.push([name, context]);
|
|
65
|
+
}
|
|
66
|
+
const runner = custom.reduceRight((next, [name, context]) => {
|
|
54
67
|
return () => context.run(stores[name] || {}, next);
|
|
55
|
-
}, callback)
|
|
68
|
+
}, callback);
|
|
69
|
+
if (slots.length === 0) return runner();
|
|
70
|
+
return runWithSlots(slots, runner);
|
|
56
71
|
}
|
|
57
72
|
/**
|
|
58
73
|
* Enter all contexts at once (for middleware)
|
|
@@ -117,6 +132,15 @@ var ContextManager = class {
|
|
|
117
132
|
}
|
|
118
133
|
};
|
|
119
134
|
/**
|
|
135
|
+
* The shared-storage slot of a context that uses the built-in `run()`, or
|
|
136
|
+
* `undefined` when it overrides `run()` or its storage.
|
|
137
|
+
*/
|
|
138
|
+
function sharedSlotOf(context) {
|
|
139
|
+
if (context.run !== Context.prototype.run) return void 0;
|
|
140
|
+
const storage = context.storage;
|
|
141
|
+
return storage instanceof ContextSlot ? storage : void 0;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
120
144
|
* Global context manager instance
|
|
121
145
|
*
|
|
122
146
|
* Use this singleton to register and manage all framework contexts.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context-manager.mjs","names":[],"sources":["../../../../../../../packages/context/src/context-manager.ts"],"
|
|
1
|
+
{"version":3,"file":"context-manager.mjs","names":[],"sources":["../../../../../../../packages/context/src/context-manager.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,IAAa,iBAAb,MAA4B;;kCACP,IAAI,IAA0B;;;;;;;;;CASjD,AAAO,SAAS,MAAc,SAA6B;EACzD,KAAK,SAAS,IAAI,MAAM,OAAO;EAC/B,OAAO;CACT;;;;;;;;;;;;;;CAeA,MAAa,OAAU,QAA6B,UAAwC;EAC1F,MAAM,QAAqD,CAAC;EAC5D,MAAM,SAAwC,CAAC;EAE/C,KAAK,MAAM,CAAC,MAAM,YAAY,KAAK,SAAS,QAAQ,GAAG;GACrD,MAAM,OAAO,aAAa,OAAO;GAEjC,IAAI,MACF,MAAM,KAAK,CAAC,MAAM,OAAO,SAAS,CAAC,CAAC,CAAC;QAErC,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;EAE/B;EAGA,MAAM,SAAS,OAAO,aAAa,MAAM,CAAC,MAAM,aAAa;GAC3D,aAAa,QAAQ,IAAI,OAAO,SAAS,CAAC,GAAG,IAAI;EACnD,GAAG,QAAQ;EAEX,IAAI,MAAM,WAAW,GACnB,OAAO,OAAO;EAGhB,OAAO,aAAa,OAAO,MAAM;CACnC;;;;;;CAOA,AAAO,SAAS,QAAmC;EACjD,KAAK,MAAM,CAAC,MAAM,YAAY,KAAK,SAAS,QAAQ,GAClD,IAAI,OAAO,OACT,QAAQ,MAAM,OAAO,KAAK;CAGhC;;;;CAKA,AAAO,WAAiB;EACtB,KAAK,MAAM,WAAW,KAAK,SAAS,OAAO,GACzC,QAAQ,MAAM;CAElB;;;;;;;CAQA,AAAO,WAAmC,MAA6B;EACrE,OAAO,KAAK,SAAS,IAAI,IAAI;CAC/B;;;;;;;CAQA,AAAO,WAAW,MAAuB;EACvC,OAAO,KAAK,SAAS,IAAI,IAAI;CAC/B;;;;;;;;;;;;;;;;CAiBA,AAAO,YAAY,SAAoD;EACrE,MAAM,SAA8B,CAAC;EAErC,KAAK,MAAM,CAAC,MAAM,YAAY,KAAK,SAAS,QAAQ,GAClD,OAAO,QAAQ,QAAQ,WAAW,OAAO,KAAK,CAAC;EAGjD,OAAO;CACT;;;;;;;CAQA,AAAO,WAAW,MAAuB;EACvC,OAAO,KAAK,SAAS,OAAO,IAAI;CAClC;AACF;;;;;AAMA,SAAS,aAAa,SAAqD;CACzE,IAAI,QAAQ,QAAQ,QAAQ,UAAU,KAAK,OAAO;CAElD,MAAM,UAAW,QAA6C;CAE9D,OAAO,mBAAmB,cAAc,UAAU;AACpD;;;;;;AAOA,MAAa,iBAAiB,IAAI,eAAe"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//#region ../../packages/context/src/shared-storage.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The store access a context needs, with the same meaning as on
|
|
4
|
+
* `AsyncLocalStorage` (`run`, `enterWith`, `getStore`), backed by the shared
|
|
5
|
+
* storage. One slot per context instance.
|
|
6
|
+
*/
|
|
7
|
+
export interface ContextStorage<TStore> {
|
|
8
|
+
/**
|
|
9
|
+
* Run `callback` with `store` as this context's store; every other context
|
|
10
|
+
* keeps its current store.
|
|
11
|
+
*/
|
|
12
|
+
run<R>(store: TStore, callback: () => R): R;
|
|
13
|
+
/**
|
|
14
|
+
* Make `store` this context's store for the rest of the current execution
|
|
15
|
+
* and the async work it starts.
|
|
16
|
+
*/
|
|
17
|
+
enterWith(store: TStore): void;
|
|
18
|
+
/**
|
|
19
|
+
* This context's store in the current scope, or `undefined`.
|
|
20
|
+
*/
|
|
21
|
+
getStore(): TStore | undefined;
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
//# sourceMappingURL=shared-storage.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared-storage.d.mts","names":[],"sources":["../../../../../../../packages/context/src/shared-storage.ts"],"mappings":";;;;;;iBAwBiB,eAAe;;;;;EAK9B,IAAI,GAAG,OAAO,QAAQ,gBAAgB,IAAI;;;;;EAK1C,UAAU,OAAO;;;;EAIjB,YAAY"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
|
|
3
|
+
//#region ../../packages/context/src/shared-storage.ts
|
|
4
|
+
/**
|
|
5
|
+
* The one `AsyncLocalStorage` behind every context. Each enabled
|
|
6
|
+
* `AsyncLocalStorage` adds work to every promise and async resource the
|
|
7
|
+
* process creates, so all contexts share this one and keep their stores in
|
|
8
|
+
* a per-scope frame.
|
|
9
|
+
*
|
|
10
|
+
* A frame is never changed once entered: `run()` and `enterWith()` copy it
|
|
11
|
+
* and replace one slot, so a scope never sees a change made in another one.
|
|
12
|
+
*/
|
|
13
|
+
const sharedStorage = new AsyncLocalStorage();
|
|
14
|
+
/**
|
|
15
|
+
* One context's place in the shared frame.
|
|
16
|
+
*
|
|
17
|
+
* @internal Created by `Context`; used by `ContextManager.runAll()` to enter
|
|
18
|
+
* several contexts with a single run.
|
|
19
|
+
*/
|
|
20
|
+
var ContextSlot = class {
|
|
21
|
+
run(store, callback) {
|
|
22
|
+
return sharedStorage.run(withSlots([[this, store]]), callback);
|
|
23
|
+
}
|
|
24
|
+
enterWith(store) {
|
|
25
|
+
sharedStorage.enterWith(withSlots([[this, store]]));
|
|
26
|
+
}
|
|
27
|
+
getStore() {
|
|
28
|
+
return sharedStorage.getStore()?.get(this);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Run `callback` with several slots set at once: one shared-storage run,
|
|
33
|
+
* however many contexts.
|
|
34
|
+
*
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
function runWithSlots(entries, callback) {
|
|
38
|
+
return sharedStorage.run(withSlots(entries), callback);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* A copy of the current frame with the given slots replaced.
|
|
42
|
+
*/
|
|
43
|
+
function withSlots(entries) {
|
|
44
|
+
const frame = new Map(sharedStorage.getStore());
|
|
45
|
+
for (const [slot, store] of entries) frame.set(slot, store);
|
|
46
|
+
return frame;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
//#endregion
|
|
50
|
+
export { ContextSlot, runWithSlots };
|
|
51
|
+
//# sourceMappingURL=shared-storage.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared-storage.mjs","names":[],"sources":["../../../../../../../packages/context/src/shared-storage.ts"],"mappings":";;;;;;;;;;;;AAiBA,MAAM,gBAAgB,IAAI,kBAAgC;;;;;;;AA8B1D,IAAa,cAAb,MAAmE;CACjE,AAAO,IAAO,OAAe,UAAsB;EACjD,OAAO,cAAc,IAAI,UAAU,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,GAAG,QAAQ;CAC/D;CAEA,AAAO,UAAU,OAAqB;EACpC,cAAc,UAAU,UAAU,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC;CACpD;CAEA,AAAO,WAA+B;EACpC,OAAO,cAAc,SAAS,CAAC,EAAE,IAAI,IAAI;CAC3C;AACF;;;;;;;AAQA,SAAgB,aACd,SACA,UACG;CACH,OAAO,cAAc,IAAI,UAAU,OAAO,GAAG,QAAQ;AACvD;;;;AAKA,SAAS,UAAU,SAA4E;CAC7F,MAAM,QAAQ,IAAI,IAAI,cAAc,SAAS,CAAC;CAE9C,KAAK,MAAM,CAAC,MAAM,UAAU,SAC1B,MAAM,IAAI,MAAM,KAAK;CAGvB,OAAO;AACT"}
|
package/llms-full.txt
CHANGED
|
@@ -285,7 +285,7 @@ app.use(async (req, res, next) => {
|
|
|
285
285
|
|
|
286
286
|
`buildStores(payload)` calls each registered context's `buildStore(payload)` (the abstract method on `Context<TStore>` — see the `define-context` topic) and returns `{ [contextName]: store }`.
|
|
287
287
|
|
|
288
|
-
`runAll(stores, fn)`
|
|
288
|
+
`runAll(stores, fn)` activates every context with its store (or `{}`) in one step, then invokes `fn`. All contexts are active inside `fn`. Every context keeps its store in one shared `AsyncLocalStorage` (one slot per context), so the per-promise cost of context tracking does not grow with the number of contexts; a context class that overrides `run()` is still run through it, nested inside the others.
|
|
289
289
|
|
|
290
290
|
## `enterAll()` — middleware without a callback
|
|
291
291
|
|
|
@@ -315,7 +315,7 @@ contextManager.clearAll(); // clear stores on every context
|
|
|
315
315
|
|
|
316
316
|
## Order matters
|
|
317
317
|
|
|
318
|
-
`
|
|
318
|
+
Contexts with the built-in `run()` become active together; a context that overrides `run()` is nested in **registration order** (first registered = outermost). If contexts have ordering constraints (database before tenant because tenant resolves via the db), register them in that order.
|
|
319
319
|
|
|
320
320
|
```ts
|
|
321
321
|
contextManager
|
package/package.json
CHANGED
|
@@ -57,7 +57,7 @@ app.use(async (req, res, next) => {
|
|
|
57
57
|
|
|
58
58
|
`buildStores(payload)` calls each registered context's `buildStore(payload)` (the abstract method on `Context<TStore>` — see the `define-context` topic) and returns `{ [contextName]: store }`.
|
|
59
59
|
|
|
60
|
-
`runAll(stores, fn)`
|
|
60
|
+
`runAll(stores, fn)` activates every context with its store (or `{}`) in one step, then invokes `fn`. All contexts are active inside `fn`. Every context keeps its store in one shared `AsyncLocalStorage` (one slot per context), so the per-promise cost of context tracking does not grow with the number of contexts; a context class that overrides `run()` is still run through it, nested inside the others.
|
|
61
61
|
|
|
62
62
|
## `enterAll()` — middleware without a callback
|
|
63
63
|
|
|
@@ -87,7 +87,7 @@ contextManager.clearAll(); // clear stores on every context
|
|
|
87
87
|
|
|
88
88
|
## Order matters
|
|
89
89
|
|
|
90
|
-
`
|
|
90
|
+
Contexts with the built-in `run()` become active together; a context that overrides `run()` is nested in **registration order** (first registered = outermost). If contexts have ordering constraints (database before tenant because tenant resolves via the db), register them in that order.
|
|
91
91
|
|
|
92
92
|
```ts
|
|
93
93
|
contextManager
|