2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2014-11-22 15:59:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2014-12-10 22:03:27 +00:00
|
|
|
// WARNING: THIS MODULE IS PENDING DEPRECATION.
|
|
|
|
//
|
|
|
|
// No new pull requests targeting this module will be accepted
|
|
|
|
// unless they address existing, critical bugs.
|
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
const {
|
2020-10-30 15:03:02 +00:00
|
|
|
ArrayPrototypeEvery,
|
|
|
|
ArrayPrototypeIndexOf,
|
|
|
|
ArrayPrototypeLastIndexOf,
|
|
|
|
ArrayPrototypePush,
|
|
|
|
ArrayPrototypeSlice,
|
|
|
|
ArrayPrototypeSplice,
|
2020-01-02 18:19:02 +00:00
|
|
|
Error,
|
2020-10-30 15:03:02 +00:00
|
|
|
FunctionPrototypeCall,
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectDefineProperty,
|
2021-07-05 22:15:30 +00:00
|
|
|
Promise,
|
2019-11-22 17:04:46 +00:00
|
|
|
ReflectApply,
|
2020-10-30 15:03:02 +00:00
|
|
|
SafeMap,
|
2022-07-14 11:05:50 +00:00
|
|
|
SafeWeakMap,
|
2024-09-17 15:08:43 +00:00
|
|
|
StringPrototypeRepeat,
|
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
|
|
|
|
2015-01-21 16:36:59 +00:00
|
|
|
const EventEmitter = require('events');
|
2018-02-27 13:55:32 +00:00
|
|
|
const {
|
|
|
|
ERR_DOMAIN_CALLBACK_NOT_AVAILABLE,
|
|
|
|
ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE,
|
2023-02-12 18:26:21 +00:00
|
|
|
ERR_UNHANDLED_ERROR,
|
2018-02-27 13:55:32 +00:00
|
|
|
} = require('internal/errors').codes;
|
2017-10-10 20:40:43 +00:00
|
|
|
const { createHook } = require('async_hooks');
|
2020-06-09 21:50:03 +00:00
|
|
|
const { useDomainTrampoline } = require('internal/async_hooks');
|
2012-04-06 23:26:18 +00:00
|
|
|
|
2019-02-07 20:19:07 +00:00
|
|
|
const kWeak = Symbol('kWeak');
|
2023-08-07 14:28:56 +00:00
|
|
|
const { WeakReference } = require('internal/util');
|
2019-02-07 20:19:07 +00:00
|
|
|
|
2018-12-03 16:15:45 +00:00
|
|
|
// Overwrite process.domain with a getter/setter that will allow for more
|
2014-01-09 19:11:40 +00:00
|
|
|
// effective optimizations
|
2019-03-26 04:21:27 +00:00
|
|
|
const _domain = [null];
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectDefineProperty(process, 'domain', {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2014-01-09 19:11:40 +00:00
|
|
|
enumerable: true,
|
|
|
|
get: function() {
|
|
|
|
return _domain[0];
|
|
|
|
},
|
|
|
|
set: function(arg) {
|
|
|
|
return _domain[0] = arg;
|
2023-02-12 18:26:21 +00:00
|
|
|
},
|
2014-01-09 19:11:40 +00:00
|
|
|
});
|
|
|
|
|
2022-07-14 11:05:50 +00:00
|
|
|
const vmPromises = new SafeWeakMap();
|
2020-10-30 15:03:02 +00:00
|
|
|
const pairing = new SafeMap();
|
2017-10-10 20:40:43 +00:00
|
|
|
const asyncHook = createHook({
|
|
|
|
init(asyncId, type, triggerAsyncId, resource) {
|
|
|
|
if (process.domain !== null && process.domain !== undefined) {
|
2018-12-10 12:27:32 +00:00
|
|
|
// If this operation is created while in a domain, let's mark it
|
2019-02-07 20:19:07 +00:00
|
|
|
pairing.set(asyncId, process.domain[kWeak]);
|
2021-07-21 16:37:51 +00:00
|
|
|
// Promises from other contexts, such as with the VM module, should not
|
|
|
|
// have a domain property as it can be used to escape the sandbox.
|
2021-07-05 22:15:30 +00:00
|
|
|
if (type !== 'PROMISE' || resource instanceof Promise) {
|
|
|
|
ObjectDefineProperty(resource, 'domain', {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2021-07-05 22:15:30 +00:00
|
|
|
configurable: true,
|
|
|
|
enumerable: false,
|
|
|
|
value: process.domain,
|
2023-02-12 18:26:21 +00:00
|
|
|
writable: true,
|
2021-07-05 22:15:30 +00:00
|
|
|
});
|
2022-07-14 11:05:50 +00:00
|
|
|
// Because promises from other contexts don't get a domain field,
|
|
|
|
// the domain needs to be held alive another way. Stuffing it in a
|
|
|
|
// weakmap connected to the promise lifetime can fix that.
|
|
|
|
} else {
|
|
|
|
vmPromises.set(resource, process.domain);
|
2021-07-05 22:15:30 +00:00
|
|
|
}
|
2017-10-10 20:40:43 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
before(asyncId) {
|
|
|
|
const current = pairing.get(asyncId);
|
2019-03-22 02:44:26 +00:00
|
|
|
if (current !== undefined) { // Enter domain for this cb
|
2019-02-07 20:19:07 +00:00
|
|
|
// We will get the domain through current.get(), because the resource
|
|
|
|
// object's .domain property makes sure it is not garbage collected.
|
2019-06-20 02:24:14 +00:00
|
|
|
// However, we do need to make the reference to the domain non-weak,
|
|
|
|
// so that it cannot be garbage collected before the after() hook.
|
|
|
|
current.incRef();
|
2019-02-07 20:19:07 +00:00
|
|
|
current.get().enter();
|
2017-10-10 20:40:43 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
after(asyncId) {
|
|
|
|
const current = pairing.get(asyncId);
|
2019-03-22 02:44:26 +00:00
|
|
|
if (current !== undefined) { // Exit domain for this cb
|
2019-06-20 02:24:14 +00:00
|
|
|
const domain = current.get();
|
|
|
|
current.decRef();
|
|
|
|
domain.exit();
|
2017-10-10 20:40:43 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
destroy(asyncId) {
|
|
|
|
pairing.delete(asyncId); // cleaning up
|
2023-02-12 18:26:21 +00:00
|
|
|
},
|
2017-10-10 20:40:43 +00:00
|
|
|
});
|
|
|
|
|
2017-11-20 18:57:20 +00:00
|
|
|
// When domains are in use, they claim full ownership of the
|
|
|
|
// uncaught exception capture callback.
|
|
|
|
if (process.hasUncaughtExceptionCaptureCallback()) {
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_DOMAIN_CALLBACK_NOT_AVAILABLE();
|
2017-11-20 18:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the stack trace at the point where `domain` was required.
|
2018-03-15 13:22:43 +00:00
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2017-11-20 18:57:20 +00:00
|
|
|
const domainRequireStack = new Error('require(`domain`) at this point').stack;
|
|
|
|
|
|
|
|
const { setUncaughtExceptionCaptureCallback } = process;
|
|
|
|
process.setUncaughtExceptionCaptureCallback = function(fn) {
|
2018-02-27 13:55:32 +00:00
|
|
|
const err = new ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE();
|
2024-09-17 15:08:43 +00:00
|
|
|
err.stack += `\n${StringPrototypeRepeat('-', 40)}\n${domainRequireStack}`;
|
2017-11-20 18:57:20 +00:00
|
|
|
throw err;
|
|
|
|
};
|
|
|
|
|
2018-02-07 19:05:45 +00:00
|
|
|
|
|
|
|
let sendMakeCallbackDeprecation = false;
|
2020-11-16 16:50:30 +00:00
|
|
|
function emitMakeCallbackDeprecation({ target, method }) {
|
2018-02-07 19:05:45 +00:00
|
|
|
if (!sendMakeCallbackDeprecation) {
|
|
|
|
process.emitWarning(
|
|
|
|
'Using a domain property in MakeCallback is deprecated. Use the ' +
|
|
|
|
'async_context variant of MakeCallback or the AsyncResource class ' +
|
2020-11-16 16:50:30 +00:00
|
|
|
'instead. ' +
|
2021-02-28 19:33:11 +00:00
|
|
|
`(Triggered by calling ${method?.name || '<anonymous>'} ` +
|
2020-11-16 16:50:30 +00:00
|
|
|
`on ${target?.constructor?.name}.)`,
|
|
|
|
'DeprecationWarning', 'DEP0097');
|
2018-02-07 19:05:45 +00:00
|
|
|
sendMakeCallbackDeprecation = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 20:42:59 +00:00
|
|
|
function topLevelDomainCallback(cb, ...args) {
|
|
|
|
const domain = this.domain;
|
2018-02-07 19:05:45 +00:00
|
|
|
if (exports.active && domain)
|
2020-11-16 16:50:30 +00:00
|
|
|
emitMakeCallbackDeprecation({ target: this, method: cb });
|
2018-02-07 19:05:45 +00:00
|
|
|
|
2018-01-19 20:42:59 +00:00
|
|
|
if (domain)
|
|
|
|
domain.enter();
|
2019-11-22 17:04:46 +00:00
|
|
|
const ret = ReflectApply(cb, this, args);
|
2018-01-19 20:42:59 +00:00
|
|
|
if (domain)
|
|
|
|
domain.exit();
|
2018-02-07 19:05:45 +00:00
|
|
|
|
2018-01-19 20:42:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-11-03 01:56:24 +00:00
|
|
|
// It's possible to enter one domain while already inside
|
|
|
|
// another one. The stack is each entered domain.
|
2019-02-19 22:29:31 +00:00
|
|
|
let stack = [];
|
2015-11-03 01:56:24 +00:00
|
|
|
exports._stack = stack;
|
2020-06-09 21:50:03 +00:00
|
|
|
useDomainTrampoline(topLevelDomainCallback);
|
2017-10-10 20:40:43 +00:00
|
|
|
|
2017-11-20 18:57:20 +00:00
|
|
|
function updateExceptionCapture() {
|
2020-10-30 15:03:02 +00:00
|
|
|
if (ArrayPrototypeEvery(stack,
|
|
|
|
(domain) => domain.listenerCount('error') === 0)) {
|
2017-11-20 18:57:20 +00:00
|
|
|
setUncaughtExceptionCaptureCallback(null);
|
|
|
|
} else {
|
|
|
|
setUncaughtExceptionCaptureCallback(null);
|
|
|
|
setUncaughtExceptionCaptureCallback((er) => {
|
|
|
|
return process.domain._errorHandler(er);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
process.on('newListener', (name, listener) => {
|
|
|
|
if (name === 'uncaughtException' &&
|
|
|
|
listener !== domainUncaughtExceptionClear) {
|
|
|
|
// Make sure the first listener for `uncaughtException` always clears
|
|
|
|
// the domain stack.
|
|
|
|
process.removeListener(name, domainUncaughtExceptionClear);
|
|
|
|
process.prependListener(name, domainUncaughtExceptionClear);
|
|
|
|
}
|
|
|
|
});
|
2015-11-03 01:56:24 +00:00
|
|
|
|
2017-11-20 18:57:20 +00:00
|
|
|
process.on('removeListener', (name, listener) => {
|
|
|
|
if (name === 'uncaughtException' &&
|
|
|
|
listener !== domainUncaughtExceptionClear) {
|
|
|
|
// If the domain listener would be the only remaining one, remove it.
|
|
|
|
const listeners = process.listeners('uncaughtException');
|
|
|
|
if (listeners.length === 1 && listeners[0] === domainUncaughtExceptionClear)
|
|
|
|
process.removeListener(name, domainUncaughtExceptionClear);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function domainUncaughtExceptionClear() {
|
|
|
|
stack.length = 0;
|
|
|
|
exports.active = process.domain = null;
|
|
|
|
updateExceptionCapture();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class Domain extends EventEmitter {
|
2017-10-10 20:40:43 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.members = [];
|
2019-02-07 20:19:07 +00:00
|
|
|
this[kWeak] = new WeakReference(this);
|
2017-10-10 20:40:43 +00:00
|
|
|
asyncHook.enable();
|
2017-11-20 18:57:20 +00:00
|
|
|
|
|
|
|
this.on('removeListener', updateExceptionCapture);
|
|
|
|
this.on('newListener', updateExceptionCapture);
|
2017-10-10 20:40:43 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-09 19:11:40 +00:00
|
|
|
|
2012-04-06 23:26:18 +00:00
|
|
|
exports.Domain = Domain;
|
|
|
|
|
2018-04-14 00:33:30 +00:00
|
|
|
exports.create = exports.createDomain = function createDomain() {
|
2013-08-21 23:12:17 +00:00
|
|
|
return new Domain();
|
2012-04-06 23:26:18 +00:00
|
|
|
};
|
|
|
|
|
2018-12-10 12:27:32 +00:00
|
|
|
// The active domain is always the one that we're currently in.
|
2012-04-06 23:26:18 +00:00
|
|
|
exports.active = null;
|
2013-08-21 23:12:17 +00:00
|
|
|
Domain.prototype.members = undefined;
|
2014-01-09 19:11:40 +00:00
|
|
|
|
|
|
|
// Called by process._fatalException in case an error was thrown.
|
2018-04-14 00:33:30 +00:00
|
|
|
Domain.prototype._errorHandler = function(er) {
|
2019-11-06 14:07:30 +00:00
|
|
|
let caught = false;
|
2015-10-05 20:08:53 +00:00
|
|
|
|
2019-10-03 00:56:18 +00:00
|
|
|
if ((typeof er === 'object' && er !== null) || typeof er === 'function') {
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectDefineProperty(er, 'domain', {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2019-02-20 00:00:06 +00:00
|
|
|
configurable: true,
|
|
|
|
enumerable: false,
|
|
|
|
value: this,
|
2023-02-12 18:26:21 +00:00
|
|
|
writable: true,
|
2019-02-20 00:00:06 +00:00
|
|
|
});
|
2013-12-24 12:33:03 +00:00
|
|
|
er.domainThrown = true;
|
|
|
|
}
|
2019-02-19 22:29:31 +00:00
|
|
|
// Pop all adjacent duplicates of the currently active domain from the stack.
|
|
|
|
// This is done to prevent a domain's error handler to run within the context
|
|
|
|
// of itself, and re-entering itself recursively handler as a result of an
|
|
|
|
// exception thrown in its context.
|
|
|
|
while (exports.active === this) {
|
|
|
|
this.exit();
|
|
|
|
}
|
2014-01-09 19:11:40 +00:00
|
|
|
|
2015-10-05 20:08:53 +00:00
|
|
|
// The top-level domain-handler is handled separately.
|
|
|
|
//
|
|
|
|
// The reason is that if V8 was passed a command line option
|
|
|
|
// asking it to abort on an uncaught exception (currently
|
|
|
|
// "--abort-on-uncaught-exception"), we want an uncaught exception
|
|
|
|
// in the top-level domain error handler to make the
|
|
|
|
// process abort. Using try/catch here would always make V8 think
|
|
|
|
// that these exceptions are caught, and thus would prevent it from
|
|
|
|
// aborting in these cases.
|
2019-02-19 22:29:31 +00:00
|
|
|
if (stack.length === 0) {
|
2015-11-03 01:56:24 +00:00
|
|
|
// If there's no error handler, do not emit an 'error' event
|
|
|
|
// as this would throw an error, make the process exit, and thus
|
|
|
|
// prevent the process 'uncaughtException' event from being emitted
|
|
|
|
// if a listener is set.
|
2016-02-15 04:53:17 +00:00
|
|
|
if (EventEmitter.listenerCount(this, 'error') > 0) {
|
2019-02-19 22:29:31 +00:00
|
|
|
// Clear the uncaughtExceptionCaptureCallback so that we know that, since
|
|
|
|
// the top-level domain is not active anymore, it would be ok to abort on
|
|
|
|
// an uncaught exception at this point
|
2017-11-20 18:57:20 +00:00
|
|
|
setUncaughtExceptionCaptureCallback(null);
|
2015-11-03 01:56:24 +00:00
|
|
|
try {
|
2016-02-15 04:53:17 +00:00
|
|
|
caught = this.emit('error', er);
|
2015-11-03 01:56:24 +00:00
|
|
|
} finally {
|
2017-11-20 18:57:20 +00:00
|
|
|
updateExceptionCapture();
|
2015-11-03 01:56:24 +00:00
|
|
|
}
|
2014-01-09 19:11:40 +00:00
|
|
|
}
|
2015-10-05 20:08:53 +00:00
|
|
|
} else {
|
2018-12-10 12:27:32 +00:00
|
|
|
// Wrap this in a try/catch so we don't get infinite throwing
|
2015-10-05 20:08:53 +00:00
|
|
|
try {
|
|
|
|
// One of three things will happen here.
|
|
|
|
//
|
|
|
|
// 1. There is a handler, caught = true
|
|
|
|
// 2. There is no handler, caught = false
|
|
|
|
// 3. It throws, caught = false
|
|
|
|
//
|
|
|
|
// If caught is false after this, then there's no need to exit()
|
|
|
|
// the domain, because we're going to crash the process anyway.
|
2016-02-15 04:53:17 +00:00
|
|
|
caught = this.emit('error', er);
|
2015-10-05 20:08:53 +00:00
|
|
|
} catch (er2) {
|
|
|
|
// The domain error handler threw! oh no!
|
|
|
|
// See if another domain can catch THIS error,
|
|
|
|
// or else crash on the original one.
|
2017-11-20 18:57:20 +00:00
|
|
|
updateExceptionCapture();
|
2015-10-05 20:08:53 +00:00
|
|
|
if (stack.length) {
|
|
|
|
exports.active = process.domain = stack[stack.length - 1];
|
2017-11-20 18:57:20 +00:00
|
|
|
caught = process.domain._errorHandler(er2);
|
2015-10-05 20:08:53 +00:00
|
|
|
} else {
|
2017-11-20 18:57:20 +00:00
|
|
|
// Pass on to the next exception handler.
|
|
|
|
throw er2;
|
2015-10-05 20:08:53 +00:00
|
|
|
}
|
2014-01-09 19:11:40 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-13 01:48:58 +00:00
|
|
|
|
|
|
|
// Exit all domains on the stack. Uncaught exceptions end the
|
|
|
|
// current tick and no domains should be left on the stack
|
|
|
|
// between ticks.
|
2017-11-20 18:57:20 +00:00
|
|
|
domainUncaughtExceptionClear();
|
2016-01-13 01:48:58 +00:00
|
|
|
|
2014-01-09 19:11:40 +00:00
|
|
|
return caught;
|
|
|
|
};
|
2013-08-21 23:12:17 +00:00
|
|
|
|
|
|
|
|
2012-04-06 23:26:18 +00:00
|
|
|
Domain.prototype.enter = function() {
|
2018-12-10 12:27:32 +00:00
|
|
|
// Note that this might be a no-op, but we still need
|
2012-04-06 23:26:18 +00:00
|
|
|
// to push it onto the stack so that we can pop it later.
|
|
|
|
exports.active = process.domain = this;
|
2020-10-30 15:03:02 +00:00
|
|
|
ArrayPrototypePush(stack, this);
|
2017-11-20 18:57:20 +00:00
|
|
|
updateExceptionCapture();
|
2012-04-06 23:26:18 +00:00
|
|
|
};
|
|
|
|
|
2013-10-29 23:35:32 +00:00
|
|
|
|
2012-04-06 23:26:18 +00:00
|
|
|
Domain.prototype.exit = function() {
|
2018-12-10 12:27:32 +00:00
|
|
|
// Don't do anything if this domain is not on the stack.
|
2020-10-30 15:03:02 +00:00
|
|
|
const index = ArrayPrototypeLastIndexOf(stack, this);
|
2017-09-14 15:58:53 +00:00
|
|
|
if (index === -1) return;
|
2012-04-06 23:26:18 +00:00
|
|
|
|
2019-03-07 00:03:53 +00:00
|
|
|
// Exit all domains until this one.
|
2020-10-30 15:03:02 +00:00
|
|
|
ArrayPrototypeSplice(stack, index);
|
2012-04-06 23:26:18 +00:00
|
|
|
|
2020-12-29 14:59:44 +00:00
|
|
|
exports.active = stack.length === 0 ? undefined : stack[stack.length - 1];
|
2012-04-06 23:26:18 +00:00
|
|
|
process.domain = exports.active;
|
2017-11-20 18:57:20 +00:00
|
|
|
updateExceptionCapture();
|
2012-04-06 23:26:18 +00:00
|
|
|
};
|
|
|
|
|
2013-10-29 23:35:32 +00:00
|
|
|
|
2012-04-06 23:26:18 +00:00
|
|
|
// note: this works for timers as well.
|
|
|
|
Domain.prototype.add = function(ee) {
|
2017-09-14 15:58:53 +00:00
|
|
|
// If the domain is already added, then nothing left to do.
|
|
|
|
if (ee.domain === this)
|
2013-10-29 23:35:32 +00:00
|
|
|
return;
|
2012-04-06 23:26:18 +00:00
|
|
|
|
2019-03-07 00:03:53 +00:00
|
|
|
// Has a domain already - remove it first.
|
2013-10-29 23:35:32 +00:00
|
|
|
if (ee.domain)
|
2012-04-06 23:26:18 +00:00
|
|
|
ee.domain.remove(ee);
|
|
|
|
|
2019-01-21 00:22:27 +00:00
|
|
|
// Check for circular Domain->Domain links.
|
2021-03-26 18:21:56 +00:00
|
|
|
// They cause big issues.
|
2012-04-06 23:26:18 +00:00
|
|
|
//
|
|
|
|
// For example:
|
|
|
|
// var d = domain.create();
|
|
|
|
// var e = domain.create();
|
|
|
|
// d.add(e);
|
|
|
|
// e.add(d);
|
|
|
|
// e.emit('error', er); // RangeError, stack overflow!
|
|
|
|
if (this.domain && (ee instanceof Domain)) {
|
2019-11-06 14:07:30 +00:00
|
|
|
for (let d = this.domain; d; d = d.domain) {
|
2012-04-06 23:26:18 +00:00
|
|
|
if (ee === d) return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectDefineProperty(ee, 'domain', {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2019-02-20 00:00:06 +00:00
|
|
|
configurable: true,
|
|
|
|
enumerable: false,
|
|
|
|
value: this,
|
2023-02-12 18:26:21 +00:00
|
|
|
writable: true,
|
2019-02-20 00:00:06 +00:00
|
|
|
});
|
2020-10-30 15:03:02 +00:00
|
|
|
ArrayPrototypePush(this.members, ee);
|
2012-04-06 23:26:18 +00:00
|
|
|
};
|
|
|
|
|
2013-10-29 23:35:32 +00:00
|
|
|
|
2012-04-06 23:26:18 +00:00
|
|
|
Domain.prototype.remove = function(ee) {
|
|
|
|
ee.domain = null;
|
2020-10-30 15:03:02 +00:00
|
|
|
const index = ArrayPrototypeIndexOf(this.members, ee);
|
2013-10-29 23:35:32 +00:00
|
|
|
if (index !== -1)
|
2020-10-30 15:03:02 +00:00
|
|
|
ArrayPrototypeSplice(this.members, index, 1);
|
2012-04-06 23:26:18 +00:00
|
|
|
};
|
|
|
|
|
2013-10-29 23:35:32 +00:00
|
|
|
|
2012-04-06 23:26:18 +00:00
|
|
|
Domain.prototype.run = function(fn) {
|
2013-10-29 23:35:32 +00:00
|
|
|
this.enter();
|
2020-10-30 15:03:02 +00:00
|
|
|
const ret = ReflectApply(fn, this, ArrayPrototypeSlice(arguments, 1));
|
2013-10-29 23:35:32 +00:00
|
|
|
this.exit();
|
2014-11-29 12:50:29 +00:00
|
|
|
|
2013-10-29 23:35:32 +00:00
|
|
|
return ret;
|
2012-04-06 23:26:18 +00:00
|
|
|
};
|
|
|
|
|
2013-10-29 23:35:32 +00:00
|
|
|
|
|
|
|
function intercepted(_this, self, cb, fnargs) {
|
|
|
|
if (fnargs[0] && fnargs[0] instanceof Error) {
|
2019-11-06 14:07:30 +00:00
|
|
|
const er = fnargs[0];
|
2018-12-18 02:15:57 +00:00
|
|
|
er.domainBound = cb;
|
|
|
|
er.domainThrown = false;
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectDefineProperty(er, 'domain', {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2019-02-20 00:00:06 +00:00
|
|
|
configurable: true,
|
|
|
|
enumerable: false,
|
|
|
|
value: self,
|
2023-02-12 18:26:21 +00:00
|
|
|
writable: true,
|
2019-02-20 00:00:06 +00:00
|
|
|
});
|
2013-10-29 23:35:32 +00:00
|
|
|
self.emit('error', er);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.enter();
|
2020-10-30 15:03:02 +00:00
|
|
|
const ret = ReflectApply(cb, _this, ArrayPrototypeSlice(fnargs, 1));
|
2013-10-29 23:35:32 +00:00
|
|
|
self.exit();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-06 23:26:18 +00:00
|
|
|
Domain.prototype.intercept = function(cb) {
|
2019-03-26 04:21:27 +00:00
|
|
|
const self = this;
|
2013-10-29 23:35:32 +00:00
|
|
|
|
|
|
|
function runIntercepted() {
|
|
|
|
return intercepted(this, self, cb, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
return runIntercepted;
|
2012-04-06 23:26:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-10-29 23:35:32 +00:00
|
|
|
function bound(_this, self, cb, fnargs) {
|
|
|
|
self.enter();
|
2020-10-30 15:03:02 +00:00
|
|
|
const ret = ReflectApply(cb, _this, fnargs);
|
2013-10-29 23:35:32 +00:00
|
|
|
self.exit();
|
2012-06-06 15:17:01 +00:00
|
|
|
|
2013-10-29 23:35:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Domain.prototype.bind = function(cb) {
|
2019-03-26 04:21:27 +00:00
|
|
|
const self = this;
|
2013-10-29 23:35:32 +00:00
|
|
|
|
|
|
|
function runBound() {
|
|
|
|
return bound(this, self, cb, arguments);
|
|
|
|
}
|
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectDefineProperty(runBound, 'domain', {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2019-02-20 00:00:06 +00:00
|
|
|
configurable: true,
|
|
|
|
enumerable: false,
|
|
|
|
value: this,
|
2023-02-12 18:26:21 +00:00
|
|
|
writable: true,
|
2019-02-20 00:00:06 +00:00
|
|
|
});
|
2013-10-29 23:35:32 +00:00
|
|
|
|
|
|
|
return runBound;
|
2012-04-06 23:26:18 +00:00
|
|
|
};
|
2017-11-30 15:40:05 +00:00
|
|
|
|
|
|
|
// Override EventEmitter methods to make it domain-aware.
|
|
|
|
EventEmitter.usingDomains = true;
|
|
|
|
|
|
|
|
const eventInit = EventEmitter.init;
|
2022-01-19 11:26:38 +00:00
|
|
|
EventEmitter.init = function(opts) {
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectDefineProperty(this, 'domain', {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2019-02-20 00:00:06 +00:00
|
|
|
configurable: true,
|
|
|
|
enumerable: false,
|
|
|
|
value: null,
|
2023-02-12 18:26:21 +00:00
|
|
|
writable: true,
|
2019-02-20 00:00:06 +00:00
|
|
|
});
|
2017-11-30 15:40:05 +00:00
|
|
|
if (exports.active && !(this instanceof exports.Domain)) {
|
|
|
|
this.domain = exports.active;
|
|
|
|
}
|
|
|
|
|
2022-01-19 11:26:38 +00:00
|
|
|
return FunctionPrototypeCall(eventInit, this, opts);
|
2017-11-30 15:40:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const eventEmit = EventEmitter.prototype.emit;
|
2021-02-28 19:38:16 +00:00
|
|
|
EventEmitter.prototype.emit = function emit(...args) {
|
2017-11-30 15:40:05 +00:00
|
|
|
const domain = this.domain;
|
|
|
|
|
|
|
|
const type = args[0];
|
2017-12-10 18:11:18 +00:00
|
|
|
const shouldEmitError = type === 'error' &&
|
|
|
|
this.listenerCount(type) > 0;
|
2017-11-30 15:40:05 +00:00
|
|
|
|
2017-12-10 18:11:18 +00:00
|
|
|
// Just call original `emit` if current EE instance has `error`
|
|
|
|
// handler, there's no active domain or this is process
|
|
|
|
if (shouldEmitError || domain === null || domain === undefined ||
|
|
|
|
this === process) {
|
2019-11-22 17:04:46 +00:00
|
|
|
return ReflectApply(eventEmit, this, args);
|
2017-12-10 18:11:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'error') {
|
|
|
|
const er = args.length > 1 && args[1] ?
|
2018-02-27 13:55:32 +00:00
|
|
|
args[1] : new ERR_UNHANDLED_ERROR();
|
2017-12-10 18:11:18 +00:00
|
|
|
|
|
|
|
if (typeof er === 'object') {
|
2017-11-30 15:40:05 +00:00
|
|
|
er.domainEmitter = this;
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectDefineProperty(er, 'domain', {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2019-02-20 00:00:06 +00:00
|
|
|
configurable: true,
|
|
|
|
enumerable: false,
|
|
|
|
value: domain,
|
2023-02-12 18:26:21 +00:00
|
|
|
writable: true,
|
2019-02-20 00:00:06 +00:00
|
|
|
});
|
2017-11-30 15:40:05 +00:00
|
|
|
er.domainThrown = false;
|
|
|
|
}
|
2017-12-10 18:11:18 +00:00
|
|
|
|
2019-02-19 22:29:31 +00:00
|
|
|
// Remove the current domain (and its duplicates) from the domains stack and
|
|
|
|
// set the active domain to its parent (if any) so that the domain's error
|
|
|
|
// handler doesn't run in its own context. This prevents any event emitter
|
|
|
|
// created or any exception thrown in that error handler from recursively
|
|
|
|
// executing that error handler.
|
2020-10-30 15:03:02 +00:00
|
|
|
const origDomainsStack = ArrayPrototypeSlice(stack);
|
2019-02-19 22:29:31 +00:00
|
|
|
const origActiveDomain = process.domain;
|
|
|
|
|
|
|
|
// Travel the domains stack from top to bottom to find the first domain
|
|
|
|
// instance that is not a duplicate of the current active domain.
|
|
|
|
let idx = stack.length - 1;
|
|
|
|
while (idx > -1 && process.domain === stack[idx]) {
|
|
|
|
--idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the stack to not contain the current active domain, and only the
|
|
|
|
// domains above it on the stack.
|
|
|
|
if (idx < 0) {
|
|
|
|
stack.length = 0;
|
|
|
|
} else {
|
2020-10-30 15:03:02 +00:00
|
|
|
ArrayPrototypeSplice(stack, idx + 1);
|
2019-02-19 22:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Change the current active domain
|
|
|
|
if (stack.length > 0) {
|
|
|
|
exports.active = process.domain = stack[stack.length - 1];
|
|
|
|
} else {
|
|
|
|
exports.active = process.domain = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateExceptionCapture();
|
|
|
|
|
2017-11-30 15:40:05 +00:00
|
|
|
domain.emit('error', er);
|
2019-02-19 22:29:31 +00:00
|
|
|
|
|
|
|
// Now that the domain's error handler has completed, restore the domains
|
|
|
|
// stack and the active domain to their original values.
|
|
|
|
exports._stack = stack = origDomainsStack;
|
|
|
|
exports.active = process.domain = origActiveDomain;
|
|
|
|
updateExceptionCapture();
|
|
|
|
|
2017-11-30 15:40:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-12-10 18:11:18 +00:00
|
|
|
|
|
|
|
domain.enter();
|
2019-11-22 17:04:46 +00:00
|
|
|
const ret = ReflectApply(eventEmit, this, args);
|
2017-12-10 18:11:18 +00:00
|
|
|
domain.exit();
|
|
|
|
|
|
|
|
return ret;
|
2017-11-30 15:40:05 +00:00
|
|
|
};
|