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 🙏

© 2026 – Pkg Stats / Ryan Hefner

middleware-emitter

v1.0.5

Published

EventEmitter with middleware chains.

Readme

Middleware Emitter

CI npm

An EventEmitter whose listeners are middleware chains. Break the logic for an event into small (req, res, next) functions, the same shape as express middleware, and share them between events.

It also lets you listen to, and emit, several events at once.

Installation

$ npm install middleware-emitter

Usage

const MiddlewareEmitter = require('middleware-emitter');

const emitter = new MiddlewareEmitter();

emitter.on('hello',

  (req, res, next) => {
    res.ctx.hello = 'world';
    next();
  },

  (req, res) => {
    console.log(res.ctx); // { hello: 'world' }
  })

  .emit('hello');

Each emit runs the chain from the start with a fresh req and res:

  • req.ctx is the request context: the data passed to emit, plus any objects in the chain. Use it for state the chain needs internally.
  • res.ctx is the response context: build up the output here.
  • req.event.name is the name of the event that fired.
  • next() moves on to the next middleware. A middleware that does not call it ends the chain.

Inside a middleware declared with function, this is the emitter.

API

MiddlewareEmitter extends Node's EventEmitter, and its constructor takes the same options.

| Method | Description | |---|---| | on(events, ...chain) | Run the chain every time an event fires. events is a name or an array of names. | | once(events, ...chain) | The same, for the first time each event fires only. | | emit(events, data) | Fire a name or an array of names. data is an object, merged into req.ctx. |

All three return the emitter, so calls can be chained. Note that emit therefore does not return a boolean as it does on EventEmitter.

A chain can hold middleware functions, error handlers, plain objects, and arrays of any of those.

The other EventEmitter methods are untouched. removeAllListeners and listenerCount work as usual; off and removeListener cannot remove a chain, because the chain is wrapped in a single listener.

Examples

Listen to and emit several events:

emitter.on(['hello', 'other', 'test'],

  (req) => {
    console.log(req.event.name);
  })

  .emit(['hello', 'other', 'test']);

// hello
// other
// test

Inject data into the request context:

emitter.on('inject',

  { hello: 'world' },

  (req) => {
    console.log(req.ctx);
  })

  .emit('inject', { some: 'data' });

// { some: 'data', hello: 'world' }

Handle errors with a function that takes a fourth err parameter:

emitter.on('ohno',

  (req, res, next) => {
    next(new Error('Oh no, something went wrong...'));
  },

  (req, res, next, err) => {
    console.error(err.message);
    next();
  },

  () => {
    console.log('But we continued anyway.');
  })

  .emit('ohno');

// Oh no, something went wrong...
// But we continued anyway.

When an error is passed to next, the next error handler in the chain is called. If there is none later in the chain, the most recently found one is used, and if the chain has no error handler the error is thrown. Error handlers are skipped when there is no error.

Tests

$ npm install
$ npm test

License

MIT