@libp2p/multistream-select 4.0.6-0f5c305af → 4.0.6-3dee5df4d
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 +1 -1
- package/dist/index.min.js +12 -2
- package/dist/src/handle.d.ts +3 -5
- package/dist/src/handle.d.ts.map +1 -1
- package/dist/src/handle.js +65 -18
- package/dist/src/handle.js.map +1 -1
- package/dist/src/index.d.ts +4 -12
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/multistream.d.ts +14 -9
- package/dist/src/multistream.d.ts.map +1 -1
- package/dist/src/multistream.js +14 -52
- package/dist/src/multistream.js.map +1 -1
- package/dist/src/select.d.ts +4 -7
- package/dist/src/select.d.ts.map +1 -1
- package/dist/src/select.js +107 -69
- package/dist/src/select.js.map +1 -1
- package/package.json +8 -14
- package/src/handle.ts +27 -24
- package/src/index.ts +4 -13
- package/src/multistream.ts +19 -71
- package/src/select.ts +69 -74
package/dist/src/select.js
CHANGED
|
@@ -1,95 +1,133 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface/errors';
|
|
2
|
-
import {
|
|
3
|
-
import { handshake } from 'it-handshake';
|
|
4
|
-
import merge from 'it-merge';
|
|
5
|
-
import { pushable } from 'it-pushable';
|
|
6
|
-
import { reader } from 'it-reader';
|
|
7
|
-
import { Uint8ArrayList } from 'uint8arraylist';
|
|
2
|
+
import { lpStream } from 'it-length-prefixed-stream';
|
|
8
3
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
|
4
|
+
import { MAX_PROTOCOL_LENGTH } from './constants.js';
|
|
9
5
|
import * as multistream from './multistream.js';
|
|
10
6
|
import { PROTOCOL_ID } from './index.js';
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Negotiate a protocol to use from a list of protocols.
|
|
9
|
+
*
|
|
10
|
+
* @param stream - A duplex iterable stream to dial on
|
|
11
|
+
* @param protocols - A list of protocols (or single protocol) to negotiate with. Protocols are attempted in order until a match is made.
|
|
12
|
+
* @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
|
|
13
|
+
* @returns A stream for the selected protocol and the protocol that was selected from the list of protocols provided to `select`.
|
|
14
|
+
* @example
|
|
15
|
+
*
|
|
16
|
+
* ```js
|
|
17
|
+
* import { pipe } from 'it-pipe'
|
|
18
|
+
* import * as mss from '@libp2p/multistream-select'
|
|
19
|
+
* import { Mplex } from '@libp2p/mplex'
|
|
20
|
+
*
|
|
21
|
+
* const muxer = new Mplex()
|
|
22
|
+
* const muxedStream = muxer.newStream()
|
|
23
|
+
*
|
|
24
|
+
* // mss.select(protocol(s))
|
|
25
|
+
* // Select from one of the passed protocols (in priority order)
|
|
26
|
+
* // Returns selected stream and protocol
|
|
27
|
+
* const { stream: dhtStream, protocol } = await mss.select(muxedStream, [
|
|
28
|
+
* // This might just be different versions of DHT, but could be different impls
|
|
29
|
+
* '/ipfs-dht/2.0.0', // Most of the time this will probably just be one item.
|
|
30
|
+
* '/ipfs-dht/1.0.0'
|
|
31
|
+
* ])
|
|
32
|
+
*
|
|
33
|
+
* // Typically this stream will be passed back to the caller of libp2p.dialProtocol
|
|
34
|
+
* //
|
|
35
|
+
* // ...it might then do something like this:
|
|
36
|
+
* // try {
|
|
37
|
+
* // await pipe(
|
|
38
|
+
* // [uint8ArrayFromString('Some DHT data')]
|
|
39
|
+
* // dhtStream,
|
|
40
|
+
* // async source => {
|
|
41
|
+
* // for await (const chunk of source)
|
|
42
|
+
* // // DHT response data
|
|
43
|
+
* // }
|
|
44
|
+
* // )
|
|
45
|
+
* // } catch (err) {
|
|
46
|
+
* // // Error in stream
|
|
47
|
+
* // }
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export async function select(stream, protocols, options) {
|
|
13
51
|
protocols = Array.isArray(protocols) ? [...protocols] : [protocols];
|
|
14
|
-
const
|
|
52
|
+
const lp = lpStream(stream, {
|
|
53
|
+
maxDataLength: MAX_PROTOCOL_LENGTH
|
|
54
|
+
});
|
|
15
55
|
const protocol = protocols.shift();
|
|
16
56
|
if (protocol == null) {
|
|
17
57
|
throw new Error('At least one protocol must be specified');
|
|
18
58
|
}
|
|
19
|
-
log.trace('select: write ["%s", "%s"]', PROTOCOL_ID, protocol);
|
|
20
|
-
const p1 = uint8ArrayFromString(PROTOCOL_ID);
|
|
21
|
-
const p2 = uint8ArrayFromString(protocol);
|
|
22
|
-
multistream.writeAll(
|
|
23
|
-
|
|
24
|
-
|
|
59
|
+
options?.log.trace('select: write ["%s", "%s"]', PROTOCOL_ID, protocol);
|
|
60
|
+
const p1 = uint8ArrayFromString(`${PROTOCOL_ID}\n`);
|
|
61
|
+
const p2 = uint8ArrayFromString(`${protocol}\n`);
|
|
62
|
+
await multistream.writeAll(lp, [p1, p2], options);
|
|
63
|
+
options?.log.trace('select: reading multistream-select header');
|
|
64
|
+
let response = await multistream.readString(lp, options);
|
|
65
|
+
options?.log.trace('select: read "%s"', response);
|
|
25
66
|
// Read the protocol response if we got the protocolId in return
|
|
26
67
|
if (response === PROTOCOL_ID) {
|
|
27
|
-
|
|
28
|
-
|
|
68
|
+
options?.log.trace('select: reading protocol response');
|
|
69
|
+
response = await multistream.readString(lp, options);
|
|
70
|
+
options?.log.trace('select: read "%s"', response);
|
|
29
71
|
}
|
|
30
72
|
// We're done
|
|
31
73
|
if (response === protocol) {
|
|
32
|
-
|
|
33
|
-
return { stream: shakeStream, protocol };
|
|
74
|
+
return { stream: lp.unwrap(), protocol };
|
|
34
75
|
}
|
|
35
76
|
// We haven't gotten a valid ack, try the other protocols
|
|
36
77
|
for (const protocol of protocols) {
|
|
37
|
-
log.trace('select: write "%s"', protocol);
|
|
38
|
-
multistream.write(
|
|
39
|
-
|
|
40
|
-
|
|
78
|
+
options?.log.trace('select: write "%s"', protocol);
|
|
79
|
+
await multistream.write(lp, uint8ArrayFromString(`${protocol}\n`), options);
|
|
80
|
+
options?.log.trace('select: reading protocol response');
|
|
81
|
+
const response = await multistream.readString(lp, options);
|
|
82
|
+
options?.log.trace('select: read "%s" for "%s"', response, protocol);
|
|
41
83
|
if (response === protocol) {
|
|
42
|
-
|
|
43
|
-
return { stream: shakeStream, protocol };
|
|
84
|
+
return { stream: lp.unwrap(), protocol };
|
|
44
85
|
}
|
|
45
86
|
}
|
|
46
|
-
rest();
|
|
47
87
|
throw new CodeError('protocol selection failed', 'ERR_UNSUPPORTED_PROTOCOL');
|
|
48
88
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
89
|
+
/**
|
|
90
|
+
* Lazily negotiates a protocol.
|
|
91
|
+
*
|
|
92
|
+
* It *does not* block writes waiting for the other end to respond. Instead, it
|
|
93
|
+
* simply assumes the negotiation went successfully and starts writing data.
|
|
94
|
+
*
|
|
95
|
+
* Use when it is known that the receiver supports the desired protocol.
|
|
96
|
+
*/
|
|
97
|
+
export function lazySelect(stream, protocol, options) {
|
|
98
|
+
const originalSink = stream.sink.bind(stream);
|
|
99
|
+
const originalSource = stream.source;
|
|
100
|
+
const lp = lpStream({
|
|
101
|
+
sink: originalSink,
|
|
102
|
+
source: originalSource
|
|
103
|
+
}, {
|
|
104
|
+
maxDataLength: MAX_PROTOCOL_LENGTH
|
|
105
|
+
});
|
|
106
|
+
stream.sink = async (source) => {
|
|
107
|
+
options?.log.trace('lazy: write ["%s", "%s"]', PROTOCOL_ID, protocol);
|
|
108
|
+
await lp.writeV([
|
|
109
|
+
uint8ArrayFromString(`${PROTOCOL_ID}\n`),
|
|
110
|
+
uint8ArrayFromString(`${protocol}\n`)
|
|
111
|
+
]);
|
|
112
|
+
options?.log.trace('lazy: writing rest of "%s" stream', protocol);
|
|
113
|
+
await lp.unwrap().sink(source);
|
|
114
|
+
};
|
|
115
|
+
stream.source = (async function* () {
|
|
116
|
+
options?.log.trace('lazy: reading multistream select header');
|
|
117
|
+
let response = await multistream.readString(lp, options);
|
|
118
|
+
options?.log.trace('lazy: read multistream select header "%s"', response);
|
|
119
|
+
if (response === PROTOCOL_ID) {
|
|
120
|
+
response = await multistream.readString(lp, options);
|
|
121
|
+
}
|
|
122
|
+
options?.log.trace('lazy: read protocol "%s", expecting "%s"', response, protocol);
|
|
123
|
+
if (response !== protocol) {
|
|
124
|
+
throw new CodeError('protocol selection failed', 'ERR_UNSUPPORTED_PROTOCOL');
|
|
125
|
+
}
|
|
126
|
+
options?.log.trace('lazy: reading rest of "%s" stream', protocol);
|
|
127
|
+
yield* lp.unwrap().source;
|
|
128
|
+
})();
|
|
54
129
|
return {
|
|
55
|
-
stream
|
|
56
|
-
sink: async (source) => {
|
|
57
|
-
await stream.sink((async function* () {
|
|
58
|
-
let first = true;
|
|
59
|
-
for await (const chunk of merge(source, negotiateTrigger)) {
|
|
60
|
-
if (first) {
|
|
61
|
-
first = false;
|
|
62
|
-
negotiated = true;
|
|
63
|
-
negotiateTrigger.end();
|
|
64
|
-
const p1 = uint8ArrayFromString(PROTOCOL_ID);
|
|
65
|
-
const p2 = uint8ArrayFromString(protocol);
|
|
66
|
-
const list = new Uint8ArrayList(multistream.encode(p1), multistream.encode(p2));
|
|
67
|
-
if (chunk.length > 0)
|
|
68
|
-
list.append(chunk);
|
|
69
|
-
yield* list;
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
yield chunk;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
})());
|
|
76
|
-
},
|
|
77
|
-
source: (async function* () {
|
|
78
|
-
if (!negotiated)
|
|
79
|
-
negotiateTrigger.push(new Uint8Array());
|
|
80
|
-
const byteReader = reader(stream.source);
|
|
81
|
-
let response = await multistream.readString(byteReader);
|
|
82
|
-
if (response === PROTOCOL_ID) {
|
|
83
|
-
response = await multistream.readString(byteReader);
|
|
84
|
-
}
|
|
85
|
-
if (response !== protocol) {
|
|
86
|
-
throw new CodeError('protocol selection failed', 'ERR_UNSUPPORTED_PROTOCOL');
|
|
87
|
-
}
|
|
88
|
-
for await (const chunk of byteReader) {
|
|
89
|
-
yield* chunk;
|
|
90
|
-
}
|
|
91
|
-
})()
|
|
92
|
-
},
|
|
130
|
+
stream,
|
|
93
131
|
protocol
|
|
94
132
|
};
|
|
95
133
|
}
|
package/dist/src/select.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"select.js","sourceRoot":"","sources":["../../src/select.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"select.js","sourceRoot":"","sources":["../../src/select.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAA;AACpD,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAA;AACpD,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAA;AAC/C,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAIxC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM,CAAyC,MAAc,EAAE,SAA4B,EAAE,OAA8B;IAC/I,SAAS,GAAG,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAA;IACnE,MAAM,EAAE,GAAG,QAAQ,CAAC,MAAM,EAAE;QAC1B,aAAa,EAAE,mBAAmB;KACnC,CAAC,CAAA;IACF,MAAM,QAAQ,GAAG,SAAS,CAAC,KAAK,EAAE,CAAA;IAElC,IAAI,QAAQ,IAAI,IAAI,EAAE;QACpB,MA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C,IAAI,CAAC,MAAM,CAAC,CAAA;IAChC,CAAC,CAAA;IAED,MAAM,CAAC,MAAM,GAAG,CAAC,KAAK,SAAU,CAAC;QAC/B,OAAO,EAAE,GAAG,CAAC,KAAK,CAAC,yCAAyC,CAAC,CAAA;QAE7D,IAAI,QAAQ,GAAG,MAAM,WAAW,CAAC,UAAU,CAAC,EAAE,EAAE,OAAO,CAAC,CAAA;QACxD,OAAO,EAAE,GAAG,CAAC,KAAK,CAAC,2CAA2C,EAAE,QAAQ,CAAC,CAAA;QAEzE,IAAI,QAAQ,KAAK,WAAW,EAAE;YAC5B,QAAQ,GAAG,MAAM,WAAW,CAAC,UAAU,CAAC,EAAE,EAAE,OAAO,CAAC,CAAA;SACrD;QAED,OAAO,EAAE,GAAG,CAAC,KAAK,CAAC,0CAA0C,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAA;QAElF,IAAI,QAAQ,KAAK,QAAQ,EAAE;YACzB,MAAM,IAAI,SAAS,CAAC,2BAA2B,EAAE,0BAA0B,CAAC,CAAA;SAC7E;QAED,OAAO,EAAE,GAAG,CAAC,KAAK,CAAC,mCAAmC,EAAE,QAAQ,CAAC,CAAA;QACjE,KAAM,CAAC,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,MAAM,CAAA;IAC5B,CAAC,CAAC,EAAE,CAAA;IAEJ,OAAO;QACL,MAAM;QACN,QAAQ;KACT,CAAA;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/multistream-select",
|
|
3
|
-
"version": "4.0.6-
|
|
3
|
+
"version": "4.0.6-3dee5df4d",
|
|
4
4
|
"description": "JavaScript implementation of multistream-select",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
|
-
"homepage": "https://github.com/libp2p/js-libp2p/tree/
|
|
6
|
+
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/multistream-select#readme",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "git+https://github.com/libp2p/js-libp2p.git"
|
|
@@ -53,26 +53,20 @@
|
|
|
53
53
|
"test:electron-main": "aegir test -t electron-main"
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@libp2p/interface": "0.1.6-
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"it-first": "^3.0.1",
|
|
60
|
-
"it-handshake": "^4.1.3",
|
|
61
|
-
"it-length-prefixed": "^9.0.1",
|
|
62
|
-
"it-merge": "^3.0.0",
|
|
56
|
+
"@libp2p/interface": "0.1.6-3dee5df4d",
|
|
57
|
+
"it-length-prefixed": "^9.0.3",
|
|
58
|
+
"it-length-prefixed-stream": "^1.1.1",
|
|
63
59
|
"it-pipe": "^3.0.1",
|
|
64
|
-
"it-pushable": "^3.2.0",
|
|
65
|
-
"it-reader": "^6.0.1",
|
|
66
60
|
"it-stream-types": "^2.0.1",
|
|
67
|
-
"uint8-varint": "^2.0.0",
|
|
68
61
|
"uint8arraylist": "^2.4.3",
|
|
69
62
|
"uint8arrays": "^4.0.6"
|
|
70
63
|
},
|
|
71
64
|
"devDependencies": {
|
|
65
|
+
"@libp2p/logger": "3.1.0-3dee5df4d",
|
|
72
66
|
"aegir": "^41.0.2",
|
|
73
67
|
"iso-random-stream": "^2.0.2",
|
|
74
|
-
"it-all": "^3.0.
|
|
75
|
-
"it-
|
|
68
|
+
"it-all": "^3.0.3",
|
|
69
|
+
"it-drain": "^3.0.5",
|
|
76
70
|
"it-pair": "^2.0.6",
|
|
77
71
|
"p-timeout": "^6.0.0"
|
|
78
72
|
}
|
package/src/handle.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { encode } from 'it-length-prefixed'
|
|
2
|
+
import { lpStream } from 'it-length-prefixed-stream'
|
|
3
3
|
import { Uint8ArrayList } from 'uint8arraylist'
|
|
4
4
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
5
|
-
import { PROTOCOL_ID } from './constants.js'
|
|
5
|
+
import { MAX_PROTOCOL_LENGTH, PROTOCOL_ID } from './constants.js'
|
|
6
6
|
import * as multistream from './multistream.js'
|
|
7
|
-
import type {
|
|
8
|
-
import type { Duplex
|
|
9
|
-
|
|
10
|
-
const log = logger('libp2p:mss:handle')
|
|
7
|
+
import type { MultistreamSelectInit, ProtocolStream } from './index.js'
|
|
8
|
+
import type { Duplex } from 'it-stream-types'
|
|
11
9
|
|
|
12
10
|
/**
|
|
13
11
|
* Handle multistream protocol selections for the given list of protocols.
|
|
@@ -55,38 +53,43 @@ const log = logger('libp2p:mss:handle')
|
|
|
55
53
|
* })
|
|
56
54
|
* ```
|
|
57
55
|
*/
|
|
58
|
-
export async function handle (stream:
|
|
59
|
-
export async function handle (stream: Duplex<Source<Uint8ArrayList | Uint8Array>, Source<Uint8ArrayList | Uint8Array>>, protocols: string | string[], options?: ByteListInit): Promise<ProtocolStream<Uint8ArrayList, Uint8ArrayList | Uint8Array>>
|
|
60
|
-
export async function handle (stream: any, protocols: string | string[], options?: MultistreamSelectInit): Promise<ProtocolStream<any>> {
|
|
56
|
+
export async function handle <Stream extends Duplex<any, any, any>> (stream: Stream, protocols: string | string[], options: MultistreamSelectInit): Promise<ProtocolStream<Stream>> {
|
|
61
57
|
protocols = Array.isArray(protocols) ? protocols : [protocols]
|
|
62
|
-
const
|
|
58
|
+
const lp = lpStream(stream, {
|
|
59
|
+
maxDataLength: MAX_PROTOCOL_LENGTH
|
|
60
|
+
})
|
|
63
61
|
|
|
64
62
|
while (true) {
|
|
65
|
-
|
|
66
|
-
|
|
63
|
+
options.log.trace('handle - available protocols %s', protocols)
|
|
64
|
+
const protocol = await multistream.readString(lp, options)
|
|
65
|
+
options.log.trace('read "%s"', protocol)
|
|
67
66
|
|
|
68
67
|
if (protocol === PROTOCOL_ID) {
|
|
69
|
-
log.trace('respond with "%s" for "%s"', PROTOCOL_ID, protocol)
|
|
70
|
-
multistream.write(
|
|
68
|
+
options.log.trace('respond with "%s" for "%s"', PROTOCOL_ID, protocol)
|
|
69
|
+
await multistream.write(lp, uint8ArrayFromString(`${PROTOCOL_ID}\n`), options)
|
|
71
70
|
continue
|
|
72
71
|
}
|
|
73
72
|
|
|
74
73
|
if (protocols.includes(protocol)) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return { stream:
|
|
74
|
+
options.log.trace('respond with "%s" for "%s"', protocol, protocol)
|
|
75
|
+
await multistream.write(lp, uint8ArrayFromString(`${protocol}\n`), options)
|
|
76
|
+
|
|
77
|
+
return { stream: lp.unwrap(), protocol }
|
|
79
78
|
}
|
|
80
79
|
|
|
81
80
|
if (protocol === 'ls') {
|
|
82
81
|
// <varint-msg-len><varint-proto-name-len><proto-name>\n<varint-proto-name-len><proto-name>\n\n
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
82
|
+
const protos = new Uint8ArrayList(
|
|
83
|
+
...protocols.map(p => encode.single(uint8ArrayFromString(`${p}\n`))),
|
|
84
|
+
uint8ArrayFromString('\n')
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
await multistream.write(lp, protos, options)
|
|
88
|
+
options.log.trace('respond with "%s" for %s', protocols, protocol)
|
|
86
89
|
continue
|
|
87
90
|
}
|
|
88
91
|
|
|
89
|
-
|
|
90
|
-
|
|
92
|
+
options.log('respond with "na" for "%s"', protocol)
|
|
93
|
+
await multistream.write(lp, uint8ArrayFromString('na\n'), options)
|
|
91
94
|
}
|
|
92
95
|
}
|
package/src/index.ts
CHANGED
|
@@ -21,26 +21,17 @@
|
|
|
21
21
|
*/
|
|
22
22
|
|
|
23
23
|
import { PROTOCOL_ID } from './constants.js'
|
|
24
|
-
import type { AbortOptions } from '@libp2p/interface'
|
|
25
|
-
import type { Duplex, Source } from 'it-stream-types'
|
|
24
|
+
import type { AbortOptions, LoggerOptions } from '@libp2p/interface'
|
|
26
25
|
|
|
27
26
|
export { PROTOCOL_ID }
|
|
28
27
|
|
|
29
|
-
export interface ProtocolStream<
|
|
30
|
-
stream:
|
|
28
|
+
export interface ProtocolStream<Stream> {
|
|
29
|
+
stream: Stream
|
|
31
30
|
protocol: string
|
|
32
31
|
}
|
|
33
32
|
|
|
34
|
-
export interface
|
|
35
|
-
writeBytes: true
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface ByteListInit extends AbortOptions {
|
|
39
|
-
writeBytes?: false
|
|
40
|
-
}
|
|
33
|
+
export interface MultistreamSelectInit extends AbortOptions, LoggerOptions {
|
|
41
34
|
|
|
42
|
-
export interface MultistreamSelectInit extends AbortOptions {
|
|
43
|
-
writeBytes?: boolean
|
|
44
35
|
}
|
|
45
36
|
|
|
46
37
|
export { select, lazySelect } from './select.js'
|
package/src/multistream.ts
CHANGED
|
@@ -1,98 +1,46 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface/errors'
|
|
2
|
-
import {
|
|
3
|
-
import { abortableSource } from 'abortable-iterator'
|
|
4
|
-
import first from 'it-first'
|
|
5
|
-
import * as lp from 'it-length-prefixed'
|
|
6
|
-
import { pipe } from 'it-pipe'
|
|
7
|
-
import { Uint8ArrayList } from 'uint8arraylist'
|
|
2
|
+
import { type Uint8ArrayList } from 'uint8arraylist'
|
|
8
3
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
9
4
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
10
|
-
import { MAX_PROTOCOL_LENGTH } from './constants.js'
|
|
11
5
|
import type { MultistreamSelectInit } from '.'
|
|
12
|
-
import type { AbortOptions } from '@libp2p/interface'
|
|
13
|
-
import type {
|
|
14
|
-
import type {
|
|
15
|
-
import type { Source } from 'it-stream-types'
|
|
16
|
-
|
|
17
|
-
const log = logger('libp2p:mss')
|
|
6
|
+
import type { AbortOptions, LoggerOptions } from '@libp2p/interface'
|
|
7
|
+
import type { LengthPrefixedStream } from 'it-length-prefixed-stream'
|
|
8
|
+
import type { Duplex, Source } from 'it-stream-types'
|
|
18
9
|
|
|
19
10
|
const NewLine = uint8ArrayFromString('\n')
|
|
20
11
|
|
|
21
|
-
export function encode (buffer: Uint8Array | Uint8ArrayList): Uint8ArrayList {
|
|
22
|
-
const list = new Uint8ArrayList(buffer, NewLine)
|
|
23
|
-
|
|
24
|
-
return lp.encode.single(list)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
12
|
/**
|
|
28
13
|
* `write` encodes and writes a single buffer
|
|
29
14
|
*/
|
|
30
|
-
export function write (writer:
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (options.writeBytes === true) {
|
|
34
|
-
writer.push(encoded.subarray())
|
|
35
|
-
} else {
|
|
36
|
-
writer.push(encoded)
|
|
37
|
-
}
|
|
15
|
+
export async function write (writer: LengthPrefixedStream<Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>>>, buffer: Uint8Array | Uint8ArrayList, options?: MultistreamSelectInit): Promise<void> {
|
|
16
|
+
await writer.write(buffer, options)
|
|
38
17
|
}
|
|
39
18
|
|
|
40
19
|
/**
|
|
41
20
|
* `writeAll` behaves like `write`, except it encodes an array of items as a single write
|
|
42
21
|
*/
|
|
43
|
-
export function writeAll (writer:
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
for (const buf of buffers) {
|
|
47
|
-
list.append(encode(buf))
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (options.writeBytes === true) {
|
|
51
|
-
writer.push(list.subarray())
|
|
52
|
-
} else {
|
|
53
|
-
writer.push(list)
|
|
54
|
-
}
|
|
22
|
+
export async function writeAll (writer: LengthPrefixedStream<Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>>>, buffers: Uint8Array[], options?: MultistreamSelectInit): Promise<void> {
|
|
23
|
+
await writer.writeV(buffers, options)
|
|
55
24
|
}
|
|
56
25
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
let input: Source<Uint8ArrayList> = varByteSource
|
|
65
|
-
|
|
66
|
-
// If we have been passed an abort signal, wrap the input source in an abortable
|
|
67
|
-
// iterator that will throw if the operation is aborted
|
|
68
|
-
if (options?.signal != null) {
|
|
69
|
-
input = abortableSource(varByteSource, options.signal)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// Once the length has been parsed, read chunk for that length
|
|
73
|
-
const onLength = (l: number): void => {
|
|
74
|
-
byteLength = l
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const buf = await pipe(
|
|
78
|
-
input,
|
|
79
|
-
(source) => lp.decode(source, { onLength, maxDataLength: MAX_PROTOCOL_LENGTH }),
|
|
80
|
-
async (source) => first(source)
|
|
81
|
-
)
|
|
82
|
-
|
|
83
|
-
if (buf == null || buf.length === 0) {
|
|
84
|
-
throw new CodeError('no buffer returned', 'ERR_INVALID_MULTISTREAM_SELECT_MESSAGE')
|
|
85
|
-
}
|
|
26
|
+
/**
|
|
27
|
+
* Read a length-prefixed buffer from the passed stream, stripping the final newline character
|
|
28
|
+
*/
|
|
29
|
+
export async function read (reader: LengthPrefixedStream<Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>, Source<Uint8Array>>>, options?: AbortOptions & LoggerOptions): Promise<Uint8ArrayList> {
|
|
30
|
+
const buf = await reader.read(options)
|
|
86
31
|
|
|
87
|
-
if (buf.get(buf.byteLength - 1) !== NewLine[0]) {
|
|
88
|
-
log.error('Invalid mss message - missing newline
|
|
32
|
+
if (buf.byteLength === 0 || buf.get(buf.byteLength - 1) !== NewLine[0]) {
|
|
33
|
+
options?.log.error('Invalid mss message - missing newline', buf)
|
|
89
34
|
throw new CodeError('missing newline', 'ERR_INVALID_MULTISTREAM_SELECT_MESSAGE')
|
|
90
35
|
}
|
|
91
36
|
|
|
92
37
|
return buf.sublist(0, -1) // Remove newline
|
|
93
38
|
}
|
|
94
39
|
|
|
95
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Read a length-prefixed string from the passed stream, stripping the final newline character
|
|
42
|
+
*/
|
|
43
|
+
export async function readString (reader: LengthPrefixedStream<Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>, Source<Uint8Array>>>, options?: AbortOptions & LoggerOptions): Promise<string> {
|
|
96
44
|
const buf = await read(reader, options)
|
|
97
45
|
|
|
98
46
|
return uint8ArrayToString(buf.subarray())
|