Alex Potsides c77d8de2e7
fix: remove use of assert module (#34)
* fix: remove use of assert module

The polyfill is big, we can simulate it by throwing an Error and it doesn't work under React Native.

* chore: pr comments
2020-02-17 22:19:13 +01:00
..
2019-11-15 14:49:39 +01:00
2019-11-14 11:52:06 +01:00

interface-topology

Implementation of the topology interface used by the js-libp2p registrar.

Topologies can be used in conjunction with js-libp2p to help shape its network and the overlays of its subsystems, such as pubsub and the DHT.

Table of Contents

Implementations

Topology

A libp2p topology with a group of common peers.

Multicodec Topology

A libp2p topology with a group of peers that support the same protocol.

Install

$ npm install libp2p-interfaces

Modules using the interface

TBA

Usage

Topology

const Topology = require('libp2p-interfaces/src/topology')

const toplogy = new Topology({
  min: 0,
  max: 50
})

Multicodec Topology

const MulticodecTopology = require('libp2p-interfaces/src/topology/multicodec-topology')

const toplogy = new MulticodecTopology({
  min: 0,
  max: 50,
  multicodecs: ['/echo/1.0.0'],
  handlers: {
    onConnect: (peerInfo, conn) => {},
    onDisconnect: (peerInfo) => {}
  }
})

API

The MulticodecTopology extends the Topology, which makes the Topology API a subset of the MulticodecTopology API.

Topology

  • Topology
    • peers<Map<string, PeerInfo>>: A Map of peers belonging to the topology.
    • disconnect<function(PeerInfo)>: Called when a peer has been disconnected

Constructor

const toplogy = new Topology({
  min: 0,
  max: 50,
  handlers: {
    onConnect: (peerInfo, conn) => {},
    onDisconnect: (peerInfo) => {}
  }
})

Parameters

  • properties is an Object containing the properties of the topology.
    • min is a number with the minimum needed connections (default: 0)
    • max is a number with the maximum needed connections (default: Infinity)
    • handlers is an optional Object containing the handler called when a peer is connected or disconnected.
      • onConnect is a function called everytime a peer is connected in the topology context.
      • onDisconnect is a function called everytime a peer is disconnected in the topology context.

Set a peer

  • topology.peers.set(id, peerInfo)

Add a peer to the topology.

Parameters

  • id is the string that identifies the peer to add.
  • peerInfo is the [PeerInfo][peer-info] of the peer to add.

Notify about a peer disconnected event

  • topology.disconnect(peerInfo)

Parameters

  • peerInfo is the [PeerInfo][peer-info] of the peer disconnected.

Multicodec Topology

  • MulticodecTopology
    • registrar<Registrar>: The Registrar of the topology. This is set by the Registrar during registration.
    • peers<Map<string, PeerInfo>>: The Map of peers that belong to the topology
    • disconnect<function(PeerInfo)>: Disconnects a peer from the topology.

Constructor

const toplogy = new MulticodecTopology({
  min: 0,
  max: 50,
  multicodecs: ['/echo/1.0.0'],
  handlers: {
    onConnect: (peerInfo, conn) => {},
    onDisconnect: (peerInfo) => {}
  }
})

Parameters

  • properties is an Object containing the properties of the topology.
    • min is a number with the minimum needed connections (default: 0)
    • max is a number with the maximum needed connections (default: Infinity)
    • multicodecs is a Array<String> with the multicodecs associated with the topology.
    • handlers is an optional Object containing the handler called when a peer is connected or disconnected.
      • onConnect is a function called everytime a peer is connected in the topology context.
      • onDisconnect is a function called everytime a peer is disconnected in the topology context.