multistream-select
Implementation of multistream spec https://github.com/ipfs/specs/blob/wire/protocol/network/wire.md#multistream
Last updated 9 years ago by daviddias .
MIT · Original npm · Tarball · package.json
$ npm install multistream-select 
SYNC missed versions from official npm registry.

js-multistream-select

Dependency Status js-standard-style

JavaScript implementation of multistream-select

Lead Maintainer

Jacob Heun

Table of Contents

Background

What is multistream-select?

TLDR; multistream-select is protocol multiplexing per connection/stream. Full spec here

Select a protocol flow

The caller will send "interactive" messages, expecting for some acknowledgement from the callee, which will "select" the handler for the desired and supported protocol:

< /multistream-select/0.3.0  # i speak multistream-select/0.3.0
> /multistream-select/0.3.0  # ok, let's speak multistream-select/0.3.0
> /ipfs-dht/0.2.3            # i want to speak ipfs-dht/0.2.3
< na                         # ipfs-dht/0.2.3 is not available
> /ipfs-dht/0.1.9            # What about ipfs-dht/0.1.9 ?
< /ipfs-dht/0.1.9            # ok let's speak ipfs-dht/0.1.9 -- in a sense acts as an ACK
> <dht-message>
> <dht-message>
> <dht-message>

This mode also packs a ls option, so that the callee can list the protocols it currently supports

Install

npm i multistream-select

Usage

const MSS = require('multistream-select')
// You can now use
// MSS.Dialer - actively select a protocol with a remote
// MSS.Listener - handle a protocol with a remote

Dialer

const pipe = require('it-pipe')
const MSS = require('multistream-select')
const Mplex = require('libp2p-mplex')

const muxer = new Mplex()
const muxedStream = muxer.newStream()

const mss = new MSS.Dialer(muxedStream)

// mss.select(protocol(s))
// Select from one of the passed protocols (in priority order)
// Returns selected stream and protocol
const { stream: dhtStream, protocol } = await mss.select([
  // This might just be different versions of DHT, but could be different impls
  '/ipfs-dht/2.0.0', // Most of the time this will probably just be one item.
  '/ipfs-dht/1.0.0'
])

// Typically this stream will be passed back to the caller of libp2p.dialProtocol
//
// ...it might then do something like this:
// try {
//   await pipe(
//     [uint8ArrayFromString('Some DHT data')]
//     dhtStream,
//     async source => {
//       for await (const chunk of source)
//         // DHT response data
//     }
//   )
// } catch (err) {
//   // Error in stream
// }

Listener

const pipe = require('it-pipe')
const MSS = require('multistream-select')
const Mplex = require('libp2p-mplex')

const muxer = new Mplex({
  async onStream (muxedStream) {
    const mss = new MSS.Listener(muxedStream)

    // mss.handle(handledProtocols)
    // Returns selected stream and protocol
    const { stream, protocol } = await mss.handle([
      '/ipfs-dht/1.0.0',
      '/ipfs-bitswap/1.0.0'
    ])

    // Typically here we'd call the handler function that was registered in
    // libp2p for the given protocol:
    // e.g. handlers[protocol].handler(stream)
    //
    // If protocol was /ipfs-dht/1.0.0 it might do something like this:
    // try {
    //   await pipe(
    //     dhtStream,
    //     source => (async function * () {
    //       for await (const chunk of source)
    //         // Incoming DHT data -> process and yield to respond
    //     })(),
    //     dhtStream
    //   )
    // } catch (err) {
    //   // Error in stream
    // }
  }
})

API

new MSS.Dialer(duplex)

Create a new multistream select "dialer" instance which can be used to negotiate a protocol to use, list all available protocols the remote supports, or do both.

Parameters

Returns

A new multistream select dialer instance.

Examples

const dialer = new MSS.Dialer(duplex)

dialer.select(protocols, [options])

Negotiate a protocol to use from a list of protocols.

Parameters

  • protocols (String[]/String) - A list of protocols (or single protocol) to negotiate with. Protocols are attempted in order until a match is made.
  • options ({ signal: AbortSignal }) - an options object containing an AbortSignal

Returns

