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

level-2pc

v6.4.3

Published

A two-phase-commit protocol for leveldb.

Downloads

14

Readme

SYNOPSIS

A two-phase-commit protocol for leveldb.

DESCRIPTION

Provides strong-consistency for local-cluster replication.

Every node in your cluster can be writable and all reads from any node will be consistent.

Uses reconnect-core to support an injectable transport for e.g. browser compatibility.

BUILD STATUS

Build Status

SPECIFICATION

The algorithm for how this works is here.

USAGE

EXAMPLE

SERVER A

var level = require('level');
var Replicator = require('level-2pc');
var net = require('net');

var db1 = level('./db', { valueEncoding: 'json' });

var opts = {
  peers: [
    { host: 'localhost', port: 3001 },
    { host: 'localhost', port: 3002 }
  ]
};

var r = Replicator(db1, opts);

net.createServer(function(con) {
  var server = r.createServer();
  server.pipe(con).pipe(server);
}).listen(3000);

SERVER B


var opts = {
  peers: [
    { host: 'localhost', port: 3000 },
    { host: 'localhost', port: 3002 }
  ]
};

var r = Replicator(db2, opts);

net.createServer(function(con) {
  var server = r.createServer();
  server.pipe(con).pipe(server);
}).listen(3001);

SERVER C

var opts = {
  peers: [
    { host: 'localhost', port: 3000 },
    { host: 'localhost', port: 3001 }
  ]
};

var r = Replicator(db3, opts);

net.createServer(function(con) {
  var server = r.createServer();
  server.pipe(con).pipe(server);
}).listen(3002);

WRITE SOME DATA

Now go ahead and write some data to one of the servers and watch the data magically appear in the other servers!

setTimeout(function() {

  db1.put('x', 100, function(err) {
    console.log(err || 'ok');
  });

  setTimeout(function() {
    db2.get('x', function() {
      console.log(arguments);
      db3.get('x', function() {
        console.log(arguments);
      });
    });
  }, 100);

}, 100);

TRANSPORT

When the server wants to connect to the peers that have been specified, it defaults to using tcp from the net module. You can inject any transportation layer you like by setting the transport property in the options object:

var net = require('net');

var opts = {
  transport: function() {
    return net.connect.apply(null, arguments);
  },
  peers: [ /* .. */ ]
};

var r = Replicator(db, opts);

API

Replicator(db, opts)

Returns a Replicator object, which is an EventEmitter.

  • db leveldb database object

  • opts options object with the following properties:

    • host host that other peers should connect to
    • port port that other peers should connect to
    • peers an array of objects that specify the host and port of each peer
    • minConsensus how many peers must connect initially or respond to quorum

Replicator#createServer()

Returns a duplex rpc-stream that can be served over e.g. http or tcp or any other transport supporting node streams.

Replicator#close()

Closes connections to all peers.

Event: 'ready'

Emitted when the replicator is ready to replicate with other peers. Happens when the replicator has enough connections for the quorum, i.e. when the number of peers is above minConsensus.

Event: 'notready'

Emitted when the replicator is not ready to replicate with other peers. Happens when the replicator doesn't have enough connections for the quorum, i.e. when the number of peers goes below minConsensus.

Event: 'connect'

Emitted when the replicator has connected to a peer.

  • host host of the connected peer
  • port port of the connected peer

Event: 'error'

Emitted when there was an error in the connection between the replicator and a peer.

  • err error object

Event: 'disconnect'

Emitted when the replicator has disconnected from a peer.

  • host host of the disconnected peer
  • port port of the disconnected peer

Event: 'reconnect'

Emitted when the replicator tries to reconnect to a peer.

  • host retrying connection to this host
  • port retrying connection to this port

Event: 'fail'

Emitted when the replicator has tried to reconnect but failed too many times. There might be a problem with the connection, or the peer is simply offline.

  • host host of the failing peer
  • port port of the failing peer