
Security News
New CNA Scorecard Tool Ranks CVE Data Quality Across the Ecosystem
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.
middleware-async
Advanced tools
A handy tool to write async/promise style middleware for express, connect-like.
Lets check at this code
app.use(async (req, res, next) => {
req.user = await User.findById(req.params.id).exec()
next()
})
The next()
will be executed after User.findById(...).exec()
is fulfilled because express allow middleware returning Promise.
However, express does not support if promise returned by the middleware is rejected. The followings middlewares will never be called.
Solution is simple by wrapping the middleware with
import middlewaareAsync from 'middleware-async'
app.use(middlewaareAsync(async (req, res, next) => {
req.user = await User.findById(req.params.id).exec()
next()
}))
Install it via npm or yarn
npm install --save middleware-async
#or
yarn add middleware-async
middlewaareAsync(middlware)
: return a middleware that covers error thrown or error that is rejected by middleware
combineMiddlewares(list of middlewares or list of list of middlewares)
: combine many middlewares into one middleware. Very useful for testingmiddlewareToPromise
: convert express-style middleware into PromisecombineToAsync
: combination of middleewareToPromise
and combineMiddlewares
import middlewareAsync, {combineMiddlewares, combineToAsync, middlewareToPromise} from 'middleeware-async'
describe('combineMiddlwares', () => {
it('should go through all middlewares', async () => {
const req = {val: 0}
await combineToAsync([
async (req, res, next) => {
await Promise.resolve()
req.val += 1
next()
},
(req, res, next) => {
req.val++
next()
},
])(req)
expect(req.val).to.equal(2)
})
})
FAQs
A handy tool to work with async/promise express middleware
The npm package middleware-async receives a total of 1,221 weekly downloads. As such, middleware-async popularity was classified as popular.
We found that middleware-async 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 CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.
Research
/Security News
Two npm packages masquerading as WhatsApp developer libraries include a kill switch that deletes all files if the phone number isn’t whitelisted.
Research
/Security News
Socket uncovered 11 malicious Go packages using obfuscated loaders to fetch and execute second-stage payloads via C2 domains.