forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_hooks.js
More file actions
286 lines (233 loc) · 8.23 KB
/
async_hooks.js
File metadata and controls
286 lines (233 loc) · 8.23 KB
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
'use strict';
const errors = require('internal/errors');
const internalUtil = require('internal/util');
const async_wrap = process.binding('async_wrap');
const internal_async_hooks = require('internal/async_hooks');
// Get functions
// Only used to support a deprecated API. pushAsyncIds, popAsyncIds should
// never be directly in this manner.
const { pushAsyncIds, popAsyncIds } = async_wrap;
// For userland AsyncResources, make sure to emit a destroy event when the
// resource gets gced.
const { registerDestroyHook } = async_wrap;
const {
// Private API
getHookArrays,
enableHooks,
disableHooks,
// Sensitive Embedder API
newUid,
initTriggerId,
setInitTriggerId,
emitInit,
emitBefore,
emitAfter,
emitDestroy,
} = internal_async_hooks;
// Get fields
const { async_id_fields } = async_wrap;
// Get symbols
const {
init_symbol, before_symbol, after_symbol, destroy_symbol,
promise_resolve_symbol
} = internal_async_hooks.symbols;
const { async_id_symbol, trigger_async_id_symbol } = async_wrap;
// Get constants
const {
kInit, kBefore, kAfter, kDestroy, kTotals, kPromiseResolve,
kExecutionAsyncId, kTriggerAsyncId
} = async_wrap.constants;
// Listener API //
class AsyncHook {
constructor({ init, before, after, destroy, promiseResolve }) {
if (init !== undefined && typeof init !== 'function')
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'init');
if (before !== undefined && typeof before !== 'function')
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'before');
if (after !== undefined && typeof after !== 'function')
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'before');
if (destroy !== undefined && typeof destroy !== 'function')
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'before');
if (promiseResolve !== undefined && typeof promiseResolve !== 'function')
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'promiseResolve');
this[init_symbol] = init;
this[before_symbol] = before;
this[after_symbol] = after;
this[destroy_symbol] = destroy;
this[promise_resolve_symbol] = promiseResolve;
}
enable() {
// The set of callbacks for a hook should be the same regardless of whether
// enable()/disable() are run during their execution. The following
// references are reassigned to the tmp arrays if a hook is currently being
// processed.
const [hooks_array, hook_fields] = getHookArrays();
// Each hook is only allowed to be added once.
if (hooks_array.includes(this))
return this;
const prev_kTotals = hook_fields[kTotals];
hook_fields[kTotals] = 0;
// createHook() has already enforced that the callbacks are all functions,
// so here simply increment the count of whether each callbacks exists or
// not.
hook_fields[kTotals] += hook_fields[kInit] += +!!this[init_symbol];
hook_fields[kTotals] += hook_fields[kBefore] += +!!this[before_symbol];
hook_fields[kTotals] += hook_fields[kAfter] += +!!this[after_symbol];
hook_fields[kTotals] += hook_fields[kDestroy] += +!!this[destroy_symbol];
hook_fields[kTotals] +=
hook_fields[kPromiseResolve] += +!!this[promise_resolve_symbol];
hooks_array.push(this);
if (prev_kTotals === 0 && hook_fields[kTotals] > 0) {
enableHooks();
}
return this;
}
disable() {
const [hooks_array, hook_fields] = getHookArrays();
const index = hooks_array.indexOf(this);
if (index === -1)
return this;
const prev_kTotals = hook_fields[kTotals];
hook_fields[kTotals] = 0;
hook_fields[kTotals] += hook_fields[kInit] -= +!!this[init_symbol];
hook_fields[kTotals] += hook_fields[kBefore] -= +!!this[before_symbol];
hook_fields[kTotals] += hook_fields[kAfter] -= +!!this[after_symbol];
hook_fields[kTotals] += hook_fields[kDestroy] -= +!!this[destroy_symbol];
hook_fields[kTotals] +=
hook_fields[kPromiseResolve] -= +!!this[promise_resolve_symbol];
hooks_array.splice(index, 1);
if (prev_kTotals > 0 && hook_fields[kTotals] === 0) {
disableHooks();
}
return this;
}
}
function createHook(fns) {
return new AsyncHook(fns);
}
function executionAsyncId() {
return async_id_fields[kExecutionAsyncId];
}
function triggerAsyncId() {
return async_id_fields[kTriggerAsyncId];
}
// Embedder API //
const destroyedSymbol = Symbol('destroyed');
class AsyncResource {
constructor(type, opts = {}) {
if (typeof type !== 'string')
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'type', 'string');
if (typeof opts === 'number') {
opts = { triggerAsyncId: opts, requireManualDestroy: false };
} else if (opts.triggerAsyncId === undefined) {
opts.triggerAsyncId = initTriggerId();
}
// Unlike emitInitScript, AsyncResource doesn't supports null as the
// triggerAsyncId.
const triggerAsyncId = opts.triggerAsyncId;
if (!Number.isSafeInteger(triggerAsyncId) || triggerAsyncId < -1) {
throw new errors.RangeError('ERR_INVALID_ASYNC_ID',
'triggerAsyncId',
triggerAsyncId);
}
this[async_id_symbol] = newUid();
this[trigger_async_id_symbol] = triggerAsyncId;
// this prop name (destroyed) has to be synchronized with C++
this[destroyedSymbol] = { destroyed: false };
emitInit(
this[async_id_symbol], type, this[trigger_async_id_symbol], this
);
if (!opts.requireManualDestroy) {
registerDestroyHook(this, this[async_id_symbol], this[destroyedSymbol]);
}
}
emitBefore() {
emitBefore(this[async_id_symbol], this[trigger_async_id_symbol]);
return this;
}
emitAfter() {
emitAfter(this[async_id_symbol]);
return this;
}
emitDestroy() {
this[destroyedSymbol].destroyed = true;
emitDestroy(this[async_id_symbol]);
return this;
}
asyncId() {
return this[async_id_symbol];
}
triggerAsyncId() {
return this[trigger_async_id_symbol];
}
}
function runInAsyncIdScope(asyncId, cb) {
// Store the async id now to make sure the stack is still good when the ids
// are popped off the stack.
const prevId = executionAsyncId();
pushAsyncIds(asyncId, prevId);
try {
cb();
} finally {
popAsyncIds(asyncId);
}
}
// Placing all exports down here because the exported classes won't export
// otherwise.
module.exports = {
// Public API
createHook,
executionAsyncId,
triggerAsyncId,
// Embedder API
AsyncResource,
};
// Deprecated API //
Object.defineProperty(module.exports, 'runInAsyncIdScope', {
get: internalUtil.deprecate(function() {
return runInAsyncIdScope;
}, 'async_hooks.runInAsyncIdScope is deprecated. ' +
'Create an AsyncResource instead.', 'DEP0086')
});
Object.defineProperty(module.exports, 'newUid', {
get: internalUtil.deprecate(function() {
return newUid;
}, 'async_hooks.newUid is deprecated. ' +
'Use AsyncResource instead.', 'DEP0085')
});
Object.defineProperty(module.exports, 'initTriggerId', {
get: internalUtil.deprecate(function() {
return initTriggerId;
}, 'async_hooks.initTriggerId is deprecated. ' +
'Use the AsyncResource default instead.', 'DEP0085')
});
Object.defineProperty(module.exports, 'setInitTriggerId', {
get: internalUtil.deprecate(function() {
return setInitTriggerId;
}, 'async_hooks.setInitTriggerId is deprecated. ' +
'Use the triggerAsyncId parameter in AsyncResource instead.', 'DEP0085')
});
Object.defineProperty(module.exports, 'emitInit', {
get: internalUtil.deprecate(function() {
return emitInit;
}, 'async_hooks.emitInit is deprecated. ' +
'Use AsyncResource constructor instead.', 'DEP0085')
});
Object.defineProperty(module.exports, 'emitBefore', {
get: internalUtil.deprecate(function() {
return emitBefore;
}, 'async_hooks.emitBefore is deprecated. ' +
'Use AsyncResource.emitBefore instead.', 'DEP0085')
});
Object.defineProperty(module.exports, 'emitAfter', {
get: internalUtil.deprecate(function() {
return emitAfter;
}, 'async_hooks.emitAfter is deprecated. ' +
'Use AsyncResource.emitAfter instead.', 'DEP0085')
});
Object.defineProperty(module.exports, 'emitDestroy', {
get: internalUtil.deprecate(function() {
return emitDestroy;
}, 'async_hooks.emitDestroy is deprecated. ' +
'Use AsyncResource.emitDestroy instead.', 'DEP0085')
});