Skip to content

Commit c4c978a

Browse files
authored
docs: publish api docs (libp2p#35)
Update project config to publish api docs
1 parent 2c9e6f0 commit c4c978a

File tree

5 files changed

+113
-158
lines changed

5 files changed

+113
-158
lines changed

.gitignore

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
node_modules
2-
coverage
3-
.nyc_output
2+
build
3+
dist
4+
.docs
5+
.coverage
6+
node_modules
47
package-lock.json
58
yarn.lock
6-
docs
7-
dist

README.md

+16-152
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,20 @@
11
# @libp2p/multistream-select <!-- omit in toc -->
22

33
[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
4-
[![IRC](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
54
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
65
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-multistream-select.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-multistream-select)
7-
[![CI](https://img.shields.io/github/workflow/status/libp2p/js-libp2p-interfaces/test%20&%20maybe%20release/master?style=flat-square)](https://github.com/libp2p/js-libp2p-multistream-select/actions/workflows/js-test-and-release.yml)
6+
[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p-multistream-select/js-test-and-release.yml?branch=master\&style=flat-square)](https://github.com/libp2p/js-libp2p-multistream-select/actions/workflows/js-test-and-release.yml?query=branch%3Amaster)
87

98
> JavaScript implementation of multistream-select
109
1110
## Table of contents <!-- omit in toc -->
1211

1312
- [Install](#install)
13+
- [Browser `<script>` tag](#browser-script-tag)
1414
- [Background](#background)
1515
- [What is `multistream-select`?](#what-is-multistream-select)
16-
- [Select a protocol flow](#select-a-protocol-flow)
17-
- [Usage](#usage)
18-
- [Dialer](#dialer)
19-
- [Listener](#listener)
20-
- [API](#api)
21-
- [`mss.select(dulpex, protocols, [options])`](#mssselectdulpex-protocols-options)
22-
- [Parameters](#parameters)
23-
- [Returns](#returns)
24-
- [Examples](#examples)
25-
- [`mss.handle(duplex, protocols, [options])`](#msshandleduplex-protocols-options)
26-
- [Parameters](#parameters-1)
27-
- [Returns](#returns-1)
28-
- [Examples](#examples-1)
16+
- [Select a protocol flow](#select-a-protocol-flow)
17+
- [API Docs](#api-docs)
2918
- [License](#license)
3019
- [Contribution](#contribution)
3120

@@ -35,17 +24,25 @@
3524
$ npm i @libp2p/multistream-select
3625
```
3726

27+
### Browser `<script>` tag
28+
29+
Loading this module through a script tag will make it's exports available as `Libp2pMultistreamSelect` in the global namespace.
30+
31+
```html
32+
<script src="https://unpkg.com/@libp2p/multistream-select/dist/index.min.js"></script>
33+
```
34+
3835
## Background
3936

4037
### What is `multistream-select`?
4138

4239
TLDR; multistream-select is protocol multiplexing per connection/stream. [Full spec here](https://github.com/multiformats/multistream-select)
4340

44-
#### Select a protocol flow
41+
### Select a protocol flow
4542

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

48-
```console
45+
```
4946
< /multistream-select/0.3.0 # i speak multistream-select/0.3.0
5047
> /multistream-select/0.3.0 # ok, let's speak multistream-select/0.3.0
5148
> /ipfs-dht/0.2.3 # i want to speak ipfs-dht/0.2.3
@@ -57,142 +54,9 @@ The caller will send "interactive" messages, expecting for some acknowledgement
5754
> <dht-message>
5855
```
5956

60-
## Usage
61-
62-
```js
63-
import { select, handle } from '@libp2p/multistream-select'
64-
// You can now use
65-
// select - actively select a protocol with a remote
66-
// handle - handle a protocol with a remote
67-
```
68-
69-
### Dialer
70-
71-
```js
72-
import { pipe } from 'it-pipe'
73-
import * as mss from '@libp2p/multistream-select'
74-
import { Mplex } from '@libp2p/mplex'
75-
76-
const muxer = new Mplex()
77-
const muxedStream = muxer.newStream()
78-
79-
// mss.select(protocol(s))
80-
// Select from one of the passed protocols (in priority order)
81-
// Returns selected stream and protocol
82-
const { stream: dhtStream, protocol } = await mss.select(muxedStream, [
83-
// This might just be different versions of DHT, but could be different impls
84-
'/ipfs-dht/2.0.0', // Most of the time this will probably just be one item.
85-
'/ipfs-dht/1.0.0'
86-
])
87-
88-
// Typically this stream will be passed back to the caller of libp2p.dialProtocol
89-
//
90-
// ...it might then do something like this:
91-
// try {
92-
// await pipe(
93-
// [uint8ArrayFromString('Some DHT data')]
94-
// dhtStream,
95-
// async source => {
96-
// for await (const chunk of source)
97-
// // DHT response data
98-
// }
99-
// )
100-
// } catch (err) {
101-
// // Error in stream
102-
// }
103-
```
104-
105-
### Listener
106-
107-
```js
108-
import { pipe } from 'it-pipe'
109-
import * as mss from '@libp2p/multistream-select'
110-
import { Mplex } from '@libp2p/mplex'
111-
112-
const muxer = new Mplex({
113-
async onStream (muxedStream) {
114-
// mss.handle(handledProtocols)
115-
// Returns selected stream and protocol
116-
const { stream, protocol } = await mss.handle(muxedStream, [
117-
'/ipfs-dht/1.0.0',
118-
'/ipfs-bitswap/1.0.0'
119-
])
120-
121-
// 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 } = await dialer.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
17258

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.
186-
187-
#### Examples
188-
189-
```js
190-
const { stream, protocol } = await mss.handle(duplex, [
191-
'/ipfs-dht/1.0.0',
192-
'/ipfs-bitswap/1.0.0'
193-
])
194-
// Remote wants to speak `protocol`
195-
```
59+
- <https://libp2p.github.io/js-libp2p-multistream-select>
19660

19761
## License
19862

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"types": "./dist/src/index.d.ts",
2727
"files": [
2828
"src",
29-
"dist/src",
29+
"dist",
3030
"!dist/test",
3131
"!**/*.tsbuildinfo"
3232
],
@@ -139,7 +139,8 @@
139139
"test:firefox-webworker": "aegir test -t webworker -- --browser firefox",
140140
"test:node": "aegir test -t node --cov",
141141
"test:electron-main": "aegir test -t electron-main",
142-
"release": "aegir release"
142+
"release": "aegir release",
143+
"docs": "aegir docs"
143144
},
144145
"dependencies": {
145146
"@libp2p/interfaces": "^3.0.2",

src/handle.ts

+46
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,52 @@ import type { ByteArrayInit, ByteListInit, MultistreamSelectInit, ProtocolStream
99

1010
const log = logger('libp2p:mss:handle')
1111

12+
/**
13+
* 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'
26+
* import { Mplex } from '@libp2p/mplex'
27+
*
28+
* const muxer = new Mplex({
29+
* async onStream (muxedStream) {
30+
* // mss.handle(handledProtocols)
31+
* // Returns selected stream and protocol
32+
* const { stream, protocol } = await mss.handle(muxedStream, [
33+
* '/ipfs-dht/1.0.0',
34+
* '/ipfs-bitswap/1.0.0'
35+
* ])
36+
*
37+
* // Typically here we'd call the handler function that was registered in
38+
* // libp2p for the given protocol:
39+
* // e.g. handlers[protocol].handler(stream)
40+
* //
41+
* // If protocol was /ipfs-dht/1.0.0 it might do something like this:
42+
* // try {
43+
* // await pipe(
44+
* // dhtStream,
45+
* // source => (async function * () {
46+
* // for await (const chunk of source)
47+
* // // Incoming DHT data -> process and yield to respond
48+
* // })(),
49+
* // dhtStream
50+
* // )
51+
* // } catch (err) {
52+
* // // Error in stream
53+
* // }
54+
* }
55+
* })
56+
* ```
57+
*/
1258
export async function handle (stream: Duplex<Uint8Array>, protocols: string | string[], options: ByteArrayInit): Promise<ProtocolStream<Uint8Array>>
1359
export async function handle (stream: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>, protocols: string | string[], options?: ByteListInit): Promise<ProtocolStream<Uint8ArrayList, Uint8ArrayList | Uint8Array>>
1460
export async function handle (stream: Duplex<any>, protocols: string | string[], options?: MultistreamSelectInit): Promise<ProtocolStream<any>> {

src/select.ts

+43
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,49 @@ import type { ByteArrayInit, ByteListInit, MultistreamSelectInit, ProtocolStream
1313

1414
const log = logger('libp2p:mss:select')
1515

16+
/**
17+
* 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)
35+
* // Returns selected stream and protocol
36+
* const { stream: dhtStream, protocol } = await mss.select(muxedStream, [
37+
* // This might just be different versions of DHT, but could be different impls
38+
* '/ipfs-dht/2.0.0', // Most of the time this will probably just be one item.
39+
* '/ipfs-dht/1.0.0'
40+
* ])
41+
*
42+
* // Typically this stream will be passed back to the caller of libp2p.dialProtocol
43+
* //
44+
* // ...it might then do something like this:
45+
* // try {
46+
* // await pipe(
47+
* // [uint8ArrayFromString('Some DHT data')]
48+
* // dhtStream,
49+
* // async source => {
50+
* // for await (const chunk of source)
51+
* // // DHT response data
52+
* // }
53+
* // )
54+
* // } catch (err) {
55+
* // // Error in stream
56+
* // }
57+
* ```
58+
*/
1659
export async function select (stream: Duplex<Uint8Array>, protocols: string | string[], options: ByteArrayInit): Promise<ProtocolStream<Uint8Array>>
1760
export async function select (stream: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>, protocols: string | string[], options?: ByteListInit): Promise<ProtocolStream<Uint8ArrayList, Uint8ArrayList | Uint8Array>>
1861
export async function select (stream: Duplex<any>, protocols: string | string[], options: MultistreamSelectInit = {}): Promise<ProtocolStream<any>> {

0 commit comments

Comments
 (0)