Comparing version 1.5.3 to 1.6.0
60
index.js
@@ -7,2 +7,3 @@ 'use strict'; | ||
var assert = require('assert'); | ||
var debug = require('debug')('ware'); | ||
@@ -33,5 +34,5 @@ var Emitter = require('events').EventEmitter; | ||
function Ware() { | ||
if (!(this instanceof Ware)) return new Ware(); | ||
if (!(this instanceof Ware)) return new Ware; | ||
this.on('error', this.onerror); | ||
this.fns = []; | ||
this.middleware = []; | ||
this.context = Object.create(null); | ||
@@ -56,4 +57,6 @@ } | ||
w.use = function (fn) { | ||
assert(fn && 'GeneratorFunction' == fn.constructor.name, | ||
'ware.use() requires a generator function'); | ||
debug('use %s', fn._name || fn.name || '-'); | ||
this.fns.push(fn); | ||
this.middleware.push(fn); | ||
return this; | ||
@@ -63,7 +66,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|Function} callback (optional) | ||
* @return {Ware} | ||
* @param {GeneratorFunction} callback (optional) | ||
* @return {Mixed} | ||
* @api public | ||
@@ -74,8 +78,7 @@ */ | ||
debug('run'); | ||
var mw = [].concat(this.fns); | ||
var mw = [].concat(this.middleware); | ||
var args = slice.call(arguments); | ||
var last = args[args.length - 1]; | ||
var callback = 'function' === typeof last ? last : null; | ||
var isGen = false; | ||
if (callback && (isGen = isGeneratorFunction(callback))) { | ||
if (callback) { | ||
args.pop(); | ||
@@ -85,12 +88,5 @@ mw.push(callback); | ||
var gen = compose(mw); | ||
var fn = co(gen); | ||
var fn = co.wrap(gen); | ||
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; | ||
return fn.call(ctx).catch(ctx.onerror); | ||
}; | ||
@@ -106,3 +102,3 @@ | ||
w.clear = function () { | ||
this.fns.length = 0; | ||
this.middleware.length = 0; | ||
return this; | ||
@@ -139,24 +135,8 @@ }; | ||
w.onerror = function (err){ | ||
assert(err instanceof Error, 'non-error thrown: ' + err); | ||
if (this.listeners('error').length) return; | ||
console.error(err.stack); | ||
var msg = err.stack || err.toString(); | ||
console.error(); | ||
console.error(msg.replace(/^/gm, ' ')); | ||
console.error(); | ||
}; | ||
/** | ||
* Noop. | ||
* | ||
* @api private | ||
*/ | ||
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.5.3", | ||
"version": "1.6.0", | ||
"description": "Ware inspired, easily create your own middleware layer using generators via co.", | ||
@@ -9,2 +9,3 @@ "main": "index.js", | ||
}, | ||
"repository": "fundon/co-ware.git", | ||
"author": "fundon <cfddream@gmail.com>", | ||
@@ -20,17 +21,10 @@ "license": "MIT", | ||
"dependencies": { | ||
"co": "^3.1.0", | ||
"debug": "^0.7.4", | ||
"co": "^4.1.0", | ||
"debug": "^2.1.1", | ||
"koa-compose": "^2.3.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^1.21.4", | ||
"should": "^3.3.2" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "http://github.com/fundon/co-ware.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/fundon/co-ware/issues" | ||
"mocha": "^2.1.0", | ||
"should": "^4.6.2" | ||
} | ||
} |
@@ -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|callback]) | ||
#### .run(input..., [__GeneratorFunction__]) | ||
Runs the middleware functions with input... and optionally calls callback(__GeneratorFunction__ or __Function__). | ||
Runs the middleware functions with input... and optionally calls callback(__GeneratorFunction__). | ||
@@ -75,0 +75,0 @@ #### .clear() |
@@ -0,8 +1,8 @@ | ||
var assert = require('assert'); | ||
var noop = function *(){}; | ||
var ware = require('..'); | ||
var co = require('co'); | ||
describe('ware', function () { | ||
var assert = require('assert'); | ||
var noop = function *(){}; | ||
var ware = require('..'); | ||
describe('#use', function () { | ||
@@ -14,5 +14,5 @@ it('should be chainable', function () { | ||
it('should add a middleware to fns', function () { | ||
it('should add a GeneratorFunction to middleware', function () { | ||
var w = ware().use(noop); | ||
assert(1 == w.fns.length); | ||
assert(1 == w.middleware.length); | ||
}); | ||
@@ -43,12 +43,2 @@ }); | ||
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) { | ||
@@ -141,3 +131,11 @@ ware() | ||
}); | ||
it('should be return a promise', function () { | ||
return ware() | ||
.use(function *() {}) | ||
.run('obj') | ||
.then(function () { | ||
}); | ||
}); | ||
}); | ||
}); |
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
10703
275
2
1
+ Addedco@4.6.0(transitive)
+ Addeddebug@2.6.9(transitive)
+ Addedms@2.0.0(transitive)
- Removedco@3.1.0(transitive)
- Removeddebug@0.7.4(transitive)
Updatedco@^4.1.0
Updateddebug@^2.1.1