You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TLDR; multistream-select is protocol multiplexing per connection/stream. [Full spec here](https://github.com/multiformats/multistream-select)
43
40
44
-
####Select a protocol flow
41
+
### Select a protocol flow
45
42
46
43
The caller will send "interactive" messages, expecting for some acknowledgement from the callee, which will "select" the handler for the desired and supported protocol:
47
44
48
-
```console
45
+
```
49
46
< /multistream-select/0.3.0 # i speak multistream-select/0.3.0
// Typically here we'd call the handler function that was registered in
122
-
// libp2p for the given protocol:
123
-
// e.g. handlers[protocol].handler(stream)
124
-
//
125
-
// If protocol was /ipfs-dht/1.0.0 it might do something like this:
126
-
// try {
127
-
// await pipe(
128
-
// dhtStream,
129
-
// source => (async function * () {
130
-
// for await (const chunk of source)
131
-
// // Incoming DHT data -> process and yield to respond
132
-
// })(),
133
-
// dhtStream
134
-
// )
135
-
// } catch (err) {
136
-
// // Error in stream
137
-
// }
138
-
}
139
-
})
140
-
```
141
-
142
-
## API
143
-
144
-
### `mss.select(dulpex, protocols, [options])`
145
-
146
-
Negotiate a protocol to use from a list of protocols.
147
-
148
-
#### Parameters
149
-
150
-
-`duplex` (`Duplex`) - A [duplex iterable stream](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#duplex-it) to dial on.
151
-
-`protocols` (`string[]`/`string`) - A list of protocols (or single protocol) to negotiate with. Protocols are attempted in order until a match is made.
152
-
-`options` (`{ signal: AbortSignal, writeBytes?: boolean }`) - an options object containing an AbortSignal and an optional boolean `writeBytes` - if this is true, `Uint8Array`s will be written into `duplex`, otherwise `Uint8ArrayList`s will
153
-
154
-
#### Returns
155
-
156
-
`Promise<{ stream<Duplex>, protocol<string> }>` - A stream for the selected protocol and the protocol that was selected from the list of protocols provided to `select`.
157
-
158
-
Note that after a protocol is selected `dialer` can no longer be used.
159
-
160
-
#### Examples
161
-
162
-
```js
163
-
const { stream, protocol } =awaitdialer.select([
164
-
// This might just be different versions of DHT, but could be different impls
165
-
'/ipfs-dht/2.0.0', // Most of the time this will probably just be one item.
166
-
'/ipfs-dht/1.0.0'
167
-
])
168
-
// Now talk `protocol` on `stream`
169
-
```
170
-
171
-
### `mss.handle(duplex, protocols, [options])`
57
+
## API Docs
172
58
173
-
Handle multistream protocol selections for the given list of protocols.
174
-
175
-
#### Parameters
176
-
177
-
-`duplex` (`Duplex`) - A [duplex iterable stream](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#duplex-it) to listen on.
178
-
-`protocols` (`String[]`/`String`) - A list of protocols (or single protocol) that this listener is able to speak.
179
-
-`options` (`{ signal: AbortSignal, writeBytes?: boolean }`) - an options object containing an AbortSignal and an optional boolean `writeBytes` - if this is true, `Uint8Array`s will be written into `duplex`, otherwise `Uint8ArrayList`s will
180
-
181
-
#### Returns
182
-
183
-
`Promise<{ stream<Duplex>, protocol<string> }>` - A stream for the selected protocol and the protocol that was selected from the list of protocols provided to `select`.
184
-
185
-
Note that after a protocol is handled `listener` can no longer be used.
* Handle multistream protocol selections for the given list of protocols.
14
+
*
15
+
* Note that after a protocol is handled `listener` can no longer be used.
16
+
*
17
+
* @param stream - A duplex iterable stream to listen on
18
+
* @param protocols - A list of protocols (or single protocol) that this listener is able to speak.
19
+
* @param options - an options object containing an AbortSignal and an optional boolean `writeBytes` - if this is true, `Uint8Array`s will be written into `duplex`, otherwise `Uint8ArrayList`s will
20
+
* @returns A stream for the selected protocol and the protocol that was selected from the list of protocols provided to `select`
21
+
* @example
22
+
*
23
+
* ```js
24
+
* import { pipe } from 'it-pipe'
25
+
* import * as mss from '@libp2p/multistream-select'
* Negotiate a protocol to use from a list of protocols.
18
+
*
19
+
* @param stream - A duplex iterable stream to dial on
20
+
* @param protocols - A list of protocols (or single protocol) to negotiate with. Protocols are attempted in order until a match is made.
21
+
* @param options - An options object containing an AbortSignal and an optional boolean `writeBytes` - if this is true, `Uint8Array`s will be written into `duplex`, otherwise `Uint8ArrayList`s will
22
+
* @returns A stream for the selected protocol and the protocol that was selected from the list of protocols provided to `select`.
23
+
* @example
24
+
*
25
+
* ```js
26
+
* import { pipe } from 'it-pipe'
27
+
* import * as mss from '@libp2p/multistream-select'
28
+
* import { Mplex } from '@libp2p/mplex'
29
+
*
30
+
* const muxer = new Mplex()
31
+
* const muxedStream = muxer.newStream()
32
+
*
33
+
* // mss.select(protocol(s))
34
+
* // Select from one of the passed protocols (in priority order)
0 commit comments