Promise<{ stream<Object>, protocol<String> }> - A stream for the selected protocol and the protocol that was selected from the list of protocols provided to select.

Note that after a protocol is selected dialer can no longer be used.

Examples

const { stream, protocol } = await dialer.select([
  // This might just be different versions of DHT, but could be different impls
  '/ipfs-dht/2.0.0', // Most of the time this will probably just be one item.
  '/ipfs-dht/1.0.0'
])
// Now talk `protocol` on `stream`

dialer.ls([options])

List protocols that the remote supports.

Parameters

  • options ({ signal: AbortSignal }) - an options object containing an AbortSignal

Returns

String[] - A list of all the protocols the remote supports.

Examples

const protocols = await dialer.ls()
const wantedProto = '/ipfs-dht/2.0.0'

if (!protocols.includes(wantedProto)) {
  throw new Error('remote does not support ' + wantedProto)
}

// Now use dialer.select to use wantedProto, safe in the knowledge it is supported

new MSS.Listener(duplex)

Construct a new multistream select "listener" instance which can be used to handle multistream protocol selections for particular protocols.

Parameters

Returns

A new multistream select listener instance.

Examples

const listener = new MSS.Listener(duplex)

listener.handle(protocols, [options])

Handle multistream protocol selections for the given list of protocols.

Parameters

  • protocols (String[]/String) - A list of protocols (or single protocol) that this listener is able to speak.
  • options ({ signal: AbortSignal }) - an options object containing an AbortSignal

Returns

Promise<{ stream<Object>, protocol<String> }> - A stream for the selected protocol and the protocol that was selected from the list of protocols provided to select.

Note that after a protocol is handled listener can no longer be used.

Examples

const { stream, protocol } = await listener.handle([
  '/ipfs-dht/1.0.0',
  '/ipfs-bitswap/1.0.0'
])
// Remote wants to speak `protocol`

Contribute

Contributions welcome. Please check out the issues.

Check out our contributing document for more information on how we work, and about contributing in general. Please be aware that all interactions related to multiformats are subject to the IPFS Code of Conduct.

Small note: If editing the README, please conform to the standard-readme specification.

License

MIT

Current Tags

  • 0.15.0                                ...           beta (5 years ago)
  • 3.0.2                                ...           latest (3 years ago)

40 Versions

  • 3.0.2                                ...           3 years ago
  • 3.0.1                                ...           3 years ago
  • 3.0.0                                ...           3 years ago
  • 2.0.1                                ...           3 years ago
  • 2.0.0                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
  • 0.15.2                                ...           5 years ago
  • 0.15.1                                ...           5 years ago
  • 0.15.0                                ...           5 years ago
  • 0.14.6                                ...           5 years ago
  • 0.14.5                                ...           5 years ago
  • 0.14.4                                ...           6 years ago
  • 0.14.3                                ...           6 years ago
  • 0.14.2                                ...           7 years ago
  • 0.14.1                                ...           7 years ago
  • 0.14.0                                ...           7 years ago
  • 0.13.5                                ...           8 years ago
  • 0.13.4                                ...           8 years ago
  • 0.13.3                                ...           8 years ago
  • 0.13.2                                ...           8 years ago
  • 0.13.1                                ...           8 years ago
  • 0.13.0                                ...           8 years ago
  • 0.12.0                                ...           8 years ago
  • 0.11.1                                ...           8 years ago
  • 0.11.0                                ...           8 years ago
  • 0.10.0                                ...           8 years ago
  • 0.9.0                                ...           8 years ago
  • 0.8.0                                ...           8 years ago
  • 0.7.0                                ...           8 years ago
  • 0.6.5                                ...           9 years ago
  • 0.6.4                                ...           9 years ago
  • 0.6.3                                ...           9 years ago
  • 0.6.2                                ...           9 years ago
  • 0.6.1                                ...           9 years ago
  • 0.6.0                                ...           9 years ago
  • 0.5.0                                ...           9 years ago
  • 0.4.0                                ...           9 years ago
  • 0.3.0                                ...           9 years ago
  • 0.2.1                                ...           9 years ago
  • 0.2.0                                ...           9 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (9)

© 2010 - cnpmjs.org x YWFE | Home | YWFE