
Security News
Open Source Maintainers Feeling the Weight of the EU’s Cyber Resilience Act
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
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
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 EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.