This repository was archived by the owner on Nov 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathContinuationLocalStorage.ts
303 lines (279 loc) · 7.69 KB
/
ContinuationLocalStorage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* eslint-disable @typescript-eslint/no-explicit-any */
import asyncHooks = require('async_hooks');
interface HookFuncs {
init(id: number, type: string, triggerId: number): void;
before(id: number): void;
after(id: number): void;
destroy(id: number): void;
}
interface HookInstance {
enable(): void;
disable(): void;
}
const nodeproc: any = process;
const ROOT_ID = 1;
interface HookInfo<T> {
id: number;
type: string;
triggerId: number;
triggerHook?: HookInfo<T>; // always defined, except for the root node
oriTriggerId?: number;
activated: boolean;
data?: T;
}
/**
* @deprecated deprecated in favour of AsyncLocalStorage
*
* please see https://nodejs.org/api/async_context.html#async_context_new_asynclocalstorage
* @example
* class ContinuationLocalStorage<T> extends AsyncLocalStorage<T> {
* public getContext(): T | undefined {
* return this.getStore();
* }
* public setContext(value: T): T {
* this.enterWith(value);
* return value;
* }
* }
*/
export class ContinuationLocalStorage<T> {
private _currId!: number;
public get currId(): number {
return this._currId;
}
private idHookMap!: Map<number, HookInfo<T>>;
private readonly hookFuncs: HookFuncs;
private readonly hookInstance: HookInstance;
/**
* Creates an instance of ContinuationLocalStorage.
*
*/
public constructor() {
this.initMap();
this.hookFuncs = {
init: (id, type, triggerId) => {
// a new async handle gets initialized:
const oriTriggerId = triggerId;
/* istanbul ignore if */
if (triggerId == null) {
// NOTES: this should not happen
nodeproc._rawDebug(`init: id: ${id}: WARNING: triggerId is not defined`);
triggerId = this._currId;
}
let triggerHook = this.idHookMap.get(triggerId);
if (!triggerHook) {
// NOTES: this is expected
// nodeproc._rawDebug(`init: id: ${id}: WARNING: triggerId: ${triggerId} is not registered`);
triggerId = ROOT_ID;
triggerHook = this.idHookMap.get(triggerId);
}
this.idHookMap.set(id, {
id,
type,
triggerId,
oriTriggerId,
triggerHook,
activated: false,
});
// this.debugId('init', id);
},
before: (id) => {
// an async handle starts
this._currId = id;
const hi = this.idHookMap.get(id);
/* istanbul ignore else */
if (hi) {
if (!hi.activated) {
const ancestor = this.findActivatedNode(hi.triggerHook as HookInfo<T>);
/* istanbul ignore else */
if (ancestor) {
hi.triggerHook = ancestor;
hi.triggerId = ancestor.id;
hi.data = ancestor.data;
}
}
hi.activated = true;
hi.triggerHook = undefined;
} else {
// since node 11 this seems to be not required anymore:
this._currId = ROOT_ID;
}
// this.debugId('before', id);
},
after: (id) => {
// an async handle ends
if (id === this._currId) {
this._currId = ROOT_ID;
}
// this.debugId('after', id);
},
destroy: (id) => {
// an async handle gets destroyed
// this.debugId('destroy', id);
if (this.idHookMap.has(id)) {
/* istanbul ignore if */
if (id === this._currId) {
// NOTES: this should not happen
nodeproc._rawDebug(
`asyncctx: destroy hook called for current context (id: ${this.currId})!`,
);
}
this.idHookMap.delete(id);
}
},
};
this.hookInstance = asyncHooks.createHook(this.hookFuncs) as HookInstance;
this.enable();
}
/**
* Get the current execution context data
*
* @returns {(T|undefined)}
*/
public getContext(): T | undefined {
const hi = this.idHookMap.get(this.currId);
return hi ? hi.data : undefined;
}
/**
* Set the current execution context data
*
* @param {T} value
* @returns {(T)}
*/
public setContext(value: T): T {
const hi = this.idHookMap.get(this.currId);
/* istanbul ignore if */
if (!hi) {
throw new Error('setContext must be called in an async context!');
}
hi.data = value;
return value;
}
/**
* Get the root execution context data
*
* @returns {(T|undefined)}
*/
public getRootContext(): T | undefined {
const hi = this.idHookMap.get(ROOT_ID);
/* istanbul ignore if */
if (!hi) {
// NOTES: this should not happen
throw new Error('internal error: root node not found (1)!');
}
return hi ? hi.data : undefined;
}
/**
* Set the root execution context data
*
* @param {T} value
* @returns {(T)}
*/
public setRootContext(value: T): T {
const hi = this.idHookMap.get(ROOT_ID);
/* istanbul ignore if */
if (!hi) {
// NOTES: this should not happen
throw new Error('internal error: root node not found (2)!');
}
hi.data = value;
return value;
}
/**
* Get the id of the caller for debugging purpose
*
* @param {number} [id=this.currId]
* @returns {(number|undefined)}
*/
/* istanbul ignore next */
public getTriggerId(id: number = this.currId): number | undefined {
const hi = this.idHookMap.get(id);
return hi ? hi.triggerId : undefined;
}
/**
* Get the hook info of the caller for testing purposes
*
* @param {number} [id=this.currId]
* @returns {(number|undefined)}
*/
/* istanbul ignore next */
public getHookInfo(id: number = this.currId): HookInfo<T> | undefined {
const hi = this.idHookMap.get(id);
return hi;
}
/**
* debug output for debugging purpose
*
* @param {string} prefix
* @param {number} [id=this.currId]
*/
/* istanbul ignore next */
public debugId(prefix: string, id: number = this.currId): void {
const hi = this.idHookMap.get(id);
if (hi) {
let data = 'undefined';
const oriTriggerId = hi.oriTriggerId ? hi.oriTriggerId : 1;
if (hi.data) {
if (typeof hi.data === 'string') {
data = hi.data;
} else {
try {
if ((hi.data as any).toString) {
data = (hi.data as any).toString();
} else {
data = JSON.stringify(hi.data);
}
// eslint-disable-next-line no-empty
} catch (_ignore) {}
}
}
nodeproc._rawDebug(
`${prefix}: id: ${id} type: '${hi.type}' triggerId: ${oriTriggerId} data: ${data} for id: ${hi.triggerId}))`,
);
} else {
nodeproc._rawDebug(`${prefix}: id: ${id}`);
}
}
/**
* clean up
*/
public dispose(): void {
this.disable();
this.idHookMap.clear();
}
/**
* enable
*
*/
public enable(): void {
this.initMap(this.getRootContext());
this.hookInstance.enable();
}
/**
* disable
*
*/
public disable(): void {
this.hookInstance.disable();
}
protected initMap(value?: T): void {
this.idHookMap = new Map<number, HookInfo<T>>();
this.idHookMap.set(ROOT_ID, { id: ROOT_ID, type: 'C++', triggerId: 0, activated: true });
this._currId = ROOT_ID;
if (value) {
this.setRootContext(value);
}
}
private readonly findActivatedNode = (hi: HookInfo<T>): HookInfo<T> => {
/* istanbul ignore if */
if (!hi) {
// NOTES: this should not happen
// the root-node is always activated and all other nodes should have a valid trigger-node (`triggerHook`)
return this.idHookMap.get(ROOT_ID) as HookInfo<T>;
}
if (hi.activated) {
return hi;
}
return this.findActivatedNode(hi.triggerHook as HookInfo<T>);
};
}