@libp2p/multistream-select 1.0.6 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -49
- package/dist/src/index.d.ts +0 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +0 -8
- package/dist/src/index.js.map +1 -1
- package/dist/src/multistream.js +3 -3
- package/dist/src/multistream.js.map +1 -1
- package/package.json +23 -17
- package/src/index.ts +0 -9
- package/src/multistream.ts +3 -3
- package/dist/src/ls.d.ts +0 -7
- package/dist/src/ls.d.ts.map +0 -1
- package/dist/src/ls.js +0 -32
- package/dist/src/ls.js.map +0 -1
- package/src/ls.ts +0 -45
package/README.md
CHANGED
|
@@ -1,30 +1,24 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @libp2p/multistream-select <!-- omit in toc -->
|
|
2
2
|
|
|
3
|
-
[](https://david-dm.org/multiformats/js-multistream-select)
|
|
9
|
-
[](https://github.com/feross/standard)
|
|
3
|
+
[](http://libp2p.io/)
|
|
4
|
+
[](http://webchat.freenode.net/?channels=%23libp2p)
|
|
5
|
+
[](https://discuss.libp2p.io)
|
|
6
|
+
[](https://codecov.io/gh/libp2p/js-libp2p-multistream-select)
|
|
7
|
+
[](https://github.com/libp2p/js-libp2p-multistream-select/actions/workflows/js-test-and-release.yml)
|
|
10
8
|
|
|
11
|
-
> JavaScript implementation of
|
|
9
|
+
> JavaScript implementation of multistream-select
|
|
12
10
|
|
|
13
|
-
##
|
|
14
|
-
|
|
15
|
-
[Jacob Heun](https://github.com/jacobheun)
|
|
16
|
-
|
|
17
|
-
## Table of Contents <!-- omit in toc -->
|
|
11
|
+
## Table of contents <!-- omit in toc -->
|
|
18
12
|
|
|
13
|
+
- [Install](#install)
|
|
19
14
|
- [Background](#background)
|
|
20
15
|
- [What is `multistream-select`?](#what-is-multistream-select)
|
|
21
16
|
- [Select a protocol flow](#select-a-protocol-flow)
|
|
22
|
-
- [Install](#install)
|
|
23
17
|
- [Usage](#usage)
|
|
24
18
|
- [Dialer](#dialer)
|
|
25
19
|
- [Listener](#listener)
|
|
26
20
|
- [API](#api)
|
|
27
|
-
- [`new
|
|
21
|
+
- [`new Dialer(duplex)`](#new-dialerduplex)
|
|
28
22
|
- [Parameters](#parameters)
|
|
29
23
|
- [Returns](#returns)
|
|
30
24
|
- [Examples](#examples)
|
|
@@ -36,7 +30,7 @@
|
|
|
36
30
|
- [Parameters](#parameters-2)
|
|
37
31
|
- [Returns](#returns-2)
|
|
38
32
|
- [Examples](#examples-2)
|
|
39
|
-
- [`new
|
|
33
|
+
- [`new Listener(duplex)`](#new-listenerduplex)
|
|
40
34
|
- [Parameters](#parameters-3)
|
|
41
35
|
- [Returns](#returns-3)
|
|
42
36
|
- [Examples](#examples-3)
|
|
@@ -44,8 +38,14 @@
|
|
|
44
38
|
- [Parameters](#parameters-4)
|
|
45
39
|
- [Returns](#returns-4)
|
|
46
40
|
- [Examples](#examples-4)
|
|
47
|
-
- [Contribute](#contribute)
|
|
48
41
|
- [License](#license)
|
|
42
|
+
- [Contribution](#contribution)
|
|
43
|
+
|
|
44
|
+
## Install
|
|
45
|
+
|
|
46
|
+
```console
|
|
47
|
+
$ npm i @libp2p/multistream-select
|
|
48
|
+
```
|
|
49
49
|
|
|
50
50
|
## Background
|
|
51
51
|
|
|
@@ -71,32 +71,26 @@ The caller will send "interactive" messages, expecting for some acknowledgement
|
|
|
71
71
|
|
|
72
72
|
This mode also packs a `ls` option, so that the callee can list the protocols it currently supports
|
|
73
73
|
|
|
74
|
-
## Install
|
|
75
|
-
|
|
76
|
-
```sh
|
|
77
|
-
npm i multistream-select
|
|
78
|
-
```
|
|
79
|
-
|
|
80
74
|
## Usage
|
|
81
75
|
|
|
82
76
|
```js
|
|
83
|
-
|
|
77
|
+
import { Dialer, Listener } from '@libp2p/multistream-select'
|
|
84
78
|
// You can now use
|
|
85
|
-
//
|
|
86
|
-
//
|
|
79
|
+
// Dialer - actively select a protocol with a remote
|
|
80
|
+
// Listener - handle a protocol with a remote
|
|
87
81
|
```
|
|
88
82
|
|
|
89
83
|
### Dialer
|
|
90
84
|
|
|
91
85
|
```js
|
|
92
86
|
import { pipe } from 'it-pipe'
|
|
93
|
-
|
|
94
|
-
|
|
87
|
+
import { Dialer } from '@libp2p/multistream-select'
|
|
88
|
+
import { Mplex } from '@libp2p/mplex'
|
|
95
89
|
|
|
96
90
|
const muxer = new Mplex()
|
|
97
91
|
const muxedStream = muxer.newStream()
|
|
98
92
|
|
|
99
|
-
const mss = new
|
|
93
|
+
const mss = new Dialer(muxedStream)
|
|
100
94
|
|
|
101
95
|
// mss.select(protocol(s))
|
|
102
96
|
// Select from one of the passed protocols (in priority order)
|
|
@@ -128,12 +122,12 @@ const { stream: dhtStream, protocol } = await mss.select([
|
|
|
128
122
|
|
|
129
123
|
```js
|
|
130
124
|
import { pipe } from 'it-pipe'
|
|
131
|
-
|
|
132
|
-
|
|
125
|
+
import { Listener } from '@libp2p/multistream-select'
|
|
126
|
+
import { Mplex } from '@libp2p/mplex'
|
|
133
127
|
|
|
134
128
|
const muxer = new Mplex({
|
|
135
129
|
async onStream (muxedStream) {
|
|
136
|
-
const mss = new
|
|
130
|
+
const mss = new Listener(muxedStream)
|
|
137
131
|
|
|
138
132
|
// mss.handle(handledProtocols)
|
|
139
133
|
// Returns selected stream and protocol
|
|
@@ -165,13 +159,13 @@ const muxer = new Mplex({
|
|
|
165
159
|
|
|
166
160
|
## API
|
|
167
161
|
|
|
168
|
-
### `new
|
|
162
|
+
### `new Dialer(duplex)`
|
|
169
163
|
|
|
170
164
|
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.
|
|
171
165
|
|
|
172
166
|
#### Parameters
|
|
173
167
|
|
|
174
|
-
|
|
168
|
+
- `duplex` (`Object`) - A [duplex iterable stream](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#duplex-it) to dial on.
|
|
175
169
|
|
|
176
170
|
#### Returns
|
|
177
171
|
|
|
@@ -189,8 +183,8 @@ Negotiate a protocol to use from a list of protocols.
|
|
|
189
183
|
|
|
190
184
|
#### Parameters
|
|
191
185
|
|
|
192
|
-
|
|
193
|
-
|
|
186
|
+
- `protocols` (`String[]`/`String`) - A list of protocols (or single protocol) to negotiate with. Protocols are attempted in order until a match is made.
|
|
187
|
+
- `options` (`{ signal: AbortSignal }`) - an options object containing an AbortSignal
|
|
194
188
|
|
|
195
189
|
#### Returns
|
|
196
190
|
|
|
@@ -215,7 +209,7 @@ List protocols that the remote supports.
|
|
|
215
209
|
|
|
216
210
|
#### Parameters
|
|
217
211
|
|
|
218
|
-
|
|
212
|
+
- `options` (`{ signal: AbortSignal }`) - an options object containing an AbortSignal
|
|
219
213
|
|
|
220
214
|
#### Returns
|
|
221
215
|
|
|
@@ -234,13 +228,13 @@ if (!protocols.includes(wantedProto)) {
|
|
|
234
228
|
// Now use dialer.select to use wantedProto, safe in the knowledge it is supported
|
|
235
229
|
```
|
|
236
230
|
|
|
237
|
-
### `new
|
|
231
|
+
### `new Listener(duplex)`
|
|
238
232
|
|
|
239
233
|
Construct a new multistream select "listener" instance which can be used to handle multistream protocol selections for particular protocols.
|
|
240
234
|
|
|
241
235
|
#### Parameters
|
|
242
236
|
|
|
243
|
-
|
|
237
|
+
- `duplex` (`Object`) - A [duplex iterable stream](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#duplex-it) to listen on.
|
|
244
238
|
|
|
245
239
|
#### Returns
|
|
246
240
|
|
|
@@ -258,8 +252,8 @@ Handle multistream protocol selections for the given list of protocols.
|
|
|
258
252
|
|
|
259
253
|
#### Parameters
|
|
260
254
|
|
|
261
|
-
|
|
262
|
-
|
|
255
|
+
- `protocols` (`String[]`/`String`) - A list of protocols (or single protocol) that this listener is able to speak.
|
|
256
|
+
- `options` (`{ signal: AbortSignal }`) - an options object containing an AbortSignal
|
|
263
257
|
|
|
264
258
|
#### Returns
|
|
265
259
|
|
|
@@ -277,14 +271,13 @@ const { stream, protocol } = await listener.handle([
|
|
|
277
271
|
// Remote wants to speak `protocol`
|
|
278
272
|
```
|
|
279
273
|
|
|
280
|
-
##
|
|
281
|
-
|
|
282
|
-
Contributions welcome. Please check out [the issues](https://github.com/multiformats/js-multistream-select/issues).
|
|
274
|
+
## License
|
|
283
275
|
|
|
284
|
-
|
|
276
|
+
Licensed under either of
|
|
285
277
|
|
|
286
|
-
|
|
278
|
+
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
|
|
279
|
+
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
|
|
287
280
|
|
|
288
|
-
##
|
|
281
|
+
## Contribution
|
|
289
282
|
|
|
290
|
-
|
|
283
|
+
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
|
package/dist/src/index.d.ts
CHANGED
|
@@ -19,7 +19,6 @@ declare class MultistreamSelect {
|
|
|
19
19
|
}
|
|
20
20
|
export declare class Dialer extends MultistreamSelect {
|
|
21
21
|
select(protocols: string | string[], options?: AbortOptions): Promise<ProtocolStream>;
|
|
22
|
-
ls(options?: AbortOptions): Promise<string[]>;
|
|
23
22
|
}
|
|
24
23
|
export declare class Listener extends MultistreamSelect {
|
|
25
24
|
handle(protocols: string | string[], options?: AbortOptions): Promise<ProtocolStream>;
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAEtD,OAAO,EAAE,WAAW,EAAE,CAAA;AAEtB,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC,CAAA;IAC1B,QAAQ,EAAE,MAAM,CAAA;CACjB;AAED,cAAM,iBAAiB;IACrB,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC,CAAA;IACpC,SAAS,CAAC,MAAM,EAAE,OAAO,CAAA;gBAEZ,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC;IAKvC;;;;OAIG;IACG,UAAU,CAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC;CASzD;AAED,qBAAa,MAAO,SAAQ,iBAAiB;IACrC,MAAM,CAAE,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,cAAc,CAAC;CAG7F;AAED,qBAAa,QAAS,SAAQ,iBAAiB;IACvC,MAAM,CAAE,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,cAAc,CAAC;CAG7F"}
|
package/dist/src/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { select } from './select.js';
|
|
2
2
|
import { handle } from './handle.js';
|
|
3
|
-
import { ls } from './ls.js';
|
|
4
3
|
import { PROTOCOL_ID } from './constants.js';
|
|
5
4
|
export { PROTOCOL_ID };
|
|
6
5
|
class MultistreamSelect {
|
|
@@ -26,13 +25,6 @@ export class Dialer extends MultistreamSelect {
|
|
|
26
25
|
async select(protocols, options) {
|
|
27
26
|
return await select(this.stream, protocols, this.shaken ? undefined : PROTOCOL_ID, options);
|
|
28
27
|
}
|
|
29
|
-
async ls(options) {
|
|
30
|
-
await this._handshake(options);
|
|
31
|
-
const res = await ls(this.stream, options);
|
|
32
|
-
const { stream, protocols } = res;
|
|
33
|
-
this.stream = stream;
|
|
34
|
-
return protocols;
|
|
35
|
-
}
|
|
36
28
|
}
|
|
37
29
|
export class Listener extends MultistreamSelect {
|
|
38
30
|
async handle(protocols, options) {
|
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAI5C,OAAO,EAAE,WAAW,EAAE,CAAA;AAOtB,MAAM,iBAAiB;IAIrB,YAAa,MAA0B;QACrC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAA;IACrB,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,UAAU,CAAE,OAAsB;QACtC,IAAI,IAAI,CAAC,MAAM,EAAE;YACf,OAAM;SACP;QAED,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,OAAO,CAAC,CAAA;QAC7E,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAA;IACpB,CAAC;CACF;AAED,MAAM,OAAO,MAAO,SAAQ,iBAAiB;IAC3C,KAAK,CAAC,MAAM,CAAE,SAA4B,EAAE,OAAsB;QAChE,OAAO,MAAM,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,SAAS,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,EAAE,OAAO,CAAC,CAAA;IAC7F,CAAC;CACF;AAED,MAAM,OAAO,QAAS,SAAQ,iBAAiB;IAC7C,KAAK,CAAC,MAAM,CAAE,SAA4B,EAAE,OAAsB;QAChE,OAAO,MAAM,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,CAAC,CAAA;IACtD,CAAC;CACF"}
|
package/dist/src/multistream.js
CHANGED
|
@@ -9,13 +9,13 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
|
|
|
9
9
|
const NewLine = uint8ArrayFromString('\n');
|
|
10
10
|
export function encode(buffer) {
|
|
11
11
|
const list = new Uint8ArrayList(buffer, NewLine);
|
|
12
|
-
return lp.encode.single(list).
|
|
12
|
+
return lp.encode.single(list).subarray();
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
15
|
* `write` encodes and writes a single buffer
|
|
16
16
|
*/
|
|
17
17
|
export function write(writer, buffer) {
|
|
18
|
-
writer.push(encode(buffer).
|
|
18
|
+
writer.push(encode(buffer).subarray());
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
21
21
|
* `writeAll` behaves like `write`, except it encodes an array of items as a single write
|
|
@@ -45,7 +45,7 @@ export async function read(reader, options) {
|
|
|
45
45
|
if (buf == null) {
|
|
46
46
|
throw errCode(new Error('no buffer returned'), 'ERR_INVALID_MULTISTREAM_SELECT_MESSAGE');
|
|
47
47
|
}
|
|
48
|
-
if (buf
|
|
48
|
+
if (buf.get(buf.byteLength - 1) !== NewLine[0]) {
|
|
49
49
|
throw errCode(new Error('missing newline'), 'ERR_INVALID_MULTISTREAM_SELECT_MESSAGE');
|
|
50
50
|
}
|
|
51
51
|
return buf.slice(0, -1); // Remove newline
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"multistream.js","sourceRoot":"","sources":["../../src/multistream.ts"],"names":[],"mappings":"AACA,OAAO,E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MAAM,IAAI,CACpB,KAAK,EACL,EAAE,CAAC,MAAM,CAAC,EAAE,QAAQ,EAAE,CAAC,EACvB,KAAK,EAAE,MAAM,EAAE,EAAE,CAAC,MAAM,KAAK,CAAC,MAAM,CAAC,CACtC,CAAA;IAED,IAAI,GAAG,IAAI,IAAI,EAAE;QACf,MAAM,OAAO,CAAC,IAAI,KAAK,CAAC,oBAAoB,CAAC,EAAE,wCAAwC,CAAC,CAAA;KACzF;IAED,IAAI,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,UAAU,GAAG,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,EAAE;QAC9C,MAAM,OAAO,CAAC,IAAI,KAAK,CAAC,iBAAiB,CAAC,EAAE,wCAAwC,CAAC,CAAA;KACtF;IAED,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA,CAAC,iBAAiB;AAC3C,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,UAAU,CAAE,MAAc,EAAE,OAAsB;IACtE,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAEvC,OAAO,kBAAkB,CAAC,GAAG,CAAC,CAAA;AAChC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/multistream-select",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "JavaScript implementation of multistream-select",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
|
-
"homepage": "https://github.com/libp2p/js-libp2p-
|
|
6
|
+
"homepage": "https://github.com/libp2p/js-libp2p-multistream-select#readme",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/libp2p/js-libp2p-
|
|
9
|
+
"url": "git+https://github.com/libp2p/js-libp2p-multistream-select.git"
|
|
10
10
|
},
|
|
11
11
|
"bugs": {
|
|
12
|
-
"url": "https://github.com/libp2p/js-libp2p-
|
|
12
|
+
"url": "https://github.com/libp2p/js-libp2p-multistream-select/issues"
|
|
13
13
|
},
|
|
14
14
|
"keywords": [
|
|
15
15
|
"ipfs",
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
],
|
|
33
33
|
"exports": {
|
|
34
34
|
".": {
|
|
35
|
+
"types": "./dist/src/index.d.ts",
|
|
35
36
|
"import": "./dist/src/index.js"
|
|
36
37
|
}
|
|
37
38
|
},
|
|
@@ -68,15 +69,15 @@
|
|
|
68
69
|
"release": "patch"
|
|
69
70
|
},
|
|
70
71
|
{
|
|
71
|
-
"type": "
|
|
72
|
+
"type": "docs",
|
|
72
73
|
"release": "patch"
|
|
73
74
|
},
|
|
74
75
|
{
|
|
75
|
-
"type": "
|
|
76
|
+
"type": "test",
|
|
76
77
|
"release": "patch"
|
|
77
78
|
},
|
|
78
79
|
{
|
|
79
|
-
"type": "
|
|
80
|
+
"type": "deps",
|
|
80
81
|
"release": "patch"
|
|
81
82
|
},
|
|
82
83
|
{
|
|
@@ -106,7 +107,11 @@
|
|
|
106
107
|
},
|
|
107
108
|
{
|
|
108
109
|
"type": "docs",
|
|
109
|
-
"section": "
|
|
110
|
+
"section": "Documentation"
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
"type": "deps",
|
|
114
|
+
"section": "Dependencies"
|
|
110
115
|
},
|
|
111
116
|
{
|
|
112
117
|
"type": "test",
|
|
@@ -137,28 +142,29 @@
|
|
|
137
142
|
"release": "aegir release"
|
|
138
143
|
},
|
|
139
144
|
"dependencies": {
|
|
140
|
-
"@libp2p/interfaces": "^
|
|
141
|
-
"@libp2p/logger": "^
|
|
145
|
+
"@libp2p/interfaces": "^3.0.2",
|
|
146
|
+
"@libp2p/logger": "^2.0.0",
|
|
142
147
|
"abortable-iterator": "^4.0.2",
|
|
143
148
|
"err-code": "^3.0.1",
|
|
144
149
|
"it-first": "^1.0.6",
|
|
145
|
-
"it-handshake": "^
|
|
146
|
-
"it-length-prefixed": "^
|
|
150
|
+
"it-handshake": "^4.0.1",
|
|
151
|
+
"it-length-prefixed": "^8.0.2",
|
|
147
152
|
"it-pipe": "^2.0.3",
|
|
148
|
-
"it-pushable": "^
|
|
149
|
-
"it-reader": "^
|
|
153
|
+
"it-pushable": "^3.0.0",
|
|
154
|
+
"it-reader": "^6.0.1",
|
|
150
155
|
"it-stream-types": "^1.0.4",
|
|
151
156
|
"p-defer": "^4.0.0",
|
|
152
|
-
"uint8arraylist": "^
|
|
157
|
+
"uint8arraylist": "^2.0.0",
|
|
153
158
|
"uint8arrays": "^3.0.0"
|
|
154
159
|
},
|
|
155
160
|
"devDependencies": {
|
|
156
|
-
"
|
|
161
|
+
"@types/varint": "^6.0.0",
|
|
162
|
+
"aegir": "^37.2.0",
|
|
157
163
|
"iso-random-stream": "^2.0.2",
|
|
158
164
|
"it-all": "^1.0.6",
|
|
159
165
|
"it-map": "^1.0.6",
|
|
160
166
|
"it-pair": "^2.0.2",
|
|
161
|
-
"p-timeout": "^
|
|
167
|
+
"p-timeout": "^6.0.0",
|
|
162
168
|
"timeout-abort-controller": "^3.0.0",
|
|
163
169
|
"util": "^0.12.4",
|
|
164
170
|
"varint": "^6.0.0"
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { select } from './select.js'
|
|
2
2
|
import { handle } from './handle.js'
|
|
3
|
-
import { ls } from './ls.js'
|
|
4
3
|
import { PROTOCOL_ID } from './constants.js'
|
|
5
4
|
import type { Duplex } from 'it-stream-types'
|
|
6
5
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
@@ -41,14 +40,6 @@ export class Dialer extends MultistreamSelect {
|
|
|
41
40
|
async select (protocols: string | string[], options?: AbortOptions): Promise<ProtocolStream> {
|
|
42
41
|
return await select(this.stream, protocols, this.shaken ? undefined : PROTOCOL_ID, options)
|
|
43
42
|
}
|
|
44
|
-
|
|
45
|
-
async ls (options?: AbortOptions): Promise<string[]> {
|
|
46
|
-
await this._handshake(options)
|
|
47
|
-
const res = await ls(this.stream, options)
|
|
48
|
-
const { stream, protocols } = res
|
|
49
|
-
this.stream = stream
|
|
50
|
-
return protocols
|
|
51
|
-
}
|
|
52
43
|
}
|
|
53
44
|
|
|
54
45
|
export class Listener extends MultistreamSelect {
|
package/src/multistream.ts
CHANGED
|
@@ -17,14 +17,14 @@ const NewLine = uint8ArrayFromString('\n')
|
|
|
17
17
|
export function encode (buffer: Uint8Array | Uint8ArrayList): Uint8Array {
|
|
18
18
|
const list = new Uint8ArrayList(buffer, NewLine)
|
|
19
19
|
|
|
20
|
-
return lp.encode.single(list).
|
|
20
|
+
return lp.encode.single(list).subarray()
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* `write` encodes and writes a single buffer
|
|
25
25
|
*/
|
|
26
26
|
export function write (writer: Pushable<Uint8Array>, buffer: Uint8Array | Uint8ArrayList) {
|
|
27
|
-
writer.push(encode(buffer).
|
|
27
|
+
writer.push(encode(buffer).subarray())
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
/**
|
|
@@ -68,7 +68,7 @@ export async function read (reader: Reader, options?: AbortOptions) {
|
|
|
68
68
|
throw errCode(new Error('no buffer returned'), 'ERR_INVALID_MULTISTREAM_SELECT_MESSAGE')
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
if (buf
|
|
71
|
+
if (buf.get(buf.byteLength - 1) !== NewLine[0]) {
|
|
72
72
|
throw errCode(new Error('missing newline'), 'ERR_INVALID_MULTISTREAM_SELECT_MESSAGE')
|
|
73
73
|
}
|
|
74
74
|
|
package/dist/src/ls.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { Duplex } from 'it-stream-types';
|
|
2
|
-
import type { AbortOptions } from '@libp2p/interfaces';
|
|
3
|
-
export declare function ls(stream: Duplex<Uint8Array>, options?: AbortOptions): Promise<{
|
|
4
|
-
stream: Duplex<Uint8Array>;
|
|
5
|
-
protocols: string[];
|
|
6
|
-
}>;
|
|
7
|
-
//# sourceMappingURL=ls.d.ts.map
|
package/dist/src/ls.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ls.d.ts","sourceRoot":"","sources":["../../src/ls.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAItD,wBAAsB,EAAE,CAAE,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC;IAAE,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC;IAAC,SAAS,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC,CA+B1I"}
|
package/dist/src/ls.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { reader as createReader } from 'it-reader';
|
|
2
|
-
import { logger } from '@libp2p/logger';
|
|
3
|
-
import * as multistream from './multistream.js';
|
|
4
|
-
import { handshake } from 'it-handshake';
|
|
5
|
-
import * as lp from 'it-length-prefixed';
|
|
6
|
-
import { pipe } from 'it-pipe';
|
|
7
|
-
import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
|
|
8
|
-
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
|
9
|
-
const log = logger('libp2p:mss:ls');
|
|
10
|
-
export async function ls(stream, options) {
|
|
11
|
-
const { reader, writer, rest, stream: shakeStream } = handshake(stream);
|
|
12
|
-
log('write "ls"');
|
|
13
|
-
multistream.write(writer, uint8ArrayFromString('ls'));
|
|
14
|
-
rest();
|
|
15
|
-
// Next message from remote will be (e.g. for 2 protocols):
|
|
16
|
-
// <varint-msg-len><varint-proto-name-len><proto-name>\n<varint-proto-name-len><proto-name>\n
|
|
17
|
-
const res = await multistream.read(reader, options);
|
|
18
|
-
// After reading response we have:
|
|
19
|
-
// <varint-proto-name-len><proto-name>\n<varint-proto-name-len><proto-name>\n
|
|
20
|
-
const protocolsReader = createReader([res]);
|
|
21
|
-
const protocols = [];
|
|
22
|
-
// Decode each of the protocols from the reader
|
|
23
|
-
await pipe(protocolsReader, lp.decode(), async (source) => {
|
|
24
|
-
for await (const protocol of source) {
|
|
25
|
-
// Remove the newline
|
|
26
|
-
protocols.push(uint8ArrayToString(protocol.slice(0, -1)));
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
const output = { stream: shakeStream, protocols };
|
|
30
|
-
return output;
|
|
31
|
-
}
|
|
32
|
-
//# sourceMappingURL=ls.js.map
|
package/dist/src/ls.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ls.js","sourceRoot":"","sources":["../../src/ls.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,YAAY,EAAE,MAAM,WAAW,CAAA;AAClD,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAA;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AACxC,OAAO,KAAK,EAAE,MAAM,oBAAoB,CAAA;AACxC,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AACtE,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAI5E,MAAM,GAAG,GAAG,MAAM,CAAC,eAAe,CAAC,CAAA;AAEnC,MAAM,CAAC,KAAK,UAAU,EAAE,CAAE,MAA0B,EAAE,OAAsB;IAC1E,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,SAAS,CAAC,MAAM,CAAC,CAAA;IAEvE,GAAG,CAAC,YAAY,CAAC,CAAA;IACjB,WAAW,CAAC,KAAK,CAAC,MAAM,EAAE,oBAAoB,CAAC,IAAI,CAAC,CAAC,CAAA;IACrD,IAAI,EAAE,CAAA;IAEN,2DAA2D;IAC3D,6FAA6F;IAC7F,MAAM,GAAG,GAAG,MAAM,WAAW,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAEnD,kCAAkC;IAClC,6EAA6E;IAC7E,MAAM,eAAe,GAAG,YAAY,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA;IAC3C,MAAM,SAAS,GAAa,EAAE,CAAA;IAE9B,+CAA+C;IAC/C,MAAM,IAAI,CACR,eAAe,EACf,EAAE,CAAC,MAAM,EAAE,EACX,KAAK,EAAE,MAAM,EAAE,EAAE;QACf,IAAI,KAAK,EAAE,MAAM,QAAQ,IAAI,MAAM,EAAE;YACnC,qBAAqB;YACrB,SAAS,CAAC,IAAI,CAAC,kBAAkB,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;SAC1D;IACH,CAAC,CACF,CAAA;IAED,MAAM,MAAM,GAAG,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,CAAA;IAEjD,OAAO,MAAM,CAAA;AACf,CAAC"}
|
package/src/ls.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { reader as createReader } from 'it-reader'
|
|
2
|
-
import { logger } from '@libp2p/logger'
|
|
3
|
-
import * as multistream from './multistream.js'
|
|
4
|
-
import { handshake } from 'it-handshake'
|
|
5
|
-
import * as lp from 'it-length-prefixed'
|
|
6
|
-
import { pipe } from 'it-pipe'
|
|
7
|
-
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
8
|
-
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
9
|
-
import type { Duplex } from 'it-stream-types'
|
|
10
|
-
import type { AbortOptions } from '@libp2p/interfaces'
|
|
11
|
-
|
|
12
|
-
const log = logger('libp2p:mss:ls')
|
|
13
|
-
|
|
14
|
-
export async function ls (stream: Duplex<Uint8Array>, options?: AbortOptions): Promise<{ stream: Duplex<Uint8Array>, protocols: string[] }> {
|
|
15
|
-
const { reader, writer, rest, stream: shakeStream } = handshake(stream)
|
|
16
|
-
|
|
17
|
-
log('write "ls"')
|
|
18
|
-
multistream.write(writer, uint8ArrayFromString('ls'))
|
|
19
|
-
rest()
|
|
20
|
-
|
|
21
|
-
// Next message from remote will be (e.g. for 2 protocols):
|
|
22
|
-
// <varint-msg-len><varint-proto-name-len><proto-name>\n<varint-proto-name-len><proto-name>\n
|
|
23
|
-
const res = await multistream.read(reader, options)
|
|
24
|
-
|
|
25
|
-
// After reading response we have:
|
|
26
|
-
// <varint-proto-name-len><proto-name>\n<varint-proto-name-len><proto-name>\n
|
|
27
|
-
const protocolsReader = createReader([res])
|
|
28
|
-
const protocols: string[] = []
|
|
29
|
-
|
|
30
|
-
// Decode each of the protocols from the reader
|
|
31
|
-
await pipe(
|
|
32
|
-
protocolsReader,
|
|
33
|
-
lp.decode(),
|
|
34
|
-
async (source) => {
|
|
35
|
-
for await (const protocol of source) {
|
|
36
|
-
// Remove the newline
|
|
37
|
-
protocols.push(uint8ArrayToString(protocol.slice(0, -1)))
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
)
|
|
41
|
-
|
|
42
|
-
const output = { stream: shakeStream, protocols }
|
|
43
|
-
|
|
44
|
-
return output
|
|
45
|
-
}
|