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

@ineentho/composable-middleware

v0.3.1

Published

Treat a sequence of middleware as middleware.

Downloads

1

Readme

composable-middleware

Treat a sequence of middleware as middleware.

Getting Started

Install the module with: npm install composable-middleware

var composable_middleware = require( 'composable-middleware' );

Documentation

Middleware is a function that follows specific conventions. Composable middleware allows a series of middleware functions to be used as if they were a single middleware function. With it, a middleware stack is middleware and becomes a first-class object. With composable middleware you can:

  • Define middleware that consists of several middleware steps and include it in another middleware stack.
  • Assign a middleware stack to a variable or index it in an object.
  • Form conditional or branching middleware paths, dynamically selecting middleware depending upon things like hostname, HTTP method or route.
  • Create a foundation middleware stack that is common to several sites or projects and includes commonly used operations, properly sequenced, and fill it in, as needed, with additions specific to a site.

You may compose middleware by defining its steps in the argument to the composable-middleware function:

var mw =
    composable_middleware(
        connect.logger(),
        connect.gzip(),
    );

or by using its use function:

var mw =
    composable_middleware()
        .use(connect.logger())
        .use(connect.gzip());

and then include it in another middleware stack:

var okay =
    composable_middleware()
        .use(mw)
        .use(
            function(req,res,next) {
                res.send('okay');
        })

You might then use that middleware in Connect, Express or any other middleware-based framework:

var app = connect();
app.use(okay);
app.listen(3000);

One of the design goals of this package is to minimize overhead when sequencing through middleware steps. Notably, it does not support mounting paths or routing. It does not examine the URL at all. That is best left to middleware, perhaps middleware that routes requests into different middleware stacks depending upon route.

The composable_middleware software also does not deal with errors or unhandled requests other than routing errors to middleware that expects an error argument. Since a middleware stack may be treated as middleware within another middleware stack, it cannot be assumed that reaching the end of the stack has any significance. You will need to provide middleware (perhaps we should call it 'finalware') to send out 404 responses or to log errors and send 500 responses if the overall framework in which the middleware is running does not do so.

Middleware conventions supported.

The Composable Middleware package supports Connect middleware and flatiron/union middleware as well as a hybrid of the two.

  • Connect normal middleware: (req,res,next)
  • Flatiron/union middleware: (). The request and response are in this.req and this.res. this.res.emit('next') passes the request to the next level of middleware.
  • Hybrid middleware: (next). The request and response are in this.req and this.res.
  • Connect error-handling middleware: (err,req,res,next)
  • Hybrid error-handling middleware: (err,next). The request and response are in this.req and this.res.

The middleware type is determined by checking its arity:

  • 0: flatiron/union
  • 1: hybrid normal
  • 2: hybrid error-handling
  • 3: Connect normal
  • 4: Connect error-handling

Error-handling middleware is skipped unless an error was passed to the next function. Normal middleware is skipped if there is an error.

Although not a convention shared by other middleware containers, this package calls all middleware serving a given request in the same context. It should thus be possible to attach an attribute to this in one middleware function and access it in a different middleware function serving the same request. This package will assure, however, that the global object is not used as the context, allocating a new object to serve as the context, if needed. To avoid the possibility of interference, if invoked as Connect middleware, composable-middleware will similarly replace the Connect context with a new context. If a new context is created, this._middleware_common_object will be defined. The constructor for the new context is exported as MiddlewareCommonObject and the function used by composable-middleware to test whether a new context is needed is exported as is_protected_content.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Status

Build Status

Release History

  • 0.3.0: Improves testing for the need to instantiate a new context to protext global variables.
  • 0.2.0: Same this object for all middleware serving a given request.
  • 0.1.0: Initial release.

License

Copyright (c) 2013 Randy McLaughlin Licensed under the MIT license.