
Security News
npm ‘is’ Package Hijacked in Expanding Supply Chain Attack
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
parallel-ware
Advanced tools
Execute middleware in parallel. For executing in series, see ware.
The following example runs all the middleware in parallel.
var parallel = require('parallel-ware');
function add (val) {
return function (calc, next) {
calc.sum += val;
console.log(calc.sum);
next();
};
}
// executes in parallel, no order guarenteed
var middleware = parallel()
.use(add(1))
.use(add(2))
.use(add(3));
middleware.run({ sum: 0 }, function (err, result) {
// all the middleware has run, calc.sum equals 6
});
// 2
// 5
// 6
You can also tell parallel-ware to wait to execute a middleware until a specific condition is met. The .when(wait, fn)
method takes the wait function as an optional first parameter.
var parallel = require('parallel-ware');
function add (val) {
return function (calc, next) {
calc.sum += val;
console.log(calc.sum);
next();
};
}
function equals (val) {
return function (calc, next) {
next(null, calc.sum === val);
};
}
var middleware = parallel()
.use(add(1))
.when(equals(4), add(2)) // executes after sum === 4
.when(equals(10), add(10)) // executes after sum === 10, which never happens
.use(add(3));
middleware.run({ sum: 0 }, function (err, calc) {
// all the middleware has run, calc.sum equals 6
});
// 3
// 4
// 6
You can limit the concurrency with which the middleware will get executed.
var middleware = parallel()
.concurrency(2)
.use(constantContact())
.use(linkedin())
.use(facebook())
.use(github());
And you can listen for progress events (following the same structure as visionmedia/batch progress events).
var middleware = parallel()
.use(constantContact())
.use(linkedin())
.use(facebook())
.use(github());
middleware.on('progress', function (progress) {
});
middleware.run({ email: 'michael.bolton@initech.com' });
Middleware errors don't stop the overall execution, but are rather passed back in batch.
var parallel = require('parallel-ware');
function add (val) {
return function (calc, next) {
calc.sum += val;
next();
};
}
function fail (animal) {
return function (calc, next) {
next(new Error(animal));
};
}
// executes in parallel, no order guarenteed
var middleware = parallel()
.use(add(1))
.use(fail('porcupine'))
.use(add(2))
.use(fail('whale'))
.use(add(3));
middleware.run({ sum: 0 }, function (err, calc) {
if (err) {
console.log(err);
console.log(err.errors)
}
console.log(calc.sum);
});
// Error: 2 error(s) encountered.
// [Error: Whale, Error: Porcupine]
// 6
Create a parallel middleware execution pipeline.
Add a middleware fn
to the execution pipeline.
Add a middleware fn
to the execution pipeline to run after the wait
says it can.
The wait
function supports a synchronous signature:
function equals (val) {
return function (calc) {
return calc.sum === val;
};
}
var middleware = parallel()
.use(add(1))
.when(equals(4), add(2)) // executes after sum === 4
.when(equals(10), add(10)) // executes after sum === 10, which never happens
.use(add(3));
middleware.run({ sum: 0 }, function (err, calc) {
// all the middleware has run, calc.sum equals 6
});
And an asynchronous signature:
function equals (val) {
return function (calc, next) {
next(null, calc.sum === val);
};
}
var middleware = parallel()
.use(add(1))
.when(equals(4), add(2)) // executes after sum === 4
.when(equals(10), add(10)) // executes after sum === 10, which never happens
.use(add(3));
middleware.run({ sum: 0 }, function (err, calc) {
// all the middleware has run, calc.sum equals 6
});
Limit the amount of concurrently executing middleware to max
.
Listen on progress events.
WWWWWW||WWWWWW
W W W||W W W
||
( OO )__________
/ | \
/o o| MIT \
\___/||_||__||_|| *
|| || || ||
_||_|| _||_||
(__|__|(__|__|
FAQs
Executes middleware in parallel
The npm package parallel-ware receives a total of 13 weekly downloads. As such, parallel-ware popularity was classified as not popular.
We found that parallel-ware demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Security News
A critical flaw in the popular npm form-data package could allow HTTP parameter pollution, affecting millions of projects until patched versions are adopted.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.