Home

Awesome

⛔️ DEPRECATED: interface-transport is now included in libp2p-interfaces

interface-transport

Discourse posts Dependency Status js-standard-style

A test suite and interface you can use to implement a libp2p transport. A libp2p transport is understood as something that offers a dial and listen interface.

The primary goal of this module is to enable developers to pick and swap their transport module as they see fit for their libp2p installation, without having to go through shims or compatibility issues. This module and test suite were heavily inspired by abstract-blob-store, interface-stream-muxer and others.

Publishing a test suite as a module lets multiple modules all ensure compatibility since they use the same test suite.

The purpose of this interface is not to reinvent any wheels when it comes to dialing and listening to transports. Instead, it tries to provide a uniform API for several transports through a shimmed interface.

Lead Maintainer

Jacob Heun

Modules that implement the interface

Badge

Include this badge in your readme if you make a module that is compatible with the interface-transport API. You can validate this by running the tests.

How to use the battery of tests

Node.js

/* eslint-env mocha */
'use strict'

const tests = require('interface-transport')
const multiaddr = require('multiaddr')
const YourTransport = require('../src')

describe('compliance', () => {
  tests({
    setup (options) {
      let transport = new YourTransport(options)

      const addrs = [
        multiaddr('valid-multiaddr-for-your-transport'),
        multiaddr('valid-multiaddr2-for-your-transport')
      ]

      const network = require('my-network-lib')
      const connect = network.connect
      const connector = {
        delay (delayMs) {
          // Add a delay in the connection mechanism for the transport
          // (this is used by the dial tests)
          network.connect = (...args) => setTimeout(() => connect(...args), delayMs)
        },
        restore () {
          // Restore the connection mechanism to normal
          network.connect = connect
        }
      }

      return { transport, addrs, connector }
    },
    teardown () {
      // Clean up any resources created by setup()
    }
  })
})

API

A valid transport (one that follows the interface defined) must implement the following API:

Table of contents:

Types

Upgrader

Upgraders have 2 methods: upgradeOutbound and upgradeInbound.

const connection = await upgrader.upgradeOutbound(multiaddrConnection)
const connection = await upgrader.upgradeInbound(multiaddrConnection)

The Upgrader methods take a MultiaddrConnection and will return an interface-connection instance.

MultiaddrConnection

Creating a transport instance

Creates a new Transport instance. options is an JavaScript object that should include the necessary parameters for the transport instance. Options MUST include an Upgrader instance, as Transports will use this to return interface-connection instances from transport.dial and the listener handlerFunction.

Note: Why is it important to instantiate a transport - Some transports have state that can be shared between the dialing and listening parts. For example with libp2p-webrtc-star, in order to dial a peer, the peer must be part of some signaling network that is shared with the listener.

Dial to another peer

This method uses a transport to dial a Peer listening on multiaddr.

multiaddr must be of the type multiaddr.

[options] the options that may be passed to the dial. Must support the signal option (see below)

Dial MUST call and return upgrader.upgradeOutbound(multiaddrConnection). The upgrader will return an interface-connection instance.

The dial may throw an Error instance if there was a problem connecting to the multiaddr.

Canceling a dial

Dials may be cancelled using an AbortController:

const AbortController = require('abort-controller')
const { AbortError } = require('interface-transport')
const controller = new AbortController()
try {
  const conn = await mytransport.dial(ma, { signal: controller.signal })
  // Do stuff with conn here ...
} catch (err) {
  if(err.code === AbortError.code) {
    // Dial was aborted, just bail out
    return
  }
  throw err
}

// ----
// In some other part of the code:
  controller.abort()
// ----

Filtering Addresses

When using a transport its important to be able to filter out multiaddrs that the transport doesn't support. A transport instance provides a filter method to return only the valid addresses it supports.

multiaddrs must be an array of type multiaddr. Filter returns an array of multiaddr.

Create a listener

This method creates a listener on the transport. Implementations MUST call upgrader.upgradeInbound(multiaddrConnection) and pass its results to the handlerFunction and any emitted connection events.

options is an optional object that contains the properties the listener must have, in order to properly listen on a given transport/socket.

handlerFunction is a function called each time a new connection is received. It must follow the following signature: function (conn) {}, where conn is a connection that follows the interface-connection.

The listener object created may emit the following events:

Start a listener

This method puts the listener in listening mode, waiting for incoming connections.

multiaddr is the address that the listener should bind to.

Get listener addrs

This method returns the addresses on which this listener is listening. Useful when listening on port 0 or any interface (0.0.0.0).

Stop a listener

This method closes the listener so that no more connections can be opened on this transport instance.

options is an optional object that may contain the following properties: