Installations
npm install multistream-select
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
16.13.0
NPM Version
8.1.0
Statistics
29 Stars
200 Commits
18 Forks
18 Watching
15 Branches
41 Contributors
Updated on 18 May 2023
Bundle Size
39.93 kB
Minified
12.82 kB
Minified + Gzipped
Languages
JavaScript (99.11%)
TypeScript (0.89%)
Total Downloads
Cumulative downloads
Total Downloads
2,192,594
Last day
135.8%
224
Compared to previous day
Last week
22.2%
1,013
Compared to previous week
Last month
-28.7%
5,054
Compared to previous month
Last year
-36.9%
174,251
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
⛔️ ARCHIVED: multistream-select can now be found at @libp2p/multistream-select
js-multistream-select
JavaScript implementation of multistream-select
Lead Maintainer
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:
1< /multistream-select/0.3.0 # i speak multistream-select/0.3.0 2> /multistream-select/0.3.0 # ok, let's speak multistream-select/0.3.0 3> /ipfs-dht/0.2.3 # i want to speak ipfs-dht/0.2.3 4< na # ipfs-dht/0.2.3 is not available 5> /ipfs-dht/0.1.9 # What about ipfs-dht/0.1.9 ? 6< /ipfs-dht/0.1.9 # ok let's speak ipfs-dht/0.1.9 -- in a sense acts as an ACK 7> <dht-message> 8> <dht-message> 9> <dht-message>
This mode also packs a ls
option, so that the callee can list the protocols it currently supports
Install
1npm i multistream-select
Usage
1const MSS = require('multistream-select') 2// You can now use 3// MSS.Dialer - actively select a protocol with a remote 4// MSS.Listener - handle a protocol with a remote
Dialer
1const pipe = require('it-pipe') 2const MSS = require('multistream-select') 3const Mplex = require('libp2p-mplex') 4 5const muxer = new Mplex() 6const muxedStream = muxer.newStream() 7 8const mss = new MSS.Dialer(muxedStream) 9 10// mss.select(protocol(s)) 11// Select from one of the passed protocols (in priority order) 12// Returns selected stream and protocol 13const { stream: dhtStream, protocol } = await mss.select([ 14 // This might just be different versions of DHT, but could be different impls 15 '/ipfs-dht/2.0.0', // Most of the time this will probably just be one item. 16 '/ipfs-dht/1.0.0' 17]) 18 19// Typically this stream will be passed back to the caller of libp2p.dialProtocol 20// 21// ...it might then do something like this: 22// try { 23// await pipe( 24// [uint8ArrayFromString('Some DHT data')] 25// dhtStream, 26// async source => { 27// for await (const chunk of source) 28// // DHT response data 29// } 30// ) 31// } catch (err) { 32// // Error in stream 33// }
Listener
1const pipe = require('it-pipe') 2const MSS = require('multistream-select') 3const Mplex = require('libp2p-mplex') 4 5const muxer = new Mplex({ 6 async onStream (muxedStream) { 7 const mss = new MSS.Listener(muxedStream) 8 9 // mss.handle(handledProtocols) 10 // Returns selected stream and protocol 11 const { stream, protocol } = await mss.handle([ 12 '/ipfs-dht/1.0.0', 13 '/ipfs-bitswap/1.0.0' 14 ]) 15 16 // Typically here we'd call the handler function that was registered in 17 // libp2p for the given protocol: 18 // e.g. handlers[protocol].handler(stream) 19 // 20 // If protocol was /ipfs-dht/1.0.0 it might do something like this: 21 // try { 22 // await pipe( 23 // dhtStream, 24 // source => (async function * () { 25 // for await (const chunk of source) 26 // // Incoming DHT data -> process and yield to respond 27 // })(), 28 // dhtStream 29 // ) 30 // } catch (err) { 31 // // Error in stream 32 // } 33 } 34})
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
duplex
(Object
) - A duplex iterable stream to dial on.
Returns
A new multistream select dialer instance.
Examples
1const 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
1const { stream, protocol } = await dialer.select([ 2 // This might just be different versions of DHT, but could be different impls 3 '/ipfs-dht/2.0.0', // Most of the time this will probably just be one item. 4 '/ipfs-dht/1.0.0' 5]) 6// 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
1const protocols = await dialer.ls() 2const wantedProto = '/ipfs-dht/2.0.0' 3 4if (!protocols.includes(wantedProto)) { 5 throw new Error('remote does not support ' + wantedProto) 6} 7 8// 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
duplex
(Object
) - A duplex iterable stream to listen on.
Returns
A new multistream select listener instance.
Examples
1const 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
1const { stream, protocol } = await listener.handle([ 2 '/ipfs-dht/1.0.0', 3 '/ipfs-bitswap/1.0.0' 4]) 5// 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
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 8/27 approved changesets -- score normalized to 2
Reason
project is archived
Details
- Warn: Repository is archived.
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/multiformats/js-multistream-select/test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:28: update your workflow using https://app.stepsecurity.io/secureworkflow/multiformats/js-multistream-select/test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:29: update your workflow using https://app.stepsecurity.io/secureworkflow/multiformats/js-multistream-select/test.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/test.yml:35: update your workflow using https://app.stepsecurity.io/secureworkflow/multiformats/js-multistream-select/test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:40: update your workflow using https://app.stepsecurity.io/secureworkflow/multiformats/js-multistream-select/test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:41: update your workflow using https://app.stepsecurity.io/secureworkflow/multiformats/js-multistream-select/test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:51: update your workflow using https://app.stepsecurity.io/secureworkflow/multiformats/js-multistream-select/test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:52: update your workflow using https://app.stepsecurity.io/secureworkflow/multiformats/js-multistream-select/test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:62: update your workflow using https://app.stepsecurity.io/secureworkflow/multiformats/js-multistream-select/test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:63: update your workflow using https://app.stepsecurity.io/secureworkflow/multiformats/js-multistream-select/test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:73: update your workflow using https://app.stepsecurity.io/secureworkflow/multiformats/js-multistream-select/test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:74: update your workflow using https://app.stepsecurity.io/secureworkflow/multiformats/js-multistream-select/test.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/test.yml:56
- Warn: npmCommand not pinned by hash: .github/workflows/test.yml:67
- Warn: npmCommand not pinned by hash: .github/workflows/test.yml:78
- Warn: npmCommand not pinned by hash: .github/workflows/test.yml:16
- Warn: npmCommand not pinned by hash: .github/workflows/test.yml:33
- Warn: npmCommand not pinned by hash: .github/workflows/test.yml:45
- Info: 0 out of 11 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
- Info: 0 out of 6 npmCommand dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/test.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 13 are checked with a SAST tool
Score
3.6
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to multistream-select
@libp2p/multistream-select
JavaScript implementation of multistream-select
multistream
A stream that emits multiple other streams one after another (streams3)
@xstorage/xs-js-multistream-select
xStorage implementation of ipfs js-multistream-select
@types/multistream
TypeScript definitions for multistream