npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

middleware-flow

v0.8.0

Published

Middleware control flow library: series, parallel, or, and

Downloads

4,071

Readme

middleware-flow Build Status

Middleware control flow library

Installation

npm install middleware-flow

Examples

series(middlewares...)

var series = require('middleware-flow').series;
var app = require('express')();

app.use(series(mw1, mw2, mw2)); // equivalent to app.use(mw1, mw2, mw3);

parallel(middlewares...)

var parallel = require('middleware-flow').parallel;
var app = require('express')();
                                  // runs the middlewares in 'parallel'
app.use(parallel(mw1, mw2, mw2)); // if err, returns the first error that occurred

parallelWait(middlewares...)

var parallelWait = require('middleware-flow').parallelWait;
var app = require('express')();
                                  // runs the middlewares in 'parallel' and waits for all of them before to return also in case of err
app.use(parallelWait(mw1, mw2, mw2)); // if err, returns the first error that occurred

each

var each = require('middleware-flow').each;
var arr = [1,2,3];
var app = require('express')();
                                  // runs the middlewares in 'parallel'
app.use(
  each(arr,
    // runs the middlewares in parallel
    function (eachReq, res, next) {
      // eachReq is a scoped req for the each function that reads from req,
      // but writes to it's own scope (prototypically inherits from request)
    },
    function (item, req, eachReq, res, next) {
      // if middleware accepts five arguments, the current item and the original req are passed
      // eachReq is a scoped req for the each function that reads from req,
      // but writes to it's own scope (prototypically inherits from request)
    })
); // if err, returns the first error that occurred

or(middlewares...)

var or = require('middleware-flow').or;
var app = require('express')();
                                             // runs the middlewares in series, until one passes (no next(err));
app.use(or(user.isOwner, user.isModerator)); // if err, returns the first error that occurred

and(middlewares...)

Same as series.

if(value).then(middlewares...).else(middlewares...)

var if = require('middleware-flow').if;
var app = require('express')();

app.use(
  if(true)
    .then(one, two, three)
    .else(error)
);

syncIf(fn).then(middlewares...).else(middlewares...)

var syncIf = require('middleware-flow').syncIf;
var app = require('express')();

app.use(
  syncIf(nameQueryExists)   // accepts a sync function that returns a boolean
    .then(one, two, three)  // true -> then, error -> skips all next(err)
    .else(error)
);
function nameQueryExists (req, res) {
  return exists(req.query.name);
}
function exists (val) {
  return val !== null && val !== undefined;
}

asyncIf(fn).then(middlewares...).else(middlewares...)

var asyncIf = require('middleware-flow').asyncIf;
var or = require('middleware-flow').or;
var fs = require('fs');
var app = require('express')();

app.use(
  asyncIf(bodyFileExists)    // expects boolean as the result argument
    .then(one, two, three)   // true -> then, false -> else, error -> skips all next(err)
    .else(other)
);
function logExists (req, res, cb) {
  fs.exists(req.body.file, function (exists) {
    cb(null, exists);
  });
}

mwIf(middleware).then(middlewares..).else(middlewares..)

var mwIf = require('middleware-flow').mwIf;
var app = require('express')();

app.use(
  mwIf(userIsModerator)    // error here, just runs the else middlewares
    .then(one, two, three) // no error -> then, error -> else
    .else(other)           // if other is an error middleware it will recieve
                           //   the error else the error will be ignored
);
function userIsModerator (req, res, next) {
  if (!req.user.isModerator) {
    next(new Error('access denied'));
  }
  else {
    next();
  }
}

try(middlewares..).catch(middlewares..)

var flow = require('middleware-flow');
var app = require('express')();

app.use(
  flow.try(saveUser) // error here, just runs the catch middlewares
    .catch(rollback) // no error -> other, error -> rollback
                     // if rollback is an error middleware it will recieve
                     //   the error else the error will be ignored
);
function saveUser (req, res, next) {
  db.save(req.user, next);
}

bg(middlewares...)

app.use(
  flow.bg(mw1, mw2, mw2)
); // runs the middlewares in series in the background

License

MIT