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

hash-exchange

v4.0.1

Published

trade hashes to replicate data with a remote endpoint

Downloads

37

Readme

hash-exchange

trade hashes to replicate data with a remote endpoint

build status

Trading hashes is very useful for replication on top of a content-addressable store where nodes have highly variable levels of information. This would be the case for a gossip network.

example

First, some code that takes messages on argv, hashes them, then provides those hashes using hash-exchange:

var exchange = require('hash-exchange');
var through = require('through2');
var concat = require('concat-stream');
var shasum = require('shasum');

var messages = process.argv.slice(2);
var data = {};
messages.forEach(function (msg) { data[shasum(msg)] = msg });

var ex = exchange(function (hash, cb) {
    var r = through();
    r.end(data[hash]);
    cb(null, r);
});
ex.provide(Object.keys(data));

ex.on('available', function (hashes) {
    ex.request(hashes);
});

ex.on('response', function (hash, stream) {
    stream.pipe(concat(function (body) {
        console.error('# BEGIN ' + hash);
        console.error(body.toString('utf8'));
        console.error('# END ' + hash);
    }));
});
process.stdin.pipe(ex).pipe(process.stdout);

Now we can run two instances of this program, one with:

[ 'beep', 'boop', 'hey yo' ]

and the other with

[ 'hey yo', 'WHATEVER', 'beep' ]

After wiring up the stdin and stdout, the programs provide each other with the data they don't individually have:

$ dupsh 'node ex.js beep boop "hey yo"' 'node ex.js "hey yo" WHATEVER beep'
# BEGIN fdb608cccac07c273ab532bb41eea07e2ddccf4e
WHATEVER
# END fdb608cccac07c273ab532bb41eea07e2ddccf4e
# BEGIN ae8d904cebfd629cdb1cc773a5bce8aca1dc1eee
boop
# END ae8d904cebfd629cdb1cc773a5bce8aca1dc1eee

methods

var exchange = require('hash-exchange')

var ex = exchange(createReadStream)

Create a hash exchange instance ex from createReadStream(hash, cb), a function that takes a hash as an argument and should call cb(err, stream, seq) with a readable stream of data for hash. Optionally the read stream can indicate a sequence integer seq.

ex is a duplex stream. You should pipe it to and from another hash exchange instance, perhaps over a network link.

You can optionally provide:

ex.provide(hashes)

Tell the remote endpoint about an array of hashes.

ex.request(hashes)

Ask the remote endpoint to read the content of an array of hashes.

ex.id(id)

Inform the other side of the connection with a unique identifier string id. The 'id' event fires on the remote side.

ex.since(seq)

Inform the other side of the connection to only serve hashes since seq through the 'since' event on the remote side.

seq must be an integer >= -1

ex.seen(seq)

Inform the other side that seq was successfully received through the 'seen' event on the remote side.

seq must be an integer >= -1

ex.close()

When the local exchange is done and doesn't need any more data, call this function to inform the remote side. When both sides have called .close(), the connection will terminate.

events

ex.on('response', function (hash, stream) {})

When a requested hash has been sent from the other end, this event fires with the hash, a readable stream of the remote file contents.

ex.on('available', function (hashes) {})

After the other end of the connection has provided some hashes, this event fires with the array of remote hashes not already provided locally.

ex.on('id', function (id) {})

When the remote instance calls .id(id), this event fires with the identifier id that was called.

ex.on('since', function (seq) {})

When the remote instance calls .since(seq), this event fires with the sequence seq that was called.

ex.on('seen', function (seq) {})

When the remote instance calls .seen(seq), this event fires with the sequence seq that was called.

ex.on('close', function () {})

This event fires when both sides of the connection have called .close() and each side doesn't need any more data.

install

With npm do:

npm install hash-exchange

license

MIT