Vasco Santos f1eb373235 feat: discovery modules from transports should be added (#510)
* feat: discovery modules from transports should be added

* chore: apply suggestions from code review

Co-Authored-By: Jacob Heun <jacobheun@gmail.com>

* chore: address review

* chore: apply suggestions from code review

Co-Authored-By: Jacob Heun <jacobheun@gmail.com>
2020-01-24 14:40:40 +01:00
2016-09-14 01:29:30 -04:00
2019-09-24 14:10:58 +02:00
2017-07-12 07:04:29 +01:00
2015-07-23 14:05:43 -07:00
2018-12-17 12:20:12 +01:00
2019-12-11 10:52:13 +01:00
2020-01-24 14:40:40 +01:00

libp2p hex logo

The JavaScript implementation of the libp2p Networking Stack.



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.

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 make 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

IMPORTANT NOTE: We are currently on the way of migrating all our libp2p modules to use async await and async iterators, instead of callbacks and pull-streams. As a consequence, when you start a new libp2p project, we must check which versions of the modules you should use. For now, it is required to use the modules using callbacks with libp2p, while we are working on getting the remaining modules ready for a full migration. For more details, you can have a look at libp2p/js-libp2p#266.

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 into account your needs (e.g. for a browser working version of libp2p that acts as the network layer of IPFS, we have 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 GossipSub = require('libp2p-gossipsub')
const defaultsDeep = require('@nodeutils/defaults-deep')
const Protector = require('libp2p/src/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
        pubsub: GossipSub
      },

      // 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
          }
        },
        pubsub: {
          enabled: true,
          emitSelf: true,      // whether the node should emit to self on publish, in the event of the topic being subscribed
          signMessages: true,  // if messages should be signed
          strictSigning: true  // if message signing should be required
        }
      }
    }

    // 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

See API.md.

Development

Clone and install dependencies:

> git clone https://github.com/libp2p/js-libp2p.git
> cd js-libp2p
> 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
libp2p npm Deps Travis CI codecov Jacob Heun
libp2p-daemon npm Deps Travis CI codecov Jacob Heun
libp2p-daemon-client npm Deps Travis CI codecov N/A
libp2p-interfaces npm Deps Travis CI codecov N/A
interop-libp2p npm Deps Travis CI codecov N/A
transports
libp2p-tcp npm Deps Travis CI codecov N/A
libp2p-utp npm Deps Travis CI codecov N/A
libp2p-webrtc-direct npm Deps Travis CI codecov N/A
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
secure channels
libp2p-secio npm Deps Travis CI codecov Friedel Ziegelmayer
stream multiplexers
libp2p-mplex npm Deps Travis CI codecov Vasco Santos
libp2p-spdy npm Deps Travis CI codecov Jacob Heun
peer discovery
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
content routing
libp2p-delegated-content-routing npm Deps Travis CI codecov N/A
libp2p-kad-dht npm Deps Travis CI codecov Vasco Santos
peer routing
libp2p-delegated-peer-routing npm Deps Travis CI codecov N/A
libp2p-kad-dht npm Deps Travis CI codecov Vasco Santos
utilities
libp2p-crypto npm Deps Travis CI codecov Jacob Heun
libp2p-crypto-secp256k1 npm Deps Travis CI codecov Friedel Ziegelmayer
data types
peer-book npm Deps Travis CI codecov N/A
peer-id npm Deps Travis CI codecov N/A
peer-info npm Deps Travis CI codecov Vasco Santos
pubsub
libp2p-pubsub npm Deps Travis CI codecov Vasco Santos
libp2p-floodsub npm Deps Travis CI codecov N/A
libp2p-gossipsub npm Deps Travis CI codecov N/A
extensions
libp2p-nat-mgnr npm Deps Travis CI codecov N/A
libp2p-utils npm Deps Travis CI codecov Vasco Santos

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

Description
No description provided
Readme 23 MiB
Languages
TypeScript 100%