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-chain-js

v2.0.2

Published

A middleware chain component for node and the browser

Downloads

36

Readme

middleware-chain

A middleware chain component for Node and the Browser.

Build Status CodeFactor

Current version: 2.0.2

Lead Maintainer: Halim Qarroum

Install

npm install --save middleware-chain-js

Motivations

The chain of responsibility pattern is pretty useful in a lot of components I write. It allows to separate responsibilities in the form of pluggable handlers along a processing chain which decouples the treatments made on the chain, while having the ability to declare in which order they will be executed.

It also makes hot-plugging of new treatments possible while the application is running, and exposes a clean and evolutive interface.

The programming interface provided by this component draws its inspiration from the Express framework, in which routers and middlewares are in charge of sequentially treating received requests.

Semantics

A chain holds a collection of middlewares, and iterates over them as a treatment is handled.

A middleware is a software component that handles an input, decides whether it can handle it, and if it does, will execute a particular routine producing an output. If a middleware cannot handle an input, the next middleware in the chain is called.

What an input and an output concretely are, is dependant on the implementation, and the client of the library will have to provide them. However, in order to keep an appropriate logic, their semantics should not change.

Usage

Requiring the library

The entry point of the library can be required through different means, each one depending on the environment in which the library is executed.

ESM

import Chain from 'middleware-chain-js';

Common JS

const gravatar = require('middleware-chain-js');

Browser (ESM)

import gravatar from 'https://unpkg.com/middleware-chain-js/dist/index.esm.js'

Browser (UMD)

<script src="https://unpkg.com/middleware-chain-js/dist/index.umd.js"></script>

Creating a chain

Creating a middleware chain is as simple as calling its constructor. A new instance does not contain any middleware.

const chain = new Chain();

Adding middlewares

It is then possible to push new middlewares in the chain using the .use method.

chain.use(function (input, output, next) {
  // Perform some treatment.
  next();
});

It is also possible to push many middlewares at once using an array, which comes in handy when grouping a particular set of actions together.

chain.use([
    function (input, output, next) {
        // Verify for instance that the
        // `input` is correctly formatted.
        next();
    },

    function (input, output, next) {
        // Perform some treatment on
        // the `input`.
        next();
    }
]);

Similarly, you can push many middlewares by simply passing them in the argument list of .use.

Middleware lifecycle

When a middleware is called in the chain, it can handle an input, and interact with the output. If the middleware cannot handle the given input, it should call the next middleware in the chain.

chain.use(function (input, output, next) {
  if (!handled(input)) {
    // Calling the next middleware.
    return next();
  }
  // Otherwise, we treat the input.
  // In this example we "pipe" the input
  // with the output.
  input.stream.pipe(output.stream);
});

Error handlers

Sometimes, it is useful to signal that an error occurred along the chain and to have an approriate handler gracefully taking care of it. As such, it is possible to insert error handlers in the chain and to signal an error in a middleware.

// A regular middleware can trigger an `Error`
// object by passing it to the next function.
chain.use(function (input, output, next) {
  next(new Error('An error occurred');
});

// An event handler can be recognized because he
// declares an error object as first argument.
chain.use(function (err, input, output, next) {
  // Handle the error, or forward it to another error
  // handler using `next`.
});

Once an Error has been triggered by a middleware, the next error callback will be called right away, and subsequent regular middleware will not be called in this case. If no error handlers are declared, the chain processing is stopped.

Triggering the chain

In order to start the chain and process the input, you need to call the .handle method. It needs two arguments, being respectively the input and the output.

// In this example, we pass to the middleware
// chain an input stream, and an output stream.
chain.handle({ stream: input }, { stream: output });