@wooksjs/event-core 0.5.6 → 0.5.8
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 -80
- package/dist/index.d.ts +13 -42
- package/dist/index.mjs +13 -79
- 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
|
-
fireEndEvent(r);
|
|
75
|
-
return r;
|
|
76
|
-
})
|
|
77
|
-
.catch(error => {
|
|
78
|
-
fireEndEvent(error);
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
fireEndEvent(result);
|
|
83
|
-
}
|
|
84
|
-
function fireEndEvent(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('Event:start', { eventType: newContext.event.type }, cb));
|
|
100
34
|
}
|
|
101
35
|
function useAsyncEventContext(expectedTypes) {
|
|
102
36
|
let cc = asyncStorage.getStore();
|
|
@@ -260,12 +194,13 @@ function useRouteParams() {
|
|
|
260
194
|
};
|
|
261
195
|
}
|
|
262
196
|
|
|
197
|
+
exports.ContextInjector = ContextInjector;
|
|
263
198
|
exports.EventLogger = EventLogger;
|
|
264
|
-
exports.Hookable = Hookable;
|
|
265
199
|
exports.asyncStorage = asyncStorage;
|
|
266
200
|
exports.attachHook = attachHook;
|
|
267
201
|
exports.createAsyncEventContext = createAsyncEventContext;
|
|
268
|
-
exports.
|
|
202
|
+
exports.getContextInjector = getContextInjector;
|
|
203
|
+
exports.replaceContextInjector = replaceContextInjector;
|
|
269
204
|
exports.useAsyncEventContext = useAsyncEventContext;
|
|
270
205
|
exports.useEventId = useEventId;
|
|
271
206
|
exports.useEventLogger = useEventLogger;
|
package/dist/index.d.ts
CHANGED
|
@@ -280,48 +280,19 @@ declare function useRouteParams<T extends object = Record<string, string | strin
|
|
|
280
280
|
get: <K extends keyof T>(name: K) => T[K];
|
|
281
281
|
};
|
|
282
282
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* Calls all callbacks registered to the specified hook name.
|
|
294
|
-
* @param name - The name of the hook.
|
|
295
|
-
* @param args - The arguments to pass to each callback.
|
|
296
|
-
*/
|
|
297
|
-
callHook(name: string, ...args: any[]): void;
|
|
298
|
-
/**
|
|
299
|
-
* Unregisters a specific callback from a hook name.
|
|
300
|
-
* @param name - The name of the hook.
|
|
301
|
-
* @param cb - The callback to unregister.
|
|
302
|
-
*/
|
|
303
|
-
unhook(name: string, cb: HookCallback): void;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
declare class EventContextHooks extends Hookable {
|
|
307
|
-
/**
|
|
308
|
-
* SHould be fired right after a new context is created
|
|
309
|
-
*/
|
|
310
|
-
fireStartEvent(eventType: string): void;
|
|
311
|
-
/**
|
|
312
|
-
* Should be fired at the very end of event processing
|
|
313
|
-
*/
|
|
314
|
-
fireEndEvent(eventType: string, abortReason?: string): void;
|
|
315
|
-
/**
|
|
316
|
-
* Fires when a new context was just created
|
|
317
|
-
*/
|
|
318
|
-
onStartEvent(cb: (eventType: string) => void): () => void;
|
|
319
|
-
/**
|
|
320
|
-
* Fires when event was processed
|
|
321
|
-
*/
|
|
322
|
-
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<N> {
|
|
290
|
+
with<T>(name: N, attributes: Record<string, string | number | boolean>, cb: TContextInjectorCallback<T>): T;
|
|
291
|
+
with<T>(name: N, cb: TContextInjectorCallback<T>): T;
|
|
323
292
|
}
|
|
324
|
-
|
|
293
|
+
type TContextInjectorCallback<T> = () => T;
|
|
294
|
+
declare function getContextInjector<N = 'Event:start'>(): ContextInjector<N>;
|
|
295
|
+
declare function replaceContextInjector(newCi: ContextInjector<string>): void;
|
|
325
296
|
|
|
326
297
|
declare function attachHook<V = unknown, T extends object | Function = object, P extends PropertyKey = 'value'>(target: T, opts: {
|
|
327
298
|
get: () => V | undefined;
|
|
@@ -331,4 +302,4 @@ type THook<T = string, K extends PropertyKey = 'value'> = {
|
|
|
331
302
|
[key in K]: T;
|
|
332
303
|
};
|
|
333
304
|
|
|
334
|
-
export {
|
|
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
|
-
fireEndEvent(r);
|
|
73
|
-
return r;
|
|
74
|
-
})
|
|
75
|
-
.catch(error => {
|
|
76
|
-
fireEndEvent(error);
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
fireEndEvent(result);
|
|
81
|
-
}
|
|
82
|
-
function fireEndEvent(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('Event:start', { eventType: newContext.event.type }, cb));
|
|
98
32
|
}
|
|
99
33
|
function useAsyncEventContext(expectedTypes) {
|
|
100
34
|
let cc = asyncStorage.getStore();
|
|
@@ -258,4 +192,4 @@ function useRouteParams() {
|
|
|
258
192
|
};
|
|
259
193
|
}
|
|
260
194
|
|
|
261
|
-
export {
|
|
195
|
+
export { ContextInjector, EventLogger, asyncStorage, attachHook, createAsyncEventContext, getContextInjector, replaceContextInjector, useAsyncEventContext, useEventId, useEventLogger, useRouteParams };
|