@warlock.js/context 4.0.174 → 4.1.2

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.
Files changed (42) hide show
  1. package/cjs/index.cjs +239 -0
  2. package/cjs/index.cjs.map +1 -0
  3. package/esm/base-context.d.mts +99 -0
  4. package/esm/base-context.d.mts.map +1 -0
  5. package/esm/base-context.mjs +110 -0
  6. package/esm/base-context.mjs.map +1 -0
  7. package/esm/context-manager.d.mts +106 -0
  8. package/esm/context-manager.d.mts.map +1 -0
  9. package/esm/context-manager.mjs +128 -0
  10. package/esm/context-manager.mjs.map +1 -0
  11. package/esm/index.d.mts +3 -0
  12. package/esm/index.mjs +4 -0
  13. package/llms-full.txt +404 -0
  14. package/llms.txt +11 -0
  15. package/package.json +42 -30
  16. package/skills/define-context/SKILL.md +158 -0
  17. package/skills/orchestrate-contexts/SKILL.md +161 -0
  18. package/skills/overview/SKILL.md +67 -0
  19. package/cjs/base-context.d.ts +0 -96
  20. package/cjs/base-context.d.ts.map +0 -1
  21. package/cjs/base-context.js +0 -105
  22. package/cjs/base-context.js.map +0 -1
  23. package/cjs/context-manager.d.ts +0 -102
  24. package/cjs/context-manager.d.ts.map +0 -1
  25. package/cjs/context-manager.js +0 -132
  26. package/cjs/context-manager.js.map +0 -1
  27. package/cjs/index.d.ts +0 -3
  28. package/cjs/index.d.ts.map +0 -1
  29. package/cjs/index.js +0 -1
  30. package/cjs/index.js.map +0 -1
  31. package/esm/base-context.d.ts +0 -96
  32. package/esm/base-context.d.ts.map +0 -1
  33. package/esm/base-context.js +0 -105
  34. package/esm/base-context.js.map +0 -1
  35. package/esm/context-manager.d.ts +0 -102
  36. package/esm/context-manager.d.ts.map +0 -1
  37. package/esm/context-manager.js +0 -132
  38. package/esm/context-manager.js.map +0 -1
  39. package/esm/index.d.ts +0 -3
  40. package/esm/index.d.ts.map +0 -1
  41. package/esm/index.js +0 -1
  42. package/esm/index.js.map +0 -1
