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

middlewarer

v1.0.0

Published

Middleware execution chain library

Downloads

4

Readme

Middlewarer

Middleware execution chain library.

npm

Creates callable middleware chains, similar to those used by express#use.

Callbacks in the chain are passed a variable number of arguments followed by a next callback which can be called to pass execution on to the next callback in the chain.

This library also exposes TypeScript helper types (and Async variants) Callable, Consumer, Provider, and Runnable, similar to those from Java.

Installation

$ yarn add middlewarer

or, with NPM

$ npm install middlewarer

Usage

CommonJS:

const { AsyncController, Controller } = require("middlewarer");

ES Modules/TypeScript:

import { AsyncController, Controller } from "middlewarer";

Controller#chain(callback: (...args, next) => any): Controller

Adds a callback to the chain. next is a callback which invokes the next callback in the chain and should be called by your callback except in conditions where the chain should be stopped. next does not accept any parameters.

If you wish to maintain a shared state between chain steps, you should modify an external variable or pass a state object to Controller#run.

Controller#run(...args): Controller

Starts the chain execution. Note: if all functions in the chain are synchronous, then this call will also be synchronous. However, if any function is asynchronous in how it calls next(), then this function will return once that async execution begins.

If you need to perform all asynchronous functions before returning from run, use an AsyncController instead.

AsyncController#chain(callback: (...args, next) => Promise<any>): AsyncController

Identical to Controller#chain, except callback should be Promise-returning and next is asynchrnous, and thus should be called with await.

AsyncController#run(...args): Promise<AsyncController>

Similar to Controller#run. Executes and awaits all chained callbacks.


Synchronous Example

import { Controller, Runnable } from "middlewarer";

const controller = new Controller<[number]>(); // Callback parameter type array

controller
  .chain((num, next) => {
    console.log(`Chain 0: ${num}`);
    next(); // Continues chain
  })
  .chain((num, next) => {
    console.log(`Chain 1: ${num}`);
    if (num === 42) next(); // Only continue chain if num is 42
  })
  .chain((num, next) => {
    console.log(`The answer has been found. You just lost The Game.`);
  });

// ... Later (or in function chain), call the controller

controller.run(4); // Will get to chain 0 and 1, but not the answer

controller.run(42); // Will trigger all three chained callbacks

Asynchronous Example

import { AsyncController, Runnable } from "middlewarer";

// A dummy representing an asynchronous API call
async function checkAnswer(value: number): Promise<boolean> {
  return value === 42;
}

const controller = new AsyncController<[number]>();

controller
  .chain(async (num, next) => {
    console.log(`Chain 0: ${num}`);
    await next();
  })
  .chain(async (num, next) => {
    console.log(`Chain 1: ${num}`);
    if (await checkAnswer(num)) await next(); // Continue dependent on async API result
  })
  .chain(async (num, next) => {
    console.log(`The answer has been found. You just lost The Game.`);
  });

await controller.run(4);

await controller.run(42);

License

Licensed under the GNU Affero General Public License v3.0 license. See LICENSE in the root of this project or https://www.gnu.org/licenses/agpl-3.0.en.html for more info.

Copyright © 2020 Brenden Campbell.