@libp2p/multistream-select 4.0.9-f537b3731 → 4.0.10
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 +2 -14
- package/dist/src/handle.d.ts +5 -3
- package/dist/src/handle.d.ts.map +1 -1
- package/dist/src/handle.js +18 -72
- package/dist/src/handle.js.map +1 -1
- package/dist/src/index.d.ts +13 -6
- 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 +9 -14
- package/dist/src/multistream.d.ts.map +1 -1
- package/dist/src/multistream.js +53 -15
- package/dist/src/multistream.js.map +1 -1
- package/dist/src/select.d.ts +15 -10
- package/dist/src/select.d.ts.map +1 -1
- package/dist/src/select.js +66 -249
- package/dist/src/select.js.map +1 -1
- package/package.json +14 -14
- package/src/handle.ts +24 -35
- package/src/index.ts +14 -6
- package/src/multistream.ts +72 -20
- package/src/select.ts +71 -251
package/dist/src/select.d.ts
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type {
|
|
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
|
-
}
|
|
1
|
+
import { Uint8ArrayList } from 'uint8arraylist';
|
|
2
|
+
import type { ByteArrayInit, ByteListInit, ProtocolStream } from './index.js';
|
|
3
|
+
import type { Duplex, Source } from 'it-stream-types';
|
|
10
4
|
/**
|
|
11
5
|
* Negotiate a protocol to use from a list of protocols.
|
|
12
6
|
*
|
|
@@ -50,5 +44,16 @@ export interface SelectStream extends Duplex<any, any, any> {
|
|
|
50
44
|
* // }
|
|
51
45
|
* ```
|
|
52
46
|
*/
|
|
53
|
-
export declare function select
|
|
47
|
+
export declare function select(stream: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>>, protocols: string | string[], options: ByteArrayInit): Promise<ProtocolStream<Uint8Array>>;
|
|
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>;
|
|
54
59
|
//# 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":"AAMA,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAI/C,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAyB,cAAc,EAAE,MAAM,YAAY,CAAA;AACpG,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAIrD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,wBAAsB,MAAM,CAAE,MAAM,EAAE,MAAM,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC,CAAA;AACxL,wBAAsB,MAAM,CAAE,MAAM,EAAE,MAAM,CAAC,cAAc,CAAC,cAAc,GAAG,UAAU,CAAC,EAAE,MAAM,CAAC,cAAc,GAAG,UAAU,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,cAAc,CAAC,cAAc,EAAE,cAAc,GAAG,UAAU,CAAC,CAAC,CAAA;AAgD3P;;;;;;;GAOG;AACH,wBAAgB,UAAU,CAAE,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,GAAG,cAAc,CAAC,UAAU,CAAC,CAAA;AACjI,wBAAgB,UAAU,CAAE,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,cAAc,GAAG,UAAU,CAAC,EAAE,MAAM,CAAC,cAAc,GAAG,UAAU,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,GAAG,cAAc,CAAC,cAAc,EAAE,cAAc,GAAG,UAAU,CAAC,CAAA"}
|
package/dist/src/select.js
CHANGED
|
@@ -1,278 +1,95 @@
|
|
|
1
|
-
import { CodeError } from '@libp2p/interface';
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import
|
|
1
|
+
import { CodeError } from '@libp2p/interface/errors';
|
|
2
|
+
import { logger } from '@libp2p/logger';
|
|
3
|
+
import { handshake } from 'it-handshake';
|
|
4
|
+
import merge from 'it-merge';
|
|
5
|
+
import { pushable } from 'it-pushable';
|
|
6
|
+
import { reader } from 'it-reader';
|
|
5
7
|
import { Uint8ArrayList } from 'uint8arraylist';
|
|
6
8
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
|
7
|
-
import { MAX_PROTOCOL_LENGTH } from './constants.js';
|
|
8
9
|
import * as multistream from './multistream.js';
|
|
9
10
|
import { PROTOCOL_ID } from './index.js';
|
|
10
|
-
|
|
11
|
-
|
|
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) {
|
|
11
|
+
const log = logger('libp2p:mss:select');
|
|
12
|
+
export async function select(stream, protocols, options = {}) {
|
|
54
13
|
protocols = Array.isArray(protocols) ? [...protocols] : [protocols];
|
|
55
|
-
|
|
56
|
-
return optimisticSelect(stream, protocols[0], options);
|
|
57
|
-
}
|
|
58
|
-
const lp = lpStream(stream, {
|
|
59
|
-
...options,
|
|
60
|
-
maxDataLength: MAX_PROTOCOL_LENGTH
|
|
61
|
-
});
|
|
14
|
+
const { reader, writer, rest, stream: shakeStream } = handshake(stream);
|
|
62
15
|
const protocol = protocols.shift();
|
|
63
16
|
if (protocol == null) {
|
|
64
17
|
throw new Error('At least one protocol must be specified');
|
|
65
18
|
}
|
|
66
|
-
|
|
67
|
-
const p1 = uint8ArrayFromString(
|
|
68
|
-
const p2 = uint8ArrayFromString(
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
options?.log?.trace('select: read "%s"', response);
|
|
19
|
+
log.trace('select: write ["%s", "%s"]', PROTOCOL_ID, protocol);
|
|
20
|
+
const p1 = uint8ArrayFromString(PROTOCOL_ID);
|
|
21
|
+
const p2 = uint8ArrayFromString(protocol);
|
|
22
|
+
multistream.writeAll(writer, [p1, p2], options);
|
|
23
|
+
let response = await multistream.readString(reader, options);
|
|
24
|
+
log.trace('select: read "%s"', response);
|
|
73
25
|
// Read the protocol response if we got the protocolId in return
|
|
74
26
|
if (response === PROTOCOL_ID) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
options?.log?.trace('select: read "%s"', response);
|
|
27
|
+
response = await multistream.readString(reader, options);
|
|
28
|
+
log.trace('select: read "%s"', response);
|
|
78
29
|
}
|
|
79
30
|
// We're done
|
|
80
31
|
if (response === protocol) {
|
|
81
|
-
|
|
32
|
+
rest();
|
|
33
|
+
return { stream: shakeStream, protocol };
|
|
82
34
|
}
|
|
83
35
|
// We haven't gotten a valid ack, try the other protocols
|
|
84
36
|
for (const protocol of protocols) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
options?.log?.trace('select: read "%s" for "%s"', response, protocol);
|
|
37
|
+
log.trace('select: write "%s"', protocol);
|
|
38
|
+
multistream.write(writer, uint8ArrayFromString(protocol), options);
|
|
39
|
+
const response = await multistream.readString(reader, options);
|
|
40
|
+
log.trace('select: read "%s" for "%s"', response, protocol);
|
|
90
41
|
if (response === protocol) {
|
|
91
|
-
|
|
42
|
+
rest(); // End our writer so others can start writing to stream
|
|
43
|
+
return { stream: shakeStream, protocol };
|
|
92
44
|
}
|
|
93
45
|
}
|
|
46
|
+
rest();
|
|
94
47
|
throw new CodeError('protocol selection failed', 'ERR_UNSUPPORTED_PROTOCOL');
|
|
95
48
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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;
|
|
49
|
+
export function lazySelect(stream, protocol) {
|
|
50
|
+
// This is a signal to write the multistream headers if the consumer tries to
|
|
51
|
+
// read from the source
|
|
52
|
+
const negotiateTrigger = pushable();
|
|
107
53
|
let negotiated = false;
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
54
|
+
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);
|
|
131
84
|
}
|
|
132
|
-
|
|
133
|
-
|
|
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();
|
|
85
|
+
if (response !== protocol) {
|
|
86
|
+
throw new CodeError('protocol selection failed', 'ERR_UNSUPPORTED_PROTOCOL');
|
|
144
87
|
}
|
|
145
|
-
|
|
146
|
-
yield
|
|
88
|
+
for await (const chunk of byteReader) {
|
|
89
|
+
yield* chunk;
|
|
147
90
|
}
|
|
148
|
-
|
|
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,
|
|
91
|
+
})()
|
|
92
|
+
},
|
|
276
93
|
protocol
|
|
277
94
|
};
|
|
278
95
|
}
|
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"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AACxC,OAAO,KAAK,MAAM,UAAU,CAAA;AAC5B,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAClC,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC/C,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAA;AAC/C,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAIxC,MAAM,GAAG,GAAG,MAAM,CAAC,mBAAmB,CAAC,CAAA;AA+CvC,MAAM,CAAC,KAAK,UAAU,MAAM,CAAE,MAAW,EAAE,SAA4B,EAAE,UAAiC,EAAE;IAC1G,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,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,SAAS,CAAC,MAAM,CAAC,CAAA;IAEvE,MAAM,QAAQ,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AC;gCAAE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;4BACxC,KAAM,CAAC,CAAC,IAAI,CAAA;wBACd,CAAC;6BAAM,CAAC;4BACN,MAAM,KAAK,CAAA;wBACb,CAAC;oBACH,CAAC;gBACH,CAAC,CAAC,EAAE,CAAC,CAAA;YACP,CAAC;YACD,MAAM,EAAE,CAAC,KAAK,SAAU,CAAC;gBACvB,IAAI,CAAC,UAAU;oBAAE,gBAAgB,CAAC,IAAI,CAAC,IAAI,UAAU,EAAE,CAAC,CAAA;gBACxD,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;gBACxC,IAAI,QAAQ,GAAG,MAAM,WAAW,CAAC,UAAU,CAAC,UAAU,CAAC,CAAA;gBACvD,IAAI,QAAQ,KAAK,WAAW,EAAE,CAAC;oBAC7B,QAAQ,GAAG,MAAM,WAAW,CAAC,UAAU,CAAC,UAAU,CAAC,CAAA;gBACrD,CAAC;gBACD,IAAI,QAAQ,KAAK,QAAQ,EAAE,CAAC;oBAC1B,MAAM,IAAI,SAAS,CAAC,2BAA2B,EAAE,0BAA0B,CAAC,CAAA;gBAC9E,CAAC;gBACD,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,UAAU,EAAE,CAAC;oBACrC,KAAM,CAAC,CAAC,KAAK,CAAA;gBACf,CAAC;YACH,CAAC,CAAC,EAAE;SACL;QACD,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.
|
|
3
|
+
"version": "4.0.10",
|
|
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/master/packages/multistream-select#readme",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "git+https://github.com/libp2p/js-libp2p.git"
|
|
@@ -11,10 +11,6 @@
|
|
|
11
11
|
"bugs": {
|
|
12
12
|
"url": "https://github.com/libp2p/js-libp2p/issues"
|
|
13
13
|
},
|
|
14
|
-
"publishConfig": {
|
|
15
|
-
"access": "public",
|
|
16
|
-
"provenance": true
|
|
17
|
-
},
|
|
18
14
|
"keywords": [
|
|
19
15
|
"ipfs",
|
|
20
16
|
"libp2p",
|
|
@@ -57,22 +53,26 @@
|
|
|
57
53
|
"test:electron-main": "aegir test -t electron-main"
|
|
58
54
|
},
|
|
59
55
|
"dependencies": {
|
|
60
|
-
"@libp2p/interface": "
|
|
61
|
-
"
|
|
62
|
-
"
|
|
56
|
+
"@libp2p/interface": "^0.1.6",
|
|
57
|
+
"@libp2p/logger": "^3.1.0",
|
|
58
|
+
"abortable-iterator": "^5.0.1",
|
|
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",
|
|
63
63
|
"it-pipe": "^3.0.1",
|
|
64
|
+
"it-pushable": "^3.2.0",
|
|
65
|
+
"it-reader": "^6.0.1",
|
|
64
66
|
"it-stream-types": "^2.0.1",
|
|
65
|
-
"
|
|
66
|
-
"uint8-varint": "^2.0.2",
|
|
67
|
+
"uint8-varint": "^2.0.0",
|
|
67
68
|
"uint8arraylist": "^2.4.3",
|
|
68
69
|
"uint8arrays": "^4.0.6"
|
|
69
70
|
},
|
|
70
71
|
"devDependencies": {
|
|
71
|
-
"@libp2p/logger": "4.0.1-f537b3731",
|
|
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.1",
|
|
75
|
+
"it-map": "^3.0.3",
|
|
76
76
|
"it-pair": "^2.0.6",
|
|
77
77
|
"p-timeout": "^6.0.0"
|
|
78
78
|
}
|
package/src/handle.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { logger } from '@libp2p/logger'
|
|
2
|
+
import { handshake } from 'it-handshake'
|
|
3
3
|
import { Uint8ArrayList } from 'uint8arraylist'
|
|
4
4
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
5
|
-
import {
|
|
5
|
+
import { PROTOCOL_ID } from './constants.js'
|
|
6
6
|
import * as multistream from './multistream.js'
|
|
7
|
-
import type { MultistreamSelectInit, ProtocolStream } from './index.js'
|
|
8
|
-
import type { Duplex } from 'it-stream-types'
|
|
7
|
+
import type { ByteArrayInit, ByteListInit, MultistreamSelectInit, ProtocolStream } from './index.js'
|
|
8
|
+
import type { Duplex, Source } from 'it-stream-types'
|
|
9
|
+
|
|
10
|
+
const log = logger('libp2p:mss:handle')
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* Handle multistream protocol selections for the given list of protocols.
|
|
@@ -53,51 +55,38 @@ import type { Duplex } from 'it-stream-types'
|
|
|
53
55
|
* })
|
|
54
56
|
* ```
|
|
55
57
|
*/
|
|
56
|
-
export async function handle
|
|
58
|
+
export async function handle (stream: Duplex<Source<Uint8Array>, Source<Uint8Array>>, protocols: string | string[], options: ByteArrayInit): Promise<ProtocolStream<Uint8Array>>
|
|
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>> {
|
|
57
61
|
protocols = Array.isArray(protocols) ? protocols : [protocols]
|
|
58
|
-
|
|
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
|
-
})
|
|
62
|
+
const { writer, reader, rest, stream: shakeStream } = handshake(stream)
|
|
65
63
|
|
|
66
64
|
while (true) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
options?.log?.trace('handle: read "%s"', protocol)
|
|
65
|
+
const protocol = await multistream.readString(reader, options)
|
|
66
|
+
log.trace('read "%s"', protocol)
|
|
70
67
|
|
|
71
68
|
if (protocol === PROTOCOL_ID) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
options?.log?.trace('handle: responded with "%s" for "%s"', PROTOCOL_ID, protocol)
|
|
69
|
+
log.trace('respond with "%s" for "%s"', PROTOCOL_ID, protocol)
|
|
70
|
+
multistream.write(writer, uint8ArrayFromString(PROTOCOL_ID), options)
|
|
75
71
|
continue
|
|
76
72
|
}
|
|
77
73
|
|
|
78
74
|
if (protocols.includes(protocol)) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
return { stream: lp.unwrap(), protocol }
|
|
75
|
+
multistream.write(writer, uint8ArrayFromString(protocol), options)
|
|
76
|
+
log.trace('respond with "%s" for "%s"', protocol, protocol)
|
|
77
|
+
rest()
|
|
78
|
+
return { stream: shakeStream, protocol }
|
|
84
79
|
}
|
|
85
80
|
|
|
86
81
|
if (protocol === 'ls') {
|
|
87
82
|
// <varint-msg-len><varint-proto-name-len><proto-name>\n<varint-proto-name-len><proto-name>\n\n
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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)
|
|
83
|
+
multistream.write(writer, new Uint8ArrayList(...protocols.map(p => multistream.encode(uint8ArrayFromString(p)))), options)
|
|
84
|
+
// multistream.writeAll(writer, protocols.map(p => uint8ArrayFromString(p)))
|
|
85
|
+
log.trace('respond with "%s" for %s', protocols, protocol)
|
|
96
86
|
continue
|
|
97
87
|
}
|
|
98
88
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
options?.log?.('handle: responded with "na" for "%s"', protocol)
|
|
89
|
+
multistream.write(writer, uint8ArrayFromString('na'), options)
|
|
90
|
+
log('respond with "na" for "%s"', protocol)
|
|
102
91
|
}
|
|
103
92
|
}
|
package/src/index.ts
CHANGED
|
@@ -21,19 +21,27 @@
|
|
|
21
21
|
*/
|
|
22
22
|
|
|
23
23
|
import { PROTOCOL_ID } from './constants.js'
|
|
24
|
-
import type { AbortOptions
|
|
25
|
-
import type {
|
|
24
|
+
import type { AbortOptions } from '@libp2p/interface'
|
|
25
|
+
import type { Duplex, Source } from 'it-stream-types'
|
|
26
26
|
|
|
27
27
|
export { PROTOCOL_ID }
|
|
28
28
|
|
|
29
|
-
export interface ProtocolStream<
|
|
30
|
-
stream:
|
|
29
|
+
export interface ProtocolStream<TSource, TSink = TSource, RSink = Promise<void>> {
|
|
30
|
+
stream: Duplex<AsyncGenerator<TSource>, Source<TSink>, RSink>
|
|
31
31
|
protocol: string
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
export interface
|
|
34
|
+
export interface ByteArrayInit extends AbortOptions {
|
|
35
|
+
writeBytes: true
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface ByteListInit extends AbortOptions {
|
|
39
|
+
writeBytes?: false
|
|
40
|
+
}
|
|
35
41
|
|
|
42
|
+
export interface MultistreamSelectInit extends AbortOptions {
|
|
43
|
+
writeBytes?: boolean
|
|
36
44
|
}
|
|
37
45
|
|
|
38
|
-
export { select } from './select.js'
|
|
46
|
+
export { select, lazySelect } from './select.js'
|
|
39
47
|
export { handle } from './handle.js'
|