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

role

v1.1.1

Published

Develop services as single processes, deploy them as multiple.

Downloads

43

Readme

role

Develop services as single processes and deploy them as multiple.

Usage

Let's write a webserver that stores and receives data from a leveldb, and operates on querystrings. Since we want to be able to split up the http server and the leveldb later, we give each a role:

var role = require('role');
var level = require('level');
var multilevel = require('multilevel');
var http = require('http');
var qs = require('querystring');

// we define the `db` role
role.set('db', function () {
  var db = level(__dirname + '/db');
  return function () {
    return multilevel.server(db);
  }
});

// we define the `main` role
role.set('main', function () {
  var db = multilevel.client();

  // we request the `db` role
  role.get('db', function (s) {
    s.pipe(db.createRpcStream()).pipe(s);
  });

  http.createServer(function (req, res) {
    var query = qs.parse(req.url.split('?').slice(1).join(''));
    if (query.get) {
      db.get(query.get, function (err, val) {
        if (err) res.end('not found');
        else res.end(val);
      });
    } else if (query.put) {
      db.put(query.put, query.value, function (err) {
        if (err) res.end(String(err));
        else res.end('ok');
      });
    } else {
      res.end('try\n\n/?put=foo&value=bar\n\n/?get=foo');
    }
  }).listen(8000, function () {
    console.log('~> localhost:8000');
  });
});

main is a special role which will always be executed immediately. All the other rules will be executed when requested via role.get().

Now, to start as a single process:

$ node example/simple.js
$ open http://localhost:8000/

To start as two seperate processes:

$ HUB=7888 ROLE=main node example/simple.js 
$ CLIENT=7888 ROLE=db node example/simple.js 

To start as three seperate processes (mind synchronisation):

$ HUB=7888 ROLE=main node example/simple.js 
$ CLIENT=7888 ROLE=db node example/simple.js 
$ CLIENT=7888 ROLE=db node example/simple.js 
$ # Now you can just kill and recreate one database at a time and the site
will stay up.

When using distributed/production mode you just have to make sure that there's always have at least one hub running.

API

role.get(name, fn)

Call fn with a stream for role name. When the connection is lost and a new one is made or already available, call fn again.

role.set(name, fn)

Set fn to be role name. fn should return a Stream or a Function that returns a Stream.

Env flags

Pass none for local mode. Pass HUB and ROLE or CLIENT and ROLE for distributed/production mode.

  • HUB: Be a hub and listen on that port.
  • CLIENT: Be a client and listen on that port.
  • ROLE: The role to serve. Multiple roles are activated when you pass a comma-seperated list, like main,db.

Installation

With npm do

$ npm install role

License

Copyright (c) 2013 Julian Gruber <[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.