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

peernet

v2.0.0

Published

peer to peer gossip network using randomized algorithms

Downloads

14

Readme

peernet

peer to peer gossip network based on randomized algorithms

The purpose of this tool is to sustain a gossip network for creating application-specific subnets. For example, a subnet might host a DHT or a p2p chat protocol. Each p2p protocol first needs a list of peers to make connections, which can be obtained from peernet.

project status

This project is very early mad science still heavily under research and development.

example

var level = require('level');
var db = level('./peer.db');

var peernet = require('peernet');
var pn = peernet(db, {
    transport: require('peernet/transport'),
    debug: true
});

var http = require('http');
var server = http.createServer(function (req, res) { res.end('...\n') });
server.listen(argv.port, function () {
    console.log('listening on ' + server.address().port);
});

var wsock = require('websocket-stream');
wsock.createServer({ server: server }, function (stream) {
    stream.pipe(pn.createStream()).pipe(stream);
});

You'll also need to bootstrap the network the first time you initialize the database. Call .save() on the peernet instance with an array of nodes you'll connect to:

pn.save([ 'ws://10.1.2.3:4000', 'ws://substack.net:2020' ]);

Peernet will attempt to connect to peers automatically from its database.

methods

var peernet = require('peernet')

var pn = peernet(db, opts)

Create a new peernet instance pn from a leveldb database handle db.

The required options are:

  • opts.transport - a transport-stream interface for resolving protocols to duplex transport streams. If you want something easy, use require('peernet/transport') here.
  • opts.debug - when true, print helpful diagnostic info with console.error().
  • opts.bootstrap - when true, automatically connect to peers from the database. Default: true.
  • opts.interval - time in milliseconds to wait between requesting new nodes from peers. Default: 5000.
  • opts.purge - time in milliseconds to wait between purging dead nodes. Default: 60*1000.
  • opts.connections - number of peer connections to bootstrap and maintain. Default: 5
  • opts.wrtc - webrtc implementation (interface like the wrtc package)

pn.save(nodes, cb)

Save nodes an array of peer address strings, to storage.

The protocol of the addresses will be interpreted by opts.transport provided to the peernet constructor.

pn.remove(nodes, cb)

Remove nodes, an array of peer address strings, from storage.

var r = pn.known()

Return a readable object stream r with all the known peers.

Each row has an address key with the string address for its value.

pn.getStats(addr, cb)

Query connection statistics for addr in cb(err, stats).

The stats object has these properties:

  • stats.connections.ok - number of successful connections
  • stats.connections.fail - number of failed connections
  • stats.nodes.rx - number of peer addresses received from this node
  • stats.nodes.tx - number of peer addresses sent to this node

pn.connect(addr, cb)

Connect to addr, a string address to be interpreted by opts.transport.

cb(err) fires with an error or upon a successful connection.

pn.disconnect(addr)

Disconnect from addr, a string address.

var stream = pn.createStream()

Create a duplex stream to wire up to a remote transport. This method is useful for hooking up a connection from inside a server.

var cons = pn.connections()

Return cons, an array of the currently connected addresses or IDs.

var r = pn.announce(msg)

Broadcast a message msg to peers who will gossip the announcement to their peers.

  • msg.id - optional id buffer or string. Should be globally unique. Random data works best because of caching used to bust routing loops. At most 64 bytes.
  • msg.type - optional type buffer or string to identify the kind of message. At most 64 bytes.
  • msg.data - optional data payload buffer or string. At most 1024 bytes.
  • msg.limit - optional maximum number of hops
  • msg.hops - starting hop count. Default 0.

Returns a readable object stream r of search rows:

  • row.id - response unique id
  • row.type - optional type of message
  • row.data - optional buffer payload data
  • row.limit - optional maximum number of hops
  • row.hops - number of hops from the originating response

Other nodes will forward the search query along or respond themselves if they have relevant content.

pn.close()

Shut down all intervals and disconnect from all peers.

events

pn.on('request', req)

When a peer sends a request to be forwarded, this event fires.

Use req.reply(msg) to send a reply.

msg should be an object with message properties (id, type, data, limit, and hops) that default to the originating request properties, but with the hop count set to 0.

If msg is a Buffer or string, it will be used as the data property.

license

MIT