package/cjs/index.cjs ADDED
@@ -0,0 +1,239 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ let async_hooks = require("async_hooks");
3
+
4
+ //#region ../../@warlock.js/context/src/base-context.ts
5
+ /**
6
+ * Base class for all AsyncLocalStorage-based contexts
7
+ *
8
+ * Provides a consistent API for managing context across async operations.
9
+ * All framework contexts (request, storage, database) extend this class.
10
+ *
11
+ * @template TStore - The type of data stored in context
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * interface MyContextStore {
16
+ * userId: string;
17
+ * tenant: string;
18
+ * }
19
+ *
20
+ * class MyContext extends Context<MyContextStore> {}
21
+ * const myContext = new MyContext();
22
+ *
23
+ * // Use it
24
+ * await myContext.run({ userId: '123', tenant: 'acme' }, async () => {
25
+ * const userId = myContext.get('userId'); // '123'
26
+ * });
27
+ * ```
28
+ */
29
+ var Context = class {
30
+ constructor() {
31
+ this.storage = new async_hooks.AsyncLocalStorage();
32
+ }
33
+ /**
34
+ * Run a callback within a new context
35
+ *
36
+ * Creates a new async context with the provided store data.
37
+ * All operations within the callback will have access to this context.
38
+ *
39
+ * @param store - Initial context data
40
+ * @param callback - Async function to execute
41
+ * @returns Result of the callback
42
+ */
43
+ run(store, callback) {
44
+ return this.storage.run(store, callback);
45
+ }
46
+ /**
47
+ * Enter a new context without a callback
48
+ *
49
+ * Useful for middleware where you want to set context for the rest of the request.
50
+ * Unlike `run()`, this doesn't require a callback.
51
+ *
52
+ * @param store - Context data to set
53
+ */
54
+ enter(store) {
55
+ this.storage.enterWith(store);
56
+ }
57
+ /**
58
+ * Update the current context
59
+ *
60
+ * Merges new data into existing context, or enters new context if none exists.
61
+ *
62
+ * @param updates - Partial context data to merge
63
+ */
64
+ update(updates) {
65
+ const current = this.storage.getStore();
66
+ if (current) Object.assign(current, updates);
67
+ else this.enter(updates);
68
+ }
69
+ /**
70
+ * Get the current context store
71
+ *
72
+ * @returns Current context or undefined if not in context
73
+ */
74
+ getStore() {
75
+ return this.storage.getStore();
76
+ }
77
+ /**
78
+ * Get a specific value from context
79
+ *
80
+ * @param key - Key to retrieve
81
+ * @returns Value or undefined
82
+ */
83
+ get(key) {
84
+ return this.storage.getStore()?.[key];
85
+ }
86
+ /**
87
+ * Set a specific value in context
88
+ *
89
+ * @param key - Key to set
90
+ * @param value - Value to store
91
+ */
92
+ set(key, value) {
93
+ this.update({ [key]: value });
94
+ }
95
+ /**
96
+ * Clear the context
97
+ */
98
+ clear() {
99
+ this.storage.enterWith({});
100
+ }
101
+ /**
102
+ * Check if currently in a context
103
+ */
104
+ hasContext() {
105
+ return this.storage.getStore() !== void 0;
106
+ }
107
+ };
108
+
109
+ //#endregion
110
+ //#region ../../@warlock.js/context/src/context-manager.ts
111
+ /**
112
+ * Context Manager - Orchestrates multiple contexts together
113
+ *
114
+ * Allows running multiple AsyncLocalStorage contexts in a single operation,
115
+ * making it easy to link request, storage, database, and other contexts.
116
+ *
117
+ * @example
118
+ * ```typescript
119
+ * // Register contexts
120
+ * contextManager
121
+ * .register('request', requestContext)
122
+ * .register('storage', storageDriverContext)
123
+ * .register('database', databaseDataSourceContext);
124
+ *
125
+ * // Run all contexts together
126
+ * await contextManager.runAll({
127
+ * request: { request, response, user },
128
+ * storage: { driver, metadata: { tenantId: '123' } },
129
+ * database: { dataSource: 'primary' },
130
+ * }, async () => {
131
+ * // All contexts active!
132
+ * await handleRequest();
133
+ * });
134
+ * ```
135
+ */
136
+ var ContextManager = class {
137
+ constructor() {
138
+ this.contexts = /* @__PURE__ */ new Map();
139
+ }
140
+ /**
141
+ * Register a context
142
+ *
143
+ * @param name - Unique context name
144
+ * @param context - Context instance
145
+ * @returns This instance for chaining
146
+ */
147
+ register(name, context) {
148
+ this.contexts.set(name, context);
149
+ return this;
150
+ }
151
+ /**
152
+ * Run all registered contexts together
153
+ *
154
+ * Nests all context.run() calls, ensuring all contexts are active
155
+ * for the duration of the callback.
156
+ *
157
+ * @param stores - Context stores keyed by context name
158
+ * @param callback - Async function to execute
159
+ * @returns Result of the callback
160
+ */
161
+ async runAll(stores, callback) {
162
+ return Array.from(this.contexts.entries()).reduceRight((next, [name, context]) => {
163
+ return () => context.run(stores[name] || {}, next);
164
+ }, callback)();
165
+ }
166
+ /**
167
+ * Enter all contexts at once (for middleware)
168
+ *
169
+ * @param stores - Context stores keyed by context name
170
+ */
171
+ enterAll(stores) {
172
+ for (const [name, context] of this.contexts.entries()) if (stores[name]) context.enter(stores[name]);
173
+ }
174
+ /**
175
+ * Clear all contexts
176
+ */
177
+ clearAll() {
178
+ for (const context of this.contexts.values()) context.clear();
179
+ }
180
+ /**
181
+ * Get a specific registered context
182
+ *
183
+ * @param name - Context name
184
+ * @returns Context instance or undefined
185
+ */
186
+ getContext(name) {
187
+ return this.contexts.get(name);
188
+ }
189
+ /**
190
+ * Check if a context is registered
191
+ *
192
+ * @param name - Context name
193
+ * @returns True if context is registered
194
+ */
195
+ hasContext(name) {
196
+ return this.contexts.has(name);
197
+ }
198
+ /**
199
+ * Build all context stores by calling each context's buildStore() method
200
+ *
201
+ * This is the immutable pattern - returns a new record of stores.
202
+ * Each context defines its own initialization logic.
203
+ *
204
+ * @param payload - Payload passed to each buildStore() (e.g., { request, response })
205
+ * @returns Record of context name -> store data
206
+ *
207
+ * @example
208
+ * ```typescript
209
+ * const httpContextStore = contextManager.buildStores({ request, response });
210
+ * await contextManager.runAll(httpContextStore, async () => { ... });
211
+ * ```
212
+ */
213
+ buildStores(payload) {
214
+ const stores = {};
215
+ for (const [name, context] of this.contexts.entries()) stores[name] = context.buildStore(payload) ?? {};
216
+ return stores;
217
+ }
218
+ /**
219
+ * Unregister a context
220
+ *
221
+ * @param name - Context name to remove
222
+ * @returns True if context was removed
223
+ */
224
+ unregister(name) {
225
+ return this.contexts.delete(name);
226
+ }
227
+ };
228
+ /**
229
+ * Global context manager instance
230
+ *
231
+ * Use this singleton to register and manage all framework contexts.
232
+ */
233
+ const contextManager = new ContextManager();
234
+
235
+ //#endregion
236
+ exports.Context = Context;
237
+ exports.ContextManager = ContextManager;
238
+ exports.contextManager = contextManager;
239
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","names":["AsyncLocalStorage"],"sources":["../../../../../@warlock.js/context/src/base-context.ts","../../../../../@warlock.js/context/src/context-manager.ts"],"sourcesContent":["import { AsyncLocalStorage } from \"async_hooks\";\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 * @param updates - Partial context data to merge\n */\n public update(updates: Partial<TStore>): void {\n const current = this.storage.getStore();\n\n if (current) {\n Object.assign(current, updates);\n } else {\n this.enter(updates 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 * @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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;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;;;;;;;;CASA,AAAO,OAAO,SAAgC;EAC5C,MAAM,UAAU,KAAK,QAAQ,SAAS;EAEtC,IAAI,SACF,OAAO,OAAO,SAAS,OAAO;OAE9B,KAAK,MAAM,OAAiB;CAEhC;;;;;;CAOA,AAAO,WAA+B;EACpC,OAAO,KAAK,QAAQ,SAAS;CAC/B;;;;;;;CAQA,AAAO,IAA4B,KAA+B;EAChE,OAAO,KAAK,QAAQ,SAAS,IAAI;CACnC;;;;;;;CAQA,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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AClGA,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"}
@@ -0,0 +1,99 @@
1
+ import { AsyncLocalStorage } from "async_hooks";
2
+
3
+ //#region ../../@warlock.js/context/src/base-context.d.ts
4
+ /**
5
+ * Base class for all AsyncLocalStorage-based contexts
6
+ *
7
+ * Provides a consistent API for managing context across async operations.
8
+ * All framework contexts (request, storage, database) extend this class.
9
+ *
10
+ * @template TStore - The type of data stored in context
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * interface MyContextStore {
15
+ * userId: string;
16
+ * tenant: string;
17
+ * }
18
+ *
19
+ * class MyContext extends Context<MyContextStore> {}
20
+ * const myContext = new MyContext();
21
+ *
22
+ * // Use it
23
+ * await myContext.run({ userId: '123', tenant: 'acme' }, async () => {
24
+ * const userId = myContext.get('userId'); // '123'
25
+ * });
26
+ * ```
27
+ */
28
+ declare abstract class Context<TStore extends Record<string, any>> {
29
+ protected readonly storage: AsyncLocalStorage<TStore>;
30
+ /**
31
+ * Run a callback within a new context
32
+ *
33
+ * Creates a new async context with the provided store data.
34
+ * All operations within the callback will have access to this context.
35
+ *
36
+ * @param store - Initial context data
37
+ * @param callback - Async function to execute
38
+ * @returns Result of the callback
39
+ */
40
+ run<T>(store: TStore, callback: () => Promise<T>): Promise<T>;
41
+ /**
42
+ * Enter a new context without a callback
43
+ *
44
+ * Useful for middleware where you want to set context for the rest of the request.
45
+ * Unlike `run()`, this doesn't require a callback.
46
+ *
47
+ * @param store - Context data to set
48
+ */
49
+ enter(store: TStore): void;
50
+ /**
51
+ * Update the current context
52
+ *
53
+ * Merges new data into existing context, or enters new context if none exists.
54
+ *
55
+ * @param updates - Partial context data to merge
56
+ */
57
+ update(updates: Partial<TStore>): void;
58
+ /**
59
+ * Get the current context store
60
+ *
61
+ * @returns Current context or undefined if not in context
62
+ */
63
+ getStore(): TStore | undefined;
64
+ /**
65
+ * Get a specific value from context
66
+ *
67
+ * @param key - Key to retrieve
68
+ * @returns Value or undefined
69
+ */
70
+ get<K extends keyof TStore>(key: K): TStore[K] | undefined;
71
+ /**
72
+ * Set a specific value in context
73
+ *
74
+ * @param key - Key to set
75
+ * @param value - Value to store
76
+ */
77
+ set<K extends keyof TStore>(key: K, value: TStore[K]): void;
78
+ /**
79
+ * Clear the context
80
+ */
81
+ clear(): void;
82
+ /**
83
+ * Check if currently in a context
84
+ */
85
+ hasContext(): boolean;
86
+ /**
87
+ * Build the initial store for this context
88
+ *
89
+ * Override this method to provide custom initialization logic.
90
+ * Called by ContextManager.buildStores() for each registered context.
91
+ *
92
+ * @param payload - Generic payload (e.g., { request, response } for HTTP contexts)
93
+ * @returns Initial store data
94
+ */
95
+ abstract buildStore(payload?: Record<string, any>): TStore;
96
+ }
97
+ //#endregion
98
+ export { Context };
99
+ //# sourceMappingURL=base-context.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base-context.d.mts","names":[],"sources":["../../../../../@warlock.js/context/src/base-context.ts"],"mappings":";;;;;AA0BA;;;;;;;;;;;;;;;;;;;;;;uBAAsB,OAAA,gBAAuB,MAAA;EAAA,mBACxB,OAAA,EAAS,iBAAA,CAAkB,MAAA;EAiGa;;;;;;;;;;EArFpD,GAAA,GAAA,CAAO,KAAA,EAAO,MAAA,EAAQ,QAAA,QAAgB,OAAA,CAAQ,CAAA,IAAK,OAAA,CAAQ,CAAA;EAApD;;;;;;;;EAYP,KAAA,CAAM,KAAA,EAAO,MAAA;EAWb;;;;;;;EAAA,MAAA,CAAO,OAAA,EAAS,OAAA,CAAQ,MAAA;EAyBJ;;;;;EAVpB,QAAA,CAAA,GAAY,MAAA;EAoBR;;;;;;EAVJ,GAAA,iBAAoB,MAAA,CAAA,CAAQ,GAAA,EAAK,CAAA,GAAI,MAAA,CAAO,CAAA;EAiB5C;;;;;;EAPA,GAAA,iBAAoB,MAAA,CAAA,CAAQ,GAAA,EAAK,CAAA,EAAG,KAAA,EAAO,MAAA,CAAO,CAAA;EA2BQ;;;EApB1D,KAAA,CAAA;;;;EAOA,UAAA,CAAA;;;;;;;;;;WAaS,UAAA,CAAW,OAAA,GAAU,MAAA,gBAAsB,MAAA;AAAA"}
@@ -0,0 +1,110 @@
1
+ import { AsyncLocalStorage } from "async_hooks";
2
+
3
+ //#region ../../@warlock.js/context/src/base-context.ts
4
+ /**
5
+ * Base class for all AsyncLocalStorage-based contexts
6
+ *
7
+ * Provides a consistent API for managing context across async operations.
8
+ * All framework contexts (request, storage, database) extend this class.
9
+ *
10
+ * @template TStore - The type of data stored in context
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * interface MyContextStore {
15
+ * userId: string;
16
+ * tenant: string;
17
+ * }
18
+ *
19
+ * class MyContext extends Context<MyContextStore> {}
20
+ * const myContext = new MyContext();
21
+ *
22
+ * // Use it
23
+ * await myContext.run({ userId: '123', tenant: 'acme' }, async () => {
24
+ * const userId = myContext.get('userId'); // '123'
25
+ * });
26
+ * ```
27
+ */
28
+ var Context = class {
29
+ constructor() {
30
+ this.storage = new AsyncLocalStorage();
31
+ }
32
+ /**
33
+ * Run a callback within a new context
34
+ *
35
+ * Creates a new async context with the provided store data.
36
+ * All operations within the callback will have access to this context.
37
+ *
38
+ * @param store - Initial context data
39
+ * @param callback - Async function to execute
40
+ * @returns Result of the callback
41
+ */
42
+ run(store, callback) {
43
+ return this.storage.run(store, callback);
44
+ }
45
+ /**
46
+ * Enter a new context without a callback
47
+ *
48
+ * Useful for middleware where you want to set context for the rest of the request.
49
+ * Unlike `run()`, this doesn't require a callback.
50
+ *
51
+ * @param store - Context data to set
52
+ */
53
+ enter(store) {
54
+ this.storage.enterWith(store);
55
+ }
56
+ /**
57
+ * Update the current context
58
+ *
59
+ * Merges new data into existing context, or enters new context if none exists.
60
+ *
61
+ * @param updates - Partial context data to merge
62
+ */
63
+ update(updates) {
64
+ const current = this.storage.getStore();
65
+ if (current) Object.assign(current, updates);
66
+ else this.enter(updates);
67
+ }
68
+ /**
69
+ * Get the current context store
70
+ *
71
+ * @returns Current context or undefined if not in context
72
+ */
73
+ getStore() {
74
+ return this.storage.getStore();
75
+ }
76
+ /**
77
+ * Get a specific value from context
78
+ *
79
+ * @param key - Key to retrieve
80
+ * @returns Value or undefined
81
+ */
82
+ get(key) {
83
+ return this.storage.getStore()?.[key];
84
+ }
85
+ /**
86
+ * Set a specific value in context
87
+ *
88
+ * @param key - Key to set
89
+ * @param value - Value to store
90
+ */
91
+ set(key, value) {
92
+ this.update({ [key]: value });
93
+ }
94
+ /**
95
+ * Clear the context
96
+ */
97
+ clear() {
98
+ this.storage.enterWith({});
99
+ }
100
+ /**
101
+ * Check if currently in a context
102
+ */
103
+ hasContext() {
104
+ return this.storage.getStore() !== void 0;
105
+ }
106
+ };
107
+
108
+ //#endregion
109
+ export { Context };
110
+ //# sourceMappingURL=base-context.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base-context.mjs","names":[],"sources":["../../../../../@warlock.js/context/src/base-context.ts"],"sourcesContent":["import { AsyncLocalStorage } from \"async_hooks\";\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 * @param updates - Partial context data to merge\n */\n public update(updates: Partial<TStore>): void {\n const current = this.storage.getStore();\n\n if (current) {\n Object.assign(current, updates);\n } else {\n this.enter(updates 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 * @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":";;;;;;;;;;;;;;;;;;;;;;;;;;;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;;;;;;;;CASA,AAAO,OAAO,SAAgC;EAC5C,MAAM,UAAU,KAAK,QAAQ,SAAS;EAEtC,IAAI,SACF,OAAO,OAAO,SAAS,OAAO;OAE9B,KAAK,MAAM,OAAiB;CAEhC;;;;;;CAOA,AAAO,WAA+B;EACpC,OAAO,KAAK,QAAQ,SAAS;CAC/B;;;;;;;CAQA,AAAO,IAA4B,KAA+B;EAChE,OAAO,KAAK,QAAQ,SAAS,IAAI;CACnC;;;;;;;CAQA,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"}
@@ -0,0 +1,106 @@
1
+ import { Context } from "./base-context.mjs";
2
+
3
+ //#region ../../@warlock.js/context/src/context-manager.d.ts
4
+ /**
5
+ * Context Manager - Orchestrates multiple contexts together
6
+ *
7
+ * Allows running multiple AsyncLocalStorage contexts in a single operation,
8
+ * making it easy to link request, storage, database, and other contexts.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * // Register contexts
13
+ * contextManager
14
+ * .register('request', requestContext)
15
+ * .register('storage', storageDriverContext)
16
+ * .register('database', databaseDataSourceContext);
17
+ *
18
+ * // Run all contexts together
19
+ * await contextManager.runAll({
20
+ * request: { request, response, user },
21
+ * storage: { driver, metadata: { tenantId: '123' } },
22
+ * database: { dataSource: 'primary' },
23
+ * }, async () => {
24
+ * // All contexts active!
25
+ * await handleRequest();
26
+ * });
27
+ * ```
28
+ */
29
+ declare class ContextManager {
30
+ private contexts;
31
+ /**
32
+ * Register a context
33
+ *
34
+ * @param name - Unique context name
35
+ * @param context - Context instance
36
+ * @returns This instance for chaining
37
+ */
38
+ register(name: string, context: Context<any>): this;
39
+ /**
40
+ * Run all registered contexts together
41
+ *
42
+ * Nests all context.run() calls, ensuring all contexts are active
43
+ * for the duration of the callback.
44
+ *
45
+ * @param stores - Context stores keyed by context name
46
+ * @param callback - Async function to execute
47
+ * @returns Result of the callback
48
+ */
49
+ runAll<T>(stores: Record<string, any>, callback: () => Promise<T>): Promise<T>;
50
+ /**
51
+ * Enter all contexts at once (for middleware)
52
+ *
53
+ * @param stores - Context stores keyed by context name
54
+ */
55
+ enterAll(stores: Record<string, any>): void;
56
+ /**
57
+ * Clear all contexts
58
+ */
59
+ clearAll(): void;
60
+ /**
61
+ * Get a specific registered context
62
+ *
63
+ * @param name - Context name
64
+ * @returns Context instance or undefined
65
+ */
66
+ getContext<T extends Context<any>>(name: string): T | undefined;
67
+ /**
68
+ * Check if a context is registered
69
+ *
70
+ * @param name - Context name
71
+ * @returns True if context is registered
72
+ */
73
+ hasContext(name: string): boolean;
74
+ /**
75
+ * Build all context stores by calling each context's buildStore() method
76
+ *
77
+ * This is the immutable pattern - returns a new record of stores.
78
+ * Each context defines its own initialization logic.
79
+ *
80
+ * @param payload - Payload passed to each buildStore() (e.g., { request, response })
81
+ * @returns Record of context name -> store data
82
+ *
83
+ * @example
84
+ * ```typescript
85
+ * const httpContextStore = contextManager.buildStores({ request, response });
86
+ * await contextManager.runAll(httpContextStore, async () => { ... });
87
+ * ```
88
+ */
89
+ buildStores(payload?: Record<string, any>): Record<string, any>;
90
+ /**
91
+ * Unregister a context
92
+ *
93
+ * @param name - Context name to remove
94
+ * @returns True if context was removed
95
+ */
96
+ unregister(name: string): boolean;
97
+ }
98
+ /**
99
+ * Global context manager instance
100
+ *
101
+ * Use this singleton to register and manage all framework contexts.
102
+ */
103
+ declare const contextManager: ContextManager;
104
+ //#endregion
105
+ export { ContextManager, contextManager };
106
+ //# sourceMappingURL=context-manager.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context-manager.d.mts","names":[],"sources":["../../../../../@warlock.js/context/src/context-manager.ts"],"mappings":";;;;;AA2BA;;;;;;;;;;;;;;;;;;;;;;;cAAa,cAAA;EAAA,QACH,QAAA;EAwB4D;;;;;;;EAf7D,QAAA,CAAS,IAAA,UAAc,OAAA,EAAS,OAAA;EA0ChC;;;;;;;;;;EA3BM,MAAA,GAAA,CAAU,MAAA,EAAQ,MAAA,eAAqB,QAAA,QAAgB,OAAA,CAAQ,CAAA,IAAK,OAAA,CAAQ,CAAA;EAoEtC;;;;AAgBrB;EApEvB,QAAA,CAAS,MAAA,EAAQ,MAAA;EA8EwB;;;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"}