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

@arve.knudsen/libp2p

v0.25.6

Published

JavaScript implementation of libp2p, a modular peer to peer network stack

Downloads

5

Readme

Project status

We've come a long way, but this project is still in Alpha, lots of development is happening, API might change, beware of the Dragons 🐉..

Want to get started? Check our examples folder. You can check the development status at the Waffle Board.

Throughput Graph

Weekly Core Dev Calls

Tech Lead

David Dias

Lead Maintainer

Jacob Heun

Table of Contents

Background

libp2p is the product of a long and arduous quest to understand the evolution of the Internet networking stack. In order to build P2P applications, devs have long had to made custom ad-hoc solutions to fit their needs, sometimes making some hard assumptions about their runtimes and the state of the network at the time of their development. Today, looking back more than 20 years, we see a clear pattern in the types of mechanisms built around the Internet Protocol, IP, which can be found throughout many layers of the OSI layer system, libp2p distils these mechanisms into flat categories and defines clear interfaces that once exposed, enable other protocols and applications to use and swap them, enabling upgradability and adaptability for the runtime, without breaking the API.

We are in the process of writing better documentation, blog posts, tutorials and a formal specification. Today you can find:

To sum up, libp2p is a "network stack" -- a protocol suite -- that cleanly separates concerns, and enables sophisticated applications to only use the protocols they absolutely need, without giving up interoperability and upgradeability. libp2p grew out of IPFS, but it is built so that lots of people can use it, for lots of different projects.

Bundles

With its modular nature, libp2p can be found being used in different projects with different sets of features, while preserving the same top level API. js-libp2p is only a skeleton and should not be installed directly, if you are looking for a prebundled libp2p stack, please check:

If you have developed a libp2p bundle, please consider submitting it to this list so that it can be found easily by the users of libp2p.

Install

Again, as noted above, this module is only a skeleton and should not be used directly other than libp2p bundle implementors that want to extend its code.

npm install --save libp2p

Usage

Tutorials and Examples

You can find multiple examples on the examples folder that will guide you through using libp2p for several scenarios.

Creating your own libp2p bundle

The libp2p module acts as a glue for every libp2p module that you can use to create your own libp2p bundle. Creating your own libp2p bundle gives you a lot of freedom when it comes to customize it with features and default setup. We recommend creating your own libp2p bundle for the app you are developing that takes in account your needs (e.g. for a browser working version of libp2p that acts as the network layer of IPFS, we have a built one that leverages the Browser transports).

Example:

// Creating a bundle that adds:
//   transport: websockets + tcp
//   stream-muxing: spdy & mplex
//   crypto-channel: secio
//   discovery: multicast-dns

const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const WS = require('libp2p-websockets')
const SPDY = require('libp2p-spdy')
const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const MulticastDNS = require('libp2p-mdns')
const DHT = require('libp2p-kad-dht')
const defaultsDeep = require('@nodeutils/defaults-deep')
const Protector = require('libp2p-pnet')
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
const DelegatedContentRouter = require('libp2p-delegated-content-routing')

class Node extends Libp2p {
  constructor (_options) {
    const peerInfo = _options.peerInfo
    const defaults = {
      // The libp2p modules for this libp2p bundle
      modules: {
        transport: [
          TCP,
          new WS()                    // It can take instances too!
        ],
        streamMuxer: [
          SPDY,
          MPLEX
        ],
        connEncryption: [
          SECIO
        ],
        /** Encryption for private networks. Needs additional private key to work **/
        // connProtector: new Protector(/*protector specific opts*/),
        /** Enable custom content routers, such as delegated routing **/
        // contentRouting: [
        //   new DelegatedContentRouter(peerInfo.id)
        // ],
        /** Enable custom peer routers, such as delegated routing **/
        // peerRouting: [
        //   new DelegatedPeerRouter()
        // ],
        peerDiscovery: [
          MulticastDNS
        ],
        dht: DHT                      // DHT enables PeerRouting, ContentRouting and DHT itself components
      },

      // libp2p config options (typically found on a config.json)
      config: {                       // The config object is the part of the config that can go into a file, config.json.
        peerDiscovery: {
          autoDial: true,             // Auto connect to discovered peers (limited by ConnectionManager minPeers)
          mdns: {                     // mdns options
            interval: 1000,           // ms
            enabled: true
          },
          webrtcStar: {               // webrtc-star options
            interval: 1000,           // ms
            enabled: false
          }
          // .. other discovery module options.
        },
        relay: {                      // Circuit Relay options
          enabled: true,
          hop: {
            enabled: false,
            active: false
          }
        },
        dht: {
          kBucketSize: 20,
          enabled: true,
          randomWalk: {
            enabled: true,      // Allows to disable discovery (enabled by default)
            interval: 300e3,
            timeout: 10e3
          }
        },
        // Enable/Disable Experimental features
        EXPERIMENTAL: {               // Experimental features ("behind a flag")
          pubsub: false
        }
      }
    }

    // overload any defaults of your bundle using https://github.com/nodeutils/defaults-deep
    super(defaultsDeep(_options, defaults))
  }
}

