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

midware-pool

v0.1.2

Published

Tiny module implementing a pool of connect-style domain-agnostic middleware layers. Runs in the browser and node.js

Downloads

3,852

Readme

midware-pool Build Status

midware-pool is a tiny module to create a pool of connect-style domain-agnostic middleware layers for any node.js or browser application. It uses midware behind the scenes.

Supports variadic arguments, stack manipulation and middleware inheritance. It's just ~180 SLOC.

To get started, see the example below or the API usage docs.

Example

var pool = require('midware-pool')()
var message = {}

// Create foo middleware stack
pool.use('foo', function(msg, next) {
  // msg === message
  next()
})

// Create another middleware stack
pool.use('bar', function(msg, next) {
  // msg === message
  next()
})

// Run both middlewares stacks
pool.run('foo', message, function(err) {
  if (err) return console.log(err)
  
  // Run next middleware stack
  pool.run('bar', message, function(err) {
    console.log('End')
  })
})

Installation

Node.js

To install midware-pool in a Node application use npm.

npm install midware-pool

Browser

Via bower:

bower install midware-pool

Via component:

component install h2non/midware-pool

Or loading the script:

<script src="//cdn.rawgit.com/h2non/midware-pool/0.1.2/midware-pool.js"></script>

Testing

$ npm install
$ npm test

Usage

Basic setup

Middleware is useful for creating a plugin system or configuring anything within an application. To use midware just require it and make a call to the module.

var pool = require('midware-pool')()

This will return a use function which when passed a callback will add it a waterfall sequence that will be invoked one after the other whenever the middleware is run.

pool.use('foo', function(next) {
  // mad science here
  next()
})

Callbacks are given a next function which will always be the last argument. Calling next will tell the middleware to call the next callback in the use sequence or will complete its run. To run the callback sequence call the method run on the use function.

pool.run('foo', function (err, end) {
  if (err) return console.log(err)
  if (end) console.log(end)
  // all done professor
})

run takes any amount of parameters that the callbacks will passed whenever run.

pool.use('foo', function (first, last, next) {
  console.log('Hello %s, %s', first, last)
  next()
})

pool.run('foo', 'Chunk', 'Norris')

Stopping

Whenever a callback should throw an exception or wish to stop the middleware from running any more calls. Give next an error or explicitly tell it stop.

pool.use('foo', function(next) {
  next(new Error()) // stops middleware and gives error
  next(null, true) // tells middleware to stop
})

pool.run('foo', function (err, ended) {
  // ...
})

Apply Context

Instead of binding context to callbacks, send the context to midware.

var context = {}
var pool = midwarePool()
pool.useCtx(context)

pool.use('foo', function (next) {
  // this === context
  next()
})

pool.run('foo', function(err) {
  // this === context
})

Removing a function

You can remove registered functions in the middleware via its function name or function reference

var pool = midwarePool()
pool.use('foo', function test(next) {
  next()
})
pool.remove('foo', 'test') // by function name
var pool = midwarePool()
function test(next) {
  next()
}
pool.use('foo', test)
pool.remove('foo', test) // by function reference

API

pool([parent]) => Pool

Pool#use(name, ...middleware)

Pool#remove(name, middlewareName|function)

Pool#run(name, [args...], [done])

Pool#runParent(name, [args...], [done])

Pool#registered(name) => boolean

Pool#useParent(pool)

Pool#useCtx(ctx)

Pool#flush(name)

Pool#flushAll()

Pool#stack(name) => midware

Pool#pool = { ...name: midware }

pool.midware => midware

pool.Pool => Pool

License

MIT

Copyright (c) 2015 Tomas Aparicio