Comparing version 0.0.0 to 1.0.0
var ware = require('..'); | ||
var middleware = ware() | ||
var w = ware() | ||
.use(function *(next) { | ||
//if ('42' != obj.value) return next(new Error()); | ||
this.x = 'hello'; | ||
yield next; | ||
console.log(this.i); | ||
}) | ||
.use(function *(next) { | ||
console.log('yes!'); | ||
this.y = 'world'; | ||
yield next; | ||
}) | ||
.use(function *(next) { | ||
console.log('no!'); | ||
yield next; | ||
}); | ||
middleware.run({ life: '41' }); // "no!" | ||
middleware.run({ life: '42' }); // "yes!" | ||
w.run({a:1}, {b:2}, function *() { | ||
console.log(this.x, this.y); | ||
}); |
115
index.js
@@ -1,1 +0,114 @@ | ||
module.exports = require('./lib/ware'); | ||
'use strict'; | ||
/** | ||
* Module dependencies. | ||
*/ | ||
var debug = require('debug')('ware'); | ||
var Emitter = require('events').EventEmitter; | ||
var compose = require('koa-compose'); | ||
var co = require('co'); | ||
/** | ||
* slice() reference. | ||
*/ | ||
var slice = Array.prototype.slice; | ||
/** | ||
* Ware prototype. | ||
*/ | ||
var w = Ware.prototype; | ||
/** | ||
* Expose `Ware`. | ||
*/ | ||
exports = module.exports = Ware; | ||
/** | ||
* Initialize a new `Ware` manager. | ||
* | ||
* @api public | ||
*/ | ||
function Ware () { | ||
if (!(this instanceof Ware)) return new Ware; | ||
this.env = process.env.NODE_ENV || 'development'; | ||
this.outputErrors = 'test' != this.env; | ||
this.on('error', this.onerror); | ||
this.fns = []; | ||
this.context = Object.create(null); | ||
} | ||
/** | ||
* Inherit from `Emitter.prototype`. | ||
*/ | ||
Ware.prototype.__proto__ = Emitter.prototype; | ||
/** | ||
* Use the given middleware `fn`. | ||
* | ||
* @param {GeneratorFunction} fn | ||
* @return {Ware} self | ||
* @api public | ||
*/ | ||
w.use = function (fn) { | ||
debug('use %s', fn._name || fn.name || '-'); | ||
this.fns.push(fn); | ||
return this; | ||
}; | ||
/** | ||
* Run through the middleware with the given `args` and optional `callback`. | ||
* | ||
* @param {Mixed} args... | ||
* @param {Function} callback (optional) | ||
* @return {Ware} | ||
* @api public | ||
*/ | ||
w.run = function () { | ||
debug('run'); | ||
var self = this; | ||
var mw = [].concat(this.fns); | ||
var args = slice.call(arguments); | ||
var last = args[args.length - 1]; | ||
var callback = 'function' === typeof last ? last : null; | ||
if (callback) args.pop(); | ||
mw.push(callback || noop); | ||
var gen = compose(mw); | ||
var fn = co(gen); | ||
var ctx = Object.create(this.context); | ||
ctx.input = args; | ||
ctx.onerror = function (err) { | ||
if (!err) return; | ||
self.emit('error', err); | ||
}; | ||
fn.call(ctx, ctx.onerror); | ||
return this; | ||
}; | ||
/** | ||
* Default error handler. | ||
* | ||
* @param {Error} err | ||
* @api private | ||
*/ | ||
w.onerror = function(err){ | ||
if (!this.outputErrors) return; | ||
console.error(err.stack); | ||
}; | ||
/** | ||
* Noop. | ||
* | ||
* @api private | ||
*/ | ||
function *noop() {} |
{ | ||
"name": "co-ware", | ||
"version": "0.0.0", | ||
"version": "1.0.0", | ||
"description": "Koa inspired, easily create your own middleware layer using generators via co.", | ||
@@ -13,6 +13,9 @@ "main": "index.js", | ||
"co": "^3.0.4", | ||
"koa-compose": "^2.2.0", | ||
"finished": "^1.1.1", | ||
"debug": "^0.7.4" | ||
"debug": "^0.7.4", | ||
"koa-compose": "^2.2.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^1.18.2", | ||
"should": "^3.2.0" | ||
} | ||
} |
# co-ware | ||
[Koa](https://github.com/segmentio/ware) inspired, easily create your own middleware layer using generators via [co](https://github.com/visionmedia/co). | ||
[Ware][] inspired, easily create your own middleware layer using generators via [co][]. | ||
### Examples | ||
```js | ||
var ware = require('..'); | ||
var w = ware() | ||
.use(function *(next) { | ||
this.x = 'hello'; | ||
yield next; | ||
}) | ||
.use(function *(next) { | ||
this.y = 'world'; | ||
yield next; | ||
}) | ||
.use(function *(next) { | ||
yield next; | ||
}); | ||
w.run({}, {}, function *() { | ||
console.log(this.x, this.y); | ||
}); | ||
``` | ||
Print the arguments of input. | ||
```js | ||
var ware = require('..'); | ||
var w = ware() | ||
.use(function *(next) { | ||
console.log(this.input); // 1, 2, 3 | ||
yield next; | ||
}); | ||
w.run(1, 2, 3); | ||
``` | ||
Handles error. | ||
```js | ||
var ware = require('..'); | ||
var w = ware() | ||
.use(function *(next) { | ||
if ('42' != this.input[0].life) throw new Error(); | ||
yield next; | ||
}) | ||
.use(function *(next) { | ||
console.log('yes!'); | ||
}) | ||
.on('error', function (err) { | ||
console.log('no!'); | ||
}); | ||
w.run({ life: '41' }); // "no!" | ||
w.run({ life: '42' }); // "yes!" | ||
``` | ||
### API | ||
#### ware() | ||
Create a new list of middleware. | ||
#### .use(*fun) | ||
Push a middleware fn(GeneratorFunction) onto the list. If the middleware has an arity of one more than the input to run it's an error middleware. | ||
#### .run(input..., [*callback]) | ||
Runs the middleware functions with input... and optionally calls callback. | ||
### License | ||
MIT | ||
[ware]: https://github.com/segmentio/ware | ||
[co]: https://github.com/visionmedia/co |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
7934
3
231
1
80
2
1
- Removedfinished@^1.1.1
- Removedee-first@1.0.3(transitive)
- Removedfinished@1.2.2(transitive)