Gathering detailed insights and metrics for co-compose
Gathering detailed insights and metrics for co-compose
Gathering detailed insights and metrics for co-compose
Gathering detailed insights and metrics for co-compose
npm install co-compose
Update dependencies
Published on 04 Nov 2022
Update dependencies
Published on 04 Apr 2022
Update dependencies
Published on 22 Feb 2022
Drop support for Node below 16
Published on 17 Dec 2021
Performance improvements and dependencies update
Published on 19 Jul 2021
update dependencies
Published on 31 May 2021
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
30 Stars
149 Commits
2 Forks
8 Watching
7 Branches
3 Contributors
Updated on 17 Sept 2024
TypeScript (97.53%)
Shell (1.62%)
JavaScript (0.85%)
Cumulative downloads
Total Downloads
Last day
-2.7%
4,189
Compared to previous day
Last week
8.1%
22,992
Compared to previous week
Last month
2.8%
94,834
Compared to previous month
Last year
23.8%
1,197,697
Compared to previous year
Compose an array of functions to be executed one after the other. Similar to Koa and AdonisJS middlewares.
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)
Fastest is Co Compose
1npm i co-compose 2 3# yarn 4yarn add co-compose
Checkout the following example to run an array of middleware functions.
1import { Middleware } from 'co-compose' 2async function fn1(next) { 3 console.log('executing fn1') 4 await next() 5} 6 7async function fn2(next) { 8 console.log('executing fn2') 9 await next() 10} 11 12const middleware = new Middleware() 13middleware.register([fn1, fn2]) 14 15await middleware.runner().run([])
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.
1async function fn1(ctx, next) { 2 ctx.stack.push('fn1') 3 await next() 4} 5 6async function fn2(ctx, next) { 7 ctx.stack.push('fn2') 8 await next() 9} 10 11const ctx = { 12 stack: [], 13} 14 15await middleware.runner().run([ctx]) 16assert.deepEqual(ctx.stack, ['fn1', 'fn2'])
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.
1class Middleware1 { 2 async handle(ctx, next) { 3 ctx.stack.push('fn1') 4 await next() 5 } 6} 7 8class Middleware2 { 9 async handle(ctx, next) { 10 ctx.stack.push('fn2') 11 await next() 12 } 13} 14 15const middleware = new Middleware() 16const ctx = { 17 stack: [], 18} 19 20middleware.register([Middleware1, Middleware2]) 21 22await middleware 23 .runner() 24 .executor(async function (MiddlewareClass, params) { 25 const instance = new MiddlewareClass() // 👈 26 await instance.handle(...params) // 👈 27 }) 28 .run([ctx])
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
1async function fn1(ctx, next) { 2 ctx.stack.push('fn1') 3 await next() 4} 5 6async function finalHandler() { 7 ctx.stack.push('final handler') 8} 9 10const ctx = { 11 stack: [], 12} 13 14await middleware.runner().finalHandler(finalHandler, [ctx]).run([ctx]) 15 16assert.deepEqual(ctx.stack, ['fn1', 'final handler'])
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
Found 0/29 approved changesets -- score normalized to 0
Reason
project is archived
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More