// Now all the nodes you create, will have TCP, WebSockets, SPDY, MPLEX, SECIO and MulticastDNS support.

API

Create a Node - Libp2p.createLibp2p(options, callback)

Behaves exactly like new Libp2p(options), but doesn't require a PeerInfo. One will be generated instead

const { createLibp2p } = require('libp2p')
createLibp2p(options, (err, libp2p) => {
  if (err) throw err
  libp2p.start((err) => {
    if (err) throw err
  })
})
  • options: Object of libp2p configuration options
  • callback: Function with signature function (Error, Libp2p) {}

Create a Node alternative - new Libp2p(options)

Creates an instance of Libp2p with a custom PeerInfo provided via options.peerInfo.

Required keys in the options object:

  • peerInfo: instance of PeerInfo that contains the PeerId, Keys and multiaddrs of the libp2p Node.
  • modules.transport: An array that must include at least 1 transport, such as libp2p-tcp.

libp2p.start(callback)

Start the libp2p Node.

callback following signature function (err) {}, where err is an Error in case starting the node fails.

libp2p.stop(callback)

Stop the libp2p Node.

callback following signature function (err) {}, where err is an Error in case stopping the node fails.

libp2p.dial(peer, callback)

Dials to another peer in the network, establishes the connection.

  • peer: can be an instance of PeerInfo, PeerId, multiaddr, or a multiaddr string
  • callback following signature function (err, conn) {}, where err is an Error in of failure to dial the connection and conn is a Connection instance in case of a protocol selected, if not it is undefined.

libp2p.dialProtocol(peer, protocol, callback)

Dials to another peer in the network and selects a protocol to talk with that peer.

  • peer: can be an instance of PeerInfo, PeerId, multiaddr, or a multiaddr string
  • protocol: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
  • callback: Function with signature function (err, conn) {}, where conn is a Connection object

callback following signature function (err, conn) {}, where err is an Error in of failure to dial the connection and conn is a Connection instance in case of a protocol selected, if not it is undefined.

libp2p.dialFSM(peer, protocol, callback)

Behaves like .dial and .dialProtocol but calls back with a Connection State Machine

  • peer: can be an instance of PeerInfo, PeerId, multiaddr, or a multiaddr string
  • protocol: an optional String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
  • callback: following signature function (err, connFSM) {}, where connFSM is a Connection State Machine

libp2p.hangUp(peer, callback)

Closes an open connection with a peer, graciously.

callback following signature function (err) {}, where err is an Error in case stopping the node fails.

libp2p.peerRouting.findPeer(id, options, callback)

Looks up for multiaddrs of a peer in the DHT

  • id: instance of PeerId
  • options: object of options
  • options.maxTimeout: Number milliseconds

libp2p.contentRouting.findProviders(key, options, callback)

  • key: Buffer
  • options: object of options
  • options.maxTimeout: Number milliseconds
  • options.maxNumProviders maximum number of providers to find

libp2p.contentRouting.provide(key, callback)

  • key: Buffer

libp2p.handle(protocol, handlerFunc [, matchFunc])

Handle new protocol

  • protocol: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
  • handlerFunc: following signature function (protocol, conn) {}, where conn is a Connection object
  • matchFunc: Function for matching on protocol (exact matching, semver, etc). Default to exact match.

libp2p.unhandle(protocol)

Stop handling protocol

  • protocol: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')

Events

libp2p.on('start', () => {})

Libp2p has started, along with all its services.

libp2p.on('stop', () => {})

Libp2p has stopped, along with all its services.

libp2p.on('error', (err) => {})

An error has occurred

  • err: instance of Error
libp2p.on('peer:discovery', (peer) => {})

Peer has been discovered.

If autoDial is true, applications should not attempt to connect to the peer unless they are performing a specific action. See peer discovery and auto dial for more information.

libp2p.on('peer:connect', (peer) => {})

We have a new muxed connection to a peer

libp2p.on('peer:disconnect', (peer) => {})

We have closed a connection to a peer

libp2p.on('connection:start', (peer) => {})

We created a new connection to a peer

libp2p.on('connection:end', (peer) => {})

