@warlock.js/context 5.2.3 → 5.3.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/cjs/index.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["AsyncLocalStorage"],"sources":["../../../../../../context/src/base-context.ts","../../../../../../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,MAAK,QAC1C,OAAO,UAAU,eAAe,KAAK,SAAS,GAAG,CAGhC,GAAG,OAAO;CAE7B,MAAM,OAA4B,EAAE,GAAI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CAAC,GAAG,IAAI;EACnD,GAAG,QAES,EAAE;CAChB;;;;;;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;;;;;;AAOA,MAAa,iBAAiB,IAAI,eAAe"}
1
+ {"version":3,"file":"index.cjs","names":["AsyncLocalStorage"],"sources":["../../../../../../context/src/base-context.ts","../../../../../../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,MAAK,QAC1C,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,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,S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OAAO;CACT;;;;;;;CAQA,AAAO,WAAW,MAAuB;EACvC,OAAO,KAAK,SAAS,OAAO,IAAI;CAClC;AACF;;;;;;AAOA,MAAa,iBAAiB,IAAI,eAAe"}
@@ -1 +1 @@
1
- {"version":3,"file":"base-context.d.mts","names":[],"sources":["../../../../../../context/src/base-context.ts"],"mappings":";;;;;AAqEA;;;;;;;;;;;;;;;;;;;;;;uBAAsB,OAAA,gBAAuB,MAAA;EAAA,mBACxB,OAAA,EAAS,iBAAA,CAAkB,MAAA;EA6Ga;;;;;;;;;;EAjGpD,GAAA,GAAA,CAAO,KAAA,EAAO,MAAA,EAAQ,QAAA,QAAgB,OAAA,CAAQ,CAAA,IAAK,OAAA,CAAQ,CAAA;EAApD;;;;;;;;EAYP,KAAA,CAAM,KAAA,EAAO,MAAA;EAkBb;;;;;;;;;;;;;;EAAA,MAAA,CAAO,OAAA,EAAS,OAAA,CAAQ,MAAA;EAwCJ;;;;;EAvBpB,QAAA,CAAA,GAAY,MAAA;EA8BZ;;;;;;EApBA,GAAA,iBAAoB,MAAA,CAAA,CAAQ,GAAA,EAAK,CAAA,GAAI,MAAA,CAAO,CAAA;EAwCc;;;;;;;;;EA3B1D,GAAA,iBAAoB,MAAA,CAAA,CAAQ,GAAA,EAAK,CAAA,EAAG,KAAA,EAAO,MAAA,CAAO,CAAA;;;;EAOlD,KAAA,CAAA;;;;EAOA,UAAA,CAAA;;;;;;;;;;WAaS,UAAA,CAAW,OAAA,GAAU,MAAA,gBAAsB,MAAA;AAAA"}
1
+ {"version":3,"file":"base-context.d.mts","names":[],"sources":["../../../../../../context/src/base-context.ts"],"mappings":";;;;;AAqEA;;;;;;;;;;;;;;;;;;;;;;uBAAsB,OAAA,gBAAuB,MAAA;EAAA,mBACxB,OAAA,EAAS,iBAAA,CAAkB,MAAA;EA6Ga;;;;;;;;;;EAjGpD,GAAA,IAAO,KAAA,EAAO,MAAA,EAAQ,QAAA,QAAgB,OAAA,CAAQ,CAAA,IAAK,OAAA,CAAQ,CAAA;EAApD;;;;;;;;EAYP,KAAA,CAAM,KAAA,EAAO,MAAA;EAkBb;;;;;;;;;;;;;;EAAA,MAAA,CAAO,OAAA,EAAS,OAAA,CAAQ,MAAA;EAwCJ;;;;;EAvBpB,QAAA,IAAY,MAAA;EA8BZ;;;;;;EApBA,GAAA,iBAAoB,MAAA,EAAQ,GAAA,EAAK,CAAA,GAAI,MAAA,CAAO,CAAA;EAwCc;;;;;;;;;EA3B1D,GAAA,iBAAoB,MAAA,EAAQ,GAAA,EAAK,CAAA,EAAG,KAAA,EAAO,MAAA,CAAO,CAAA;;;;EAOlD,KAAA;;;;EAOA,UAAA;;;;;;;;;;WAaS,UAAA,CAAW,OAAA,GAAU,MAAA,gBAAsB,MAAA;AAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"base-context.mjs","names":[],"sources":["../../../../../../context/src/base-context.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"],"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,MAAK,QAC1C,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,IAAI,kBAA0B;;;;;;;;;;;;CAYtF,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,IAAI;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"}
1
+ {"version":3,"file":"base-context.mjs","names":[],"sources":["../../../../../../context/src/base-context.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"],"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,MAAK,QAC1C,OAAO,UAAU,eAAe,KAAK,SAAS,GAAG,CAGhC,GAAG,OAAO;CAE7B,MAAM,OAA4B,EAAE,GAAI,QAAgC;CAExE,KAAK,MAAM,OAAO,gBAChB,OAAO,KAAK;CAGd,OAA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A8EwB;;;EAnEzC,QAAA,CAAA;;;;;;;EAYA,UAAA,WAAqB,OAAA,MAAA,CAAc,IAAA,WAAe,CAAA;;;;;;;EAUlD,UAAA,CAAW,IAAA;;;;;;;;;;;;;;;;EAmBX,WAAA,CAAY,OAAA,GAAU,MAAA,gBAAsB,MAAA;;;;;;;EAgB5C,UAAA,CAAW,IAAA;AAAA;;;;;;cAUP,cAAA,EAAc,cAAuB"}
1
+ {"version":3,"file":"context-manager.d.mts","names":[],"sources":["../../../../../../context/src/context-manager.ts"],"mappings":";;;;;AA2BA;;;;;;;;;;;;;;;;;;;;;;;cAAa,cAAA;EAAA,QACH,QAAA;EAwB4D;;;;;;;EAf7D,QAAA,CAAS,IAAA,UAAc,OAAA,EAAS,OAAA;EA0ChC;;;;;;;;;;EA3BM,MAAA,IAAU,MAAA,EAAQ,MAAA,eAAqB,QAAA,QAAgB,OAAA,CAAQ,CAAA,IAAK,OAAA,CAAQ,CAAA;EAoEtC;;;;AAgBrB;EApEvB,QAAA,CAAS,MAAA,EAAQ,MAAA;EA8EwB;;;EAnEzC,QAAA;;;;;;;EAYA,UAAA,WAAqB,OAAA,OAAc,IAAA,WAAe,CAAA;;;;;;;EAUlD,UAAA,CAAW,IAAA;;;;;;;;;;;;;;;;EAmBX,WAAA,CAAY,OAAA,GAAU,MAAA,gBAAsB,MAAA;;;;;;;EAgB5C,UAAA,CAAW,IAAA;AAAA;;;;;;cAUP,cAAA,EAAc,cAAuB"}
@@ -1 +1 @@
1
- {"version":3,"file":"context-manager.mjs","names":[],"sources":["../../../../../../context/src/context-manager.ts"],"sourcesContent":["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":";;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,IAAa,iBAAb,MAA4B;;kCACP,IAAI,IAA0B;;;;;;;;;CASjD,AAAO,SAAS,MAAc,SAA6B;EACzD,KAAK,SAAS,IAAI,MAAM,OAAO;EAC/B,OAAO;CACT;;;;;;;;;;;CAYA,MAAa,OAAU,QAA6B,UAAwC;EAQ1F,OAPgB,MAAM,KAAK,KAAK,SAAS,QAAQ,CAG5B,EAAE,aAAa,MAAM,CAAC,MAAM,aAAa;GAC5D,aAAa,QAAQ,IAAI,OAAO,SAAS,CAAC,GAAG,IAAI;EACnD,GAAG,QAES,EAAE;CAChB;;;;;;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;;;;;;AAOA,MAAa,iBAAiB,IAAI,eAAe"}
1
+ {"version":3,"file":"context-manager.mjs","names":[],"sources":["../../../../../../context/src/context-manager.ts"],"sourcesContent":["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":";;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,IAAa,iBAAb,MAA4B;;kCACP,IAAI,IAA0B;;;;;;;;;CASjD,AAAO,SAAS,MAAc,SAA6B;EACzD,KAAK,SAAS,IAAI,MAAM,OAAO;EAC/B,OAAO;CACT;;;;;;;;;;;CAYA,MAAa,OAAU,QAA6B,UAAwC;EAQ1F,OAPgB,MAAM,KAAK,KAAK,SAAS,QAAQ,CAG5B,CAAC,CAAC,aAAa,MAAM,CAAC,MAAM,aAAa;GAC5D,aAAa,QAAQ,IAAI,OAAO,SAAS,CAAC,GAAG,IAAI;EACnD,GAAG,QAES,CAAC,CAAC;CAChB;;;;;;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;;;;;;AAOA,MAAa,iBAAiB,IAAI,eAAe"}
package/package.json CHANGED
@@ -24,7 +24,7 @@
24
24
  "engines": {
25
25
  "node": ">=18.0.0"
26
26
  },
27
- "version": "5.2.3",
27
+ "version": "5.3.0",
28
28
  "main": "./cjs/index.cjs",
29
29
  "module": "./esm/index.mjs",
30
30
  "types": "./esm/index.d.mts",