@libp2p/multistream-select 4.0.6-05b52d69c → 4.0.6-13a870cbe
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 -16
- package/dist/src/handle.js.map +1 -1
- package/dist/src/index.d.ts +2 -10
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/multistream.d.ts +13 -8
- package/dist/src/multistream.d.ts.map +1 -1
- package/dist/src/multistream.js +14 -50
- 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 +101 -61
- package/dist/src/select.js.map +1 -1
- package/package.json +6 -12
- package/src/handle.ts +27 -21
- package/src/index.ts +3 -12
- package/src/multistream.ts +18 -67
- package/src/select.ts +64 -66
package/src/select.ts
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface/errors'
|
|
2
|
-
import {
|
|
3
|
-
import merge from 'it-merge'
|
|
4
|
-
import { pushable } from 'it-pushable'
|
|
5
|
-
import { reader } from 'it-reader'
|
|
6
|
-
import { Uint8ArrayList } from 'uint8arraylist'
|
|
2
|
+
import { lpStream } from 'it-length-prefixed-stream'
|
|
7
3
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
4
|
+
import { MAX_PROTOCOL_LENGTH } from './constants.js'
|
|
8
5
|
import * as multistream from './multistream.js'
|
|
9
6
|
import { PROTOCOL_ID } from './index.js'
|
|
10
|
-
import type {
|
|
11
|
-
import type { Duplex
|
|
7
|
+
import type { MultistreamSelectInit, ProtocolStream } from './index.js'
|
|
8
|
+
import type { Duplex } from 'it-stream-types'
|
|
12
9
|
|
|
13
10
|
/**
|
|
14
11
|
* Negotiate a protocol to use from a list of protocols.
|
|
@@ -53,12 +50,11 @@ import type { Duplex, Source } from 'it-stream-types'
|
|
|
53
50
|
* // }
|
|
54
51
|
* ```
|
|
55
52
|
*/
|
|
56
|
-
export async function select (stream:
|
|
57
|
-
export async function select (stream: Duplex<AsyncGenerator<Uint8ArrayList | Uint8Array>, Source<Uint8ArrayList | Uint8Array>>, protocols: string | string[], options?: ByteListInit): Promise<ProtocolStream<Uint8ArrayList, Uint8ArrayList | Uint8Array>>
|
|
58
|
-
export async function select (stream: any, protocols: string | string[], options?: MultistreamSelectInit): Promise<ProtocolStream<any>> {
|
|
53
|
+
export async function select <Stream extends Duplex<any, any, any>> (stream: Stream, protocols: string | string[], options: MultistreamSelectInit): Promise<ProtocolStream<Stream>> {
|
|
59
54
|
protocols = Array.isArray(protocols) ? [...protocols] : [protocols]
|
|
60
|
-
const
|
|
61
|
-
|
|
55
|
+
const lp = lpStream(stream, {
|
|
56
|
+
maxDataLength: MAX_PROTOCOL_LENGTH
|
|
57
|
+
})
|
|
62
58
|
const protocol = protocols.shift()
|
|
63
59
|
|
|
64
60
|
if (protocol == null) {
|
|
@@ -66,39 +62,39 @@ export async function select (stream: any, protocols: string | string[], options
|
|
|
66
62
|
}
|
|
67
63
|
|
|
68
64
|
options?.log.trace('select: write ["%s", "%s"]', PROTOCOL_ID, protocol)
|
|
69
|
-
const p1 = uint8ArrayFromString(PROTOCOL_ID)
|
|
70
|
-
const p2 = uint8ArrayFromString(protocol)
|
|
71
|
-
multistream.writeAll(
|
|
65
|
+
const p1 = uint8ArrayFromString(`${PROTOCOL_ID}\n`)
|
|
66
|
+
const p2 = uint8ArrayFromString(`${protocol}\n`)
|
|
67
|
+
await multistream.writeAll(lp, [p1, p2], options)
|
|
72
68
|
|
|
73
|
-
|
|
69
|
+
options?.log.trace('select: reading multistream-select header')
|
|
70
|
+
let response = await multistream.readString(lp, options)
|
|
74
71
|
options?.log.trace('select: read "%s"', response)
|
|
75
72
|
|
|
76
73
|
// Read the protocol response if we got the protocolId in return
|
|
77
74
|
if (response === PROTOCOL_ID) {
|
|
78
|
-
|
|
75
|
+
options?.log.trace('select: reading protocol response')
|
|
76
|
+
response = await multistream.readString(lp, options)
|
|
79
77
|
options?.log.trace('select: read "%s"', response)
|
|
80
78
|
}
|
|
81
79
|
|
|
82
80
|
// We're done
|
|
83
81
|
if (response === protocol) {
|
|
84
|
-
|
|
85
|
-
return { stream: shakeStream, protocol }
|
|
82
|
+
return { stream: lp.unwrap(), protocol }
|
|
86
83
|
}
|
|
87
84
|
|
|
88
85
|
// We haven't gotten a valid ack, try the other protocols
|
|
89
86
|
for (const protocol of protocols) {
|
|
90
87
|
options?.log.trace('select: write "%s"', protocol)
|
|
91
|
-
multistream.write(
|
|
92
|
-
|
|
88
|
+
await multistream.write(lp, uint8ArrayFromString(`${protocol}\n`), options)
|
|
89
|
+
options?.log.trace('select: reading protocol response')
|
|
90
|
+
const response = await multistream.readString(lp, options)
|
|
93
91
|
options?.log.trace('select: read "%s" for "%s"', response, protocol)
|
|
94
92
|
|
|
95
93
|
if (response === protocol) {
|
|
96
|
-
|
|
97
|
-
return { stream: shakeStream, protocol }
|
|
94
|
+
return { stream: lp.unwrap(), protocol }
|
|
98
95
|
}
|
|
99
96
|
}
|
|
100
97
|
|
|
101
|
-
rest()
|
|
102
98
|
throw new CodeError('protocol selection failed', 'ERR_UNSUPPORTED_PROTOCOL')
|
|
103
99
|
}
|
|
104
100
|
|
|
@@ -110,49 +106,51 @@ export async function select (stream: any, protocols: string | string[], options
|
|
|
110
106
|
*
|
|
111
107
|
* Use when it is known that the receiver supports the desired protocol.
|
|
112
108
|
*/
|
|
113
|
-
export function lazySelect (stream:
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
109
|
+
export function lazySelect <Stream extends Duplex<any, any, any>> (stream: Stream, protocol: string, options: MultistreamSelectInit): ProtocolStream<Stream> {
|
|
110
|
+
const originalSink = stream.sink.bind(stream)
|
|
111
|
+
const originalSource = stream.source
|
|
112
|
+
|
|
113
|
+
const lp = lpStream({
|
|
114
|
+
sink: originalSink,
|
|
115
|
+
source: originalSource
|
|
116
|
+
}, {
|
|
117
|
+
maxDataLength: MAX_PROTOCOL_LENGTH
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
stream.sink = async source => {
|
|
121
|
+
options?.log.trace('lazy: write ["%s", "%s"]', PROTOCOL_ID, protocol)
|
|
122
|
+
|
|
123
|
+
await lp.writeV([
|
|
124
|
+
uint8ArrayFromString(`${PROTOCOL_ID}\n`),
|
|
125
|
+
uint8ArrayFromString(`${protocol}\n`)
|
|
126
|
+
])
|
|
127
|
+
|
|
128
|
+
options?.log.trace('lazy: writing rest of "%s" stream', protocol)
|
|
129
|
+
await lp.unwrap().sink(source)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
stream.source = (async function * () {
|
|
133
|
+
options?.log.trace('lazy: reading multistream select header')
|
|
134
|
+
|
|
135
|
+
let response = await multistream.readString(lp, options)
|
|
136
|
+
options?.log.trace('lazy: read multistream select header "%s"', response)
|
|
137
|
+
|
|
138
|
+
if (response === PROTOCOL_ID) {
|
|
139
|
+
response = await multistream.readString(lp, options)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
options?.log.trace('lazy: read protocol "%s", expecting "%s"', response, protocol)
|
|
143
|
+
|
|
144
|
+
if (response !== protocol) {
|
|
145
|
+
throw new CodeError('protocol selection failed', 'ERR_UNSUPPORTED_PROTOCOL')
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
options?.log.trace('lazy: reading rest of "%s" stream', protocol)
|
|
149
|
+
yield * lp.unwrap().source
|
|
150
|
+
})()
|
|
151
|
+
|
|
120
152
|
return {
|
|
121
|
-
stream
|
|
122
|
-
sink: async source => {
|
|
123
|
-
await stream.sink((async function * () {
|
|
124
|
-
let first = true
|
|
125
|
-
for await (const chunk of merge(source, negotiateTrigger)) {
|
|
126
|
-
if (first) {
|
|
127
|
-
first = false
|
|
128
|
-
negotiated = true
|
|
129
|
-
negotiateTrigger.end()
|
|
130
|
-
const p1 = uint8ArrayFromString(PROTOCOL_ID)
|
|
131
|
-
const p2 = uint8ArrayFromString(protocol)
|
|
132
|
-
const list = new Uint8ArrayList(multistream.encode(p1), multistream.encode(p2))
|
|
133
|
-
if (chunk.length > 0) list.append(chunk)
|
|
134
|
-
yield * list
|
|
135
|
-
} else {
|
|
136
|
-
yield chunk
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
})())
|
|
140
|
-
},
|
|
141
|
-
source: (async function * () {
|
|
142
|
-
if (!negotiated) negotiateTrigger.push(new Uint8Array())
|
|
143
|
-
const byteReader = reader(stream.source)
|
|
144
|
-
let response = await multistream.readString(byteReader)
|
|
145
|
-
if (response === PROTOCOL_ID) {
|
|
146
|
-
response = await multistream.readString(byteReader)
|
|
147
|
-
}
|
|
148
|
-
if (response !== protocol) {
|
|
149
|
-
throw new CodeError('protocol selection failed', 'ERR_UNSUPPORTED_PROTOCOL')
|
|
150
|
-
}
|
|
151
|
-
for await (const chunk of byteReader) {
|
|
152
|
-
yield * chunk
|
|
153
|
-
}
|
|
154
|
-
})()
|
|
155
|
-
},
|
|
153
|
+
stream,
|
|
156
154
|
protocol
|
|
157
155
|
}
|
|
158
156
|
}
|