@libp2p/webrtc 2.0.11 → 3.0.0-fdd80820
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 +7 -2
- package/dist/index.min.js +17 -17
- package/dist/src/error.d.ts +2 -2
- package/dist/src/error.d.ts.map +1 -1
- package/dist/src/error.js +1 -1
- package/dist/src/error.js.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/maconn.d.ts +5 -4
- package/dist/src/maconn.d.ts.map +1 -1
- package/dist/src/maconn.js +8 -5
- package/dist/src/maconn.js.map +1 -1
- package/dist/src/muxer.d.ts +10 -5
- package/dist/src/muxer.d.ts.map +1 -1
- package/dist/src/muxer.js +6 -2
- package/dist/src/muxer.js.map +1 -1
- package/dist/src/private-to-private/handler.d.ts +3 -3
- package/dist/src/private-to-private/handler.d.ts.map +1 -1
- package/dist/src/private-to-private/handler.js +10 -7
- package/dist/src/private-to-private/handler.js.map +1 -1
- package/dist/src/private-to-private/listener.d.ts +4 -3
- package/dist/src/private-to-private/listener.d.ts.map +1 -1
- package/dist/src/private-to-private/listener.js +1 -1
- package/dist/src/private-to-private/listener.js.map +1 -1
- package/dist/src/private-to-private/transport.d.ts +6 -5
- package/dist/src/private-to-private/transport.d.ts.map +1 -1
- package/dist/src/private-to-private/transport.js +11 -6
- package/dist/src/private-to-private/transport.js.map +1 -1
- package/dist/src/private-to-public/options.d.ts +1 -1
- package/dist/src/private-to-public/sdp.js +1 -1
- package/dist/src/private-to-public/transport.d.ts +4 -4
- package/dist/src/private-to-public/transport.d.ts.map +1 -1
- package/dist/src/private-to-public/transport.js +1 -1
- package/dist/src/private-to-public/transport.js.map +1 -1
- package/dist/src/private-to-public/util.d.ts.map +1 -1
- package/dist/src/private-to-public/util.js.map +1 -1
- package/dist/src/stream.d.ts +34 -3
- package/dist/src/stream.d.ts.map +1 -1
- package/dist/src/stream.js +24 -14
- package/dist/src/stream.js.map +1 -1
- package/package.json +23 -120
- package/src/error.ts +2 -2
- package/src/index.ts +1 -1
- package/src/maconn.ts +13 -8
- package/src/muxer.ts +11 -5
- package/src/private-to-private/handler.ts +12 -9
- package/src/private-to-private/listener.ts +4 -3
- package/src/private-to-private/transport.ts +18 -12
- package/src/private-to-public/options.ts +1 -1
- package/src/private-to-public/sdp.ts +1 -1
- package/src/private-to-public/transport.ts +4 -4
- package/src/private-to-public/util.ts +0 -1
- package/src/stream.ts +29 -16
package/src/stream.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { CodeError } from '@libp2p/interface/errors'
|
|
2
|
+
import { AbstractStream, type AbstractStreamInit } from '@libp2p/interface/stream-muxer/stream'
|
|
3
3
|
import { logger } from '@libp2p/logger'
|
|
4
4
|
import * as lengthPrefixed from 'it-length-prefixed'
|
|
5
5
|
import { type Pushable, pushable } from 'it-pushable'
|
|
6
6
|
import { pEvent, TimeoutError } from 'p-event'
|
|
7
7
|
import { Uint8ArrayList } from 'uint8arraylist'
|
|
8
8
|
import { Message } from './pb/message.js'
|
|
9
|
-
import type { Direction
|
|
9
|
+
import type { Direction } from '@libp2p/interface/connection'
|
|
10
10
|
|
|
11
11
|
const log = logger('libp2p:webrtc:stream')
|
|
12
12
|
|
|
@@ -26,6 +26,8 @@ export interface WebRTCStreamInit extends AbstractStreamInit {
|
|
|
26
26
|
channel: RTCDataChannel
|
|
27
27
|
|
|
28
28
|
dataChannelOptions?: Partial<DataChannelOpts>
|
|
29
|
+
|
|
30
|
+
maxDataSize: number
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
// Max message size that can be sent to the DataChannel
|
|
@@ -40,7 +42,7 @@ const BUFFERED_AMOUNT_LOW_TIMEOUT = 30 * 1000
|
|
|
40
42
|
// protobuf field definition overhead
|
|
41
43
|
const PROTOBUF_OVERHEAD = 3
|
|
42
44
|
|
|
43
|
-
class WebRTCStream extends AbstractStream {
|
|
45
|
+
export class WebRTCStream extends AbstractStream {
|
|
44
46
|
/**
|
|
45
47
|
* The data channel used to send and receive data
|
|
46
48
|
*/
|
|
@@ -58,6 +60,7 @@ class WebRTCStream extends AbstractStream {
|
|
|
58
60
|
private readonly incomingData: Pushable<Uint8Array>
|
|
59
61
|
|
|
60
62
|
private messageQueue?: Uint8ArrayList
|
|
63
|
+
private readonly maxDataSize: number
|
|
61
64
|
|
|
62
65
|
constructor (init: WebRTCStreamInit) {
|
|
63
66
|
super(init)
|
|
@@ -71,6 +74,7 @@ class WebRTCStream extends AbstractStream {
|
|
|
71
74
|
maxBufferedAmount: init.dataChannelOptions?.maxBufferedAmount ?? MAX_BUFFERED_AMOUNT,
|
|
72
75
|
maxMessageSize: init.dataChannelOptions?.maxMessageSize ?? MAX_MESSAGE_SIZE
|
|
73
76
|
}
|
|
77
|
+
this.maxDataSize = init.maxDataSize
|
|
74
78
|
|
|
75
79
|
// set up initial state
|
|
76
80
|
switch (this.channel.readyState) {
|
|
@@ -79,8 +83,8 @@ class WebRTCStream extends AbstractStream {
|
|
|
79
83
|
|
|
80
84
|
case 'closed':
|
|
81
85
|
case 'closing':
|
|
82
|
-
if (this.
|
|
83
|
-
this.
|
|
86
|
+
if (this.timeline.close === undefined || this.timeline.close === 0) {
|
|
87
|
+
this.timeline.close = Date.now()
|
|
84
88
|
}
|
|
85
89
|
break
|
|
86
90
|
case 'connecting':
|
|
@@ -94,7 +98,7 @@ class WebRTCStream extends AbstractStream {
|
|
|
94
98
|
|
|
95
99
|
// handle RTCDataChannel events
|
|
96
100
|
this.channel.onopen = (_evt) => {
|
|
97
|
-
this.
|
|
101
|
+
this.timeline.open = new Date().getTime()
|
|
98
102
|
|
|
99
103
|
if (this.messageQueue != null) {
|
|
100
104
|
// send any queued messages
|
|
@@ -107,7 +111,9 @@ class WebRTCStream extends AbstractStream {
|
|
|
107
111
|
}
|
|
108
112
|
|
|
109
113
|
this.channel.onclose = (_evt) => {
|
|
110
|
-
this.close()
|
|
114
|
+
void this.close().catch(err => {
|
|
115
|
+
log.error('error closing stream after channel closed', err)
|
|
116
|
+
})
|
|
111
117
|
}
|
|
112
118
|
|
|
113
119
|
this.channel.onerror = (evt) => {
|
|
@@ -153,7 +159,6 @@ class WebRTCStream extends AbstractStream {
|
|
|
153
159
|
await pEvent(this.channel, 'bufferedamountlow', { timeout: this.dataChannelOptions.bufferedAmountLowEventTimeout })
|
|
154
160
|
} catch (err: any) {
|
|
155
161
|
if (err instanceof TimeoutError) {
|
|
156
|
-
this.abort(err)
|
|
157
162
|
throw new Error('Timed out waiting for DataChannel buffer to clear')
|
|
158
163
|
}
|
|
159
164
|
|
|
@@ -184,10 +189,17 @@ class WebRTCStream extends AbstractStream {
|
|
|
184
189
|
}
|
|
185
190
|
|
|
186
191
|
async sendData (data: Uint8ArrayList): Promise<void> {
|
|
187
|
-
|
|
188
|
-
const sendbuf = lengthPrefixed.encode.single(msgbuf)
|
|
192
|
+
data = data.sublist()
|
|
189
193
|
|
|
190
|
-
|
|
194
|
+
while (data.byteLength > 0) {
|
|
195
|
+
const toSend = Math.min(data.byteLength, this.maxDataSize)
|
|
196
|
+
const buf = data.subarray(0, toSend)
|
|
197
|
+
const msgbuf = Message.encode({ message: buf })
|
|
198
|
+
const sendbuf = lengthPrefixed.encode.single(msgbuf)
|
|
199
|
+
await this._sendMessage(sendbuf)
|
|
200
|
+
|
|
201
|
+
data.consume(toSend)
|
|
202
|
+
}
|
|
191
203
|
}
|
|
192
204
|
|
|
193
205
|
async sendReset (): Promise<void> {
|
|
@@ -212,7 +224,7 @@ class WebRTCStream extends AbstractStream {
|
|
|
212
224
|
if (message.flag === Message.Flag.FIN) {
|
|
213
225
|
// We should expect no more data from the remote, stop reading
|
|
214
226
|
this.incomingData.end()
|
|
215
|
-
this.
|
|
227
|
+
this.remoteCloseWrite()
|
|
216
228
|
}
|
|
217
229
|
|
|
218
230
|
if (message.flag === Message.Flag.RESET) {
|
|
@@ -222,7 +234,7 @@ class WebRTCStream extends AbstractStream {
|
|
|
222
234
|
|
|
223
235
|
if (message.flag === Message.Flag.STOP_SENDING) {
|
|
224
236
|
// The remote has stopped reading
|
|
225
|
-
this.
|
|
237
|
+
this.remoteCloseRead()
|
|
226
238
|
}
|
|
227
239
|
}
|
|
228
240
|
|
|
@@ -259,7 +271,7 @@ export interface WebRTCStreamOptions {
|
|
|
259
271
|
onEnd?: (err?: Error | undefined) => void
|
|
260
272
|
}
|
|
261
273
|
|
|
262
|
-
export function createStream (options: WebRTCStreamOptions):
|
|
274
|
+
export function createStream (options: WebRTCStreamOptions): WebRTCStream {
|
|
263
275
|
const { channel, direction, onEnd, dataChannelOptions } = options
|
|
264
276
|
|
|
265
277
|
return new WebRTCStream({
|
|
@@ -268,6 +280,7 @@ export function createStream (options: WebRTCStreamOptions): Stream {
|
|
|
268
280
|
maxDataSize: (dataChannelOptions?.maxMessageSize ?? MAX_MESSAGE_SIZE) - PROTOBUF_OVERHEAD,
|
|
269
281
|
dataChannelOptions,
|
|
270
282
|
onEnd,
|
|
271
|
-
channel
|
|
283
|
+
channel,
|
|
284
|
+
log: logger(`libp2p:mplex:stream:${direction}:${channel.id}`)
|
|
272
285
|
})
|
|
273
286
|
}
|