We closed a connection to a peer

libp2p.isStarted()

Check if libp2p is started

libp2p.ping(peer [, options], callback)

Ping a node in the network

libp2p.peerBook

PeerBook instance of the node

libp2p.peerInfo

PeerInfo instance of the node

libp2p.pubsub

Same API as IPFS PubSub, defined in the CORE API Spec. Just replace ipfs by libp2p and you are golden.


DHT methods also exposed for the time being

libp2p.dht.put(key, value, callback)

  • key: Buffer
  • value: Buffer

libp2p.dht.get(key, options, callback)

  • key: Buffer
  • options: object of options
  • options.maxTimeout: Number milliseconds

libp2p.dht.getMany(key, nVals, options, callback)

  • key: Buffer
  • nVals: Number
  • options: object of options
  • options.maxTimeout: Number milliseconds

Switch Stats API

libp2p.stats.emit('update')

Every time any stat value changes, this object emits an update event.

Global stats

libp2p.stats.global.snapshot

Should return a stats snapshot, which is an object containing the following keys and respective values:

  • dataSent: amount of bytes sent, Big number
  • dataReceived: amount of bytes received, Big number
libp2p.stats.global.movingAverages

Returns an object containing the following keys:

  • dataSent
  • dataReceived

Each one of them contains an object that has a key for each interval (60000, 300000 and 900000 miliseconds).

Each one of these values is an exponential moving-average instance.

Per-transport stats

libp2p.stats.transports()

Returns an array containing the tags (string) for each observed transport.

libp2p.stats.forTransport(transportTag).snapshot

Should return a stats snapshot, which is an object containing the following keys and respective values:

  • dataSent: amount of bytes sent, Big number
  • dataReceived: amount of bytes received, Big number
libp2p.stats.forTransport(transportTag).movingAverages

Returns an object containing the following keys:

dataSent dataReceived

Each one of them contains an object that has a key for each interval (60000, 300000 and 900000 miliseconds).

Each one of these values is an exponential moving-average instance.

Per-protocol stats

libp2p.stats.protocols()

Returns an array containing the tags (string) for each observed protocol.

libp2p.stats.forProtocol(protocolTag).snapshot

Should return a stats snapshot, which is an object containing the following keys and respective values:

  • dataSent: amount of bytes sent, Big number
  • dataReceived: amount of bytes received, Big number
libp2p.stats.forProtocol(protocolTag).movingAverages

Returns an object containing the following keys:

  • dataSent
  • dataReceived

Each one of them contains an object that has a key for each interval (60000, 300000 and 900000 miliseconds).

Each one of these values is an exponential moving-average instance.

Per-peer stats

libp2p.stats.peers()

Returns an array containing the peerIDs (B58-encoded string) for each observed peer.

libp2p.stats.forPeer(peerId:String).snapshot

Should return a stats snapshot, which is an object containing the following keys and respective values:

  • dataSent: amount of bytes sent, Big number
  • dataReceived: amount of bytes received, Big number
libp2p.stats.forPeer(peerId:String).movingAverages

Returns an object containing the following keys:

  • dataSent
  • dataReceived

Each one of them contains an object that has a key for each interval (60000, 300000 and 900000 miliseconds).

Each one of these values is an exponential moving-average instance.

Stats update interval

Stats are not updated in real-time. Instead, measurements are buffered and stats are updated at an interval. The maximum interval can be defined through the Switch constructor option stats.computeThrottleTimeout, defined in miliseconds.

Private Networks

Enforcement

Libp2p provides support for connection protection, such as for private networks. You can enforce network protection by setting the environment variable LIBP2P_FORCE_PNET=1. When this variable is on, if no protector is set via options.connProtector, Libp2p will throw an error upon creation.

Protectors

Some available network protectors:

Development

Clone and install dependencies:

> git clone https://github.com/ipfs/js-ipfs.git
> cd js-ipfs
> npm install

Tests

Run unit tests

# run all the unit tsts
> npm test

# run just Node.js tests
> npm run test:node

# run just Browser tests (Chrome)
> npm run test:browser

Packages

List of packages currently in existence for libp2p

This table is generated using the module package-table with package-table --data=package-list.json.

