@wooksjs/event-core 0.6.0 → 0.6.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.
- package/dist/index.cjs +32 -16
- package/dist/index.d.ts +17 -0
- package/dist/index.mjs +21 -4
- package/package.json +3 -4
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
//#region rolldown:runtime
|
|
3
2
|
var __create = Object.create;
|
|
4
3
|
var __defProp = Object.defineProperty;
|
|
@@ -27,6 +26,12 @@ const node_async_hooks = __toESM(require("node:async_hooks"));
|
|
|
27
26
|
const __prostojs_logger = __toESM(require("@prostojs/logger"));
|
|
28
27
|
|
|
29
28
|
//#region packages/event-core/src/context-injector.ts
|
|
29
|
+
/**
|
|
30
|
+
* ContextInjector
|
|
31
|
+
*
|
|
32
|
+
* Provides a way to inject context
|
|
33
|
+
* Usefull when working with opentelemetry spans
|
|
34
|
+
*/
|
|
30
35
|
var ContextInjector = class {
|
|
31
36
|
with(name, attributes, cb) {
|
|
32
37
|
const fn = typeof attributes === "function" ? attributes : cb;
|
|
@@ -54,7 +59,15 @@ function attachHook(target, opts, name) {
|
|
|
54
59
|
|
|
55
60
|
//#endregion
|
|
56
61
|
//#region packages/event-core/src/context.ts
|
|
62
|
+
/**
|
|
63
|
+
* AsyncLocalStorage instance
|
|
64
|
+
*
|
|
65
|
+
* Use on your own risk only if you know what you're doing
|
|
66
|
+
*/
|
|
57
67
|
const asyncStorage = new node_async_hooks.AsyncLocalStorage();
|
|
68
|
+
/**
|
|
69
|
+
* Create Wooks Context
|
|
70
|
+
*/
|
|
58
71
|
function createAsyncEventContext(data) {
|
|
59
72
|
const newContext = { ...data };
|
|
60
73
|
const cc = asyncStorage.getStore();
|
|
@@ -62,6 +75,9 @@ function createAsyncEventContext(data) {
|
|
|
62
75
|
const ci$1 = getContextInjector();
|
|
63
76
|
return (cb) => asyncStorage.run(newContext, () => ci$1.with("Event:start", { eventType: newContext.event.type }, cb));
|
|
64
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Use Wooks Context
|
|
80
|
+
*/
|
|
65
81
|
function useAsyncEventContext(expectedTypes) {
|
|
66
82
|
let cc = asyncStorage.getStore();
|
|
67
83
|
if (!cc) throw new Error("Event context does not exist at this point.");
|
|
@@ -69,7 +85,7 @@ function useAsyncEventContext(expectedTypes) {
|
|
|
69
85
|
const type = cc.event.type;
|
|
70
86
|
const types = typeof expectedTypes === "string" ? [expectedTypes] : expectedTypes;
|
|
71
87
|
if (!types.includes(type)) if (cc.parentCtx?.event.type && types.includes(cc.parentCtx.event.type)) cc = cc.parentCtx;
|
|
72
|
-
else throw new Error(`Event context type mismatch: expected ${types.map((t) => `"${t}"`).join(", ")}, received "${type}"`);
|
|
88
|
+
else throw new Error(`Event context type mismatch: expected ${types.map((t) => `"${t}"`).join(", ")}, received "${type}"`);
|
|
73
89
|
}
|
|
74
90
|
return _getCtxHelpers(cc);
|
|
75
91
|
}
|
|
@@ -120,18 +136,18 @@ function _getCtxHelpers(cc) {
|
|
|
120
136
|
return obj$1;
|
|
121
137
|
}
|
|
122
138
|
function setNested(key2, v) {
|
|
123
|
-
if (obj.value ===
|
|
139
|
+
if (obj.value === void 0) obj.value = {};
|
|
124
140
|
obj.value[key2] = v;
|
|
125
141
|
return v;
|
|
126
142
|
}
|
|
127
143
|
function delNested(key2) {
|
|
128
|
-
setNested(key2,
|
|
144
|
+
setNested(key2, void 0);
|
|
129
145
|
}
|
|
130
146
|
function getNested(key2) {
|
|
131
147
|
return (obj.value || {})[key2];
|
|
132
148
|
}
|
|
133
149
|
function hasNested(key2) {
|
|
134
|
-
return (obj.value || {})[key2] !==
|
|
150
|
+
return (obj.value || {})[key2] !== void 0;
|
|
135
151
|
}
|
|
136
152
|
function entries() {
|
|
137
153
|
return Object.entries(obj.value || {});
|
|
@@ -233,14 +249,14 @@ function useRouteParams() {
|
|
|
233
249
|
}
|
|
234
250
|
|
|
235
251
|
//#endregion
|
|
236
|
-
exports.ContextInjector = ContextInjector
|
|
237
|
-
exports.EventLogger = EventLogger
|
|
238
|
-
exports.asyncStorage = asyncStorage
|
|
239
|
-
exports.attachHook = attachHook
|
|
240
|
-
exports.createAsyncEventContext = createAsyncEventContext
|
|
241
|
-
exports.getContextInjector = getContextInjector
|
|
242
|
-
exports.replaceContextInjector = replaceContextInjector
|
|
243
|
-
exports.useAsyncEventContext = useAsyncEventContext
|
|
244
|
-
exports.useEventId = useEventId
|
|
245
|
-
exports.useEventLogger = useEventLogger
|
|
246
|
-
exports.useRouteParams = useRouteParams
|
|
252
|
+
exports.ContextInjector = ContextInjector;
|
|
253
|
+
exports.EventLogger = EventLogger;
|
|
254
|
+
exports.asyncStorage = asyncStorage;
|
|
255
|
+
exports.attachHook = attachHook;
|
|
256
|
+
exports.createAsyncEventContext = createAsyncEventContext;
|
|
257
|
+
exports.getContextInjector = getContextInjector;
|
|
258
|
+
exports.replaceContextInjector = replaceContextInjector;
|
|
259
|
+
exports.useAsyncEventContext = useAsyncEventContext;
|
|
260
|
+
exports.useEventId = useEventId;
|
|
261
|
+
exports.useEventLogger = useEventLogger;
|
|
262
|
+
exports.useRouteParams = useRouteParams;
|
package/dist/index.d.ts
CHANGED
|
@@ -39,8 +39,19 @@ interface TGenericContextStore<CustomEventType = TEmpty> {
|
|
|
39
39
|
routeParams?: Record<string, string | string[]>;
|
|
40
40
|
_ended?: boolean;
|
|
41
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* AsyncLocalStorage instance
|
|
44
|
+
*
|
|
45
|
+
* Use on your own risk only if you know what you're doing
|
|
46
|
+
*/
|
|
42
47
|
declare const asyncStorage: AsyncLocalStorage<TGenericContextStore>;
|
|
48
|
+
/**
|
|
49
|
+
* Create Wooks Context
|
|
50
|
+
*/
|
|
43
51
|
declare function createAsyncEventContext<S = TEmpty, EventTypeToCreate = TEmpty>(data: S & TGenericContextStore<EventTypeToCreate>): <T>(cb: (...a: any[]) => T) => T;
|
|
52
|
+
/**
|
|
53
|
+
* Use Wooks Context
|
|
54
|
+
*/
|
|
44
55
|
declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTypes?: string | string[]): TCtxHelpers<S & TGenericContextStore<EventType>>;
|
|
45
56
|
interface TCtxHelpers<T> {
|
|
46
57
|
getCtx: () => T;
|
|
@@ -65,6 +76,12 @@ interface TCtxHelpers<T> {
|
|
|
65
76
|
getParentCtx: <T2 = T>() => TCtxHelpers<T2>;
|
|
66
77
|
}
|
|
67
78
|
|
|
79
|
+
/**
|
|
80
|
+
* ContextInjector
|
|
81
|
+
*
|
|
82
|
+
* Provides a way to inject context
|
|
83
|
+
* Usefull when working with opentelemetry spans
|
|
84
|
+
*/
|
|
68
85
|
declare class ContextInjector<N> {
|
|
69
86
|
with<T>(name: N, attributes: Record<string, string | number | boolean>, cb: TContextInjectorCallback<T>): T;
|
|
70
87
|
with<T>(name: N, cb: TContextInjectorCallback<T>): T;
|
package/dist/index.mjs
CHANGED
|
@@ -3,6 +3,12 @@ import { AsyncLocalStorage } from "node:async_hooks";
|
|
|
3
3
|
import { ProstoLogger, coloredConsole, createConsoleTransort } from "@prostojs/logger";
|
|
4
4
|
|
|
5
5
|
//#region packages/event-core/src/context-injector.ts
|
|
6
|
+
/**
|
|
7
|
+
* ContextInjector
|
|
8
|
+
*
|
|
9
|
+
* Provides a way to inject context
|
|
10
|
+
* Usefull when working with opentelemetry spans
|
|
11
|
+
*/
|
|
6
12
|
var ContextInjector = class {
|
|
7
13
|
with(name, attributes, cb) {
|
|
8
14
|
const fn = typeof attributes === "function" ? attributes : cb;
|
|
@@ -30,7 +36,15 @@ function attachHook(target, opts, name) {
|
|
|
30
36
|
|
|
31
37
|
//#endregion
|
|
32
38
|
//#region packages/event-core/src/context.ts
|
|
39
|
+
/**
|
|
40
|
+
* AsyncLocalStorage instance
|
|
41
|
+
*
|
|
42
|
+
* Use on your own risk only if you know what you're doing
|
|
43
|
+
*/
|
|
33
44
|
const asyncStorage = new AsyncLocalStorage();
|
|
45
|
+
/**
|
|
46
|
+
* Create Wooks Context
|
|
47
|
+
*/
|
|
34
48
|
function createAsyncEventContext(data) {
|
|
35
49
|
const newContext = { ...data };
|
|
36
50
|
const cc = asyncStorage.getStore();
|
|
@@ -38,6 +52,9 @@ function createAsyncEventContext(data) {
|
|
|
38
52
|
const ci$1 = getContextInjector();
|
|
39
53
|
return (cb) => asyncStorage.run(newContext, () => ci$1.with("Event:start", { eventType: newContext.event.type }, cb));
|
|
40
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Use Wooks Context
|
|
57
|
+
*/
|
|
41
58
|
function useAsyncEventContext(expectedTypes) {
|
|
42
59
|
let cc = asyncStorage.getStore();
|
|
43
60
|
if (!cc) throw new Error("Event context does not exist at this point.");
|
|
@@ -45,7 +62,7 @@ function useAsyncEventContext(expectedTypes) {
|
|
|
45
62
|
const type = cc.event.type;
|
|
46
63
|
const types = typeof expectedTypes === "string" ? [expectedTypes] : expectedTypes;
|
|
47
64
|
if (!types.includes(type)) if (cc.parentCtx?.event.type && types.includes(cc.parentCtx.event.type)) cc = cc.parentCtx;
|
|
48
|
-
else throw new Error(`Event context type mismatch: expected ${types.map((t) => `"${t}"`).join(", ")}, received "${type}"`);
|
|
65
|
+
else throw new Error(`Event context type mismatch: expected ${types.map((t) => `"${t}"`).join(", ")}, received "${type}"`);
|
|
49
66
|
}
|
|
50
67
|
return _getCtxHelpers(cc);
|
|
51
68
|
}
|
|
@@ -96,18 +113,18 @@ function _getCtxHelpers(cc) {
|
|
|
96
113
|
return obj$1;
|
|
97
114
|
}
|
|
98
115
|
function setNested(key2, v) {
|
|
99
|
-
if (obj.value ===
|
|
116
|
+
if (obj.value === void 0) obj.value = {};
|
|
100
117
|
obj.value[key2] = v;
|
|
101
118
|
return v;
|
|
102
119
|
}
|
|
103
120
|
function delNested(key2) {
|
|
104
|
-
setNested(key2,
|
|
121
|
+
setNested(key2, void 0);
|
|
105
122
|
}
|
|
106
123
|
function getNested(key2) {
|
|
107
124
|
return (obj.value || {})[key2];
|
|
108
125
|
}
|
|
109
126
|
function hasNested(key2) {
|
|
110
|
-
return (obj.value || {})[key2] !==
|
|
127
|
+
return (obj.value || {})[key2] !== void 0;
|
|
111
128
|
}
|
|
112
129
|
function entries() {
|
|
113
130
|
return Object.entries(obj.value || {});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/event-core",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "@wooksjs/event-core",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -43,9 +43,8 @@
|
|
|
43
43
|
},
|
|
44
44
|
"homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/event-core#readme",
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"vitest": "^2.1.8"
|
|
46
|
+
"typescript": "^5.8.3",
|
|
47
|
+
"vitest": "^3.2.4"
|
|
49
48
|
},
|
|
50
49
|
"scripts": {
|
|
51
50
|
"build": "rolldown -c ../../rolldown.config.mjs"
|