Comparing version 1.3.0 to 1.5.0
110
index.js
'use strict'; | ||
/** | ||
* Module dependencies. | ||
* Module dependencies. | ||
*/ | ||
@@ -11,11 +11,6 @@ | ||
var co = require('co'); | ||
/** | ||
* slice() reference. | ||
*/ | ||
var slice = Array.prototype.slice; | ||
/** | ||
* Ware prototype. | ||
* Ware prototype. | ||
*/ | ||
@@ -26,3 +21,3 @@ | ||
/** | ||
* Expose `Ware`. | ||
* Expose Ware. | ||
*/ | ||
@@ -33,11 +28,9 @@ | ||
/** | ||
* Initialize a new `Ware` manager. | ||
* Initialize a new `Ware` manager. | ||
* | ||
* @api public | ||
* @api public | ||
*/ | ||
function Ware () { | ||
if (!(this instanceof Ware)) return new Ware; | ||
this.env = process.env.NODE_ENV || 'development'; | ||
this.outputErrors = 'test' != this.env; | ||
function Ware() { | ||
if (!(this instanceof Ware)) return new Ware(); | ||
this.on('error', this.onerror); | ||
@@ -50,3 +43,3 @@ this.fns = []; | ||
/** | ||
* Inherit from `Emitter.prototype`. | ||
* Inherit from `Emitter.prototype`. | ||
*/ | ||
@@ -57,7 +50,7 @@ | ||
/** | ||
* Use the given middleware `fn`. | ||
* Use the given middleware `fn`. | ||
* | ||
* @param {GeneratorFunction} fn | ||
* @return {Ware} self | ||
* @api public | ||
* @param {GeneratorFunction} fn | ||
* @return {Ware} self | ||
* @api public | ||
*/ | ||
@@ -72,8 +65,8 @@ | ||
/** | ||
* Run through the middleware with the given `args` and optional `callback`. | ||
* Run through the middleware with the given `args` and optional `callback`. | ||
* | ||
* @param {Mixed} args... | ||
* @param {GeneratorFunction} callback (optional) | ||
* @return {Ware} | ||
* @api public | ||
* @param {Mixed} args... | ||
* @param {GeneratorFunction|Function} callback (optional) | ||
* @return {Ware} | ||
* @api public | ||
*/ | ||
@@ -87,8 +80,20 @@ | ||
var callback = 'function' === typeof last ? last : null; | ||
if (callback) args.pop(); | ||
mw.push(callback || noop); | ||
var isGen = false; | ||
if (callback) { | ||
args.pop(); | ||
isGen = isGeneratorFunction(callback); | ||
if (isGen) { | ||
mw.push(callback || noop); | ||
} | ||
} | ||
var gen = compose(mw); | ||
var fn = co(gen); | ||
var ctx = this.createContext(args, this); | ||
fn.call(ctx, ctx.onerror); | ||
var ctx = this.createContext(args, Object.create(null), this); | ||
function done(err, res) { | ||
if (!isGen) { | ||
(callback || noop).call(ctx, err, res); | ||
} | ||
return ctx.onerror(err); | ||
} | ||
fn.call(ctx, done); | ||
return this; | ||
@@ -98,6 +103,6 @@ }; | ||
/** | ||
* Clear the midleware. | ||
* Clear the midleware. | ||
* | ||
* @return {Object} self | ||
* @api public | ||
* @return {Object} self | ||
* @api public | ||
*/ | ||
@@ -111,15 +116,16 @@ | ||
/** | ||
* Create a context. | ||
* Create a context. | ||
* | ||
* @param {Mixed} input | ||
* @return {Object} ctx | ||
* @api private | ||
* @param {Mixed} input | ||
* @return {Object} ctx | ||
* @api private | ||
*/ | ||
w.createContext = function (input, self, ctx) { | ||
ctx = Object.create(this.context); | ||
w.createContext = function (input, output, self) { | ||
var ctx = Object.create(self.context); | ||
ctx.input = input; | ||
ctx.output = Object.create(null); | ||
ctx.output = output; | ||
ctx.onerror = function (err) { | ||
if (!err) return; | ||
self.removeListener('error', self.onerror); | ||
self.emit('error', err); | ||
@@ -131,10 +137,10 @@ }; | ||
/** | ||
* Default error handler. | ||
* Default error handler. | ||
* | ||
* @param {Error} err | ||
* @api private | ||
* @param {Error} err | ||
* @api private | ||
*/ | ||
w.onerror = function(err){ | ||
if (!this.outputErrors) return; | ||
w.onerror = function (err){ | ||
if (this.listeners('error').length) return; | ||
console.error(err.stack); | ||
@@ -144,7 +150,19 @@ }; | ||
/** | ||
* Noop. | ||
* Noop. | ||
* | ||
* @api private | ||
* @api private | ||
*/ | ||
function *noop() {} | ||
function noop() {} | ||
/** | ||
* Check if `obj` is a generator function. | ||
* | ||
* @param {Mixed} obj | ||
* @return {Boolean} | ||
* @api private | ||
*/ | ||
function isGeneratorFunction(obj) { | ||
return obj && obj.constructor && 'GeneratorFunction' == obj.constructor.name; | ||
} |
{ | ||
"name": "co-ware", | ||
"version": "1.3.0", | ||
"version": "1.5.0", | ||
"description": "Ware inspired, easily create your own middleware layer using generators via co.", | ||
@@ -19,9 +19,9 @@ "main": "index.js", | ||
"dependencies": { | ||
"co": "^3.0.4", | ||
"co": "^3.1.0", | ||
"debug": "^0.7.4", | ||
"koa-compose": "^2.2.0" | ||
"koa-compose": "^2.3.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^1.18.2", | ||
"should": "^3.2.0" | ||
"mocha": "^1.21.4", | ||
"should": "^3.3.2" | ||
}, | ||
@@ -28,0 +28,0 @@ "repository": { |
@@ -70,5 +70,5 @@ # co-ware [![Build Status](https://travis-ci.org/fundon/co-ware.svg)](https://travis-ci.org/fundon/co-ware) | ||
#### .run(input..., [*callback]) | ||
#### .run(input..., [*callback|callback]) | ||
Runs the middleware functions with input... and optionally calls callback. | ||
Runs the middleware functions with input... and optionally calls callback(__GeneratorFunction__ or __Function__). | ||
@@ -75,0 +75,0 @@ #### .clear() |
@@ -32,6 +32,6 @@ | ||
it('should receive initial arguments', function (done) { | ||
it('should receive initial arguments, if callback is generator function', function (done) { | ||
ware() | ||
.use(function *(next) { yield next; }) | ||
.run('req', 'res', function *() { | ||
.run('req', 'res', function *(err, res) { | ||
assert('req' == this.input[0]); | ||
@@ -43,2 +43,12 @@ assert('res' == this.input[1]); | ||
it('should receive initial arguments, if callback is normal function', function (done) { | ||
ware() | ||
.use(function *(next) { yield next; }) | ||
.run('req', 'res', function (err, res) { | ||
assert('req' == this.input[0]); | ||
assert('res' == this.input[1]); | ||
done(); | ||
}); | ||
}); | ||
it('should take any number of arguments', function (done) { | ||
@@ -108,2 +118,19 @@ ware() | ||
it('should not call error middleware on error', function (done) { | ||
var errors = 0; | ||
ware() | ||
.use(function *(next) { throw new Error(); }) | ||
.use(function *(next) { errors++; yield next; }) | ||
.use(function *(next) { errors++; yield next; }) | ||
.on('error', function (err) { | ||
assert(err); | ||
assert(2 != errors); | ||
}) | ||
.run(function (err) { | ||
assert(err); | ||
assert(2 != errors); | ||
done(); | ||
}); | ||
}); | ||
it('should not require a callback', function (done) { | ||
@@ -110,0 +137,0 @@ ware() |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
11152
295
0
Updatedco@^3.1.0
Updatedkoa-compose@^2.3.0