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
Implementation of the chain of responsibility design pattern
npm install co-compose
Typescript
Module System
Node Version
NPM Version
95.6
Supply Chain
100
Quality
76.2
Maintenance
100
Vulnerability
100
License
Update dependencies
Updated on Nov 04, 2022
Update dependencies
Updated on Apr 04, 2022
Update dependencies
Updated on Feb 22, 2022
Drop support for Node below 16
Updated on Dec 17, 2021
Performance improvements and dependencies update
Updated on Jul 19, 2021
update dependencies
Updated on May 31, 2021
TypeScript (97.53%)
Shell (1.62%)
JavaScript (0.85%)
Total Downloads
5,091,102
Last Day
1,068
Last Week
17,512
Last Month
74,830
Last Year
1,073,457
MIT License
30 Stars
149 Commits
2 Forks
7 Watchers
7 Branches
3 Contributors
Updated on Sep 17, 2024
Minified
Minified + Gzipped
Latest Version
7.0.3
Package Id
co-compose@7.0.3
Unpacked Size
14.60 kB
Size
5.11 kB
File Count
11
NPM Version
8.18.0
Node Version
19.0.0
Cumulative downloads
Total Downloads
Last Day
-10.9%
1,068
Compared to previous day
Last Week
-8.4%
17,512
Compared to previous week
Last Month
-3.3%
74,830
Compared to previous month
Last Year
-7.3%
1,073,457
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
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
security policy file detected
Details
Reason
project is archived
Details
Reason
Found 0/29 approved changesets -- score normalized to 0
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 2025-06-23
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