@libp2p/multistream-select 3.0.0 → 3.1.1
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/dist/src/constants.d.ts +1 -0
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +3 -0
- package/dist/src/constants.js.map +1 -1
- package/dist/src/index.d.ts +1 -1
- 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.map +1 -1
- package/dist/src/multistream.js +3 -2
- package/dist/src/multistream.js.map +1 -1
- package/dist/src/select.d.ts +11 -1
- package/dist/src/select.d.ts.map +1 -1
- package/dist/src/select.js +49 -0
- package/dist/src/select.js.map +1 -1
- package/package.json +4 -3
- package/src/constants.ts +4 -0
- package/src/index.ts +1 -1
- package/src/multistream.ts +3 -2
- package/src/select.ts +57 -1
package/dist/src/constants.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,WAAW,uBAAuB,CAAA"}
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,WAAW,uBAAuB,CAAA;AAI/C,eAAO,MAAM,mBAAmB,OAAO,CAAA"}
|
package/dist/src/constants.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AACA,MAAM,CAAC,MAAM,WAAW,GAAG,oBAAoB,CAAA"}
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AACA,MAAM,CAAC,MAAM,WAAW,GAAG,oBAAoB,CAAA;AAE/C,0BAA0B;AAC1B,qFAAqF;AACrF,MAAM,CAAC,MAAM,mBAAmB,GAAG,IAAI,CAAA"}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -15,6 +15,6 @@ export interface ByteListInit extends AbortOptions {
|
|
|
15
15
|
export interface MultistreamSelectInit extends AbortOptions {
|
|
16
16
|
writeBytes?: boolean;
|
|
17
17
|
}
|
|
18
|
-
export { select } from './select.js';
|
|
18
|
+
export { select, lazySelect } from './select.js';
|
|
19
19
|
export { handle } from './handle.js';
|
|
20
20
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,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,CAAC,OAAO,EAAE,KAAK,GAAG,OAAO;IACtD,MAAM,EAAE,MAAM,CAAC,OAAO,EAAE,KAAK,CAAC,CAAA;IAC9B,QAAQ,EAAE,MAAM,CAAA;CACjB;AAED,MAAM,WAAW,aAAc,SAAQ,YAAY;IACjD,UAAU,EAAE,IAAI,CAAA;CACjB;AAED,MAAM,WAAW,YAAa,SAAQ,YAAY;IAChD,UAAU,CAAC,EAAE,KAAK,CAAA;CACnB;AAED,MAAM,WAAW,qBAAsB,SAAQ,YAAY;IACzD,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB;AAED,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,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,CAAC,OAAO,EAAE,KAAK,GAAG,OAAO;IACtD,MAAM,EAAE,MAAM,CAAC,OAAO,EAAE,KAAK,CAAC,CAAA;IAC9B,QAAQ,EAAE,MAAM,CAAA;CACjB;AAED,MAAM,WAAW,aAAc,SAAQ,YAAY;IACjD,UAAU,EAAE,IAAI,CAAA;CACjB;AAED,MAAM,WAAW,YAAa,SAAQ,YAAY;IAChD,UAAU,CAAC,EAAE,KAAK,CAAA;CACnB;AAED,MAAM,WAAW,qBAAsB,SAAQ,YAAY;IACzD,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB;AAED,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAChD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA"}
|
package/dist/src/index.js
CHANGED
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,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAI5C,OAAO,EAAE,WAAW,EAAE,CAAA;AAmBtB,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAI5C,OAAO,EAAE,WAAW,EAAE,CAAA;AAmBtB,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAChD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"multistream.d.ts","sourceRoot":"","sources":["../../src/mult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ANGED
|
@@ -6,6 +6,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
|
|
6
6
|
import first from 'it-first';
|
|
7
7
|
import { abortableSource } from 'abortable-iterator';
|
|
8
8
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
|
|
9
|
+
import { MAX_PROTOCOL_LENGTH } from './constants.js';
|
|
9
10
|
const NewLine = uint8ArrayFromString('\n');
|
|
10
11
|
export function encode(buffer) {
|
|
11
12
|
const list = new Uint8ArrayList(buffer, NewLine);
|
|
@@ -54,8 +55,8 @@ export async function read(reader, options) {
|
|
|
54
55
|
const onLength = (l) => {
|
|
55
56
|
byteLength = l;
|
|
56
57
|
};
|
|
57
|
-
const buf = await pipe(input, lp.decode({ onLength }), async (source) => await first(source));
|
|
58
|
-
if (buf == null) {
|
|
58
|
+
const buf = await pipe(input, lp.decode({ onLength, maxDataLength: MAX_PROTOCOL_LENGTH }), async (source) => await first(source));
|
|
59
|
+
if (buf == null || buf.length === 0) {
|
|
59
60
|
throw errCode(new Error('no buffer returned'), 'ERR_INVALID_MULTISTREAM_SELECT_MESSAGE');
|
|
60
61
|
}
|
|
61
62
|
if (buf.get(buf.byteLength - 1) !== NewLine[0]) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"multistream.js","sourceRoot":"","sources":["../../src/multistream.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC/C,OAAO,KAAK,EAAE,MAAM,oBAAoB,CAAA;AACxC,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,OAAO,MAAM,UAAU,CAAA;AAC9B,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,KAAK,MAAM,UAAU,CAAA;AAC5B,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,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AAM,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,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA,CAAC,iBAAiB;AAC7C,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,QAAQ,EAAE,CAAC,CAAA;AAC3C,CAAC"}
|
package/dist/src/select.d.ts
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
import type { Duplex } from 'it-stream-types';
|
|
2
|
-
import
|
|
2
|
+
import { Uint8ArrayList } from 'uint8arraylist';
|
|
3
3
|
import type { ByteArrayInit, ByteListInit, ProtocolStream } from './index.js';
|
|
4
4
|
export declare function select(stream: Duplex<Uint8Array>, protocols: string | string[], options: ByteArrayInit): Promise<ProtocolStream<Uint8Array>>;
|
|
5
5
|
export declare function select(stream: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>, protocols: string | string[], options?: ByteListInit): Promise<ProtocolStream<Uint8ArrayList, Uint8ArrayList | Uint8Array>>;
|
|
6
|
+
/**
|
|
7
|
+
* Lazily negotiates a protocol.
|
|
8
|
+
*
|
|
9
|
+
* It *does not* block writes waiting for the other end to respond. Instead, it
|
|
10
|
+
* simply assumes the negotiation went successfully and starts writing data.
|
|
11
|
+
*
|
|
12
|
+
* Use when it is known that the receiver supports the desired protocol.
|
|
13
|
+
*/
|
|
14
|
+
export declare function lazySelect(stream: Duplex<Uint8Array>, protocol: string): ProtocolStream<Uint8Array>;
|
|
15
|
+
export declare function lazySelect(stream: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>, protocol: string): ProtocolStream<Uint8ArrayList, Uint8ArrayList | Uint8Array>;
|
|
6
16
|
//# 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":"AAMA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,
|
|
1
|
+
{"version":3,"file":"select.d.ts","sourceRoot":"","sources":["../../src/select.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAI/C,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAyB,cAAc,EAAE,MAAM,YAAY,CAAA;AAIpG,wBAAsB,MAAM,CAAE,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC,CAAA;AACpJ,wBAAsB,MAAM,CAAE,MAAM,EAAE,MAAM,CAAC,cAAc,EAAE,cAAc,GAAG,UAAU,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;AAgDtN;;;;;;;GAOG;AACH,wBAAgB,UAAU,CAAE,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC,EAAE,QAAQ,EAAE,MAAM,GAAG,cAAc,CAAC,UAAU,CAAC,CAAA;AACrG,wBAAgB,UAAU,CAAE,MAAM,EAAE,MAAM,CAAC,cAAc,EAAE,cAAc,GAAG,UAAU,CAAC,EAAE,QAAQ,EAAE,MAAM,GAAG,cAAc,CAAC,cAAc,EAAE,cAAc,GAAG,UAAU,CAAC,CAAA"}
|
package/dist/src/select.js
CHANGED
|
@@ -4,6 +4,10 @@ import * as multistream from './multistream.js';
|
|
|
4
4
|
import { handshake } from 'it-handshake';
|
|
5
5
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
|
6
6
|
import { PROTOCOL_ID } from './index.js';
|
|
7
|
+
import { Uint8ArrayList } from 'uint8arraylist';
|
|
8
|
+
import { pushable } from 'it-pushable';
|
|
9
|
+
import merge from 'it-merge';
|
|
10
|
+
import { reader } from 'it-reader';
|
|
7
11
|
const log = logger('libp2p:mss:select');
|
|
8
12
|
export async function select(stream, protocols, options = {}) {
|
|
9
13
|
protocols = Array.isArray(protocols) ? [...protocols] : [protocols];
|
|
@@ -42,4 +46,49 @@ export async function select(stream, protocols, options = {}) {
|
|
|
42
46
|
rest();
|
|
43
47
|
throw errCode(new Error('protocol selection failed'), 'ERR_UNSUPPORTED_PROTOCOL');
|
|
44
48
|
}
|
|
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();
|
|
53
|
+
let negotiated = false;
|
|
54
|
+
return {
|
|
55
|
+
stream: {
|
|
56
|
+
sink: async (source) => await stream.sink((async function* () {
|
|
57
|
+
let first = true;
|
|
58
|
+
for await (const chunk of merge(source, negotiateTrigger)) {
|
|
59
|
+
if (first) {
|
|
60
|
+
first = false;
|
|
61
|
+
negotiated = true;
|
|
62
|
+
negotiateTrigger.end();
|
|
63
|
+
const p1 = uint8ArrayFromString(PROTOCOL_ID);
|
|
64
|
+
const p2 = uint8ArrayFromString(protocol);
|
|
65
|
+
const list = new Uint8ArrayList(multistream.encode(p1), multistream.encode(p2));
|
|
66
|
+
if (chunk.length > 0)
|
|
67
|
+
list.append(chunk);
|
|
68
|
+
yield* list;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
yield chunk;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
})()),
|
|
75
|
+
source: (async function* () {
|
|
76
|
+
if (!negotiated)
|
|
77
|
+
negotiateTrigger.push(new Uint8Array());
|
|
78
|
+
const byteReader = reader(stream.source);
|
|
79
|
+
let response = await multistream.readString(byteReader);
|
|
80
|
+
if (response === PROTOCOL_ID) {
|
|
81
|
+
response = await multistream.readString(byteReader);
|
|
82
|
+
}
|
|
83
|
+
if (response !== protocol) {
|
|
84
|
+
throw errCode(new Error('protocol selection failed'), 'ERR_UNSUPPORTED_PROTOCOL');
|
|
85
|
+
}
|
|
86
|
+
for await (const chunk of byteReader) {
|
|
87
|
+
yield* chunk;
|
|
88
|
+
}
|
|
89
|
+
})()
|
|
90
|
+
},
|
|
91
|
+
protocol
|
|
92
|
+
};
|
|
93
|
+
}
|
|
45
94
|
//# sourceMappingURL=select.js.map
|
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,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,OAAO,MAAM,UAAU,CAAA;AAC9B,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAA;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AACxC,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"select.js","sourceRoot":"","sources":["../../src/select.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,OAA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AC,UAAU,CAAC,CAAA;iBACpD;gBACD,IAAI,QAAQ,KAAK,QAAQ,EAAE;oBACzB,MAAM,OAAO,CAAC,IAAI,KAAK,CAAC,2BAA2B,CAAC,EAAE,0BAA0B,CAAC,CAAA;iBAClF;gBACD,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,UAAU,EAAE;oBACpC,KAAM,CAAC,CAAC,KAAK,CAAA;iBACd;YACH,CAAC,CAAC,EAAE;SACL;QACD,QAAQ;KACT,CAAA;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/multistream-select",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.1",
|
|
4
4
|
"description": "JavaScript implementation of multistream-select",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p-multistream-select#readme",
|
|
@@ -147,15 +147,16 @@
|
|
|
147
147
|
"abortable-iterator": "^4.0.2",
|
|
148
148
|
"err-code": "^3.0.1",
|
|
149
149
|
"it-first": "^1.0.6",
|
|
150
|
-
"it-handshake": "^4.
|
|
150
|
+
"it-handshake": "^4.1.2",
|
|
151
151
|
"it-length-prefixed": "^8.0.2",
|
|
152
|
+
"it-merge": "^1.0.4",
|
|
152
153
|
"it-pipe": "^2.0.3",
|
|
153
154
|
"it-pushable": "^3.0.0",
|
|
154
155
|
"it-reader": "^6.0.1",
|
|
155
156
|
"it-stream-types": "^1.0.4",
|
|
156
157
|
"p-defer": "^4.0.0",
|
|
157
158
|
"uint8arraylist": "^2.3.1",
|
|
158
|
-
"uint8arrays": "^
|
|
159
|
+
"uint8arrays": "^4.0.2"
|
|
159
160
|
},
|
|
160
161
|
"devDependencies": {
|
|
161
162
|
"@types/varint": "^6.0.0",
|
package/src/constants.ts
CHANGED
package/src/index.ts
CHANGED
package/src/multistream.ts
CHANGED
|
@@ -12,6 +12,7 @@ import type { AbortOptions } from '@libp2p/interfaces'
|
|
|
12
12
|
import type { Source } from 'it-stream-types'
|
|
13
13
|
import type { Reader } from 'it-reader'
|
|
14
14
|
import type { MultistreamSelectInit } from '.'
|
|
15
|
+
import { MAX_PROTOCOL_LENGTH } from './constants.js'
|
|
15
16
|
|
|
16
17
|
const NewLine = uint8ArrayFromString('\n')
|
|
17
18
|
|
|
@@ -73,11 +74,11 @@ export async function read (reader: Reader, options?: AbortOptions): Promise<Uin
|
|
|
73
74
|
|
|
74
75
|
const buf = await pipe(
|
|
75
76
|
input,
|
|
76
|
-
lp.decode({ onLength }),
|
|
77
|
+
lp.decode({ onLength, maxDataLength: MAX_PROTOCOL_LENGTH }),
|
|
77
78
|
async (source) => await first(source)
|
|
78
79
|
)
|
|
79
80
|
|
|
80
|
-
if (buf == null) {
|
|
81
|
+
if (buf == null || buf.length === 0) {
|
|
81
82
|
throw errCode(new Error('no buffer returned'), 'ERR_INVALID_MULTISTREAM_SELECT_MESSAGE')
|
|
82
83
|
}
|
|
83
84
|
|
package/src/select.ts
CHANGED
|
@@ -5,7 +5,10 @@ import { handshake } from 'it-handshake'
|
|
|
5
5
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
6
6
|
import { PROTOCOL_ID } from './index.js'
|
|
7
7
|
import type { Duplex } from 'it-stream-types'
|
|
8
|
-
import
|
|
8
|
+
import { Uint8ArrayList } from 'uint8arraylist'
|
|
9
|
+
import { pushable } from 'it-pushable'
|
|
10
|
+
import merge from 'it-merge'
|
|
11
|
+
import { reader } from 'it-reader'
|
|
9
12
|
import type { ByteArrayInit, ByteListInit, MultistreamSelectInit, ProtocolStream } from './index.js'
|
|
10
13
|
|
|
11
14
|
const log = logger('libp2p:mss:select')
|
|
@@ -58,3 +61,56 @@ export async function select (stream: Duplex<any>, protocols: string | string[],
|
|
|
58
61
|
rest()
|
|
59
62
|
throw errCode(new Error('protocol selection failed'), 'ERR_UNSUPPORTED_PROTOCOL')
|
|
60
63
|
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Lazily negotiates a protocol.
|
|
67
|
+
*
|
|
68
|
+
* It *does not* block writes waiting for the other end to respond. Instead, it
|
|
69
|
+
* simply assumes the negotiation went successfully and starts writing data.
|
|
70
|
+
*
|
|
71
|
+
* Use when it is known that the receiver supports the desired protocol.
|
|
72
|
+
*/
|
|
73
|
+
export function lazySelect (stream: Duplex<Uint8Array>, protocol: string): ProtocolStream<Uint8Array>
|
|
74
|
+
export function lazySelect (stream: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>, protocol: string): ProtocolStream<Uint8ArrayList, Uint8ArrayList | Uint8Array>
|
|
75
|
+
export function lazySelect (stream: Duplex<any>, protocol: string): ProtocolStream<any> {
|
|
76
|
+
// This is a signal to write the multistream headers if the consumer tries to
|
|
77
|
+
// read from the source
|
|
78
|
+
const negotiateTrigger = pushable()
|
|
79
|
+
let negotiated = false
|
|
80
|
+
return {
|
|
81
|
+
stream: {
|
|
82
|
+
sink: async source => await stream.sink((async function * () {
|
|
83
|
+
let first = true
|
|
84
|
+
for await (const chunk of merge(source, negotiateTrigger)) {
|
|
85
|
+
if (first) {
|
|
86
|
+
first = false
|
|
87
|
+
negotiated = true
|
|
88
|
+
negotiateTrigger.end()
|
|
89
|
+
const p1 = uint8ArrayFromString(PROTOCOL_ID)
|
|
90
|
+
const p2 = uint8ArrayFromString(protocol)
|
|
91
|
+
const list = new Uint8ArrayList(multistream.encode(p1), multistream.encode(p2))
|
|
92
|
+
if (chunk.length > 0) list.append(chunk)
|
|
93
|
+
yield * list
|
|
94
|
+
} else {
|
|
95
|
+
yield chunk
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
})()),
|
|
99
|
+
source: (async function * () {
|
|
100
|
+
if (!negotiated) negotiateTrigger.push(new Uint8Array())
|
|
101
|
+
const byteReader = reader(stream.source)
|
|
102
|
+
let response = await multistream.readString(byteReader)
|
|
103
|
+
if (response === PROTOCOL_ID) {
|
|
104
|
+
response = await multistream.readString(byteReader)
|
|
105
|
+
}
|
|
106
|
+
if (response !== protocol) {
|
|
107
|
+
throw errCode(new Error('protocol selection failed'), 'ERR_UNSUPPORTED_PROTOCOL')
|
|
108
|
+
}
|
|
109
|
+
for await (const chunk of byteReader) {
|
|
110
|
+
yield * chunk
|
|
111
|
+
}
|
|
112
|
+
})()
|
|
113
|
+
},
|
|
114
|
+
protocol
|
|
115
|
+
}
|
|
116
|
+
}
|