middleware-flow
Advanced tools
Comparing version 0.3.0 to 0.3.1
71
index.js
var createCount = require('callback-count'); | ||
var flow = module.exports = { | ||
series: function (/* middlewares */) { | ||
var args = Array.prototype.slice.call(arguments); | ||
return function (req, res, next) { | ||
var middlewares = args.slice(); // copy | ||
step(middlewares.shift()); | ||
var error; | ||
function step (mw) { | ||
if (mw) { | ||
if (error) { | ||
mw(error, req, res, nextStep); | ||
} | ||
else { | ||
mw(req, res, nextStep); | ||
} | ||
} | ||
else { | ||
next(error); // done | ||
} | ||
} | ||
function nextStep (err) { | ||
if (err) { | ||
error = err; | ||
middlewares = middlewares.filter(lengthOf(4)); | ||
} | ||
step(middlewares.shift()); // continue | ||
} | ||
}; | ||
}, | ||
parallel: function (/* middlewares */) { | ||
var args = Array.prototype.slice.call(arguments); | ||
return function (req, res, next) { | ||
var count = createCount(next); | ||
var middlewares = args.slice(); // copy | ||
middlewares.forEach(function () { | ||
count.inc(); // inc first just in case the middlewares are sync | ||
}); | ||
middlewares.forEach(function (mw) { | ||
mw(req, res, count.next); | ||
}); | ||
}; | ||
}, | ||
or: function (/* middlewares */) { | ||
var args = Array.prototype.slice.call(arguments); | ||
return function (req, res, next) { | ||
var middlewares = args.slice(); // copy | ||
var firstErr; | ||
step(middlewares.shift()); | ||
function step (mw) { | ||
if (mw) { | ||
mw(req, res, nextStep); | ||
} | ||
else { | ||
next(firstErr); // done w/ err or no mw | ||
} | ||
} | ||
function nextStep (err) { | ||
if (err) { | ||
firstErr = firstErr || err; | ||
step(middlewares.shift()); // continue | ||
} | ||
else { | ||
next(); // done | ||
} | ||
} | ||
}; | ||
}, | ||
series: require('./lib/series'), | ||
parallel: require('./lib/parallel'), | ||
or: require('./lib/or'), | ||
next: function (req, res, next) { | ||
@@ -73,0 +8,0 @@ next(); |
{ | ||
"name": "middleware-flow", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Middleware control flow library: series, parallel, or, and", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
22444
726