@wooksjs/event-core 0.5.5 → 0.5.7
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/index.cjs +15 -85
- package/dist/index.d.ts +13 -41
- package/dist/index.mjs +13 -85
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -4,53 +4,19 @@ var crypto = require('crypto');
|
|
|
4
4
|
var node_async_hooks = require('node:async_hooks');
|
|
5
5
|
var logger = require('@prostojs/logger');
|
|
6
6
|
|
|
7
|
-
class
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
hook(name, cb) {
|
|
12
|
-
if (!this.hooks[name]) {
|
|
13
|
-
this.hooks[name] = [];
|
|
14
|
-
}
|
|
15
|
-
this.hooks[name].push(cb);
|
|
16
|
-
return () => {
|
|
17
|
-
this.unhook(name, cb);
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
callHook(name, ...args) {
|
|
21
|
-
if (this.hooks[name]) {
|
|
22
|
-
for (const cb of this.hooks[name]) {
|
|
23
|
-
try {
|
|
24
|
-
cb(...args);
|
|
25
|
-
}
|
|
26
|
-
catch (error) {
|
|
27
|
-
console.error(`Error in hook ${name}:`, error);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
unhook(name, cb) {
|
|
33
|
-
if (this.hooks[name]) {
|
|
34
|
-
this.hooks[name] = this.hooks[name].filter(hookCb => hookCb !== cb);
|
|
35
|
-
}
|
|
7
|
+
class ContextInjector {
|
|
8
|
+
with(name, attributes, cb) {
|
|
9
|
+
const fn = typeof attributes === 'function' ? attributes : cb;
|
|
10
|
+
return fn();
|
|
36
11
|
}
|
|
37
12
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
this.callHook('end-event', eventType, abortReason);
|
|
45
|
-
}
|
|
46
|
-
onStartEvent(cb) {
|
|
47
|
-
return this.hook('start-event', cb);
|
|
48
|
-
}
|
|
49
|
-
onEndEvent(cb) {
|
|
50
|
-
return this.hook('end-event', cb);
|
|
51
|
-
}
|
|
13
|
+
let ci = new ContextInjector();
|
|
14
|
+
function getContextInjector() {
|
|
15
|
+
return ci;
|
|
16
|
+
}
|
|
17
|
+
function replaceContextInjector(newCi) {
|
|
18
|
+
ci = newCi;
|
|
52
19
|
}
|
|
53
|
-
const eventContextHooks = new EventContextHooks();
|
|
54
20
|
|
|
55
21
|
function attachHook(target, opts, name) {
|
|
56
22
|
Object.defineProperty(target, name || 'value', {
|
|
@@ -63,40 +29,8 @@ function attachHook(target, opts, name) {
|
|
|
63
29
|
const asyncStorage = new node_async_hooks.AsyncLocalStorage();
|
|
64
30
|
function createAsyncEventContext(data) {
|
|
65
31
|
const newContext = { ...data };
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
eventContextHooks.fireStartEvent(newContext.event.type);
|
|
69
|
-
});
|
|
70
|
-
const result = asyncStorage.run(newContext, cb);
|
|
71
|
-
if (result instanceof Promise) {
|
|
72
|
-
result
|
|
73
|
-
.then(r => {
|
|
74
|
-
endEvent(r);
|
|
75
|
-
return r;
|
|
76
|
-
})
|
|
77
|
-
.catch(error => {
|
|
78
|
-
endEvent(error);
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
endEvent(result);
|
|
83
|
-
}
|
|
84
|
-
function endEvent(output) {
|
|
85
|
-
if (!newContext._ended) {
|
|
86
|
-
if (output instanceof Error) {
|
|
87
|
-
asyncStorage.run(newContext, () => {
|
|
88
|
-
eventContextHooks.fireEndEvent(newContext.event.type, output.message);
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
asyncStorage.run(newContext, () => {
|
|
93
|
-
eventContextHooks.fireEndEvent(newContext.event.type);
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return result;
|
|
99
|
-
};
|
|
32
|
+
const ci = getContextInjector();
|
|
33
|
+
return (cb) => asyncStorage.run(newContext, () => ci.with(`${newContext.event.type} event`, cb));
|
|
100
34
|
}
|
|
101
35
|
function useAsyncEventContext(expectedTypes) {
|
|
102
36
|
let cc = asyncStorage.getStore();
|
|
@@ -194,12 +128,6 @@ function _getCtxHelpers(cc) {
|
|
|
194
128
|
const hasParentCtx = () => !!cc.parentCtx;
|
|
195
129
|
return {
|
|
196
130
|
getCtx,
|
|
197
|
-
endEvent: (abortReason) => {
|
|
198
|
-
if (cc && !cc._ended) {
|
|
199
|
-
cc._ended = true;
|
|
200
|
-
eventContextHooks.fireEndEvent(cc.event.type, abortReason);
|
|
201
|
-
}
|
|
202
|
-
},
|
|
203
131
|
store,
|
|
204
132
|
getStore: get,
|
|
205
133
|
setStore: set,
|
|
@@ -266,11 +194,13 @@ function useRouteParams() {
|
|
|
266
194
|
};
|
|
267
195
|
}
|
|
268
196
|
|
|
197
|
+
exports.ContextInjector = ContextInjector;
|
|
269
198
|
exports.EventLogger = EventLogger;
|
|
270
199
|
exports.asyncStorage = asyncStorage;
|
|
271
200
|
exports.attachHook = attachHook;
|
|
272
201
|
exports.createAsyncEventContext = createAsyncEventContext;
|
|
273
|
-
exports.
|
|
202
|
+
exports.getContextInjector = getContextInjector;
|
|
203
|
+
exports.replaceContextInjector = replaceContextInjector;
|
|
274
204
|
exports.useAsyncEventContext = useAsyncEventContext;
|
|
275
205
|
exports.useEventId = useEventId;
|
|
276
206
|
exports.useEventLogger = useEventLogger;
|
package/dist/index.d.ts
CHANGED
|
@@ -35,7 +35,6 @@ declare const asyncStorage: AsyncLocalStorage<unknown>;
|
|
|
35
35
|
declare function createAsyncEventContext<S = TEmpty, EventTypeToCreate = TEmpty>(data: S & TGenericContextStore<EventTypeToCreate>): <T>(cb: (...a: any[]) => T) => T;
|
|
36
36
|
declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTypes?: string | string[]): {
|
|
37
37
|
getCtx: () => S & TGenericContextStore<EventType>;
|
|
38
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
39
38
|
store: <K extends keyof S | keyof TGenericContextStore<EventType>>(key: K) => {
|
|
40
39
|
value: (S & TGenericContextStore<EventType>)[K];
|
|
41
40
|
hook: <K2 extends keyof Required<S & TGenericContextStore<EventType>>[K]>(key2: K2) => {
|
|
@@ -56,7 +55,6 @@ declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTy
|
|
|
56
55
|
hasParentCtx: () => boolean;
|
|
57
56
|
getParentCtx: <T2 = S & TGenericContextStore<EventType>>() => {
|
|
58
57
|
getCtx: () => T2;
|
|
59
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
60
58
|
store: <K_3 extends keyof T2>(key: K_3) => {
|
|
61
59
|
value: T2[K_3];
|
|
62
60
|
hook: <K2_6 extends keyof Required<T2>[K_3]>(key2: K2_6) => {
|
|
@@ -77,7 +75,6 @@ declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTy
|
|
|
77
75
|
hasParentCtx: () => boolean;
|
|
78
76
|
getParentCtx: <T2_1 = T2>() => {
|
|
79
77
|
getCtx: () => T2_1;
|
|
80
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
81
78
|
store: <K_6 extends keyof T2_1>(key: K_6) => {
|
|
82
79
|
value: T2_1[K_6];
|
|
83
80
|
hook: <K2_12 extends keyof Required<T2_1>[K_6]>(key2: K2_12) => {
|
|
@@ -98,7 +95,6 @@ declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTy
|
|
|
98
95
|
hasParentCtx: () => boolean;
|
|
99
96
|
getParentCtx: <T2_2 = T2_1>() => {
|
|
100
97
|
getCtx: () => T2_2;
|
|
101
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
102
98
|
store: <K_9 extends keyof T2_2>(key: K_9) => {
|
|
103
99
|
value: T2_2[K_9];
|
|
104
100
|
hook: <K2_18 extends keyof Required<T2_2>[K_9]>(key2: K2_18) => {
|
|
@@ -119,7 +115,6 @@ declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTy
|
|
|
119
115
|
hasParentCtx: () => boolean;
|
|
120
116
|
getParentCtx: <T2_3 = T2_2>() => {
|
|
121
117
|
getCtx: () => T2_3;
|
|
122
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
123
118
|
store: <K_12 extends keyof T2_3>(key: K_12) => {
|
|
124
119
|
value: T2_3[K_12];
|
|
125
120
|
hook: <K2_24 extends keyof Required<T2_3>[K_12]>(key2: K2_24) => {
|
|
@@ -140,7 +135,6 @@ declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTy
|
|
|
140
135
|
hasParentCtx: () => boolean;
|
|
141
136
|
getParentCtx: <T2_4 = T2_3>() => {
|
|
142
137
|
getCtx: () => T2_4;
|
|
143
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
144
138
|
store: <K_15 extends keyof T2_4>(key: K_15) => {
|
|
145
139
|
value: T2_4[K_15];
|
|
146
140
|
hook: <K2_30 extends keyof Required<T2_4>[K_15]>(key2: K2_30) => {
|
|
@@ -161,7 +155,6 @@ declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTy
|
|
|
161
155
|
hasParentCtx: () => boolean;
|
|
162
156
|
getParentCtx: <T2_5 = T2_4>() => {
|
|
163
157
|
getCtx: () => T2_5;
|
|
164
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
165
158
|
store: <K_18 extends keyof T2_5>(key: K_18) => {
|
|
166
159
|
value: T2_5[K_18];
|
|
167
160
|
hook: <K2_36 extends keyof Required<T2_5>[K_18]>(key2: K2_36) => {
|
|
@@ -182,7 +175,6 @@ declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTy
|
|
|
182
175
|
hasParentCtx: () => boolean;
|
|
183
176
|
getParentCtx: <T2_6 = T2_5>() => {
|
|
184
177
|
getCtx: () => T2_6;
|
|
185
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
186
178
|
store: <K_21 extends keyof T2_6>(key: K_21) => {
|
|
187
179
|
value: T2_6[K_21];
|
|
188
180
|
hook: <K2_42 extends keyof Required<T2_6>[K_21]>(key2: K2_42) => {
|
|
@@ -203,7 +195,6 @@ declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTy
|
|
|
203
195
|
hasParentCtx: () => boolean;
|
|
204
196
|
getParentCtx: <T2_7 = T2_6>() => {
|
|
205
197
|
getCtx: () => T2_7;
|
|
206
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
207
198
|
store: <K_24 extends keyof T2_7>(key: K_24) => {
|
|
208
199
|
value: T2_7[K_24];
|
|
209
200
|
hook: <K2_48 extends keyof Required<T2_7>[K_24]>(key2: K2_48) => {
|
|
@@ -224,7 +215,6 @@ declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTy
|
|
|
224
215
|
hasParentCtx: () => boolean;
|
|
225
216
|
getParentCtx: <T2_8 = T2_7>() => {
|
|
226
217
|
getCtx: () => T2_8;
|
|
227
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
228
218
|
store: <K_27 extends keyof T2_8>(key: K_27) => {
|
|
229
219
|
value: T2_8[K_27];
|
|
230
220
|
hook: <K2_54 extends keyof Required<T2_8>[K_27]>(key2: K2_54) => {
|
|
@@ -245,7 +235,6 @@ declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTy
|
|
|
245
235
|
hasParentCtx: () => boolean;
|
|
246
236
|
getParentCtx: <T2_9 = T2_8>() => {
|
|
247
237
|
getCtx: () => T2_9;
|
|
248
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
249
238
|
store: <K_30 extends keyof T2_9>(key: K_30) => {
|
|
250
239
|
value: T2_9[K_30];
|
|
251
240
|
hook: <K2_60 extends keyof Required<T2_9>[K_30]>(key2: K2_60) => {
|
|
@@ -291,36 +280,19 @@ declare function useRouteParams<T extends object = Record<string, string | strin
|
|
|
291
280
|
get: <K extends keyof T>(name: K) => T[K];
|
|
292
281
|
};
|
|
293
282
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* Calls all callbacks registered to the specified hook name.
|
|
305
|
-
* @param name - The name of the hook.
|
|
306
|
-
* @param args - The arguments to pass to each callback.
|
|
307
|
-
*/
|
|
308
|
-
callHook(name: string, ...args: any[]): void;
|
|
309
|
-
/**
|
|
310
|
-
* Unregisters a specific callback from a hook name.
|
|
311
|
-
* @param name - The name of the hook.
|
|
312
|
-
* @param cb - The callback to unregister.
|
|
313
|
-
*/
|
|
314
|
-
unhook(name: string, cb: HookCallback): void;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
declare class EventContextHooks extends Hookable {
|
|
318
|
-
fireStartEvent(eventType: string): void;
|
|
319
|
-
fireEndEvent(eventType: string, abortReason?: string): void;
|
|
320
|
-
onStartEvent(cb: (eventType: string) => void): () => void;
|
|
321
|
-
onEndEvent(cb: (eventType: string, abortReason?: string) => void): () => void;
|
|
283
|
+
/**
|
|
284
|
+
* ContextInjector
|
|
285
|
+
*
|
|
286
|
+
* Provides a way to inject context
|
|
287
|
+
* Usefull when working with opentelemetry spans
|
|
288
|
+
*/
|
|
289
|
+
declare class ContextInjector {
|
|
290
|
+
with<T>(name: string, attributes: Record<string, string | number | boolean>, cb: TContextInjectorCallback<T>): T;
|
|
291
|
+
with<T>(name: string, cb: TContextInjectorCallback<T>): T;
|
|
322
292
|
}
|
|
323
|
-
|
|
293
|
+
type TContextInjectorCallback<T> = () => T;
|
|
294
|
+
declare function getContextInjector(): ContextInjector;
|
|
295
|
+
declare function replaceContextInjector(newCi: ContextInjector): void;
|
|
324
296
|
|
|
325
297
|
declare function attachHook<V = unknown, T extends object | Function = object, P extends PropertyKey = 'value'>(target: T, opts: {
|
|
326
298
|
get: () => V | undefined;
|
|
@@ -330,4 +302,4 @@ type THook<T = string, K extends PropertyKey = 'value'> = {
|
|
|
330
302
|
[key in K]: T;
|
|
331
303
|
};
|
|
332
304
|
|
|
333
|
-
export { EventLogger, type TEmpty, type TEventLoggerData, type TEventOptions, type TGenericContextStore, type TGenericEvent, type THook, asyncStorage, attachHook, createAsyncEventContext,
|
|
305
|
+
export { ContextInjector, EventLogger, type TEmpty, type TEventLoggerData, type TEventOptions, type TGenericContextStore, type TGenericEvent, type THook, asyncStorage, attachHook, createAsyncEventContext, getContextInjector, replaceContextInjector, useAsyncEventContext, useEventId, useEventLogger, useRouteParams };
|
package/dist/index.mjs
CHANGED
|
@@ -2,53 +2,19 @@ import { randomUUID } from 'crypto';
|
|
|
2
2
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
3
|
import { ProstoLogger, createConsoleTransort, coloredConsole } from '@prostojs/logger';
|
|
4
4
|
|
|
5
|
-
class
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
hook(name, cb) {
|
|
10
|
-
if (!this.hooks[name]) {
|
|
11
|
-
this.hooks[name] = [];
|
|
12
|
-
}
|
|
13
|
-
this.hooks[name].push(cb);
|
|
14
|
-
return () => {
|
|
15
|
-
this.unhook(name, cb);
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
callHook(name, ...args) {
|
|
19
|
-
if (this.hooks[name]) {
|
|
20
|
-
for (const cb of this.hooks[name]) {
|
|
21
|
-
try {
|
|
22
|
-
cb(...args);
|
|
23
|
-
}
|
|
24
|
-
catch (error) {
|
|
25
|
-
console.error(`Error in hook ${name}:`, error);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
unhook(name, cb) {
|
|
31
|
-
if (this.hooks[name]) {
|
|
32
|
-
this.hooks[name] = this.hooks[name].filter(hookCb => hookCb !== cb);
|
|
33
|
-
}
|
|
5
|
+
class ContextInjector {
|
|
6
|
+
with(name, attributes, cb) {
|
|
7
|
+
const fn = typeof attributes === 'function' ? attributes : cb;
|
|
8
|
+
return fn();
|
|
34
9
|
}
|
|
35
10
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
this.callHook('end-event', eventType, abortReason);
|
|
43
|
-
}
|
|
44
|
-
onStartEvent(cb) {
|
|
45
|
-
return this.hook('start-event', cb);
|
|
46
|
-
}
|
|
47
|
-
onEndEvent(cb) {
|
|
48
|
-
return this.hook('end-event', cb);
|
|
49
|
-
}
|
|
11
|
+
let ci = new ContextInjector();
|
|
12
|
+
function getContextInjector() {
|
|
13
|
+
return ci;
|
|
14
|
+
}
|
|
15
|
+
function replaceContextInjector(newCi) {
|
|
16
|
+
ci = newCi;
|
|
50
17
|
}
|
|
51
|
-
const eventContextHooks = new EventContextHooks();
|
|
52
18
|
|
|
53
19
|
function attachHook(target, opts, name) {
|
|
54
20
|
Object.defineProperty(target, name || 'value', {
|
|
@@ -61,40 +27,8 @@ function attachHook(target, opts, name) {
|
|
|
61
27
|
const asyncStorage = new AsyncLocalStorage();
|
|
62
28
|
function createAsyncEventContext(data) {
|
|
63
29
|
const newContext = { ...data };
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
eventContextHooks.fireStartEvent(newContext.event.type);
|
|
67
|
-
});
|
|
68
|
-
const result = asyncStorage.run(newContext, cb);
|
|
69
|
-
if (result instanceof Promise) {
|
|
70
|
-
result
|
|
71
|
-
.then(r => {
|
|
72
|
-
endEvent(r);
|
|
73
|
-
return r;
|
|
74
|
-
})
|
|
75
|
-
.catch(error => {
|
|
76
|
-
endEvent(error);
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
endEvent(result);
|
|
81
|
-
}
|
|
82
|
-
function endEvent(output) {
|
|
83
|
-
if (!newContext._ended) {
|
|
84
|
-
if (output instanceof Error) {
|
|
85
|
-
asyncStorage.run(newContext, () => {
|
|
86
|
-
eventContextHooks.fireEndEvent(newContext.event.type, output.message);
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
asyncStorage.run(newContext, () => {
|
|
91
|
-
eventContextHooks.fireEndEvent(newContext.event.type);
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return result;
|
|
97
|
-
};
|
|
30
|
+
const ci = getContextInjector();
|
|
31
|
+
return (cb) => asyncStorage.run(newContext, () => ci.with(`${newContext.event.type} event`, cb));
|
|
98
32
|
}
|
|
99
33
|
function useAsyncEventContext(expectedTypes) {
|
|
100
34
|
let cc = asyncStorage.getStore();
|
|
@@ -192,12 +126,6 @@ function _getCtxHelpers(cc) {
|
|
|
192
126
|
const hasParentCtx = () => !!cc.parentCtx;
|
|
193
127
|
return {
|
|
194
128
|
getCtx,
|
|
195
|
-
endEvent: (abortReason) => {
|
|
196
|
-
if (cc && !cc._ended) {
|
|
197
|
-
cc._ended = true;
|
|
198
|
-
eventContextHooks.fireEndEvent(cc.event.type, abortReason);
|
|
199
|
-
}
|
|
200
|
-
},
|
|
201
129
|
store,
|
|
202
130
|
getStore: get,
|
|
203
131
|
setStore: set,
|
|
@@ -264,4 +192,4 @@ function useRouteParams() {
|
|
|
264
192
|
};
|
|
265
193
|
}
|
|
266
194
|
|
|
267
|
-
export { EventLogger, asyncStorage, attachHook, createAsyncEventContext,
|
|
195
|
+
export { ContextInjector, EventLogger, asyncStorage, attachHook, createAsyncEventContext, getContextInjector, replaceContextInjector, useAsyncEventContext, useEventId, useEventLogger, useRouteParams };
|