Fix context propagation broken by async@2.x

Rework the REST middleware to use a hand-written version of
"async.eachSeries". Before this change, we were loosing CLS context
when the application was relying on the REST middleware to load the
context middleware.

This is fixing a problem introduced by post-1.0 versions of async,
which we upgraded to via fea3b781a.
This commit is contained in:
Miroslav Bajtoš 2018-10-15 13:30:20 +02:00
parent 43a1f537db
commit 228bc7519b
No known key found for this signature in database
GPG Key ID: 6F2304BA9361C7E3
1 changed files with 16 additions and 3 deletions

View File

@ -73,8 +73,21 @@ function rest() {
if (handlers.length === 1) {
return handlers[0](req, res, next);
}
async.eachSeries(handlers, function(handler, done) {
handler(req, res, done);
}, next);
executeHandlers(handlers, req, res, next);
};
}
// A trimmed-down version of async.series that preserves current CLS context
function executeHandlers(handlers, req, res, cb) {
var ix = -1;
next();
function next(err) {
if (err || ++ix >= handlers.length) {
cb(err);
} else {
handlers[ix](req, res, next);
}
}
}