2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
2014-12-10 23:03:27 +01:00
|
|
|
// WARNING: THIS MODULE IS PENDING DEPRECATION.
|
|
|
|
//
|
|
|
|
// No new pull requests targeting this module will be accepted
|
|
|
|
// unless they address existing, critical bugs.
|
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const util = require('util');
|
|
|
|
const EventEmitter = require('events');
|
|
|
|
const inherits = util.inherits;
|
2012-04-07 01:26:18 +02:00
|
|
|
|
|
|
|
// communicate with events module, but don't require that
|
|
|
|
// module to have to load this one, since this module has
|
|
|
|
// a few side effects.
|
2013-10-30 00:35:32 +01:00
|
|
|
EventEmitter.usingDomains = true;
|
2013-02-27 20:46:35 +01:00
|
|
|
|
2014-01-09 20:11:40 +01:00
|
|
|
// overwrite process.domain with a getter/setter that will allow for more
|
|
|
|
// effective optimizations
|
|
|
|
var _domain = [null];
|
|
|
|
Object.defineProperty(process, 'domain', {
|
|
|
|
enumerable: true,
|
|
|
|
get: function() {
|
|
|
|
return _domain[0];
|
|
|
|
},
|
|
|
|
set: function(arg) {
|
|
|
|
return _domain[0] = arg;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// objects with external array data are excellent ways to communicate state
|
|
|
|
// between js and c++ w/o much overhead
|
|
|
|
var _domain_flag = {};
|
|
|
|
|
|
|
|
// let the process know we're using domains
|
|
|
|
process._setupDomainUse(_domain, _domain_flag);
|
|
|
|
|
2012-04-07 01:26:18 +02:00
|
|
|
exports.Domain = Domain;
|
|
|
|
|
2013-08-22 01:12:17 +02:00
|
|
|
exports.create = exports.createDomain = function() {
|
|
|
|
return new Domain();
|
2012-04-07 01:26:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// it's possible to enter one domain while already inside
|
|
|
|
// another one. the stack is each entered domain.
|
|
|
|
var stack = [];
|
2012-07-17 01:38:11 +02:00
|
|
|
exports._stack = stack;
|
2012-04-07 01:26:18 +02:00
|
|
|
// the active domain is always the one that we're currently in.
|
|
|
|
exports.active = null;
|
|
|
|
|
|
|
|
|
|
|
|
inherits(Domain, EventEmitter);
|
|
|
|
|
|
|
|
function Domain() {
|
2012-12-27 16:51:31 +01:00
|
|
|
EventEmitter.call(this);
|
2014-01-09 20:11:40 +01:00
|
|
|
|
2012-04-07 01:26:18 +02:00
|
|
|
this.members = [];
|
|
|
|
}
|
|
|
|
|
2013-08-22 01:12:17 +02:00
|
|
|
Domain.prototype.members = undefined;
|
|
|
|
Domain.prototype._disposed = undefined;
|
2014-01-09 20:11:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
// Called by process._fatalException in case an error was thrown.
|
|
|
|
Domain.prototype._errorHandler = function errorHandler(er) {
|
|
|
|
var caught = false;
|
|
|
|
// ignore errors on disposed domains.
|
|
|
|
//
|
|
|
|
// XXX This is a bit stupid. We should probably get rid of
|
|
|
|
// domain.dispose() altogether. It's almost always a terrible
|
|
|
|
// idea. --isaacs
|
|
|
|
if (this._disposed)
|
|
|
|
return true;
|
|
|
|
|
2013-12-24 13:33:03 +01:00
|
|
|
if (!util.isPrimitive(er)) {
|
|
|
|
er.domain = this;
|
|
|
|
er.domainThrown = true;
|
|
|
|
}
|
2014-01-09 20:11:40 +01:00
|
|
|
// wrap this in a try/catch so we don't get infinite throwing
|
|
|
|
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.
|
|
|
|
caught = this.emit('error', er);
|
|
|
|
|
|
|
|
// Exit all domains on the stack. Uncaught exceptions end the
|
|
|
|
// current tick and no domains should be left on the stack
|
|
|
|
// between ticks.
|
|
|
|
stack.length = 0;
|
|
|
|
exports.active = process.domain = null;
|
|
|
|
} catch (er2) {
|
|
|
|
// The domain error handler threw! oh no!
|
|
|
|
// See if another domain can catch THIS error,
|
|
|
|
// or else crash on the original one.
|
|
|
|
// If the user already exited it, then don't double-exit.
|
|
|
|
if (this === exports.active) {
|
|
|
|
stack.pop();
|
|
|
|
}
|
|
|
|
if (stack.length) {
|
|
|
|
exports.active = process.domain = stack[stack.length - 1];
|
|
|
|
caught = process._fatalException(er2);
|
|
|
|
} else {
|
|
|
|
caught = false;
|
|
|
|
}
|
|
|
|
return caught;
|
|
|
|
}
|
|
|
|
return caught;
|
|
|
|
};
|
2013-08-22 01:12:17 +02:00
|
|
|
|
|
|
|
|
2012-04-07 01:26:18 +02:00
|
|
|
Domain.prototype.enter = function() {
|
|
|
|
if (this._disposed) return;
|
|
|
|
|
|
|
|
// note that this might be a no-op, but we still need
|
|
|
|
// to push it onto the stack so that we can pop it later.
|
|
|
|
exports.active = process.domain = this;
|
|
|
|
stack.push(this);
|
2014-01-09 20:11:40 +01:00
|
|
|
_domain_flag[0] = stack.length;
|
2012-04-07 01:26:18 +02:00
|
|
|
};
|
|
|
|
|
2013-10-30 00:35:32 +01:00
|
|
|
|
2012-04-07 01:26:18 +02:00
|
|
|
Domain.prototype.exit = function() {
|
2014-01-09 06:19:31 +01:00
|
|
|
// skip disposed domains, as usual, but also don't do anything if this
|
|
|
|
// domain is not on the stack.
|
|
|
|
var index = stack.lastIndexOf(this);
|
|
|
|
if (this._disposed || index === -1) return;
|
2012-04-07 01:26:18 +02:00
|
|
|
|
|
|
|
// exit all domains until this one.
|
2014-01-09 06:19:31 +01:00
|
|
|
stack.splice(index);
|
2014-01-09 20:11:40 +01:00
|
|
|
_domain_flag[0] = stack.length;
|
2012-04-07 01:26:18 +02:00
|
|
|
|
|
|
|
exports.active = stack[stack.length - 1];
|
|
|
|
process.domain = exports.active;
|
|
|
|
};
|
|
|
|
|
2013-10-30 00:35:32 +01:00
|
|
|
|
2012-04-07 01:26:18 +02:00
|
|
|
// note: this works for timers as well.
|
|
|
|
Domain.prototype.add = function(ee) {
|
2013-10-30 00:35:32 +01:00
|
|
|
// If the domain is disposed or already added, then nothing left to do.
|
|
|
|
if (this._disposed || ee.domain === this)
|
|
|
|
return;
|
2012-04-07 01:26:18 +02:00
|
|
|
|
|
|
|
// has a domain already - remove it first.
|
2013-10-30 00:35:32 +01:00
|
|
|
if (ee.domain)
|
2012-04-07 01:26:18 +02:00
|
|
|
ee.domain.remove(ee);
|
|
|
|
|
|
|
|
// check for circular Domain->Domain links.
|
|
|
|
// This causes bad insanity!
|
|
|
|
//
|
|
|
|
// 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)) {
|
|
|
|
for (var d = this.domain; d; d = d.domain) {
|
|
|
|
if (ee === d) return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ee.domain = this;
|
|
|
|
this.members.push(ee);
|
|
|
|
};
|
|
|
|
|
2013-10-30 00:35:32 +01:00
|
|
|
|
2012-04-07 01:26:18 +02:00
|
|
|
Domain.prototype.remove = function(ee) {
|
|
|
|
ee.domain = null;
|
|
|
|
var index = this.members.indexOf(ee);
|
2013-10-30 00:35:32 +01:00
|
|
|
if (index !== -1)
|
2012-04-07 01:26:18 +02:00
|
|
|
this.members.splice(index, 1);
|
|
|
|
};
|
|
|
|
|
2013-10-30 00:35:32 +01:00
|
|
|
|
2012-04-07 01:26:18 +02:00
|
|
|
Domain.prototype.run = function(fn) {
|
2013-10-30 00:35:32 +01:00
|
|
|
if (this._disposed)
|
|
|
|
return;
|
2014-11-29 13:50:29 +01:00
|
|
|
|
|
|
|
var ret;
|
|
|
|
|
2013-10-30 00:35:32 +01:00
|
|
|
this.enter();
|
2014-11-29 13:50:29 +01:00
|
|
|
if (arguments.length >= 2) {
|
|
|
|
var len = arguments.length;
|
|
|
|
var args = new Array(len - 1);
|
|
|
|
|
|
|
|
for (var i = 1; i < len; i++)
|
|
|
|
args[i - 1] = arguments[i];
|
|
|
|
|
|
|
|
ret = fn.apply(this, args);
|
|
|
|
} else {
|
|
|
|
ret = fn.call(this);
|
|
|
|
}
|
2013-10-30 00:35:32 +01:00
|
|
|
this.exit();
|
2014-11-29 13:50:29 +01:00
|
|
|
|
2013-10-30 00:35:32 +01:00
|
|
|
return ret;
|
2012-04-07 01:26:18 +02:00
|
|
|
};
|
|
|
|
|
2013-10-30 00:35:32 +01:00
|
|
|
|
|
|
|
function intercepted(_this, self, cb, fnargs) {
|
|
|
|
if (self._disposed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (fnargs[0] && fnargs[0] instanceof Error) {
|
|
|
|
var er = fnargs[0];
|
|
|
|
util._extend(er, {
|
|
|
|
domainBound: cb,
|
|
|
|
domainThrown: false,
|
|
|
|
domain: self
|
|
|
|
});
|
|
|
|
self.emit('error', er);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var args = [];
|
|
|
|
var i, ret;
|
|
|
|
|
|
|
|
self.enter();
|
|
|
|
if (fnargs.length > 1) {
|
|
|
|
for (i = 1; i < fnargs.length; i++)
|
|
|
|
args.push(fnargs[i]);
|
|
|
|
ret = cb.apply(_this, args);
|
|
|
|
} else {
|
|
|
|
ret = cb.call(_this);
|
|
|
|
}
|
|
|
|
self.exit();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-07 01:26:18 +02:00
|
|
|
Domain.prototype.intercept = function(cb) {
|
2013-10-30 00:35:32 +01:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
function runIntercepted() {
|
|
|
|
return intercepted(this, self, cb, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
return runIntercepted;
|
2012-04-07 01:26:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-10-30 00:35:32 +01:00
|
|
|
function bound(_this, self, cb, fnargs) {
|
|
|
|
if (self._disposed)
|
|
|
|
return;
|
2012-04-07 01:26:18 +02:00
|
|
|
|
2014-06-11 21:16:48 +02:00
|
|
|
var ret;
|
2012-04-07 01:26:18 +02:00
|
|
|
|
2013-10-30 00:35:32 +01:00
|
|
|
self.enter();
|
|
|
|
if (fnargs.length > 0)
|
|
|
|
ret = cb.apply(_this, fnargs);
|
|
|
|
else
|
|
|
|
ret = cb.call(_this);
|
|
|
|
self.exit();
|
2012-06-06 17:17:01 +02:00
|
|
|
|
2013-10-30 00:35:32 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Domain.prototype.bind = function(cb) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
function runBound() {
|
|
|
|
return bound(this, self, cb, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
runBound.domain = this;
|
|
|
|
|
|
|
|
return runBound;
|
2012-04-07 01:26:18 +02:00
|
|
|
};
|
|
|
|
|
2013-10-30 00:35:32 +01:00
|
|
|
|
2013-03-14 22:11:15 +01:00
|
|
|
Domain.prototype.dispose = util.deprecate(function() {
|
2012-04-07 01:26:18 +02:00
|
|
|
if (this._disposed) return;
|
|
|
|
|
domain: Properly exit() on domain disposal
This addresses #4034. There are two problems happening:
1. The domain is not exited automatically when calling dispose() on it.
Then, since the domain is disposed, attempting to exit it again will do
nothing.
2. The active domain is stored on process.domain. Since thrown errors
call `process.emit('uncaughtException', er)`, and the process is an
event emitter with a `.domain` member, it re-enters the domain a second
time before calling the error handler, pushing it onto the stack again.
Thus, if the handler calls `domain.dispose()`, then the domain is now on
the stack twice, and cannot be exited properly. Since the domain is
disposed, any subsequent IO will be no-op'ed, since we've declared that
this context is done and best forgotten.
The solution here is twofold:
1. In EventEmitter.emit, do not enter the domain if `this===process`.
2. Automatically exit the domain when calling `domain.dispose()`.
2012-09-21 00:13:36 +02:00
|
|
|
// if we're the active domain, then get out now.
|
|
|
|
this.exit();
|
|
|
|
|
2012-04-07 01:26:18 +02:00
|
|
|
// remove from parent domain, if there is one.
|
|
|
|
if (this.domain) this.domain.remove(this);
|
|
|
|
|
|
|
|
// kill the references so that they can be properly gc'ed.
|
|
|
|
this.members.length = 0;
|
|
|
|
|
2013-03-14 22:11:15 +01:00
|
|
|
// mark this domain as 'no longer relevant'
|
2012-04-07 01:26:18 +02:00
|
|
|
// so that it can't be entered or activated.
|
|
|
|
this._disposed = true;
|
2013-03-14 22:11:15 +01:00
|
|
|
});
|