
Co Compose
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.
Benchmarks
Co Compose x 488,078 ops/sec ±0.32% (82 runs sampled)
fastseries x 94,426 ops/sec ±6.53% (47 runs sampled)
middie x 80,062 ops/sec ±8.71% (47 runs sampled)
Fastest is Co Compose
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.
middleware.register 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.
{ ctxstack await } { ctxstack await } const ctx = stack: await middlewareassert
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.
async { ctxstack await } async { ctxstack await } const middleware = const ctx = stack: middleware await middleware
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
{ ctxstack await } { ctxstack} const ctx = stack: await middleware assert