Git Product home page Git Product logo

ware's Introduction

ware

Note
Segment has paused maintenance on this project, but may return it to an active status in the future. Issues and pull requests from external contributors are not being considered, although internal contributions may appear from time to time. The project remains available under its open source license for anyone to use.

Easily create your own middleware layer.

Build Status

Installation

Node:

$ npm install ware

Component:

$ component install segmentio/ware

Duo:

var ware = require('segmentio/ware');

Example

var ware = require('ware');
var middleware = ware()
  .use(function (req, res, next) {
    res.x = 'hello';
    next();
  })
  .use(function (req, res, next) {
    res.y = 'world';
    next();
  });

middleware.run({}, {}, function (err, req, res) {
  res.x; // "hello"
  res.y; // "world"
});

Give it any number of arguments:

var ware = require('ware');
var middleware = ware()
  .use(function (a, b, c, next) {
    console.log(a, b, c);
    next();
  })

middleware.run(1, 2, 3); // 1, 2, 3

Supports generators (on the server):

var ware = require('ware');
var middleware = ware()
  .use(function (obj) {
    obj.url = 'http://google.com';
  })
  .use(function *(obj) {
    obj.src = yield http.get(obj.url);
  })

middleware.run({ url: 'http://facebook.com' }, function(err, obj) {
  if (err) throw err;
  obj.src // "obj.url" source
});

API

ware()

Create a new list of middleware.

.use(fn)

Push a middleware fn onto the list. fn can be a synchronous, asynchronous, or generator function. (it can also return a Promise)

fn can also be an array of functions or an instance of Ware.

.run(input..., [callback])

Runs the middleware functions with input... and optionally calls callback(err, input...).

License

(The MIT License)

Copyright (c) 2013 Segment.io <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

ware's People

Contributors

bhavanki avatar blakeembrey avatar calvinfo avatar dominicbarnes avatar ianstormtaylor avatar jocelynlecomte avatar kevva avatar matthewmueller avatar tj avatar wooorm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ware's Issues

Provide documentation of error handling process

How does Ware handle errors in middleware?

It seems that any error in a middleware will immediately stop the processing of any other middlewares and call the callback with ONLY the error -- not any of the other expected parameters.

It would be helpful if this behavior was documented!

Provide original arguments in callback in case of error

function handler (err, obj) {
  if (err) handleWithContext(err, obj); // obj is undefined :(
}

middleware.run({ url: 'http://facebook.com' }, handler);

The callback should provide the arguments of .run() in case of an error so that I have a context.
I.e. I want to cleanup some resources. Right now you can do this with a closure but not if the callback function is defined somewhere else.

I'd do a PR if you are OK with the API change.

Provide documentation of adding multiple plugins.

Plugins can be added not only via .use(plugin), but also multiple plugins as an array (.use([plugin1, plugin2])), or through the initial object (ware([plugin1, plugin2])). Can you please add documentation to this effect?

allow passing arrays

to use and to the constructor, so that say you already have a bunch of validators you can just do:

ware(validators).run(val, function (err) {
  ...
});

Wrap in try/catch

I think step.js should wrap in a try/catch as a precaution. A duo plugin I was using threw an exception and brought my whole server down. (only in dev, but I still think it would be fair to ask that of this lib)

Synchronous middleware

It'd be nice if middleware could be synchronous (basically, not require next()).

Test case:

it('should allow synchronous middleware', function (done) {
  ware()
  .use(function (obj) {
    obj.foo = true
  })
  .use(function (obj, next) {
    assert(true == obj.foo);
    next();
  })
  .run({}, function () {
    done();
  });
});

Support promises

I'd like to use this in a project that is using promises internally, it would be great if sync functions that return a Promise (or any then-able) could be treated as async.

The 3rd example of the readme contains an error

The third example of the readme is bugged:

The middleware stack is launched like this:

middleware.run({ life: '41' }); // "no!"
middleware.run({ life: '42' }); // "yes!"

and the first middleware is

var middleware = ware()
  .use(function (obj, next) {
    if ('42' != obj.value) return next(new Error());
    next();
  })

but the test should be:

 if ('42' != obj.life) return next(new Error());

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.