Skip to content

trekjs/middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Middleware

The Modern (ES6) Middleware Composition.

Installation

$ npm install trek-middleware --save

Examples

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

$ 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

Build Status codecov


fundon.me  ·  GitHub @fundon  ·  Twitter @_fundon

About

☀ The Modern (ES6) Middleware Composition

Resources

License

Stars

Watchers

Forks

Packages

No packages published