Gathering detailed insights and metrics for multistream-select
Gathering detailed insights and metrics for multistream-select
Gathering detailed insights and metrics for multistream-select
Gathering detailed insights and metrics for 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
npm install multistream-select
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
29 Stars
200 Commits
18 Forks
18 Watching
15 Branches
41 Contributors
Updated on 18 May 2023
Minified
Minified + Gzipped
JavaScript (99.11%)
TypeScript (0.89%)
Cumulative downloads
Total Downloads
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
JavaScript implementation of multistream-select
multistream-select
?TLDR; multistream-select is protocol multiplexing per connection/stream. Full spec here
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
1npm i multistream-select
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
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// }
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})
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.
duplex
(Object
) - A duplex iterable stream to dial on.A new multistream select dialer instance.
1const dialer = new MSS.Dialer(duplex)
dialer.select(protocols, [options])
Negotiate a protocol to use from a list of protocols.
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 AbortSignalPromise<{ 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.
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.
options
({ signal: AbortSignal }
) - an options object containing an AbortSignalString[]
- A list of all the protocols the remote supports.
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.
duplex
(Object
) - A duplex iterable stream to listen on.A new multistream select listener instance.
1const listener = new MSS.Listener(duplex)
listener.handle(protocols, [options])
Handle multistream protocol selections for the given list of protocols.
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 AbortSignalPromise<{ 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.
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`
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.
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
Reason
Found 8/27 approved changesets -- score normalized to 2
Reason
project is archived
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
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 More