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

co-compose

v7.0.3

Published

AdonisJS and Koa style middleware layer with ability to run parallel middleware

Downloads

89,046

Readme

Co Compose

Compose an array of functions to be executed one after the other. Similar to Koa and AdonisJS middlewares.

gh-workflow-image typescript-image npm-image license-image synk-image

Co compose composes an array of middleware to be executed in sequence. The library is framework independent and can be used in any Javascript/Typescript project.

Co Compose x 2,145,829 ops/sec ±0.16% (90 runs sampled)
fastseries x 166,990 ops/sec ±2.04% (64 runs sampled)
middie x 122,162 ops/sec ±7.84% (28 runs sampled)

Table of contents

Installation

npm i co-compose

# yarn
yarn add co-compose

Usage

Checkout the following example to run an array of middleware functions.

import { Middleware } from 'co-compose'
async function fn1(next) {
  console.log('executing fn1')
  await next()
}

async function fn2(next) {
  console.log('executing fn2')
  await next()
}

const middleware = new Middleware()
middleware.register([fn1, fn2])

await middleware.runner().run([])

Passing values

You can also pass values to all middleware functions. An array of values passed to runner.run() will be passed to middleware functions as multiple arguments.

async function fn1(ctx, next) {
  ctx.stack.push('fn1')
  await next()
}

async function fn2(ctx, next) {
  ctx.stack.push('fn2')
  await next()
}

const ctx = {
  stack: [],
}

await middleware.runner().run([ctx])
assert.deepEqual(ctx.stack, ['fn1', 'fn2'])

Custom executors

The default behavior is to define middleware as functions. However, you can define them in any shape and then stick a custom executor to execute them.

Check the following example where ES6 classes are used.

class Middleware1 {
  async handle(ctx, next) {
    ctx.stack.push('fn1')
    await next()
  }
}

class Middleware2 {
  async handle(ctx, next) {
    ctx.stack.push('fn2')
    await next()
  }
}

const middleware = new Middleware()
const ctx = {
  stack: [],
}

middleware.register([Middleware1, Middleware2])

await middleware
  .runner()
  .executor(async function (MiddlewareClass, params) {
    const instance = new MiddlewareClass() // 👈
    await instance.handle(...params) // 👈
  })
  .run([ctx])

Final Handler

The final handler is a executed when the entire middleware chain ends by calling next. This makes it easier to execute custom functions, which are not part of the chain, however must be executed when chain ends.

Also, the arguments for the final handler can be different from the middleware arguments

async function fn1(ctx, next) {
  ctx.stack.push('fn1')
  await next()
}

async function finalHandler() {
  ctx.stack.push('final handler')
}

const ctx = {
  stack: [],
}

await middleware.runner().finalHandler(finalHandler, [ctx]).run([ctx])

assert.deepEqual(ctx.stack, ['fn1', 'final handler'])