koa-compose
Advanced tools
Comparing version 2.3.0 to 3.0.0
44
index.js
@@ -0,1 +1,2 @@ | ||
'use strict' | ||
@@ -19,22 +20,31 @@ /** | ||
function compose(middleware){ | ||
return function *(next){ | ||
var i = middleware.length; | ||
var prev = next || noop(); | ||
var curr; | ||
if (!Array.isArray(middleware)) throw new TypeError('Middleware stack must be an array!') | ||
for (const fn of middleware) { | ||
if (typeof fn !== 'function') throw new TypeError('Middleware must be composed of functions!') | ||
} | ||
while (i--) { | ||
curr = middleware[i]; | ||
prev = curr.call(this, prev); | ||
/** | ||
* @param {Object} context | ||
* @return {Promise} | ||
* @api public | ||
*/ | ||
return function (context, next) { | ||
// last called middleware # | ||
let index = -1 | ||
return dispatch(0) | ||
function dispatch(i) { | ||
if (i <= index) return Promise.reject(new Error('next() called multiple times')) | ||
index = i | ||
const fn = middleware[i] || next | ||
if (!fn) return Promise.resolve() | ||
try { | ||
return Promise.resolve(fn(context, function next() { | ||
return dispatch(i + 1) | ||
})) | ||
} catch(err) { | ||
return Promise.reject(err); | ||
} | ||
} | ||
yield *prev; | ||
} | ||
} | ||
/** | ||
* Noop. | ||
* | ||
* @api private | ||
*/ | ||
function *noop(){} |
@@ -5,3 +5,3 @@ { | ||
"repository": "koajs/compose", | ||
"version": "2.3.0", | ||
"version": "3.0.0", | ||
"keywords": [ | ||
@@ -17,11 +17,13 @@ "koa", | ||
"devDependencies": { | ||
"co": "~3.0.1", | ||
"mocha": "~1.6.0", | ||
"should": "~2.1.0", | ||
"koa": "~0.3.0" | ||
"co": "^4.6.0", | ||
"istanbul": "0", | ||
"mocha": "2", | ||
"should": "2" | ||
}, | ||
"scripts": { | ||
"test": "make test" | ||
"test": "mocha --harmony --require should --reporter spec", | ||
"test-cov": "node --harmony ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --require should", | ||
"test-travis": "node --harmony ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- --require should" | ||
}, | ||
"license": "MIT" | ||
} |
@@ -6,2 +6,8 @@ | ||
## Installation | ||
```js | ||
$ npm install koa-compose | ||
``` | ||
## API | ||
@@ -15,2 +21,2 @@ | ||
MIT | ||
MIT |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2516
4
43
21