@libp2p/multistream-select 4.0.6 → 4.0.7
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 +14 -2
- package/dist/src/handle.d.ts +3 -5
- package/dist/src/handle.d.ts.map +1 -1
- package/dist/src/handle.js +72 -18
- package/dist/src/handle.js.map +1 -1
- package/dist/src/index.d.ts +6 -13
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +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 +15 -53
- package/dist/src/multistream.js.map +1 -1
- package/dist/src/select.d.ts +10 -15
- package/dist/src/select.d.ts.map +1 -1
- package/dist/src/select.js +249 -66
- package/dist/src/select.js.map +1 -1
- package/dist/typedoc-urls.json +0 -5
- package/package.json +14 -14
- package/src/handle.ts +35 -24
- package/src/index.ts +6 -14
- package/src/multistream.ts +20 -72
- package/src/select.ts +251 -71
package/dist/src/select.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type {
|
|
3
|
-
import type { Duplex
|
|
1
|
+
import type { MultistreamSelectInit, ProtocolStream } from './index.js';
|
|
2
|
+
import type { AbortOptions } from '@libp2p/interface';
|
|
3
|
+
import type { Duplex } from 'it-stream-types';
|
|
4
|
+
export interface SelectStream extends Duplex<any, any, any> {
|
|
5
|
+
readStatus?: string;
|
|
6
|
+
closeWrite?(options?: AbortOptions): Promise<void>;
|
|
7
|
+
closeRead?(options?: AbortOptions): Promise<void>;
|
|
8
|
+
close?(options?: AbortOptions): Promise<void>;
|
|
9
|
+
}
|
|
4
10
|
/**
|
|
5
11
|
* Negotiate a protocol to use from a list of protocols.
|
|
6
12
|
*
|
|
@@ -44,16 +50,5 @@ import type { Duplex, Source } from 'it-stream-types';
|
|
|
44
50
|
* // }
|
|
45
51
|
* ```
|
|
46
52
|
*/
|
|
47
|
-
export declare function select(stream:
|
|
48
|
-
export declare function select(stream: Duplex<AsyncGenerator<Uint8ArrayList | Uint8Array>, Source<Uint8ArrayList | Uint8Array>>, protocols: string | string[], options?: ByteListInit): Promise<ProtocolStream<Uint8ArrayList, Uint8ArrayList | Uint8Array>>;
|
|
49
|
-
/**
|
|
50
|
-
* Lazily negotiates a protocol.
|
|
51
|
-
*
|
|
52
|
-
* It *does not* block writes waiting for the other end to respond. Instead, it
|
|
53
|
-
* simply assumes the negotiation went successfully and starts writing data.
|
|
54
|
-
*
|
|
55
|
-
* Use when it is known that the receiver supports the desired protocol.
|
|
56
|
-
*/
|
|
57
|
-
export declare function lazySelect(stream: Duplex<Source<Uint8Array>, Source<Uint8Array>>, protocol: string): ProtocolStream<Uint8Array>;
|
|
58
|
-
export declare function lazySelect(stream: Duplex<Source<Uint8ArrayList | Uint8Array>, Source<Uint8ArrayList | Uint8Array>>, protocol: string): ProtocolStream<Uint8ArrayList, Uint8ArrayList | Uint8Array>;
|
|
53
|
+
export declare function select<Stream extends SelectStream>(stream: Stream, protocols: string | string[], options: MultistreamSelectInit): Promise<ProtocolStream<Stream>>;
|
|
59
54
|
//# sourceMappingURL=select.d.ts.map
|
package/dist/src/select.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"select.d.ts","sourceRoot":"","sources":["../../src/select.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"select.d.ts","sourceRoot":"","sources":["../../src/select.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AACvE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACrD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAE7C,MAAM,WAAW,YAAa,SAAQ,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC;IACzD,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,UAAU,CAAC,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAClD,SAAS,CAAC,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IACjD,KAAK,CAAC,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CAC9C;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,wBAAsB,MAAM,CAAE,MAAM,SAAS,YAAY,EAAG,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,CAoDzK"}
|
package/dist/src/select.js
CHANGED
|
@@ -1,95 +1,278 @@
|
|
|
1
|
-
import { CodeError } from '@libp2p/interface
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import { pushable } from 'it-pushable';
|
|
6
|
-
import { reader } from 'it-reader';
|
|
1
|
+
import { CodeError } from '@libp2p/interface';
|
|
2
|
+
import { lpStream } from 'it-length-prefixed-stream';
|
|
3
|
+
import pDefer from 'p-defer';
|
|
4
|
+
import * as varint from 'uint8-varint';
|
|
7
5
|
import { Uint8ArrayList } from 'uint8arraylist';
|
|
8
6
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
|
7
|
+
import { MAX_PROTOCOL_LENGTH } from './constants.js';
|
|
9
8
|
import * as multistream from './multistream.js';
|
|
10
9
|
import { PROTOCOL_ID } from './index.js';
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Negotiate a protocol to use from a list of protocols.
|
|
12
|
+
*
|
|
13
|
+
* @param stream - A duplex iterable stream to dial on
|
|
14
|
+
* @param protocols - A list of protocols (or single protocol) to negotiate with. Protocols are attempted in order until a match is made.
|
|
15
|
+
* @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
|
|
16
|
+
* @returns A stream for the selected protocol and the protocol that was selected from the list of protocols provided to `select`.
|
|
17
|
+
* @example
|
|
18
|
+
*
|
|
19
|
+
* ```js
|
|
20
|
+
* import { pipe } from 'it-pipe'
|
|
21
|
+
* import * as mss from '@libp2p/multistream-select'
|
|
22
|
+
* import { Mplex } from '@libp2p/mplex'
|
|
23
|
+
*
|
|
24
|
+
* const muxer = new Mplex()
|
|
25
|
+
* const muxedStream = muxer.newStream()
|
|
26
|
+
*
|
|
27
|
+
* // mss.select(protocol(s))
|
|
28
|
+
* // Select from one of the passed protocols (in priority order)
|
|
29
|
+
* // Returns selected stream and protocol
|
|
30
|
+
* const { stream: dhtStream, protocol } = await mss.select(muxedStream, [
|
|
31
|
+
* // This might just be different versions of DHT, but could be different impls
|
|
32
|
+
* '/ipfs-dht/2.0.0', // Most of the time this will probably just be one item.
|
|
33
|
+
* '/ipfs-dht/1.0.0'
|
|
34
|
+
* ])
|
|
35
|
+
*
|
|
36
|
+
* // Typically this stream will be passed back to the caller of libp2p.dialProtocol
|
|
37
|
+
* //
|
|
38
|
+
* // ...it might then do something like this:
|
|
39
|
+
* // try {
|
|
40
|
+
* // await pipe(
|
|
41
|
+
* // [uint8ArrayFromString('Some DHT data')]
|
|
42
|
+
* // dhtStream,
|
|
43
|
+
* // async source => {
|
|
44
|
+
* // for await (const chunk of source)
|
|
45
|
+
* // // DHT response data
|
|
46
|
+
* // }
|
|
47
|
+
* // )
|
|
48
|
+
* // } catch (err) {
|
|
49
|
+
* // // Error in stream
|
|
50
|
+
* // }
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export async function select(stream, protocols, options) {
|
|
13
54
|
protocols = Array.isArray(protocols) ? [...protocols] : [protocols];
|
|
14
|
-
|
|
55
|
+
if (protocols.length === 1) {
|
|
56
|
+
return optimisticSelect(stream, protocols[0], options);
|
|
57
|
+
}
|
|
58
|
+
const lp = lpStream(stream, {
|
|
59
|
+
...options,
|
|
60
|
+
maxDataLength: MAX_PROTOCOL_LENGTH
|
|
61
|
+
});
|
|
15
62
|
const protocol = protocols.shift();
|
|
16
63
|
if (protocol == null) {
|
|
17
64
|
throw new Error('At least one protocol must be specified');
|
|
18
65
|
}
|
|
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
|
-
|
|
66
|
+
options?.log.trace('select: write ["%s", "%s"]', PROTOCOL_ID, protocol);
|
|
67
|
+
const p1 = uint8ArrayFromString(`${PROTOCOL_ID}\n`);
|
|
68
|
+
const p2 = uint8ArrayFromString(`${protocol}\n`);
|
|
69
|
+
await multistream.writeAll(lp, [p1, p2], options);
|
|
70
|
+
options?.log.trace('select: reading multistream-select header');
|
|
71
|
+
let response = await multistream.readString(lp, options);
|
|
72
|
+
options?.log.trace('select: read "%s"', response);
|
|
25
73
|
// Read the protocol response if we got the protocolId in return
|
|
26
74
|
if (response === PROTOCOL_ID) {
|
|
27
|
-
|
|
28
|
-
|
|
75
|
+
options?.log.trace('select: reading protocol response');
|
|
76
|
+
response = await multistream.readString(lp, options);
|
|
77
|
+
options?.log.trace('select: read "%s"', response);
|
|
29
78
|
}
|
|
30
79
|
// We're done
|
|
31
80
|
if (response === protocol) {
|
|
32
|
-
|
|
33
|
-
return { stream: shakeStream, protocol };
|
|
81
|
+
return { stream: lp.unwrap(), protocol };
|
|
34
82
|
}
|
|
35
83
|
// We haven't gotten a valid ack, try the other protocols
|
|
36
84
|
for (const protocol of protocols) {
|
|
37
|
-
log.trace('select: write "%s"', protocol);
|
|
38
|
-
multistream.write(
|
|
39
|
-
|
|
40
|
-
|
|
85
|
+
options?.log.trace('select: write "%s"', protocol);
|
|
86
|
+
await multistream.write(lp, uint8ArrayFromString(`${protocol}\n`), options);
|
|
87
|
+
options?.log.trace('select: reading protocol response');
|
|
88
|
+
const response = await multistream.readString(lp, options);
|
|
89
|
+
options?.log.trace('select: read "%s" for "%s"', response, protocol);
|
|
41
90
|
if (response === protocol) {
|
|
42
|
-
|
|
43
|
-
return { stream: shakeStream, protocol };
|
|
91
|
+
return { stream: lp.unwrap(), protocol };
|
|
44
92
|
}
|
|
45
93
|
}
|
|
46
|
-
rest();
|
|
47
94
|
throw new CodeError('protocol selection failed', 'ERR_UNSUPPORTED_PROTOCOL');
|
|
48
95
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
96
|
+
/**
|
|
97
|
+
* Optimistically negotiates a protocol.
|
|
98
|
+
*
|
|
99
|
+
* It *does not* block writes waiting for the other end to respond. Instead, it
|
|
100
|
+
* simply assumes the negotiation went successfully and starts writing data.
|
|
101
|
+
*
|
|
102
|
+
* Use when it is known that the receiver supports the desired protocol.
|
|
103
|
+
*/
|
|
104
|
+
function optimisticSelect(stream, protocol, options) {
|
|
105
|
+
const originalSink = stream.sink.bind(stream);
|
|
106
|
+
const originalSource = stream.source;
|
|
53
107
|
let negotiated = false;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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);
|
|
108
|
+
let negotiating = false;
|
|
109
|
+
const doneNegotiating = pDefer();
|
|
110
|
+
let sentProtocol = false;
|
|
111
|
+
let sendingProtocol = false;
|
|
112
|
+
const doneSendingProtocol = pDefer();
|
|
113
|
+
let readProtocol = false;
|
|
114
|
+
let readingProtocol = false;
|
|
115
|
+
const doneReadingProtocol = pDefer();
|
|
116
|
+
const lp = lpStream({
|
|
117
|
+
sink: originalSink,
|
|
118
|
+
source: originalSource
|
|
119
|
+
}, {
|
|
120
|
+
...options,
|
|
121
|
+
maxDataLength: MAX_PROTOCOL_LENGTH
|
|
122
|
+
});
|
|
123
|
+
stream.sink = async (source) => {
|
|
124
|
+
const { sink } = lp.unwrap();
|
|
125
|
+
await sink(async function* () {
|
|
126
|
+
let sentData = false;
|
|
127
|
+
for await (const buf of source) {
|
|
128
|
+
// started reading before the source yielded, wait for protocol send
|
|
129
|
+
if (sendingProtocol) {
|
|
130
|
+
await doneSendingProtocol.promise;
|
|
84
131
|
}
|
|
85
|
-
|
|
86
|
-
|
|
132
|
+
// writing before reading, send the protocol and the first chunk of data
|
|
133
|
+
if (!sentProtocol) {
|
|
134
|
+
sendingProtocol = true;
|
|
135
|
+
options?.log.trace('optimistic: write ["%s", "%s", data(%d)] in sink', PROTOCOL_ID, protocol, buf.byteLength);
|
|
136
|
+
const protocolString = `${protocol}\n`;
|
|
137
|
+
// send protocols in first chunk of data written to transport
|
|
138
|
+
yield new Uint8ArrayList(Uint8Array.from([19]), // length of PROTOCOL_ID plus newline
|
|
139
|
+
uint8ArrayFromString(`${PROTOCOL_ID}\n`), varint.encode(protocolString.length), uint8ArrayFromString(protocolString), buf).subarray();
|
|
140
|
+
options?.log.trace('optimistic: wrote ["%s", "%s", data(%d)] in sink', PROTOCOL_ID, protocol, buf.byteLength);
|
|
141
|
+
sentProtocol = true;
|
|
142
|
+
sendingProtocol = false;
|
|
143
|
+
doneSendingProtocol.resolve();
|
|
87
144
|
}
|
|
88
|
-
|
|
89
|
-
yield
|
|
145
|
+
else {
|
|
146
|
+
yield buf;
|
|
90
147
|
}
|
|
91
|
-
|
|
92
|
-
|
|
148
|
+
sentData = true;
|
|
149
|
+
}
|
|
150
|
+
// special case - the source passed to the sink has ended but we didn't
|
|
151
|
+
// negotiated the protocol yet so do it now
|
|
152
|
+
if (!sentData) {
|
|
153
|
+
await negotiate();
|
|
154
|
+
}
|
|
155
|
+
}());
|
|
156
|
+
};
|
|
157
|
+
async function negotiate() {
|
|
158
|
+
if (negotiating) {
|
|
159
|
+
options?.log.trace('optimistic: already negotiating %s stream', protocol);
|
|
160
|
+
await doneNegotiating.promise;
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
negotiating = true;
|
|
164
|
+
try {
|
|
165
|
+
// we haven't sent the protocol yet, send it now
|
|
166
|
+
if (!sentProtocol) {
|
|
167
|
+
options?.log.trace('optimistic: doing send protocol for %s stream', protocol);
|
|
168
|
+
await doSendProtocol();
|
|
169
|
+
}
|
|
170
|
+
// if we haven't read the protocol response yet, do it now
|
|
171
|
+
if (!readProtocol) {
|
|
172
|
+
options?.log.trace('optimistic: doing read protocol for %s stream', protocol);
|
|
173
|
+
await doReadProtocol();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
finally {
|
|
177
|
+
negotiating = false;
|
|
178
|
+
negotiated = true;
|
|
179
|
+
doneNegotiating.resolve();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
async function doSendProtocol() {
|
|
183
|
+
if (sendingProtocol) {
|
|
184
|
+
await doneSendingProtocol.promise;
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
sendingProtocol = true;
|
|
188
|
+
try {
|
|
189
|
+
options?.log.trace('optimistic: write ["%s", "%s", data] in source', PROTOCOL_ID, protocol);
|
|
190
|
+
await lp.writeV([
|
|
191
|
+
uint8ArrayFromString(`${PROTOCOL_ID}\n`),
|
|
192
|
+
uint8ArrayFromString(`${protocol}\n`)
|
|
193
|
+
]);
|
|
194
|
+
options?.log.trace('optimistic: wrote ["%s", "%s", data] in source', PROTOCOL_ID, protocol);
|
|
195
|
+
}
|
|
196
|
+
finally {
|
|
197
|
+
sentProtocol = true;
|
|
198
|
+
sendingProtocol = false;
|
|
199
|
+
doneSendingProtocol.resolve();
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
async function doReadProtocol() {
|
|
203
|
+
if (readingProtocol) {
|
|
204
|
+
await doneReadingProtocol.promise;
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
readingProtocol = true;
|
|
208
|
+
try {
|
|
209
|
+
options?.log.trace('optimistic: reading multistream select header');
|
|
210
|
+
let response = await multistream.readString(lp, options);
|
|
211
|
+
options?.log.trace('optimistic: read multistream select header "%s"', response);
|
|
212
|
+
if (response === PROTOCOL_ID) {
|
|
213
|
+
response = await multistream.readString(lp, options);
|
|
214
|
+
}
|
|
215
|
+
options?.log.trace('optimistic: read protocol "%s", expecting "%s"', response, protocol);
|
|
216
|
+
if (response !== protocol) {
|
|
217
|
+
throw new CodeError('protocol selection failed', 'ERR_UNSUPPORTED_PROTOCOL');
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
finally {
|
|
221
|
+
readProtocol = true;
|
|
222
|
+
readingProtocol = false;
|
|
223
|
+
doneReadingProtocol.resolve();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
stream.source = (async function* () {
|
|
227
|
+
// make sure we've done protocol negotiation before we read stream data
|
|
228
|
+
await negotiate();
|
|
229
|
+
options?.log.trace('optimistic: reading data from "%s" stream', protocol);
|
|
230
|
+
yield* lp.unwrap().source;
|
|
231
|
+
})();
|
|
232
|
+
if (stream.closeRead != null) {
|
|
233
|
+
const originalCloseRead = stream.closeRead.bind(stream);
|
|
234
|
+
stream.closeRead = async (opts) => {
|
|
235
|
+
// we need to read & write to negotiate the protocol so ensure we've done
|
|
236
|
+
// this before closing the readable end of the stream
|
|
237
|
+
if (!negotiated) {
|
|
238
|
+
await negotiate().catch(err => {
|
|
239
|
+
options?.log.error('could not negotiate protocol before close read', err);
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
// protocol has been negotiated, ok to close the readable end
|
|
243
|
+
await originalCloseRead(opts);
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
if (stream.closeWrite != null) {
|
|
247
|
+
const originalCloseWrite = stream.closeWrite.bind(stream);
|
|
248
|
+
stream.closeWrite = async (opts) => {
|
|
249
|
+
// we need to read & write to negotiate the protocol so ensure we've done
|
|
250
|
+
// this before closing the writable end of the stream
|
|
251
|
+
if (!negotiated) {
|
|
252
|
+
await negotiate().catch(err => {
|
|
253
|
+
options?.log.error('could not negotiate protocol before close write', err);
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
// protocol has been negotiated, ok to close the writable end
|
|
257
|
+
await originalCloseWrite(opts);
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
if (stream.close != null) {
|
|
261
|
+
const originalClose = stream.close.bind(stream);
|
|
262
|
+
stream.close = async (opts) => {
|
|
263
|
+
// the stream is being closed, don't try to negotiate a protocol if we
|
|
264
|
+
// haven't already
|
|
265
|
+
if (!negotiated) {
|
|
266
|
+
negotiated = true;
|
|
267
|
+
negotiating = false;
|
|
268
|
+
doneNegotiating.resolve();
|
|
269
|
+
}
|
|
270
|
+
// protocol has been negotiated, ok to close the writable end
|
|
271
|
+
await originalClose(opts);
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
return {
|
|
275
|
+
stream,
|
|
93
276
|
protocol
|
|
94
277
|
};
|
|
95
278
|
}
|
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,
|
|
1
|
+
{"version":3,"file":"select.js","sourceRoot":"","sources":["../../src/select.ts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AAM,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QAE/C,MAAM,CAAC,KAAK,GAAG,KAAK,EAAE,IAAI,EAAE,EAAE;YAC5B,sEAAsE;YACtE,kBAAkB;YAClB,IAAI,CAAC,UAAU,EAAE,CAAC;gBAChB,UAAU,GAAG,IAAI,CAAA;gBACjB,WAAW,GAAG,KAAK,CAAA;gBACnB,eAAe,CAAC,OAAO,EAAE,CAAA;YAC3B,CAAC;YAED,6DAA6D;YAC7D,MAAM,aAAa,CAAC,IAAI,CAAC,CAAA;QAC3B,CAAC,CAAA;IACH,CAAC;IAED,OAAO;QACL,MAAM;QACN,QAAQ;KACT,CAAA;AACH,CAAC"}
|
package/dist/typedoc-urls.json
CHANGED
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
{
|
|
2
|
-
"ByteArrayInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_multistream_select.ByteArrayInit.html",
|
|
3
|
-
".:ByteArrayInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_multistream_select.ByteArrayInit.html",
|
|
4
|
-
"ByteListInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_multistream_select.ByteListInit.html",
|
|
5
|
-
".:ByteListInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_multistream_select.ByteListInit.html",
|
|
6
2
|
"MultistreamSelectInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_multistream_select.MultistreamSelectInit.html",
|
|
7
3
|
".:MultistreamSelectInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_multistream_select.MultistreamSelectInit.html",
|
|
8
4
|
"ProtocolStream": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_multistream_select.ProtocolStream.html",
|
|
9
5
|
".:ProtocolStream": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_multistream_select.ProtocolStream.html",
|
|
10
6
|
"PROTOCOL_ID": "https://libp2p.github.io/js-libp2p/variables/_libp2p_multistream_select.PROTOCOL_ID.html",
|
|
11
7
|
"handle": "https://libp2p.github.io/js-libp2p/functions/_libp2p_multistream_select.handle.html",
|
|
12
|
-
"lazySelect": "https://libp2p.github.io/js-libp2p/functions/_libp2p_multistream_select.lazySelect.html",
|
|
13
8
|
"select": "https://libp2p.github.io/js-libp2p/functions/_libp2p_multistream_select.select.html"
|
|
14
9
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/multistream-select",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.7",
|
|
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"
|
|
@@ -11,6 +11,10 @@
|
|
|
11
11
|
"bugs": {
|
|
12
12
|
"url": "https://github.com/libp2p/js-libp2p/issues"
|
|
13
13
|
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public",
|
|
16
|
+
"provenance": true
|
|
17
|
+
},
|
|
14
18
|
"keywords": [
|
|
15
19
|
"ipfs",
|
|
16
20
|
"libp2p",
|
|
@@ -53,26 +57,22 @@
|
|
|
53
57
|
"test:electron-main": "aegir test -t electron-main"
|
|
54
58
|
},
|
|
55
59
|
"dependencies": {
|
|
56
|
-
"@libp2p/interface": "^0.
|
|
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",
|
|
60
|
+
"@libp2p/interface": "^1.0.0",
|
|
61
|
+
"it-length-prefixed": "^9.0.3",
|
|
62
|
+
"it-length-prefixed-stream": "^1.1.1",
|
|
63
63
|
"it-pipe": "^3.0.1",
|
|
64
|
-
"it-pushable": "^3.2.0",
|
|
65
|
-
"it-reader": "^6.0.1",
|
|
66
64
|
"it-stream-types": "^2.0.1",
|
|
67
|
-
"
|
|
65
|
+
"p-defer": "^4.0.0",
|
|
66
|
+
"uint8-varint": "^2.0.2",
|
|
68
67
|
"uint8arraylist": "^2.4.3",
|
|
69
68
|
"uint8arrays": "^4.0.6"
|
|
70
69
|
},
|
|
71
70
|
"devDependencies": {
|
|
71
|
+
"@libp2p/logger": "^4.0.0",
|
|
72
72
|
"aegir": "^41.0.2",
|
|
73
73
|
"iso-random-stream": "^2.0.2",
|
|
74
|
-
"it-all": "^3.0.
|
|
75
|
-
"it-
|
|
74
|
+
"it-all": "^3.0.3",
|
|
75
|
+
"it-drain": "^3.0.5",
|
|
76
76
|
"it-pair": "^2.0.6",
|
|
77
77
|
"p-timeout": "^6.0.0"
|
|
78
78
|
}
|
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,51 @@ 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
|
-
|
|
58
|
+
options.log.trace('handle: available protocols %s', protocols)
|
|
59
|
+
|
|
60
|
+
const lp = lpStream(stream, {
|
|
61
|
+
...options,
|
|
62
|
+
maxDataLength: MAX_PROTOCOL_LENGTH,
|
|
63
|
+
maxLengthLength: 2 // 2 bytes is enough to length-prefix MAX_PROTOCOL_LENGTH
|
|
64
|
+
})
|
|
63
65
|
|
|
64
66
|
while (true) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
+
options?.log.trace('handle: reading incoming string')
|
|
68
|
+
const protocol = await multistream.readString(lp, options)
|
|
69
|
+
options.log.trace('handle: read "%s"', protocol)
|
|
67
70
|
|
|
68
71
|
if (protocol === PROTOCOL_ID) {
|
|
69
|
-
log.trace('respond with "%s" for "%s"', PROTOCOL_ID, protocol)
|
|
70
|
-
multistream.write(
|
|
72
|
+
options.log.trace('handle: respond with "%s" for "%s"', PROTOCOL_ID, protocol)
|
|
73
|
+
await multistream.write(lp, uint8ArrayFromString(`${PROTOCOL_ID}\n`), options)
|
|
74
|
+
options.log.trace('handle: responded with "%s" for "%s"', PROTOCOL_ID, protocol)
|
|
71
75
|
continue
|
|
72
76
|
}
|
|
73
77
|
|
|
74
78
|
if (protocols.includes(protocol)) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
+
options.log.trace('handle: respond with "%s" for "%s"', protocol, protocol)
|
|
80
|
+
await multistream.write(lp, uint8ArrayFromString(`${protocol}\n`), options)
|
|
81
|
+
options.log.trace('handle: responded with "%s" for "%s"', protocol, protocol)
|
|
82
|
+
|
|
83
|
+
return { stream: lp.unwrap(), protocol }
|
|
79
84
|
}
|
|
80
85
|
|
|
81
86
|
if (protocol === 'ls') {
|
|
82
87
|
// <varint-msg-len><varint-proto-name-len><proto-name>\n<varint-proto-name-len><proto-name>\n\n
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
88
|
+
const protos = new Uint8ArrayList(
|
|
89
|
+
...protocols.map(p => encode.single(uint8ArrayFromString(`${p}\n`))),
|
|
90
|
+
uint8ArrayFromString('\n')
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
options.log.trace('handle: respond with "%s" for %s', protocols, protocol)
|
|
94
|
+
await multistream.write(lp, protos, options)
|
|
95
|
+
options.log.trace('handle: responded with "%s" for %s', protocols, protocol)
|
|
86
96
|
continue
|
|
87
97
|
}
|
|
88
98
|
|
|
89
|
-
|
|
90
|
-
|
|
99
|
+
options.log('handle: respond with "na" for "%s"', protocol)
|
|
100
|
+
await multistream.write(lp, uint8ArrayFromString('na\n'), options)
|
|
101
|
+
options.log('handle: responded with "na" for "%s"', protocol)
|
|
91
102
|
}
|
|
92
103
|
}
|
package/src/index.ts
CHANGED
|
@@ -21,27 +21,19 @@
|
|
|
21
21
|
*/
|
|
22
22
|
|
|
23
23
|
import { PROTOCOL_ID } from './constants.js'
|
|
24
|
-
import type { AbortOptions } from '@libp2p/interface'
|
|
25
|
-
import type {
|
|
24
|
+
import type { AbortOptions, LoggerOptions } from '@libp2p/interface'
|
|
25
|
+
import type { LengthPrefixedStreamOpts } from 'it-length-prefixed-stream'
|
|
26
26
|
|
|
27
27
|
export { PROTOCOL_ID }
|
|
28
28
|
|
|
29
|
-
export interface ProtocolStream<
|
|
30
|
-
stream:
|
|
29
|
+
export interface ProtocolStream<Stream> {
|
|
30
|
+
stream: Stream
|
|
31
31
|
protocol: string
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
export interface
|
|
35
|
-
writeBytes: true
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface ByteListInit extends AbortOptions {
|
|
39
|
-
writeBytes?: false
|
|
40
|
-
}
|
|
34
|
+
export interface MultistreamSelectInit extends AbortOptions, LoggerOptions, Partial<LengthPrefixedStreamOpts> {
|
|
41
35
|
|
|
42
|
-
export interface MultistreamSelectInit extends AbortOptions {
|
|
43
|
-
writeBytes?: boolean
|
|
44
36
|
}
|
|
45
37
|
|
|
46
|
-
export { select
|
|
38
|
+
export { select } from './select.js'
|
|
47
39
|
export { handle } from './handle.js'
|