Skip to content

Commit

Permalink
Merge pull request #1633 from spumko/domains
Browse files Browse the repository at this point in the history
Rework domains to single entry
  • Loading branch information
kpdecker committed May 12, 2014
2 parents 43e2e71 + c185732 commit 9caab29
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 77 deletions.
22 changes: 6 additions & 16 deletions lib/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ internals.Auth.prototype._authenticate = function (request, next) {
var strategy = config.strategies[strategyPos];
++strategyPos;

request._protect.run(validate, function (enter, exit) {
request._protect.run(validate, function (exit) {

var savedResults = undefined;
var transfer = function (err) {
Expand All @@ -187,11 +187,7 @@ internals.Auth.prototype._authenticate = function (request, next) {
};

var reply = Handler.replyInterface(request, transfer, root);

enter(function () {

self._strategies[strategy].authenticate.call(null, request, reply);
});
self._strategies[strategy].authenticate.call(null, request, reply);
});
};

Expand Down Expand Up @@ -326,12 +322,9 @@ internals.Auth.payload = function (request, next) {

var strategy = auth._strategies[request.auth.strategy];

request._protect.run(finalize, function (enter, exit) {

enter(function () {
request._protect.run(finalize, function (exit) {

strategy.payload.call(null, request, exit);
});
strategy.payload.call(null, request, exit);
});
};

Expand All @@ -352,11 +345,8 @@ internals.Auth.response = function (request, next) {
return next();
}

request._protect.run(next, function (enter, exit) {

enter(function () {
request._protect.run(next, function (exit) {

strategy.response.call(null, request, exit);
});
strategy.response.call(null, request, exit);
});
};
46 changes: 20 additions & 26 deletions lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@ exports.execute = function (request, next) {
return next(); // Must not include an argument
};

request._protect.run(finalize, function (enter, exit) {
request._protect.run(finalize, function (exit) {

enter(function () {

if (request._route.prerequisites) {
internals.prerequisites(request, Utils.once(exit));
}
else {
internals.handler(request, exit);
}
});
if (request._route.prerequisites) {
internals.prerequisites(request, Utils.once(exit));
}
else {
internals.handler(request, exit);
}
});
};

Expand Down Expand Up @@ -370,29 +367,26 @@ exports.invoke = function (request, event, callback) {
return Utils.nextTick(callback)();
}

request._protect.run(callback, function (enter, exit) {

enter(function () {
request._protect.run(callback, function (exit) {

Async.forEachSeries(exts.nodes, function (ext, next) {
Async.forEachSeries(exts.nodes, function (ext, next) {

var finalize = function (err, result) {
var finalize = function (err, result) {

return (err === undefined && result === undefined ? next() : reply._root(err || result));
};
return (err === undefined && result === undefined ? next() : reply._root(err || result));
};

finalize.env = ext.env;
finalize.env = ext.env;

var filter = function (result) {
var filter = function (result) {

return next(result.source !== null ? result : null);
};
return next(result.source !== null ? result : null);
};

var reply = exports.replyInterface(request, filter, finalize);
var bind = (ext.bind || (ext.env && ext.env.bind));
var reply = exports.replyInterface(request, filter, finalize);
var bind = (ext.bind || (ext.env && ext.env.bind));

ext.func.call(bind, request, reply);
}, exit);
});
ext.func.call(bind, request, reply);
}, exit);
});
};
20 changes: 6 additions & 14 deletions lib/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ var Utils = require('./utils');
var Defaults = require('./defaults');
var Ext = require('./ext');
var Methods = require('./methods');
var Protect = require('./protect');
var Handler = require('./handler');
var Hapi = require('./index');

Expand Down Expand Up @@ -38,7 +37,6 @@ exports = module.exports = internals.Pack = function (options) {
this._env = {}; // Plugin-specific environment (e.g. views manager)
this._caches = {}; // Cache clients
this._methods = new Methods(this); // Server methods
this._protect = new Protect(this);
this._handlers = {}; // Registered handlers

this.list = {}; // Loaded plugins by name
Expand Down Expand Up @@ -554,7 +552,7 @@ internals.packagePath = function (name, packageFile) {
var keys = Object.keys(require.cache);
var i = 0;
var il = keys.length;

while (path === null && i < il) {
var key = keys[i];
if (key.indexOf(packageFile) === key.length - packageFile.length) {
Expand Down Expand Up @@ -708,18 +706,12 @@ internals.Pack.prototype._invoke = function (event, callback) {
return Utils.nextTick(callback)();
}

self._protect.run(callback, function (enter, exit) {

enter(function () {

Async.forEachSeries(exts.nodes, function (ext, next) {
Async.forEachSeries(exts.nodes, function (ext, next) {

ext.func.call(null, ext.env.root, next);
},
function (err) {
ext.func.call(null, ext.env.root, next);
},
function (err) {

return exit(err);
});
});
return callback(err);
});
};
20 changes: 4 additions & 16 deletions lib/protect.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ exports = module.exports = internals.Protect = function (parent) {

var self = this;

this._parent = parent;
this._error = null;

this._baseDomain = Domain.createDomain(); // Envelope domain to force exit all nested inner domains

this.domain = Domain.createDomain();
this.domain = Domain.create();
this.domain.on('error', function (err) {

var handler = self._error;
Expand All @@ -28,22 +25,20 @@ exports = module.exports = internals.Protect = function (parent) {
return handler(err);
}

self._parent.log(['hapi', 'internal', 'implementation', 'error'], err);
parent.log(['hapi', 'internal', 'implementation', 'error'], err);
});
};


internals.Protect.prototype.run = function (next, setup) { // setup: function (enter, exit)
internals.Protect.prototype.run = function (next, enter) { // enter: function (exit)

var self = this;

Utils.assert(!this._error, 'Invalid nested use of protect.run()');

var finish = function (/* arguments */) {

self._baseDomain.exit(); // Forces exit from this.domain along with any nested domains
self._error = null;

return next.apply(null, arguments);
};

Expand All @@ -54,12 +49,5 @@ internals.Protect.prototype.run = function (next, setup) { // setup: fu
return finish(Boom.badImplementation('Uncaught error', err));
};

var protect = function (run) {

self._baseDomain.enter();
self.domain.enter();
run();
};

setup(protect, finish);
enter(finish);
};
5 changes: 4 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,10 @@ internals.Server.prototype._dispatch = function (options) {

// Execute request lifecycle

request._execute();
request._protect.domain.run(function () {

request._execute();
});
}
};
};
Expand Down
2 changes: 0 additions & 2 deletions test/ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ var ChildProcess = require('child_process');
var Lab = require('lab');
var Hapi = require('..');
var Ext = require('../lib/ext');
var Handler = require('../lib/handler');
var Protect = require('../lib/protect');


// Declare internals
Expand Down
4 changes: 2 additions & 2 deletions test/protect.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('Protect', function () {

plugin.after(function (plugin, afterNext) {

var client = new Client(); // Created in the server domain
var client = new Client(); // Created in the global domain
plugin.bind({ client: client });
afterNext();
});
Expand All @@ -98,7 +98,7 @@ describe('Protect', function () {

this.client.on('event', request.domain.bind(function () {

throw new Error('boom'); // Caught by the server domain by default, not request domain
throw new Error('boom'); // Caught by the global domain by default, not request domain
}));

this.client.emit('event');
Expand Down

0 comments on commit 9caab29

Please sign in to comment.