npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

beads

v0.2.1

Published

beads

Downloads

3

Readme

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的条件本早已满足,却在最后被执行了。

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