Skip to content

Commit

Permalink
use chain
Browse files Browse the repository at this point in the history
  • Loading branch information
retrohacker committed Mar 14, 2019
1 parent ef17834 commit 7db83a5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 25 deletions.
63 changes: 44 additions & 19 deletions lib/server.js
Expand Up @@ -117,7 +117,10 @@ function Server(options) {

this.onceNext = !!options.onceNext;
this.strictNext = !!options.strictNext;
this.earliestChain = [];
this.earliestChain = new Chain({
onceNext: this.onceNext,
strictNext: this.strictNext
});
this.preChain = new Chain({
onceNext: this.onceNext,
strictNext: this.strictNext
Expand Down Expand Up @@ -218,18 +221,36 @@ function Server(options) {
res.writeContinue();
}

self._onRequest(req, res);
self.earliestChain.run(req, res, function earliestDone(ignore) {
if (ignore === false || Boolean(ignore)) {
return;
}
self._onRequest(req, res);
});
});

if (options.handleUpgrades) {
this.server.on('upgrade', function onUpgrade(req, socket, head) {
req._upgradeRequest = true;
var res = upgrade.createResponse(req, socket, head);
self._onRequest(req, res);
req._upgradeRequest = true;
self.earliestChain.run(req, res, function earliestDone(ignore) {
if (ignore === false || Boolean(ignore)) {
return;
}
self._onRequest(req, res);
});
});
}

this.server.on('request', this._onRequest.bind(this));
this.server.on('request', function onRequest(req, res) {
self.earliestChain.run(req, res, function earliestDone(ignore) {
if (ignore === false || Boolean(ignore)) {
return;
}

self._onRequest(req, res);
});
});

this.__defineGetter__('maxHeadersCount', function getMaxHeadersCount() {
return self.server.maxHeadersCount;
Expand Down Expand Up @@ -527,11 +548,15 @@ Server.prototype.pre = function pre() {
* })
*/
Server.prototype.earliest = function earliest() {
var args = Array.prototype.slice.call(arguments);
for (var i = 0; i < args.length; i++) {
assert.func(args[i]);
this.earliestChain.push(args[i]);
}
var self = this;
var handlers = Array.prototype.slice.call(arguments);

argumentsToChain(handlers).forEach(function forEach(handler) {
handler._name =
handler.name || 'earliest-' + self.earliestChain.count();
self.earliestChain.add(handler);
});

return this;
};

Expand Down Expand Up @@ -710,6 +735,9 @@ Server.prototype.getDebugInfo = function getDebugInfo() {
// output the actual routes registered with restify
var routeInfo = self.router.getDebugInfo();

var earliestHandlers = self.earliestChain
.getHandlers()
.map(funcNameMapper);
var preHandlers = self.preChain.getHandlers().map(funcNameMapper);
var useHandlers = self.useChain.getHandlers().map(funcNameMapper);

Expand All @@ -731,6 +759,7 @@ Server.prototype.getDebugInfo = function getDebugInfo() {
address: addressInfo && addressInfo.address,
port: addressInfo && addressInfo.port,
inflightRequests: self.inflightRequests(),
earliest: earliestHandlers,
pre: preHandlers,
use: useHandlers,
after: self.listeners('after').map(funcNameMapper)
Expand Down Expand Up @@ -792,6 +821,11 @@ Server.prototype.toString = function toString() {

str += sprintf(LINE_FMT, 'Accepts', this.acceptable.join(', '));
str += sprintf(LINE_FMT, 'Name', this.name);
str += sprintf(
LINE_FMT,
'Earliest',
handlersToString(this.earliestChain.getHandlers())
);
str += sprintf(
LINE_FMT,
'Pre',
Expand Down Expand Up @@ -853,15 +887,6 @@ Server.prototype.toString = function toString() {
Server.prototype._onRequest = function _onRequest(req, res) {
var self = this;

// Give the earliest chain the earliest possible opportunity to process
// this request before we do any work on it
var earliestChain = self.earliestChain;
for (var i = 0; i < earliestChain.length; i++) {
if (earliestChain[i](req, res) === false) {
return;
}
}

this.emit('request', req, res);

// Skip Socket.io endpoints
Expand Down
13 changes: 7 additions & 6 deletions test/server.test.js
Expand Up @@ -2563,10 +2563,10 @@ test('earliest chain should get to reject requests', function(t) {
t.fail('should not call handler');
});

SERVER.earliest(function(req, res) {
SERVER.earliest(function(req, res, next) {
res.statusCode = 413; // I'm a teapot!
res.end();
return false;
return next(false);
});

CLIENT.get('/foobar', function(_, __, res) {
Expand All @@ -2581,8 +2581,8 @@ test('earliest chain should get to allow requests', function(t) {
return next();
});

SERVER.earliest(function(req, res) {
return true;
SERVER.earliest(function(req, res, next) {
return next();
});

CLIENT.get('/foobar', function(_, __, res) {
Expand All @@ -2598,8 +2598,9 @@ test('earliest chain should allow multiple handlers', function(t) {
});

var count = 0;
var handler = function() {
return count++;
var handler = function(_, __, next) {
count++;
return next();
};

SERVER.earliest(handler, handler, handler);
Expand Down

0 comments on commit 7db83a5

Please sign in to comment.