| Package | Version | Deps | CI | Coverage | Lead Maintainer | | ---------|---------|---------|---------|---------|--------- | | Libp2p | | interface-libp2p | npm | Deps | Travis CI | codecov | N/A | | libp2p | npm | Deps | Travis CI | codecov | Jacob Heun | | Connection | | interface-connection | npm | Deps | Travis CI | codecov | Jacob Heun | | Transport | | interface-transport | npm | Deps | Travis CI | codecov | Jacob Heun | | libp2p-tcp | npm | Deps | Travis CI | codecov | Jacob Heun | | libp2p-udp | npm | Deps | Travis CI | codecov | N/A | | libp2p-udt | npm | Deps | Travis CI | codecov | N/A | | libp2p-utp | npm | Deps | Travis CI | codecov | N/A | | libp2p-webrtc-direct | npm | Deps | Travis CI | codecov | Vasco Santos | | libp2p-webrtc-star | npm | Deps | Travis CI | codecov | Vasco Santos | | libp2p-websockets | npm | Deps | Travis CI | codecov | Jacob Heun | | libp2p-websocket-star | npm | Deps | Travis CI | codecov | Jacob Heun | | libp2p-websocket-star-rendezvous | npm | Deps | Travis CI | codecov | Jacob Heun | | Crypto Channels | | libp2p-secio | npm | Deps | Travis CI | codecov | Friedel Ziegelmayer | | Stream Muxers | | interface-stream-muxer | npm | Deps | Travis CI | codecov | Jacob Heun | | libp2p-mplex | npm | Deps | Travis CI | codecov | Vasco Santos | | libp2p-spdy | npm | Deps | Travis CI | codecov | Jacob Heun | | Discovery | | interface-peer-discovery | npm | Deps | Travis CI | codecov | N/A | | libp2p-bootstrap | npm | Deps | Travis CI | codecov | Vasco Santos | | libp2p-kad-dht | npm | Deps | Travis CI | codecov | Vasco Santos | | libp2p-mdns | npm | Deps | Travis CI | codecov | Jacob Heun | | libp2p-rendezvous | npm | Deps | Travis CI | codecov | N/A | | libp2p-webrtc-star | npm | Deps | Travis CI | codecov | Vasco Santos | | libp2p-websocket-star | npm | Deps | Travis CI | codecov | Jacob Heun | | NAT Traversal | | libp2p-circuit | npm | Deps | Travis CI | codecov | Jacob Heun | | libp2p-nat-mngr | npm | Deps | Travis CI | codecov | N/A | | Data Types | | peer-book | npm | Deps | Travis CI | codecov | Pedro Teixeira | | peer-id | npm | Deps | Travis CI | codecov | Pedro Teixeira | | peer-info | npm | Deps | Travis CI | codecov | Pedro Teixeira | | Content Routing | | interface-content-routing | npm | Deps | Travis CI | codecov | N/A | | libp2p-delegated-content-routing | npm | Deps | Travis CI | codecov | Jacob Heun | | libp2p-kad-dht | npm | Deps | Travis CI | codecov | Vasco Santos | | Peer Routing | | interface-peer-routing | npm | Deps | Travis CI | codecov | N/A | | libp2p-delegated-peer-routing | npm | Deps | Travis CI | codecov | Jacob Heun | | libp2p-kad-dht | npm | Deps | Travis CI | codecov | Vasco Santos | | Record Store | | interface-record-store | npm | Deps | Travis CI | codecov | N/A | | libp2p-record | npm | Deps | Travis CI | codecov | Vasco Santos | | Generics | | libp2p-connection-manager | npm | Deps | Travis CI | codecov | N/A | | libp2p-crypto | npm | Deps | Travis CI | codecov | Friedel Ziegelmayer | | libp2p-crypto-secp256k1 | npm | Deps | Travis CI | codecov | Friedel Ziegelmayer | | libp2p-switch | npm | Deps | Travis CI | codecov | Jacob Heun | | Extensions | | libp2p-floodsub | npm | Deps | Travis CI | codecov | Vasco Santos | | libp2p-identify | npm | Deps | Travis CI | codecov | Jacob Heun | | libp2p-keychain | npm | Deps | Travis CI | codecov | Vasco Santos | | libp2p-ping | npm | Deps | Travis CI | codecov | Jacob Heun | | libp2p-pnet | npm | Deps | Travis CI | codecov | Jacob Heun | | Utilities | | p2pcat | npm | Deps | Travis CI | codecov | N/A |

Contribute

The libp2p implementation in JavaScript is a work in progress. As such, there are a few things you can do right now to help out:

  • Go through the modules and check out existing issues. This would be especially useful for modules in active development. Some knowledge of IPFS/libp2p may be required, as well as the infrastructure behind it - for instance, you may need to read up on p2p and more complex operations like muxing to be able to help technically.
  • Perform code reviews. Most of this has been developed by @diasdavid, which means that more eyes will help a) speed the project along b) ensure quality and c) reduce possible future bugs.
  • Add tests. There can never be enough tests.

License

MIT © Protocol Labs