@geekmidas/services 0.1.0 → 0.2.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/dist/ServiceDiscovery-BO2FzEGt.cjs +200 -0
- package/dist/ServiceDiscovery-BO2FzEGt.cjs.map +1 -0
- package/dist/ServiceDiscovery-CfVBrQHt.d.cts +189 -0
- package/dist/ServiceDiscovery-CfVBrQHt.d.cts.map +1 -0
- package/dist/ServiceDiscovery-DLMyH_yM.mjs +195 -0
- package/dist/ServiceDiscovery-DLMyH_yM.mjs.map +1 -0
- package/dist/ServiceDiscovery-DpeEKNe2.d.mts +189 -0
- package/dist/ServiceDiscovery-DpeEKNe2.d.mts.map +1 -0
- package/dist/ServiceDiscovery.cjs +4 -0
- package/dist/ServiceDiscovery.d.cts +3 -0
- package/dist/ServiceDiscovery.d.mts +3 -0
- package/dist/ServiceDiscovery.mjs +4 -0
- package/dist/context-B5YTspJR.mjs +61 -0
- package/dist/context-B5YTspJR.mjs.map +1 -0
- package/dist/context-BVeZOvOd.d.cts +45 -0
- package/dist/context-BVeZOvOd.d.cts.map +1 -0
- package/dist/context-DMExgNzl.d.mts +45 -0
- package/dist/context-DMExgNzl.d.mts.map +1 -0
- package/dist/context-DUTDtYd2.cjs +95 -0
- package/dist/context-DUTDtYd2.cjs.map +1 -0
- package/dist/context.cjs +4 -0
- package/dist/context.d.cts +3 -0
- package/dist/context.d.mts +3 -0
- package/dist/context.mjs +3 -0
- package/dist/index.cjs +5 -193
- package/dist/index.d.cts +4 -227
- package/dist/index.d.mts +4 -227
- package/dist/index.mjs +3 -192
- package/dist/types-CcHmCx_U.d.mts +86 -0
- package/dist/types-CcHmCx_U.d.mts.map +1 -0
- package/dist/types-D7d_yeU5.d.cts +86 -0
- package/dist/types-D7d_yeU5.d.cts.map +1 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.cts +2 -0
- package/dist/types.d.mts +2 -0
- package/dist/types.mjs +0 -0
- package/package.json +10 -5
- package/src/ServiceDiscovery.ts +254 -0
- package/src/__tests__/context.spec.ts +276 -0
- package/src/__tests__/index.spec.ts +556 -558
- package/src/context.ts +91 -0
- package/src/index.ts +15 -295
- package/src/types.ts +85 -0
- package/tsconfig.json +9 -0
- package/dist/index.cjs.map +0 -1
- package/dist/index.mjs.map +0 -1
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { Service } from "./types-CcHmCx_U.mjs";
|
|
2
|
+
import { EnvironmentParser } from "@geekmidas/envkit";
|
|
3
|
+
|
|
4
|
+
//#region src/ServiceDiscovery.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Service discovery container that manages service registration and retrieval.
|
|
8
|
+
* Implements a singleton pattern with lazy initialization of services.
|
|
9
|
+
*
|
|
10
|
+
* @template TServices - Record type mapping service names to their instance types
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Define service types
|
|
15
|
+
* interface MyServices {
|
|
16
|
+
* database: Database;
|
|
17
|
+
* cache: CacheService;
|
|
18
|
+
* auth: AuthService;
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* // Get service discovery instance
|
|
22
|
+
* const discovery = ServiceDiscovery.getInstance<MyServices>(envParser);
|
|
23
|
+
*
|
|
24
|
+
* // Register services
|
|
25
|
+
* await discovery.register([
|
|
26
|
+
* databaseService,
|
|
27
|
+
* cacheService,
|
|
28
|
+
* authService
|
|
29
|
+
* ]);
|
|
30
|
+
*
|
|
31
|
+
* // Retrieve services
|
|
32
|
+
* const db = await discovery.get('database');
|
|
33
|
+
* const { cache, auth } = await discovery.getMany(['cache', 'auth']);
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
declare class ServiceDiscovery<TServices extends Record<string, unknown> = {}> {
|
|
37
|
+
readonly envParser: EnvironmentParser<{}>;
|
|
38
|
+
/** Singleton instance of ServiceDiscovery */
|
|
39
|
+
private static _instance;
|
|
40
|
+
/** Map of registered service definitions */
|
|
41
|
+
private services;
|
|
42
|
+
/** Map of instantiated service instances */
|
|
43
|
+
private instances;
|
|
44
|
+
/**
|
|
45
|
+
* Gets the singleton instance of ServiceDiscovery.
|
|
46
|
+
* Creates a new instance if one doesn't exist.
|
|
47
|
+
*
|
|
48
|
+
* @template T - Record type mapping service names to their instance types
|
|
49
|
+
* @param envParser - Environment parser for service configuration
|
|
50
|
+
* @returns The ServiceDiscovery singleton instance
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const services = ServiceDiscovery.getInstance<MyServices>(envParser);
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
static getInstance<T extends Record<any, unknown> = any>(envParser: EnvironmentParser<{}>): ServiceDiscovery<T>;
|
|
58
|
+
/**
|
|
59
|
+
* Resets the singleton instance. Use only for testing purposes.
|
|
60
|
+
* This clears all cached services and allows a fresh instance to be created.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* // In test teardown
|
|
65
|
+
* afterEach(() => {
|
|
66
|
+
* ServiceDiscovery.reset();
|
|
67
|
+
* });
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
static reset(): void;
|
|
71
|
+
/**
|
|
72
|
+
* Private constructor to enforce singleton pattern.
|
|
73
|
+
*
|
|
74
|
+
* @param envParser - Environment parser for service configuration
|
|
75
|
+
* @private
|
|
76
|
+
*/
|
|
77
|
+
private constructor();
|
|
78
|
+
/**
|
|
79
|
+
* Register multiple services with the service discovery.
|
|
80
|
+
* Services are instantiated lazily on first access.
|
|
81
|
+
* Already instantiated services are returned from cache.
|
|
82
|
+
*
|
|
83
|
+
* @template T - Array type of services to register
|
|
84
|
+
* @param services - Array of services to register
|
|
85
|
+
* @returns Promise resolving to a record of service names to instances
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* const services = await discovery.register([
|
|
90
|
+
* databaseService,
|
|
91
|
+
* cacheService,
|
|
92
|
+
* authService
|
|
93
|
+
* ]);
|
|
94
|
+
*
|
|
95
|
+
* // services = {
|
|
96
|
+
* // database: Database instance,
|
|
97
|
+
* // cache: CacheService instance,
|
|
98
|
+
* // auth: AuthService instance
|
|
99
|
+
* // }
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
register<T extends Service[]>(services: T): Promise<ServiceRecord<T>>;
|
|
103
|
+
/**
|
|
104
|
+
* Get a service from the service discovery.
|
|
105
|
+
* Services are instantiated on first access if not already cached.
|
|
106
|
+
*
|
|
107
|
+
* @template K - The service name key
|
|
108
|
+
* @param name - The name of the service to get
|
|
109
|
+
* @returns Promise resolving to the service instance
|
|
110
|
+
* @throws {Error} If the service is not registered
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const database = await discovery.get('database');
|
|
115
|
+
* const users = await database.query('SELECT * FROM users');
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
get<K extends keyof TServices & string>(name: K): Promise<TServices[K]>;
|
|
119
|
+
/**
|
|
120
|
+
* Get multiple services from the service discovery.
|
|
121
|
+
* Useful for retrieving multiple dependencies at once.
|
|
122
|
+
*
|
|
123
|
+
* @template K - Array of service name keys
|
|
124
|
+
* @param names - Array of service names to retrieve
|
|
125
|
+
* @returns Promise resolving to an object containing the service instances
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* const { database, cache, auth } = await discovery.getMany([
|
|
130
|
+
* 'database',
|
|
131
|
+
* 'cache',
|
|
132
|
+
* 'auth'
|
|
133
|
+
* ]);
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
getMany<K extends (keyof TServices & string)[]>(names: [...K]): Promise<{ [P in K[number]]: TServices[P] }>;
|
|
137
|
+
/**
|
|
138
|
+
* Check if a service exists in the service discovery.
|
|
139
|
+
* Can check by service name or service instance.
|
|
140
|
+
*
|
|
141
|
+
* @param service - The service name or service instance to check
|
|
142
|
+
* @returns True if the service exists, false otherwise
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* if (discovery.has('database')) {
|
|
147
|
+
* const db = await discovery.get('database');
|
|
148
|
+
* }
|
|
149
|
+
*
|
|
150
|
+
* // Or check with service instance
|
|
151
|
+
* if (!discovery.has(databaseService)) {
|
|
152
|
+
* await discovery.register([databaseService]);
|
|
153
|
+
* }
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
has(service: string | Service): boolean;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Utility type to extract service names from an array of services.
|
|
160
|
+
*
|
|
161
|
+
* @template T - Array of Service types
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```typescript
|
|
165
|
+
* type Names = ExtractServiceNames<[typeof databaseService, typeof cacheService]>;
|
|
166
|
+
* // type Names = 'database' | 'cache'
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
type ExtractServiceNames<T extends Service[]> = T[number]['serviceName'];
|
|
170
|
+
/**
|
|
171
|
+
* Utility type to create a record type from an array of services.
|
|
172
|
+
* Maps service names to their registered instance types.
|
|
173
|
+
*
|
|
174
|
+
* @template T - Array of Service types
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* type MyServiceRecord = ServiceRecord<[typeof databaseService, typeof cacheService]>;
|
|
179
|
+
* // type MyServiceRecord = {
|
|
180
|
+
* // database: DatabaseInstance;
|
|
181
|
+
* // cache: CacheInstance;
|
|
182
|
+
* // }
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
type ServiceRecord<T extends Service[]> = { [K in T[number] as K['serviceName']]: K extends Service ? Awaited<ReturnType<K['register']>> : never };
|
|
186
|
+
//# sourceMappingURL=ServiceDiscovery.d.ts.map
|
|
187
|
+
//#endregion
|
|
188
|
+
export { ExtractServiceNames, ServiceDiscovery, ServiceRecord };
|
|
189
|
+
//# sourceMappingURL=ServiceDiscovery-DpeEKNe2.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ServiceDiscovery-DpeEKNe2.d.mts","names":[],"sources":["../src/ServiceDiscovery.ts"],"sourcesContent":[],"mappings":";;;;;;;AAkCA;;;;;;;;;;;;;;;;;;;;;;;;;AAkL8B;AAoB9B;;AAA0C,cAtM7B,gBAsM6B,CAAA,kBAtMM,MAsMN,CAAA,MAAA,EAAA,OAAA,CAAA,GAAA,CAAA,CAAA,CAAA,CAAA;EAAO,SAAM,SAAA,EAlJd,iBAkJc,CAAA,CAAA,CAAA,CAAA;EAAC;EAiB5C,eAAA,SAAa;EAAA;EAAA,QAAW,QAAA;EAAO;EACnC,QAAY,SAAA;EAAC;;;;;AACV;;;;;;;;+BApMmB,uCACjB,wBACT,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAuDK,qBAAqB,IAAI,QAAQ,cAAc;;;;;;;;;;;;;;;;sBAwCpD,0BAA0B,IAAI,QAAQ,UAAU;;;;;;;;;;;;;;;;;;2BA6BrC,kCACnB,KACT,gBAAgB,YAAY,UAAU;;;;;;;;;;;;;;;;;;;;wBA6BnB;;;;;;;;;;;;;KAoBX,8BAA8B,aAAa;;;;;;;;;;;;;;;;KAiB3C,wBAAwB,qBAC7B,aAAa,mBAAmB,UAAU,UAC7C,QAAQ,WAAW"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
|
|
3
|
+
//#region src/context.ts
|
|
4
|
+
/**
|
|
5
|
+
* Internal AsyncLocalStorage instance for request context.
|
|
6
|
+
* Not exported - use runWithRequestContext() to establish context
|
|
7
|
+
* and serviceContext to access it.
|
|
8
|
+
*/
|
|
9
|
+
const requestContextStorage = new AsyncLocalStorage();
|
|
10
|
+
/**
|
|
11
|
+
* ServiceContext implementation.
|
|
12
|
+
* Singleton that reads from AsyncLocalStorage.
|
|
13
|
+
* Methods throw if called outside a request context (catches bugs early).
|
|
14
|
+
*/
|
|
15
|
+
const serviceContext = {
|
|
16
|
+
getLogger() {
|
|
17
|
+
const store = requestContextStorage.getStore();
|
|
18
|
+
if (!store) throw new Error("ServiceContext.getLogger() called outside request context. Ensure code runs within runWithRequestContext().");
|
|
19
|
+
return store.logger;
|
|
20
|
+
},
|
|
21
|
+
getRequestId() {
|
|
22
|
+
const store = requestContextStorage.getStore();
|
|
23
|
+
if (!store) throw new Error("ServiceContext.getRequestId() called outside request context. Ensure code runs within runWithRequestContext().");
|
|
24
|
+
return store.requestId;
|
|
25
|
+
},
|
|
26
|
+
getRequestStartTime() {
|
|
27
|
+
const store = requestContextStorage.getStore();
|
|
28
|
+
if (!store) throw new Error("ServiceContext.getRequestStartTime() called outside request context. Ensure code runs within runWithRequestContext().");
|
|
29
|
+
return store.startTime;
|
|
30
|
+
},
|
|
31
|
+
hasContext() {
|
|
32
|
+
return requestContextStorage.getStore() !== void 0;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Run a function with request context.
|
|
37
|
+
* Used by endpoint/function/subscriber adaptors.
|
|
38
|
+
*
|
|
39
|
+
* @param data - Request context data (logger, requestId, startTime)
|
|
40
|
+
* @param fn - Function to run with context
|
|
41
|
+
* @returns Result of the function
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* const result = await runWithRequestContext(
|
|
46
|
+
* { logger, requestId, startTime: Date.now() },
|
|
47
|
+
* async () => {
|
|
48
|
+
* // Inside here, serviceContext.getLogger() returns `logger`
|
|
49
|
+
* // serviceContext.getRequestId() returns `requestId`
|
|
50
|
+
* return await handleRequest();
|
|
51
|
+
* }
|
|
52
|
+
* );
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
function runWithRequestContext(data, fn) {
|
|
56
|
+
return requestContextStorage.run(data, fn);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
//#endregion
|
|
60
|
+
export { runWithRequestContext, serviceContext };
|
|
61
|
+
//# sourceMappingURL=context-B5YTspJR.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-B5YTspJR.mjs","names":["serviceContext: ServiceContext","data: RequestContextData","fn: () => T | Promise<T>"],"sources":["../src/context.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\nimport type { Logger } from '@geekmidas/logger';\nimport type { ServiceContext } from './types';\n\n/**\n * Internal storage for request context data.\n * Not exported - services use ServiceContext interface.\n */\nexport interface RequestContextData {\n\tlogger: Logger;\n\trequestId: string;\n\tstartTime: number;\n}\n\n/**\n * Internal AsyncLocalStorage instance for request context.\n * Not exported - use runWithRequestContext() to establish context\n * and serviceContext to access it.\n */\nconst requestContextStorage = new AsyncLocalStorage<RequestContextData>();\n\n/**\n * ServiceContext implementation.\n * Singleton that reads from AsyncLocalStorage.\n * Methods throw if called outside a request context (catches bugs early).\n */\nexport const serviceContext: ServiceContext = {\n\tgetLogger() {\n\t\tconst store = requestContextStorage.getStore();\n\t\tif (!store) {\n\t\t\tthrow new Error(\n\t\t\t\t'ServiceContext.getLogger() called outside request context. ' +\n\t\t\t\t\t'Ensure code runs within runWithRequestContext().',\n\t\t\t);\n\t\t}\n\t\treturn store.logger;\n\t},\n\n\tgetRequestId() {\n\t\tconst store = requestContextStorage.getStore();\n\t\tif (!store) {\n\t\t\tthrow new Error(\n\t\t\t\t'ServiceContext.getRequestId() called outside request context. ' +\n\t\t\t\t\t'Ensure code runs within runWithRequestContext().',\n\t\t\t);\n\t\t}\n\t\treturn store.requestId;\n\t},\n\n\tgetRequestStartTime() {\n\t\tconst store = requestContextStorage.getStore();\n\t\tif (!store) {\n\t\t\tthrow new Error(\n\t\t\t\t'ServiceContext.getRequestStartTime() called outside request context. ' +\n\t\t\t\t\t'Ensure code runs within runWithRequestContext().',\n\t\t\t);\n\t\t}\n\t\treturn store.startTime;\n\t},\n\n\thasContext() {\n\t\treturn requestContextStorage.getStore() !== undefined;\n\t},\n};\n\n/**\n * Run a function with request context.\n * Used by endpoint/function/subscriber adaptors.\n *\n * @param data - Request context data (logger, requestId, startTime)\n * @param fn - Function to run with context\n * @returns Result of the function\n *\n * @example\n * ```typescript\n * const result = await runWithRequestContext(\n * { logger, requestId, startTime: Date.now() },\n * async () => {\n * // Inside here, serviceContext.getLogger() returns `logger`\n * // serviceContext.getRequestId() returns `requestId`\n * return await handleRequest();\n * }\n * );\n * ```\n */\nexport function runWithRequestContext<T>(\n\tdata: RequestContextData,\n\tfn: () => T | Promise<T>,\n): T | Promise<T> {\n\treturn requestContextStorage.run(data, fn);\n}\n"],"mappings":";;;;;;;;AAmBA,MAAM,wBAAwB,IAAI;;;;;;AAOlC,MAAaA,iBAAiC;CAC7C,YAAY;EACX,MAAM,QAAQ,sBAAsB,UAAU;AAC9C,OAAK,MACJ,OAAM,IAAI,MACT;AAIF,SAAO,MAAM;CACb;CAED,eAAe;EACd,MAAM,QAAQ,sBAAsB,UAAU;AAC9C,OAAK,MACJ,OAAM,IAAI,MACT;AAIF,SAAO,MAAM;CACb;CAED,sBAAsB;EACrB,MAAM,QAAQ,sBAAsB,UAAU;AAC9C,OAAK,MACJ,OAAM,IAAI,MACT;AAIF,SAAO,MAAM;CACb;CAED,aAAa;AACZ,SAAO,sBAAsB,UAAU;CACvC;AACD;;;;;;;;;;;;;;;;;;;;;AAsBD,SAAgB,sBACfC,MACAC,IACiB;AACjB,QAAO,sBAAsB,IAAI,MAAM,GAAG;AAC1C"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { ServiceContext } from "./types-D7d_yeU5.cjs";
|
|
2
|
+
import { Logger } from "@geekmidas/logger";
|
|
3
|
+
|
|
4
|
+
//#region src/context.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Internal storage for request context data.
|
|
8
|
+
* Not exported - services use ServiceContext interface.
|
|
9
|
+
*/
|
|
10
|
+
interface RequestContextData {
|
|
11
|
+
logger: Logger;
|
|
12
|
+
requestId: string;
|
|
13
|
+
startTime: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* ServiceContext implementation.
|
|
17
|
+
* Singleton that reads from AsyncLocalStorage.
|
|
18
|
+
* Methods throw if called outside a request context (catches bugs early).
|
|
19
|
+
*/
|
|
20
|
+
declare const serviceContext: ServiceContext;
|
|
21
|
+
/**
|
|
22
|
+
* Run a function with request context.
|
|
23
|
+
* Used by endpoint/function/subscriber adaptors.
|
|
24
|
+
*
|
|
25
|
+
* @param data - Request context data (logger, requestId, startTime)
|
|
26
|
+
* @param fn - Function to run with context
|
|
27
|
+
* @returns Result of the function
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const result = await runWithRequestContext(
|
|
32
|
+
* { logger, requestId, startTime: Date.now() },
|
|
33
|
+
* async () => {
|
|
34
|
+
* // Inside here, serviceContext.getLogger() returns `logger`
|
|
35
|
+
* // serviceContext.getRequestId() returns `requestId`
|
|
36
|
+
* return await handleRequest();
|
|
37
|
+
* }
|
|
38
|
+
* );
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
declare function runWithRequestContext<T>(data: RequestContextData, fn: () => T | Promise<T>): T | Promise<T>;
|
|
42
|
+
//# sourceMappingURL=context.d.ts.map
|
|
43
|
+
//#endregion
|
|
44
|
+
export { RequestContextData, runWithRequestContext, serviceContext };
|
|
45
|
+
//# sourceMappingURL=context-BVeZOvOd.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-BVeZOvOd.d.cts","names":[],"sources":["../src/context.ts"],"sourcesContent":[],"mappings":";;;;;;;AAQA;AAkBA;AA2DgB,UA7EC,kBAAA,CA6EoB;EAAA,MAAA,EA5E5B,MA4E4B;EAAA,SAC9B,EAAA,MAAA;EAAkB,SACd,EAAA,MAAA;;;;;;AACG;cA9DD,gBAAgB;;;;;;;;;;;;;;;;;;;;;iBA2Db,+BACT,8BACI,IAAI,QAAQ,KACpB,IAAI,QAAQ"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { ServiceContext } from "./types-CcHmCx_U.mjs";
|
|
2
|
+
import { Logger } from "@geekmidas/logger";
|
|
3
|
+
|
|
4
|
+
//#region src/context.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Internal storage for request context data.
|
|
8
|
+
* Not exported - services use ServiceContext interface.
|
|
9
|
+
*/
|
|
10
|
+
interface RequestContextData {
|
|
11
|
+
logger: Logger;
|
|
12
|
+
requestId: string;
|
|
13
|
+
startTime: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* ServiceContext implementation.
|
|
17
|
+
* Singleton that reads from AsyncLocalStorage.
|
|
18
|
+
* Methods throw if called outside a request context (catches bugs early).
|
|
19
|
+
*/
|
|
20
|
+
declare const serviceContext: ServiceContext;
|
|
21
|
+
/**
|
|
22
|
+
* Run a function with request context.
|
|
23
|
+
* Used by endpoint/function/subscriber adaptors.
|
|
24
|
+
*
|
|
25
|
+
* @param data - Request context data (logger, requestId, startTime)
|
|
26
|
+
* @param fn - Function to run with context
|
|
27
|
+
* @returns Result of the function
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const result = await runWithRequestContext(
|
|
32
|
+
* { logger, requestId, startTime: Date.now() },
|
|
33
|
+
* async () => {
|
|
34
|
+
* // Inside here, serviceContext.getLogger() returns `logger`
|
|
35
|
+
* // serviceContext.getRequestId() returns `requestId`
|
|
36
|
+
* return await handleRequest();
|
|
37
|
+
* }
|
|
38
|
+
* );
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
declare function runWithRequestContext<T>(data: RequestContextData, fn: () => T | Promise<T>): T | Promise<T>;
|
|
42
|
+
//# sourceMappingURL=context.d.ts.map
|
|
43
|
+
//#endregion
|
|
44
|
+
export { RequestContextData, runWithRequestContext, serviceContext };
|
|
45
|
+
//# sourceMappingURL=context-DMExgNzl.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-DMExgNzl.d.mts","names":[],"sources":["../src/context.ts"],"sourcesContent":[],"mappings":";;;;;;;AAQA;AAkBA;AA2DgB,UA7EC,kBAAA,CA6EoB;EAAA,MAAA,EA5E5B,MA4E4B;EAAA,SAC9B,EAAA,MAAA;EAAkB,SACd,EAAA,MAAA;;;;;;AACG;cA9DD,gBAAgB;;;;;;;;;;;;;;;;;;;;;iBA2Db,+BACT,8BACI,IAAI,QAAQ,KACpB,IAAI,QAAQ"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
const node_async_hooks = __toESM(require("node:async_hooks"));
|
|
25
|
+
|
|
26
|
+
//#region src/context.ts
|
|
27
|
+
/**
|
|
28
|
+
* Internal AsyncLocalStorage instance for request context.
|
|
29
|
+
* Not exported - use runWithRequestContext() to establish context
|
|
30
|
+
* and serviceContext to access it.
|
|
31
|
+
*/
|
|
32
|
+
const requestContextStorage = new node_async_hooks.AsyncLocalStorage();
|
|
33
|
+
/**
|
|
34
|
+
* ServiceContext implementation.
|
|
35
|
+
* Singleton that reads from AsyncLocalStorage.
|
|
36
|
+
* Methods throw if called outside a request context (catches bugs early).
|
|
37
|
+
*/
|
|
38
|
+
const serviceContext = {
|
|
39
|
+
getLogger() {
|
|
40
|
+
const store = requestContextStorage.getStore();
|
|
41
|
+
if (!store) throw new Error("ServiceContext.getLogger() called outside request context. Ensure code runs within runWithRequestContext().");
|
|
42
|
+
return store.logger;
|
|
43
|
+
},
|
|
44
|
+
getRequestId() {
|
|
45
|
+
const store = requestContextStorage.getStore();
|
|
46
|
+
if (!store) throw new Error("ServiceContext.getRequestId() called outside request context. Ensure code runs within runWithRequestContext().");
|
|
47
|
+
return store.requestId;
|
|
48
|
+
},
|
|
49
|
+
getRequestStartTime() {
|
|
50
|
+
const store = requestContextStorage.getStore();
|
|
51
|
+
if (!store) throw new Error("ServiceContext.getRequestStartTime() called outside request context. Ensure code runs within runWithRequestContext().");
|
|
52
|
+
return store.startTime;
|
|
53
|
+
},
|
|
54
|
+
hasContext() {
|
|
55
|
+
return requestContextStorage.getStore() !== void 0;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Run a function with request context.
|
|
60
|
+
* Used by endpoint/function/subscriber adaptors.
|
|
61
|
+
*
|
|
62
|
+
* @param data - Request context data (logger, requestId, startTime)
|
|
63
|
+
* @param fn - Function to run with context
|
|
64
|
+
* @returns Result of the function
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* const result = await runWithRequestContext(
|
|
69
|
+
* { logger, requestId, startTime: Date.now() },
|
|
70
|
+
* async () => {
|
|
71
|
+
* // Inside here, serviceContext.getLogger() returns `logger`
|
|
72
|
+
* // serviceContext.getRequestId() returns `requestId`
|
|
73
|
+
* return await handleRequest();
|
|
74
|
+
* }
|
|
75
|
+
* );
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
function runWithRequestContext(data, fn) {
|
|
79
|
+
return requestContextStorage.run(data, fn);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
//#endregion
|
|
83
|
+
Object.defineProperty(exports, 'runWithRequestContext', {
|
|
84
|
+
enumerable: true,
|
|
85
|
+
get: function () {
|
|
86
|
+
return runWithRequestContext;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
Object.defineProperty(exports, 'serviceContext', {
|
|
90
|
+
enumerable: true,
|
|
91
|
+
get: function () {
|
|
92
|
+
return serviceContext;
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
//# sourceMappingURL=context-DUTDtYd2.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-DUTDtYd2.cjs","names":["AsyncLocalStorage","serviceContext: ServiceContext","data: RequestContextData","fn: () => T | Promise<T>"],"sources":["../src/context.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\nimport type { Logger } from '@geekmidas/logger';\nimport type { ServiceContext } from './types';\n\n/**\n * Internal storage for request context data.\n * Not exported - services use ServiceContext interface.\n */\nexport interface RequestContextData {\n\tlogger: Logger;\n\trequestId: string;\n\tstartTime: number;\n}\n\n/**\n * Internal AsyncLocalStorage instance for request context.\n * Not exported - use runWithRequestContext() to establish context\n * and serviceContext to access it.\n */\nconst requestContextStorage = new AsyncLocalStorage<RequestContextData>();\n\n/**\n * ServiceContext implementation.\n * Singleton that reads from AsyncLocalStorage.\n * Methods throw if called outside a request context (catches bugs early).\n */\nexport const serviceContext: ServiceContext = {\n\tgetLogger() {\n\t\tconst store = requestContextStorage.getStore();\n\t\tif (!store) {\n\t\t\tthrow new Error(\n\t\t\t\t'ServiceContext.getLogger() called outside request context. ' +\n\t\t\t\t\t'Ensure code runs within runWithRequestContext().',\n\t\t\t);\n\t\t}\n\t\treturn store.logger;\n\t},\n\n\tgetRequestId() {\n\t\tconst store = requestContextStorage.getStore();\n\t\tif (!store) {\n\t\t\tthrow new Error(\n\t\t\t\t'ServiceContext.getRequestId() called outside request context. ' +\n\t\t\t\t\t'Ensure code runs within runWithRequestContext().',\n\t\t\t);\n\t\t}\n\t\treturn store.requestId;\n\t},\n\n\tgetRequestStartTime() {\n\t\tconst store = requestContextStorage.getStore();\n\t\tif (!store) {\n\t\t\tthrow new Error(\n\t\t\t\t'ServiceContext.getRequestStartTime() called outside request context. ' +\n\t\t\t\t\t'Ensure code runs within runWithRequestContext().',\n\t\t\t);\n\t\t}\n\t\treturn store.startTime;\n\t},\n\n\thasContext() {\n\t\treturn requestContextStorage.getStore() !== undefined;\n\t},\n};\n\n/**\n * Run a function with request context.\n * Used by endpoint/function/subscriber adaptors.\n *\n * @param data - Request context data (logger, requestId, startTime)\n * @param fn - Function to run with context\n * @returns Result of the function\n *\n * @example\n * ```typescript\n * const result = await runWithRequestContext(\n * { logger, requestId, startTime: Date.now() },\n * async () => {\n * // Inside here, serviceContext.getLogger() returns `logger`\n * // serviceContext.getRequestId() returns `requestId`\n * return await handleRequest();\n * }\n * );\n * ```\n */\nexport function runWithRequestContext<T>(\n\tdata: RequestContextData,\n\tfn: () => T | Promise<T>,\n): T | Promise<T> {\n\treturn requestContextStorage.run(data, fn);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmBA,MAAM,wBAAwB,IAAIA;;;;;;AAOlC,MAAaC,iBAAiC;CAC7C,YAAY;EACX,MAAM,QAAQ,sBAAsB,UAAU;AAC9C,OAAK,MACJ,OAAM,IAAI,MACT;AAIF,SAAO,MAAM;CACb;CAED,eAAe;EACd,MAAM,QAAQ,sBAAsB,UAAU;AAC9C,OAAK,MACJ,OAAM,IAAI,MACT;AAIF,SAAO,MAAM;CACb;CAED,sBAAsB;EACrB,MAAM,QAAQ,sBAAsB,UAAU;AAC9C,OAAK,MACJ,OAAM,IAAI,MACT;AAIF,SAAO,MAAM;CACb;CAED,aAAa;AACZ,SAAO,sBAAsB,UAAU;CACvC;AACD;;;;;;;;;;;;;;;;;;;;;AAsBD,SAAgB,sBACfC,MACAC,IACiB;AACjB,QAAO,sBAAsB,IAAI,MAAM,GAAG;AAC1C"}
|
package/dist/context.cjs
ADDED
package/dist/context.mjs
ADDED