2017-03-09 23:13:34 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
const {
|
2019-11-27 18:59:29 +00:00
|
|
|
NumberIsSafeInteger,
|
2020-07-31 02:53:32 +00:00
|
|
|
ObjectDefineProperties,
|
2020-08-03 21:19:54 +00:00
|
|
|
ObjectIs,
|
2019-11-22 17:04:46 +00:00
|
|
|
ReflectApply,
|
2019-11-30 15:55:29 +00:00
|
|
|
Symbol,
|
2019-11-22 17:04:46 +00:00
|
|
|
} = primordials;
|
2019-03-31 11:30:12 +00:00
|
|
|
|
2018-02-27 13:55:32 +00:00
|
|
|
const {
|
|
|
|
ERR_ASYNC_CALLBACK,
|
2019-12-14 20:02:50 +00:00
|
|
|
ERR_ASYNC_TYPE,
|
2020-07-31 02:53:32 +00:00
|
|
|
ERR_INVALID_ARG_TYPE,
|
2018-02-27 13:55:32 +00:00
|
|
|
ERR_INVALID_ASYNC_ID
|
|
|
|
} = require('internal/errors').codes;
|
2018-08-02 22:51:02 +00:00
|
|
|
const { validateString } = require('internal/validators');
|
2017-11-12 17:46:55 +00:00
|
|
|
const internal_async_hooks = require('internal/async_hooks');
|
|
|
|
|
|
|
|
// Get functions
|
2017-11-09 21:57:04 +00:00
|
|
|
// For userland AsyncResources, make sure to emit a destroy event when the
|
|
|
|
// resource gets gced.
|
2018-03-15 04:54:10 +00:00
|
|
|
const { registerDestroyHook } = internal_async_hooks;
|
2017-11-12 17:46:55 +00:00
|
|
|
const {
|
2018-02-11 21:35:59 +00:00
|
|
|
executionAsyncId,
|
|
|
|
triggerAsyncId,
|
2017-11-12 17:46:55 +00:00
|
|
|
// Private API
|
2020-02-14 03:30:33 +00:00
|
|
|
hasAsyncIdStack,
|
2017-11-12 17:46:55 +00:00
|
|
|
getHookArrays,
|
|
|
|
enableHooks,
|
|
|
|
disableHooks,
|
2020-04-16 22:23:57 +00:00
|
|
|
updatePromiseHookMode,
|
2018-05-04 06:00:07 +00:00
|
|
|
executionAsyncResource,
|
2017-11-22 10:25:03 +00:00
|
|
|
// Internal Embedder API
|
2018-02-11 21:35:59 +00:00
|
|
|
newAsyncId,
|
2017-11-22 12:54:38 +00:00
|
|
|
getDefaultTriggerAsyncId,
|
2017-11-12 17:46:55 +00:00
|
|
|
emitInit,
|
|
|
|
emitBefore,
|
|
|
|
emitAfter,
|
|
|
|
emitDestroy,
|
2019-12-14 20:02:50 +00:00
|
|
|
enabledHooksExist,
|
2019-03-31 19:41:14 +00:00
|
|
|
initHooksExist,
|
2020-03-22 18:53:52 +00:00
|
|
|
destroyHooksExist,
|
2017-11-12 17:46:55 +00:00
|
|
|
} = internal_async_hooks;
|
2017-08-02 07:34:59 +00:00
|
|
|
|
2017-11-12 17:46:55 +00:00
|
|
|
// Get symbols
|
|
|
|
const {
|
2018-02-11 21:35:59 +00:00
|
|
|
async_id_symbol, trigger_async_id_symbol,
|
2017-11-12 17:46:55 +00:00
|
|
|
init_symbol, before_symbol, after_symbol, destroy_symbol,
|
|
|
|
promise_resolve_symbol
|
|
|
|
} = internal_async_hooks.symbols;
|
2017-03-09 23:13:34 +00:00
|
|
|
|
2017-11-12 17:46:55 +00:00
|
|
|
// Get constants
|
|
|
|
const {
|
|
|
|
kInit, kBefore, kAfter, kDestroy, kTotals, kPromiseResolve,
|
2018-03-15 04:54:10 +00:00
|
|
|
} = internal_async_hooks.constants;
|
2017-03-09 23:13:34 +00:00
|
|
|
|
2017-11-12 17:46:55 +00:00
|
|
|
// Listener API //
|
2017-03-09 23:13:34 +00:00
|
|
|
|
|
|
|
class AsyncHook {
|
2017-09-09 15:50:42 +00:00
|
|
|
constructor({ init, before, after, destroy, promiseResolve }) {
|
2017-05-18 08:09:50 +00:00
|
|
|
if (init !== undefined && typeof init !== 'function')
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_ASYNC_CALLBACK('hook.init');
|
2017-05-18 08:09:50 +00:00
|
|
|
if (before !== undefined && typeof before !== 'function')
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_ASYNC_CALLBACK('hook.before');
|
2017-05-18 08:09:50 +00:00
|
|
|
if (after !== undefined && typeof after !== 'function')
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_ASYNC_CALLBACK('hook.after');
|
2017-05-18 08:09:50 +00:00
|
|
|
if (destroy !== undefined && typeof destroy !== 'function')
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_ASYNC_CALLBACK('hook.destroy');
|
2017-09-09 15:50:42 +00:00
|
|
|
if (promiseResolve !== undefined && typeof promiseResolve !== 'function')
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_ASYNC_CALLBACK('hook.promiseResolve');
|
2017-03-09 23:13:34 +00:00
|
|
|
|
|
|
|
this[init_symbol] = init;
|
|
|
|
this[before_symbol] = before;
|
|
|
|
this[after_symbol] = after;
|
|
|
|
this[destroy_symbol] = destroy;
|
2017-09-09 15:50:42 +00:00
|
|
|
this[promise_resolve_symbol] = promiseResolve;
|
2017-03-09 23:13:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
2017-06-02 21:49:55 +00:00
|
|
|
return this;
|
2017-03-09 23:13:34 +00:00
|
|
|
|
2017-06-06 16:57:10 +00:00
|
|
|
const prev_kTotals = hook_fields[kTotals];
|
|
|
|
|
2017-03-09 23:13:34 +00:00
|
|
|
// createHook() has already enforced that the callbacks are all functions,
|
|
|
|
// so here simply increment the count of whether each callbacks exists or
|
|
|
|
// not.
|
2018-03-07 18:59:23 +00:00
|
|
|
hook_fields[kTotals] = hook_fields[kInit] += +!!this[init_symbol];
|
2017-06-06 16:57:10 +00:00
|
|
|
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];
|
2017-09-09 15:50:42 +00:00
|
|
|
hook_fields[kTotals] +=
|
|
|
|
hook_fields[kPromiseResolve] += +!!this[promise_resolve_symbol];
|
2017-03-09 23:13:34 +00:00
|
|
|
hooks_array.push(this);
|
2017-06-06 16:57:10 +00:00
|
|
|
|
2017-10-19 10:43:40 +00:00
|
|
|
if (prev_kTotals === 0 && hook_fields[kTotals] > 0) {
|
2017-11-12 17:46:55 +00:00
|
|
|
enableHooks();
|
2017-10-19 10:43:40 +00:00
|
|
|
}
|
2017-06-06 16:57:10 +00:00
|
|
|
|
2020-04-16 22:23:57 +00:00
|
|
|
updatePromiseHookMode();
|
|
|
|
|
2017-03-09 23:13:34 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
disable() {
|
|
|
|
const [hooks_array, hook_fields] = getHookArrays();
|
|
|
|
|
|
|
|
const index = hooks_array.indexOf(this);
|
|
|
|
if (index === -1)
|
2017-06-02 21:49:55 +00:00
|
|
|
return this;
|
2017-03-09 23:13:34 +00:00
|
|
|
|
2017-06-06 16:57:10 +00:00
|
|
|
const prev_kTotals = hook_fields[kTotals];
|
|
|
|
|
2018-03-07 18:59:23 +00:00
|
|
|
hook_fields[kTotals] = hook_fields[kInit] -= +!!this[init_symbol];
|
2017-06-06 16:57:10 +00:00
|
|
|
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];
|
2017-09-09 15:50:42 +00:00
|
|
|
hook_fields[kTotals] +=
|
|
|
|
hook_fields[kPromiseResolve] -= +!!this[promise_resolve_symbol];
|
2017-03-09 23:13:34 +00:00
|
|
|
hooks_array.splice(index, 1);
|
2017-06-06 16:57:10 +00:00
|
|
|
|
2017-10-19 10:43:40 +00:00
|
|
|
if (prev_kTotals > 0 && hook_fields[kTotals] === 0) {
|
2017-11-12 17:46:55 +00:00
|
|
|
disableHooks();
|
2017-10-19 10:43:40 +00:00
|
|
|
}
|
2017-06-06 16:57:10 +00:00
|
|
|
|
2017-03-09 23:13:34 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function createHook(fns) {
|
|
|
|
return new AsyncHook(fns);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Embedder API //
|
|
|
|
|
2017-11-09 21:57:04 +00:00
|
|
|
const destroyedSymbol = Symbol('destroyed');
|
|
|
|
|
2017-05-24 11:15:05 +00:00
|
|
|
class AsyncResource {
|
2017-11-09 21:57:04 +00:00
|
|
|
constructor(type, opts = {}) {
|
2018-08-02 22:51:02 +00:00
|
|
|
validateString(type, 'type');
|
2017-08-30 21:45:21 +00:00
|
|
|
|
2019-06-06 07:18:41 +00:00
|
|
|
let triggerAsyncId = opts;
|
|
|
|
let requireManualDestroy = false;
|
|
|
|
if (typeof opts !== 'number') {
|
|
|
|
triggerAsyncId = opts.triggerAsyncId === undefined ?
|
|
|
|
getDefaultTriggerAsyncId() : opts.triggerAsyncId;
|
|
|
|
requireManualDestroy = !!opts.requireManualDestroy;
|
2017-11-09 21:57:04 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 12:28:33 +00:00
|
|
|
// Unlike emitInitScript, AsyncResource doesn't supports null as the
|
|
|
|
// triggerAsyncId.
|
2019-11-27 18:59:29 +00:00
|
|
|
if (!NumberIsSafeInteger(triggerAsyncId) || triggerAsyncId < -1) {
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_INVALID_ASYNC_ID('triggerAsyncId', triggerAsyncId);
|
async_hooks: don't abort unnecessarily
* id values of -1 are allowed. They indicate that the id was never
correctly assigned to the async resource. These will appear in any
call graph, and will only be apparent to those using the async_hooks
module, then reported in an issue.
* ids < -1 are still not allowed and will cause the application to
exit the process; because there is no scenario where this should ever
happen.
* Add asyncId range checks to emitAfterScript().
* Fix emitBeforeScript() range checks which should have been || not &&.
* Replace errors with entries in internal/errors.
* Fix async_hooks tests that check for exceptions to match new
internal/errors entries.
NOTE: emit{Before,After,Destroy}() must continue to exit the process
because in the case of an exception during hook execution the state of
the application is unknowable. For example, an exception could cause a
memory leak:
const id_map = new Map();
before(id) {
id_map.set(id, /* data object or similar */);
},
after(id) {
throw new Error('id never dies!');
id_map.delete(id);
}
Allowing a recoverable exception may also cause an abort because of a
stack check in Environment::AsyncHooks::pop_ids() that verifies the
async id and pop'd ids match. This case would be more difficult to debug
than if fatalError() (lib/async_hooks.js) was called immediately.
try {
async_hooks.emitBefore(null, NaN);
} catch (e) { }
// do something
async_hooks.emitAfter(5);
It also allows an edge case where emitBefore() could be called twice and
not have the pop_ids() CHECK fail:
try {
async_hooks.emitBefore(5, NaN);
} catch (e) { }
async_hooks.emitBefore(5);
// do something
async_hooks.emitAfter(5);
There is the option of allowing mismatches in the stack and ignoring the
check if no async hooks are enabled, but I don't believe going this far
is necessary.
PR-URL: https://github.com/nodejs/node/pull/14722
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Refael Ackermann <refack@gmail.com>
2017-08-03 20:43:41 +00:00
|
|
|
}
|
2017-03-09 23:13:34 +00:00
|
|
|
|
2019-03-31 19:41:14 +00:00
|
|
|
const asyncId = newAsyncId();
|
|
|
|
this[async_id_symbol] = asyncId;
|
2017-09-26 13:50:10 +00:00
|
|
|
this[trigger_async_id_symbol] = triggerAsyncId;
|
2019-03-31 19:41:14 +00:00
|
|
|
|
|
|
|
if (initHooksExist()) {
|
2019-12-14 20:02:50 +00:00
|
|
|
if (enabledHooksExist() && type.length === 0) {
|
|
|
|
throw new ERR_ASYNC_TYPE(type);
|
|
|
|
}
|
|
|
|
|
2019-03-31 19:41:14 +00:00
|
|
|
emitInit(asyncId, type, triggerAsyncId, this);
|
|
|
|
}
|
2017-11-09 21:57:04 +00:00
|
|
|
|
2020-03-22 18:53:52 +00:00
|
|
|
if (!requireManualDestroy && destroyHooksExist()) {
|
2019-06-06 07:18:41 +00:00
|
|
|
// This prop name (destroyed) has to be synchronized with C++
|
|
|
|
const destroyed = { destroyed: false };
|
|
|
|
this[destroyedSymbol] = destroyed;
|
2019-03-31 19:41:14 +00:00
|
|
|
registerDestroyHook(this, asyncId, destroyed);
|
2017-11-09 21:57:04 +00:00
|
|
|
}
|
2017-03-09 23:13:34 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 23:25:41 +00:00
|
|
|
runInAsyncScope(fn, thisArg, ...args) {
|
2019-03-31 19:41:14 +00:00
|
|
|
const asyncId = this[async_id_symbol];
|
2018-05-04 06:00:07 +00:00
|
|
|
emitBefore(asyncId, this[trigger_async_id_symbol], this);
|
2019-12-14 18:53:03 +00:00
|
|
|
|
2020-02-14 03:30:33 +00:00
|
|
|
try {
|
|
|
|
const ret = thisArg === undefined ?
|
|
|
|
fn(...args) :
|
|
|
|
ReflectApply(fn, thisArg, args);
|
2019-12-14 18:53:03 +00:00
|
|
|
|
2020-02-14 03:30:33 +00:00
|
|
|
return ret;
|
|
|
|
} finally {
|
|
|
|
if (hasAsyncIdStack())
|
|
|
|
emitAfter(asyncId);
|
|
|
|
}
|
2018-02-01 23:25:41 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 23:13:34 +00:00
|
|
|
emitDestroy() {
|
2019-06-06 07:18:41 +00:00
|
|
|
if (this[destroyedSymbol] !== undefined) {
|
|
|
|
this[destroyedSymbol].destroyed = true;
|
|
|
|
}
|
2017-11-12 17:46:55 +00:00
|
|
|
emitDestroy(this[async_id_symbol]);
|
2017-03-09 23:13:34 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
asyncId() {
|
|
|
|
return this[async_id_symbol];
|
|
|
|
}
|
|
|
|
|
2017-06-14 10:39:53 +00:00
|
|
|
triggerAsyncId() {
|
2017-09-26 13:50:10 +00:00
|
|
|
return this[trigger_async_id_symbol];
|
2017-03-09 23:13:34 +00:00
|
|
|
}
|
2020-07-31 02:53:32 +00:00
|
|
|
|
|
|
|
bind(fn) {
|
|
|
|
if (typeof fn !== 'function')
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
|
|
|
|
const ret = this.runInAsyncScope.bind(this, fn);
|
|
|
|
ObjectDefineProperties(ret, {
|
|
|
|
'length': {
|
2020-08-04 08:15:42 +00:00
|
|
|
configurable: true,
|
|
|
|
enumerable: false,
|
2020-07-31 02:53:32 +00:00
|
|
|
value: fn.length,
|
2020-08-04 08:15:42 +00:00
|
|
|
writable: false,
|
2020-07-31 02:53:32 +00:00
|
|
|
},
|
|
|
|
'asyncResource': {
|
2020-08-04 08:15:42 +00:00
|
|
|
configurable: true,
|
2020-07-31 02:53:32 +00:00
|
|
|
enumerable: true,
|
|
|
|
value: this,
|
2020-08-04 08:15:42 +00:00
|
|
|
writable: true,
|
2020-07-31 02:53:32 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bind(fn, type) {
|
|
|
|
type = type || fn.name;
|
|
|
|
return (new AsyncResource(type || 'bound-anonymous-fn')).bind(fn);
|
|
|
|
}
|
2017-03-09 23:13:34 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 20:31:36 +00:00
|
|
|
const storageList = [];
|
|
|
|
const storageHook = createHook({
|
|
|
|
init(asyncId, type, triggerAsyncId, resource) {
|
|
|
|
const currentResource = executionAsyncResource();
|
|
|
|
// Value of currentResource is always a non null object
|
|
|
|
for (let i = 0; i < storageList.length; ++i) {
|
|
|
|
storageList[i]._propagate(resource, currentResource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
class AsyncLocalStorage {
|
|
|
|
constructor() {
|
|
|
|
this.kResourceStore = Symbol('kResourceStore');
|
|
|
|
this.enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
disable() {
|
|
|
|
if (this.enabled) {
|
|
|
|
this.enabled = false;
|
|
|
|
// If this.enabled, the instance must be in storageList
|
|
|
|
storageList.splice(storageList.indexOf(this), 1);
|
|
|
|
if (storageList.length === 0) {
|
|
|
|
storageHook.disable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Propagate the context from a parent resource to a child one
|
|
|
|
_propagate(resource, triggerResource) {
|
|
|
|
const store = triggerResource[this.kResourceStore];
|
|
|
|
if (this.enabled) {
|
|
|
|
resource[this.kResourceStore] = store;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 05:46:46 +00:00
|
|
|
enterWith(store) {
|
2019-03-01 20:31:36 +00:00
|
|
|
if (!this.enabled) {
|
|
|
|
this.enabled = true;
|
|
|
|
storageList.push(this);
|
|
|
|
storageHook.enable();
|
|
|
|
}
|
|
|
|
const resource = executionAsyncResource();
|
2020-02-24 10:00:59 +00:00
|
|
|
resource[this.kResourceStore] = store;
|
2019-03-01 20:31:36 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 06:06:06 +00:00
|
|
|
run(store, callback, ...args) {
|
2020-08-03 21:19:54 +00:00
|
|
|
// Avoid creation of an AsyncResource if store is already active
|
|
|
|
if (ObjectIs(store, this.getStore())) {
|
|
|
|
return callback(...args);
|
|
|
|
}
|
2020-02-25 14:12:51 +00:00
|
|
|
const resource = new AsyncResource('AsyncLocalStorage');
|
|
|
|
return resource.runInAsyncScope(() => {
|
|
|
|
this.enterWith(store);
|
2019-03-01 20:31:36 +00:00
|
|
|
return callback(...args);
|
2020-02-25 14:12:51 +00:00
|
|
|
});
|
2019-03-01 20:31:36 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 06:06:06 +00:00
|
|
|
exit(callback, ...args) {
|
2020-03-04 11:57:56 +00:00
|
|
|
if (!this.enabled) {
|
|
|
|
return callback(...args);
|
|
|
|
}
|
2019-03-01 20:31:36 +00:00
|
|
|
this.enabled = false;
|
|
|
|
try {
|
|
|
|
return callback(...args);
|
|
|
|
} finally {
|
|
|
|
this.enabled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getStore() {
|
|
|
|
if (this.enabled) {
|
2020-08-03 21:52:58 +00:00
|
|
|
const resource = executionAsyncResource();
|
2019-03-01 20:31:36 +00:00
|
|
|
return resource[this.kResourceStore];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-09 23:13:34 +00:00
|
|
|
|
|
|
|
// Placing all exports down here because the exported classes won't export
|
|
|
|
// otherwise.
|
|
|
|
module.exports = {
|
|
|
|
// Public API
|
2019-03-01 20:31:36 +00:00
|
|
|
AsyncLocalStorage,
|
2017-03-09 23:13:34 +00:00
|
|
|
createHook,
|
2017-06-14 10:39:53 +00:00
|
|
|
executionAsyncId,
|
|
|
|
triggerAsyncId,
|
2018-05-04 06:00:07 +00:00
|
|
|
executionAsyncResource,
|
2017-03-09 23:13:34 +00:00
|
|
|
// Embedder API
|
2017-05-24 11:15:05 +00:00
|
|
|
AsyncResource,
|
2017-03-09 23:13:34 +00:00
|
|
|
};
|