Remove unnecessary allocations from rest handler

Correctly cache the list of `preHandlers` so that they are built only
once in application lifetime.

Remove array concatenation, use a new callback function instead.
This commit is contained in:
Miroslav Bajtoš 2015-01-13 08:47:49 +01:00
parent 32ed60904c
commit 0058efb2ee
1 changed files with 11 additions and 5 deletions

View File

@ -23,6 +23,8 @@ module.exports = rest;
*/
function rest() {
var preHandlers;
return function restApiHandler(req, res, next) {
var app = req.app;
var restHandler = app.handler('rest');
@ -33,8 +35,6 @@ function rest() {
return res.send(app.remotes().toJSON());
}
var preHandlers;
if (!preHandlers) {
preHandlers = [];
var remotingOptions = app.get('remoting') || {};
@ -58,8 +58,14 @@ function rest() {
}
}
async.eachSeries(preHandlers.concat(restHandler), function(handler, done) {
handler(req, res, done);
}, next);
async.eachSeries(
preHandlers,
function(handler, done) {
handler(req, res, done);
},
function(err) {
if (err) return next(err);
restHandler(req, res, next);
});
};
}