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-wrap-emitter

v1.0.0

Published

EventEmitter with middleware.

Downloads

14

Readme

middleware-wrap-emitter Build Status

Forked and Inspired by [middleware-emitter][https://github.com/jarradseers/middleware-emitter]. The libraries are similar except, this library wraps an emitter instead of inheriting one.

Use middleware to chain your event logic, compatible with express middleware (and possibly others). This allows you to not only break up logic for when an event is fired, but let's you share middleware between frameworks if necessary.

The middleware-wrap-emitter project also brings multiple event triggering and capturing.

req The req is the 'request', req object which can be used as context or whatever you want.

res The res is the 'response', is the context on the response, it's used to build up the output to be used later on.

You can think of the two like this - req = internal (chain only, imagine storing all data for calculation), res = external, when you are at the end of the chain - it's this object that you will want to use for display.

Usage

Assuming you have broken up your async (promised based) middleware functions:

const emitter = require('middleware-wrap-emitter');
const app = require('./middleware/app');

emitter
  .on('hello', app.handleError, app.load, app.hello, app.output)
  .emit('hello');

Just like koa error handling middlewares should be defined early on.

Installation

$ npm install middleware-wrap-emitter

Features

  • Create an event middleware chain.
  • Inject middleware functions.
  • Build up the res chain for output.
  • Build up the req chain for internal chain state.
  • Listen to multiple events on the same chain.
  • Emit multiple events at once.
  • Simple, fast, light-weight.
  • Written in ES6+ for node.js 8+.
  • Test driven.

Options

MiddlewareWrapEmitter extends the base EventEmitter class, therefore all standard options apply.

Examples

A simple standalone example:

const Emitter = require('middleware-wrap-emitter');

const emitter = new Emitter() || new Emitter({}, require('election').ipcMain);

emitter
  .on(
    'hello',

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

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

  .emit('hello');

Listen / emit multiple events:

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

    async ({ req, res }, next) => {
      console.log(req.event.name);
    }
  )

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

// hello
// other
// test

Inject data into the req (request) context:

emitter
  .on(
    'inject',

    { hello: 'world' },

    async ({ req, res }, next) => {
      await next();
      console.log(req.ctx);
    }
  )

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

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

If you add a function with the 4th parameter of 'err', you can gracefully handle errors:

emitter
  .on(
    'ohno',
    async ({ req, res }, next) => {
      try {
        await next();
      } catch (err) {
        console.error(err); // Oh no, something went wrong...
      }
    },
    async (req, res, next) => {
      await next();
    },
    async ({ req, res }, next) => {
      await next();
      new Error('Oh no, something went wrong...');
    }
  )
  .emit('ohno');

// Error: Oh no, something went wrong... + stack, will not continue!

Error handling should be done early on in the chain. If an error ocurs and is handled the rest of the chain is not processed. Therefor you need to handle your errors!

Check out the test folder for more!

Tests

From the package

$ npm test

License

MIT