Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

beads

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

beads

beads

  • 0.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Beads

Beads is a Koa like, general purpose middleware layer for nodejs.

Usage

app.when(function *() {
    return isReady;
}).use(function *(next) {
    // do sth
    yield next;
    // do sth later
});
// will be fired as soon as possible
app.use(function *(next) {
    // do sth
    yield next;
    // do sth later
});
// add wrap for middleware (adding logger, and log BEGIN and END)
var wrap = function(middleware) {
    return function *(_next) {
        this.logger = require("log4js").getLogger('YOUR MIDDLEWARE NAME');
        this.logger.debug('BEGIN');
        var next = function *() {
            yield _next;
            this.logger = require("log4js").getLogger('YOUR MIDDLEWARE NAME');
        }
        yield middleware.call(this, next);
        this.logger.debug('END');
    };
};
app.wrap(wrap).when(function *() {
    return isReady;
}).use(function *(next) {
    // do sth
    yield next;
    // do sth later
});

Features

  • Simple dependency resolution system

    Use app.when. See the example below.

    Dependency Test -> Functions whose dependency was satisfied -> RUN those functions one by one -> Dependency Test

  • Shared context among middlewares

  • Use co and generators for middlewares

Example

var context = {};
var app = new (require("../index.js"))(context);

app.use(function *(next) {
    console.log("set mark1!");
    this.mark1 = true;
    yield next;
});

app.when(function *() {
    return this.mark1;
}).use(function *(next) {
    console.log("mark1 set, set mark2!");
    this.mark2 = true;
    yield next;
});

app.when(function *() {
    return this.mark2;
}).use(function *(next) {
    console.log("mark2 set!");
    yield next;
});

app.when(function *() {
    return this.mark1;
}).use(function *(next) {
    console.log("mark1 set!");
    yield next;
});

app.use(function *(next){
    var start = new Date;
    console.log("timer start");
    yield next;
    var t = new Date - start;
    console.log("timer end");
    console.log(t + "ms");
});

app.run(function(err, context) {
    console.log("Output Context: " + JSON.stringify(context));
});

The output:

set mark1!
timer start
mark1 set, set mark2!
mark1 set!
mark2 set!
Output Context: {"mark1":true,"mark2":true}
timer end
2ms

Installation

$ npm install beads

Compatibility

You can use the --harmony flag when using node 0.11+.

Or, you can use gnode if you are using node 0.10 or below.

FAQ

Why app.wrap(wrap)?

Because sometimes you may want to add wrapper for some middleware, add logger or profile. So it's useful to have app.wrap.

SyntaxError: Unexpected identifier

You can't use yield inside a regular function!

http://stackoverflow.com/questions/20834113/syntaxerror-unexpected-identifier-generators-in-es6

这目标于一种情况

每次依赖条件已解决,执行一批插件。
插件 A, B 依赖于条件1,插件 C, D 依赖于条件2。
B中会解决条件2。
那么A, B的顺序不确定,但是A, B都先于C, D执行。

如果不是成批执行,
若定义顺序为 B C D A 则实际运行顺序为
B C D A,A的条件本早已满足,却在最后被执行了。

为此依赖必须单独与函数体存在。
否则在执行函数前无法知道依赖是否满足,
但是执行函数可能改变依赖条件,
这样就无法实现无损的依赖判断。

Keywords

FAQs

Package last updated on 07 Sep 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc