https://github.com/trekjs/middleware
☀ The Modern (ES6) Middleware Composition
https://github.com/trekjs/middleware
Last synced: 2 months ago
JSON representation
☀ The Modern (ES6) Middleware Composition
- Host: GitHub
- URL: https://github.com/trekjs/middleware
- Owner: trekjs
- License: mit
- Created: 2016-08-19T06:35:31.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-09-25T00:09:27.000Z (over 7 years ago)
- Last Synced: 2024-04-15T02:12:45.552Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 130 KB
- Stars: 7
- Watchers: 2
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Middleware
The Modern (ES6) Middleware Composition.
## Installation
```console
$ npm install trek-middleware --save
```## Examples
```js
const co = require('co')
const Middleware = require('trek-middleware')
const middleware = new Middleware()middleware.push((ctx, next) => {
ctx.arr.push(1)
return next().then(() => {
ctx.arr.push(6)
})
})middleware.push(async (ctx, next) => {
ctx.arr.push(2)
await next()
ctx.arr.push(5)
})middleware.push(co.wrap(function * (ctx, next) {
ctx.arr.push(3)
yield next()
ctx.arr.push(4)
}))const ctx = { arr: [] }
middleware.compose(ctx).then(() => {
console.log(ctx.arr.toString() === '1,2,3,4,5,6')
})
```## Benchmarks
```console
$ npm run bench
```Results from https://travis-ci.org/trekjs/middleware.
```
1024 middlewares
koa-compose x 777 ops/sec ±1.91% (75 runs sampled)
trek-middleware x 829 ops/sec ±2.95% (82 runs sampled)
Fastest is trek-middleware
```## Badges
[](https://travis-ci.org/trekjs/middleware)
[](https://codecov.io/gh/trekjs/middleware)
---
> [fundon.me](https://fundon.me) ·
> GitHub [@fundon](https://github.com/fundon) ·
> Twitter [@_fundon](https://twitter.com/_fundon)