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

middle-run

v2.0.1

Published

Run a series of async middleware on both client and server

Downloads

2,558

Readme

middle-run

Run a series of async middleware functions on both client and server.

npm Version Build Status JS Standard Style MIT License

middle-run is a simple library to compose and run ES7 async functions in order. This allows you to build out a process in a manner similar to Connect, only not necessarily specific to fulfilling a request on a server. middle-run can be used on both client and server.

Quick Example

npm install middle-run --save

import run from 'middle-run'

run([
  // middleware can be synchronous
  function (step) {
    // the namespace object is always passed as the first argument
    // the context property is shared across all middleware functions
    step.context.foo = 'bar'
    // the next middleware is automatically run after this completes
  },
  // async functions can be used for a "true" middleware
  async function ({ context, next }) {
    let start = Date.now()
    // next causes the next middleware to begin
    await next()
    // after awaiting next() to resolve, all downstream middleware are done
    context.duration = Date.now() - start
  },
  // since run returns a function with the same signature of middleware
  // you can compose multiple together if needed
  run([
    ({ resolve }) => {
      // all info is passed in the first argument, no need for `this`
      // resolve() prevents further downstream middleware from running
      // you can optionally resolve to a particular value,
      // the top-level promise will resolve to this value
      resolve('someValue')
    }
  ]),
  () => {
    // this won't run because `resolve()` was called
  }

// run returns a function with the same signature as a middleware function
// to start the series, pass in the desired context object
// you can optionally pass in functions that are called when the series completes
// and when the series is stopped
])({ context: {}, next() {}, resolve() {} })

API

run

import run from 'middle-run'
// or var run = require('middle-run')

run(middleware)({ context: {}, extra: 'stuff' })
  .then(function (value) {})

run is a function that takes an array of middleware functions, and returns a middleware function. Run the series of middleware by calling the function, and optionally pass in an object with properties to add to the argument given to each middleware.

middleware function

run([
  async ({ context, next, resolve }) => {
    let v1 = await next()
    let v2 = await resolve('foo')
    v1 === 'foo' // true
    v2 === 'foo' // true
  }
])

Each middleware function recieves a single object argument containing a context object, a next function, and a resolve function. Properties of the object passed to the function returned from run will also be added to the object passed.

context

By default, the context is an object that is part of the middleware argument. The same object will be passed to each and every middleware that runs.

You can replace the default object with whatever you like by passing a context property to the top-level middleware returned by run.

Any properties added to the root argument object will not be carried to next middleware, so if you need something shared, context is the place to put it.

next

Calling next() will immediately start the next middleware, and return a promise that will resolve to the value passed to resolve in a downstream middleware. When the promise resolves, all downstream middleware have completely finished running.

Control will continue upstream when this middleware returns (or resolves if it is an async function or returns a promise), or as soon as resolve is called.

next should only be called once, but if you do call next again in the same middleware function, it will simply return the same promise it did initially.

resolve

Calling resolve() will allow control to flow back to the previous middleware, even if this middleware function hasn't completed. A value can be passed as the first argument, and will set the value to resolve the whole series with.

resolve returns a promise that will resolve to the final value, which may have been changed by upstream middleware.

Calling resolve will prevent any downstream middleware from running if called before this middleware has completed or next is called. If next is called after resolve, it will not trigger the next middleware, but will return a promise that resolves to the current value (last passed to resolve).

Async Middleware

middle-run can work with any promised-based async middleware, but it was designed specifically for ES7 async functions. Inspired by koa's yield next, middle-run allows you to await next() so you can next() "downstream" and the await for control to flow back "upstream".

This is a barebones middleware runner, and has no use() methods or other ways to build out the list of middlewares, nor any url routing logic. Expanding the argument object with properties passed in the object to the function returned by run hopefully is enough to make middle-run useful in building a router or app framework.

License

This software is free to use under the MIT license. See the LICENSE-MIT file for license text and copyright information.