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

supply

v0.0.4

Published

Supply your library with plugin and middleware management

Downloads

591

Readme

Supply

From bigpipe.ioVersion npmBuild StatusDependenciesCoverage Status

Supply is a minimal but high performance middleware system for Node.js. It's extremely flexible in term of usage.

Installation

npm install --save supply

Usage

In all examples we assume that you've already required and created your first supply instance using:

'use strict';

var Supply = require('supply')
  , supply = new Supply();

Extending the Supply instance can be done using the extend method which is exposed on the Supply constructor:

var MySupply = Supply.extend({
  another: function method() {
    // do stuff
  }
});

So you could use this pattern to build your own framework or module on top of a middleware system. Or override methods.

length

To see how many layers are in your middleware system, you can check the .length property.

supply.length; // 0
supply.use(function foo() {});
supply.length; // 1

use

Add a new middleware layer to the stack. This method accepts 3 arguments:

  1. name, Name of the middleware layer so we can easily remove it again if needed. If no name is provided we attempt to extract it from the supplied function. So if you have function foobar() {} as middleware we will use foobar as name.
  2. fn, Function which should be executed every. Please note that the callbacks will not have their this value set to supply.
  3. opts, Optional object which allows you to further configure the middleware handling. The following options are currently supported:
    • at Specify the index or name where this layer should be added at. If a name is supplied we will resolve it back to the it's current index.

When you add a new middleware layer it will always be added as last item unless you've specified the at option.

supply.use('foo', function (arg) {
  console.log('arg', arg, 'foo');
});

supply.use('bar', function (arg, next) {
  console.log('arg', arg, 'bar');
  next();
});

supply.each('pez', function () {
  console.log('done');
});

In the example above you can see that we support async and sync execution of the middleware. This is decided based on the amount of arguments supplied in the each method (excluding it's optional callback). If you call each with 2 arguments e.g. supply.each(1,2) then your async middleware layer needs 3 arguments where the last argument is the callback function.

The supplied middleware layers are also able to stop the execution of the rest of the middleware layers. In async mode you can supply the truthy value as second argument to the callback:

supply.use(function example(arg, next) {
  next(undefined, true);
});

If you have a sync function you can just return true:

supply.use(function example(arg) {
  return true;
});

Error handling also build in. The async middleware layers can just call the supplied callback with an error as first argument while the sync layers can just throw errors as they are wrapped in a try {} catch (e) {} statement.

before

Same as the use method, but it automatically sets the at option to 0 so it will be inserted at the beginning of the stack instead of the end. It also accepts all the same arguments, except for the at option as that will forcefully be overridden.

supply.before('xxx', function yyy() {});

remove

Remove a middleware layer from the stack based on the name. The method will return a boolean as indication if the layer was found and removed successfully.

supply.use('foo', function bar() {});
supply.remove('foo');

each

Call all layers in the middleware stack with the supplied arguments. There is no fixed limit to the amount of arguments that can be supplied. If the last argument is a function we automatically assume that this should be the callback for when all middleware layers are executed. The callback should follow the error first callback pattern.

supply.each('foo', 'bar'); // No callback
supply.each('beep', 'boop', function done(err, early) {

});

As you can see from the example above the callback receives two arguments. The error and an boolean which will indicate the callback was called early so one of the layers stopped the iteration.

indexOf

Find the location of a middleware layer in the stack based on the name. This is used internally but might be useful for you in some use cases as well.

supply.use('bar', function banana() {});
supply.use('foo', function bar() {});

var index = supply.indexOf('foo'); // 1 (it's zero based)

destroy

Destroy the middleware instance which removes all middleware layers, internal references and object we've setup. Don't call this if you still have a each running.

supply.destroy();

License

MIT