koa-compose
Advanced tools
Comparing version 2.0.1 to 2.1.0
93
index.js
/** | ||
* Module dependencies. | ||
*/ | ||
var debug = require('debug')('koa-compose'); | ||
var fmt = require('util').inspect; | ||
/** | ||
* Expose compositor. | ||
*/ | ||
module.exports = compose; | ||
module.exports = debug.enabled | ||
? instrumented | ||
: compose; | ||
@@ -19,20 +28,46 @@ /** | ||
function compose(middleware){ | ||
return function *(downstream){ | ||
var done = false; | ||
var ctx = this; | ||
var i = 0; | ||
return function *(next){ | ||
var i = middleware.length; | ||
var prev = next || noop(); | ||
var curr; | ||
yield *next(); | ||
while (i--) { | ||
curr = middleware[i]; | ||
prev = curr.call(this, prev); | ||
} | ||
function next(){ | ||
var mw = middleware[i++]; | ||
yield *prev; | ||
} | ||
} | ||
if (!mw) { | ||
if (done) throw new Error('middleware yielded control multiple times'); | ||
done = true; | ||
return downstream || noop(); | ||
} | ||
/** | ||
* Compose `middleware` returning | ||
* an instrumented set of middleware | ||
* for debugging manipulation between | ||
* continuation. | ||
* | ||
* @param {Array} middleware | ||
* @return {Function} | ||
* @api public | ||
*/ | ||
return mw.call(ctx, next()); | ||
function instrumented(middleware){ | ||
console.warn('Warning: do not run DEBUG=koa-compose in production'); | ||
console.warn('as it will greatly affect the performance of your'); | ||
console.warn('application - it is designed for a development'); | ||
console.warn('environment only.\n'); | ||
return function *(next){ | ||
var i = middleware.length; | ||
var prev = next || noop(); | ||
var name = prev.name || 'noop'; | ||
var curr; | ||
while (i--) { | ||
curr = middleware[i]; | ||
prev = wrap.call(this, curr, prev, name); | ||
name = curr.name; | ||
} | ||
yield *prev; | ||
} | ||
@@ -42,2 +77,32 @@ } | ||
/** | ||
* Wrap to output debugging info. | ||
* | ||
* @api private | ||
*/ | ||
function wrap(curr, prev, name) { | ||
return curr.call(this, function *(next){ | ||
if ('noop' == name) return yield next; | ||
this._level = this._level || 0; | ||
// downstream | ||
console.log(' \033[1m%d | \033[0m>> \033[36m%s\033[0m', this._level, name); | ||
console.log(); | ||
console.log(fmt(this.response, { depth: 5, colors: true }).replace(/^/gm, ' ')); | ||
console.log(); | ||
// yield | ||
this._level++; | ||
yield next; | ||
this._level--; | ||
// upstream | ||
console.log(' \033[1m%d | \033[0m<< \033[36m%s\033[0m', this._level, name); | ||
console.log(); | ||
console.log(fmt(this.response, { depth: 5, colors: true }).replace(/^/gm, ' ')); | ||
console.log(); | ||
}.call(this, prev)); | ||
} | ||
/** | ||
* Noop. | ||
@@ -44,0 +109,0 @@ * |
@@ -5,10 +5,19 @@ { | ||
"repository": "koajs/compose", | ||
"version": "2.0.1", | ||
"keywords": ["koa", "middleware", "compose"], | ||
"files": ["index.js"], | ||
"dependencies": { "debug": "*" }, | ||
"version": "2.1.0", | ||
"keywords": [ | ||
"koa", | ||
"middleware", | ||
"compose" | ||
], | ||
"files": [ | ||
"index.js" | ||
], | ||
"dependencies": { | ||
"debug": "*" | ||
}, | ||
"devDependencies": { | ||
"co": "~3.0.1", | ||
"mocha": "~1.6.0", | ||
"should": "~2.1.0" | ||
"should": "~2.1.0", | ||
"koa": "~0.1.2" | ||
}, | ||
@@ -19,2 +28,2 @@ "scripts": { | ||
"license": "MIT" | ||
} | ||
} |
@@ -12,4 +12,18 @@ | ||
## Debugging | ||
To debug the interactions between middleware, you may use | ||
the __DEBUG__ environment variable, for example: | ||
``` | ||
$ DEBUG=koa-compose node --harmony app.js | ||
``` | ||
When enabled this will output verbose response information and the | ||
middleware names to help visualize how they interact. | ||
![koa middleware debugging](https://dl.dropboxusercontent.com/u/6396913/koa/Screen%20Shot%202013-12-22%20at%208.46.46%20AM.png) | ||
## License | ||
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
3347
90
28
0
4