@libp2p/webrtc 2.0.10-ab0e3980 → 2.0.10-b1024c6c
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/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 +9 -6
- 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 +5 -5
- 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/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/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 +17 -23
- 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 +12 -11
- package/src/private-to-public/options.ts +1 -1
- package/src/private-to-public/transport.ts +4 -4
- package/src/stream.ts +29 -16
package/dist/src/stream.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { CodeError } from '@libp2p/interface/errors';
|
|
2
|
+
import { AbstractStream } 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 { pushable } from 'it-pushable';
|
|
@@ -15,7 +15,7 @@ const MAX_BUFFERED_AMOUNT = 16 * 1024 * 1024;
|
|
|
15
15
|
const BUFFERED_AMOUNT_LOW_TIMEOUT = 30 * 1000;
|
|
16
16
|
// protobuf field definition overhead
|
|
17
17
|
const PROTOBUF_OVERHEAD = 3;
|
|
18
|
-
class WebRTCStream extends AbstractStream {
|
|
18
|
+
export class WebRTCStream extends AbstractStream {
|
|
19
19
|
/**
|
|
20
20
|
* The data channel used to send and receive data
|
|
21
21
|
*/
|
|
@@ -30,6 +30,7 @@ class WebRTCStream extends AbstractStream {
|
|
|
30
30
|
*/
|
|
31
31
|
incomingData;
|
|
32
32
|
messageQueue;
|
|
33
|
+
maxDataSize;
|
|
33
34
|
constructor(init) {
|
|
34
35
|
super(init);
|
|
35
36
|
this.channel = init.channel;
|
|
@@ -41,14 +42,15 @@ class WebRTCStream extends AbstractStream {
|
|
|
41
42
|
maxBufferedAmount: init.dataChannelOptions?.maxBufferedAmount ?? MAX_BUFFERED_AMOUNT,
|
|
42
43
|
maxMessageSize: init.dataChannelOptions?.maxMessageSize ?? MAX_MESSAGE_SIZE
|
|
43
44
|
};
|
|
45
|
+
this.maxDataSize = init.maxDataSize;
|
|
44
46
|
// set up initial state
|
|
45
47
|
switch (this.channel.readyState) {
|
|
46
48
|
case 'open':
|
|
47
49
|
break;
|
|
48
50
|
case 'closed':
|
|
49
51
|
case 'closing':
|
|
50
|
-
if (this.
|
|
51
|
-
this.
|
|
52
|
+
if (this.timeline.close === undefined || this.timeline.close === 0) {
|
|
53
|
+
this.timeline.close = Date.now();
|
|
52
54
|
}
|
|
53
55
|
break;
|
|
54
56
|
case 'connecting':
|
|
@@ -60,7 +62,7 @@ class WebRTCStream extends AbstractStream {
|
|
|
60
62
|
}
|
|
61
63
|
// handle RTCDataChannel events
|
|
62
64
|
this.channel.onopen = (_evt) => {
|
|
63
|
-
this.
|
|
65
|
+
this.timeline.open = new Date().getTime();
|
|
64
66
|
if (this.messageQueue != null) {
|
|
65
67
|
// send any queued messages
|
|
66
68
|
this._sendMessage(this.messageQueue)
|
|
@@ -71,7 +73,9 @@ class WebRTCStream extends AbstractStream {
|
|
|
71
73
|
}
|
|
72
74
|
};
|
|
73
75
|
this.channel.onclose = (_evt) => {
|
|
74
|
-
this.close()
|
|
76
|
+
void this.close().catch(err => {
|
|
77
|
+
log.error('error closing stream after channel closed', err);
|
|
78
|
+
});
|
|
75
79
|
};
|
|
76
80
|
this.channel.onerror = (evt) => {
|
|
77
81
|
const err = evt.error;
|
|
@@ -109,7 +113,6 @@ class WebRTCStream extends AbstractStream {
|
|
|
109
113
|
}
|
|
110
114
|
catch (err) {
|
|
111
115
|
if (err instanceof TimeoutError) {
|
|
112
|
-
this.abort(err);
|
|
113
116
|
throw new Error('Timed out waiting for DataChannel buffer to clear');
|
|
114
117
|
}
|
|
115
118
|
throw err;
|
|
@@ -137,9 +140,15 @@ class WebRTCStream extends AbstractStream {
|
|
|
137
140
|
}
|
|
138
141
|
}
|
|
139
142
|
async sendData(data) {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
+
data = data.sublist();
|
|
144
|
+
while (data.byteLength > 0) {
|
|
145
|
+
const toSend = Math.min(data.byteLength, this.maxDataSize);
|
|
146
|
+
const buf = data.subarray(0, toSend);
|
|
147
|
+
const msgbuf = Message.encode({ message: buf });
|
|
148
|
+
const sendbuf = lengthPrefixed.encode.single(msgbuf);
|
|
149
|
+
await this._sendMessage(sendbuf);
|
|
150
|
+
data.consume(toSend);
|
|
151
|
+
}
|
|
143
152
|
}
|
|
144
153
|
async sendReset() {
|
|
145
154
|
await this._sendFlag(Message.Flag.RESET);
|
|
@@ -159,7 +168,7 @@ class WebRTCStream extends AbstractStream {
|
|
|
159
168
|
if (message.flag === Message.Flag.FIN) {
|
|
160
169
|
// We should expect no more data from the remote, stop reading
|
|
161
170
|
this.incomingData.end();
|
|
162
|
-
this.
|
|
171
|
+
this.remoteCloseWrite();
|
|
163
172
|
}
|
|
164
173
|
if (message.flag === Message.Flag.RESET) {
|
|
165
174
|
// Stop reading and writing to the stream immediately
|
|
@@ -167,7 +176,7 @@ class WebRTCStream extends AbstractStream {
|
|
|
167
176
|
}
|
|
168
177
|
if (message.flag === Message.Flag.STOP_SENDING) {
|
|
169
178
|
// The remote has stopped reading
|
|
170
|
-
this.
|
|
179
|
+
this.remoteCloseRead();
|
|
171
180
|
}
|
|
172
181
|
}
|
|
173
182
|
return message.message;
|
|
@@ -187,7 +196,8 @@ export function createStream(options) {
|
|
|
187
196
|
maxDataSize: (dataChannelOptions?.maxMessageSize ?? MAX_MESSAGE_SIZE) - PROTOBUF_OVERHEAD,
|
|
188
197
|
dataChannelOptions,
|
|
189
198
|
onEnd,
|
|
190
|
-
channel
|
|
199
|
+
channel,
|
|
200
|
+
log: logger(`libp2p:mplex:stream:${direction}:${channel.id}`)
|
|
191
201
|
});
|
|
192
202
|
}
|
|
193
203
|
//# sourceMappingURL=stream.js.map
|
package/dist/src/stream.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM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;QACnE,SAAS;QACT,WAAW,EAAE,CAAC,kBAAkB,EAAE,cAAc,IAAI,gBAAgB,CAAC,GAAG,iBAAiB;QACzF,kBAAkB;QAClB,KAAK;QACL,OAAO;QACP,GAAG,EAAE,MAAM,CAAC,uBAAuB,SAAS,IAAI,OAAO,CAAC,EAAE,EAAE,CAAC;KAC9D,CAAC,CAAA;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/webrtc",
|
|
3
|
-
"version": "2.0.10-
|
|
3
|
+
"version": "2.0.10-b1024c6c",
|
|
4
4
|
"description": "A libp2p transport using WebRTC connections",
|
|
5
5
|
"author": "",
|
|
6
6
|
"license": "Apache-2.0 OR MIT",
|
|
@@ -41,52 +41,46 @@
|
|
|
41
41
|
"lint": "aegir lint",
|
|
42
42
|
"lint:fix": "aegir lint --fix",
|
|
43
43
|
"clean": "aegir clean",
|
|
44
|
-
"dep-check": "aegir dep-check
|
|
44
|
+
"dep-check": "aegir dep-check"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"@chainsafe/libp2p-noise": "^12.0.1",
|
|
48
|
-
"@libp2p/interface
|
|
49
|
-
"@libp2p/interface-
|
|
50
|
-
"@libp2p/
|
|
51
|
-
"@libp2p/
|
|
52
|
-
"@libp2p/interface-stream-muxer": "4.1.2-ab0e3980",
|
|
53
|
-
"@libp2p/interface-transport": "4.0.3-ab0e3980",
|
|
54
|
-
"@libp2p/interfaces": "3.3.2-ab0e3980",
|
|
55
|
-
"@libp2p/logger": "2.1.1-ab0e3980",
|
|
56
|
-
"@libp2p/peer-id": "2.0.3-ab0e3980",
|
|
48
|
+
"@libp2p/interface": "0.0.1-b1024c6c",
|
|
49
|
+
"@libp2p/interface-internal": "0.0.1-b1024c6c",
|
|
50
|
+
"@libp2p/logger": "2.1.1-b1024c6c",
|
|
51
|
+
"@libp2p/peer-id": "2.0.3-b1024c6c",
|
|
57
52
|
"@multiformats/mafmt": "^12.1.2",
|
|
58
53
|
"@multiformats/multiaddr": "^12.1.3",
|
|
59
54
|
"abortable-iterator": "^5.0.1",
|
|
60
55
|
"detect-browser": "^5.3.0",
|
|
61
56
|
"it-length-prefixed": "^9.0.1",
|
|
62
|
-
"it-
|
|
57
|
+
"it-protobuf-stream": "^1.0.0",
|
|
63
58
|
"it-pipe": "^3.0.1",
|
|
64
|
-
"it-pushable": "^3.
|
|
59
|
+
"it-pushable": "^3.2.0",
|
|
65
60
|
"it-stream-types": "^2.0.1",
|
|
66
61
|
"it-to-buffer": "^4.0.2",
|
|
67
|
-
"multiformats": "^
|
|
62
|
+
"multiformats": "^12.0.1",
|
|
68
63
|
"multihashes": "^4.0.3",
|
|
69
64
|
"p-defer": "^4.0.0",
|
|
70
65
|
"p-event": "^6.0.0",
|
|
71
66
|
"protons-runtime": "^5.0.0",
|
|
72
67
|
"uint8arraylist": "^2.4.3",
|
|
73
|
-
"uint8arrays": "^4.0.
|
|
68
|
+
"uint8arrays": "^4.0.4"
|
|
74
69
|
},
|
|
75
70
|
"devDependencies": {
|
|
76
|
-
"@chainsafe/libp2p-yamux": "^4.0.
|
|
77
|
-
"@libp2p/interface-
|
|
78
|
-
"@libp2p/
|
|
79
|
-
"@libp2p/
|
|
80
|
-
"@libp2p/websockets": "6.0.3-ab0e3980",
|
|
71
|
+
"@chainsafe/libp2p-yamux": "^4.0.0",
|
|
72
|
+
"@libp2p/interface-compliance-tests": "3.0.7-b1024c6c",
|
|
73
|
+
"@libp2p/peer-id-factory": "2.0.3-b1024c6c",
|
|
74
|
+
"@libp2p/websockets": "6.0.3-b1024c6c",
|
|
81
75
|
"@types/sinon": "^10.0.15",
|
|
82
|
-
"aegir": "^39.0.
|
|
76
|
+
"aegir": "^39.0.13",
|
|
83
77
|
"delay": "^6.0.0",
|
|
84
78
|
"it-length": "^3.0.2",
|
|
85
79
|
"it-map": "^3.0.3",
|
|
86
80
|
"it-pair": "^2.0.6",
|
|
87
|
-
"libp2p": "0.45.9-
|
|
81
|
+
"libp2p": "0.45.9-b1024c6c",
|
|
88
82
|
"protons": "^7.0.2",
|
|
89
|
-
"sinon": "^15.1.
|
|
83
|
+
"sinon": "^15.1.2",
|
|
90
84
|
"sinon-ts": "^1.0.0"
|
|
91
85
|
},
|
|
92
86
|
"typedoc": {
|
package/src/error.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { CodeError } from '@libp2p/
|
|
2
|
-
import type { Direction } from '@libp2p/interface
|
|
1
|
+
import { CodeError } from '@libp2p/interface/errors'
|
|
2
|
+
import type { Direction } from '@libp2p/interface/connection'
|
|
3
3
|
|
|
4
4
|
export enum codes {
|
|
5
5
|
ERR_ALREADY_ABORTED = 'ERR_ALREADY_ABORTED',
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { WebRTCTransport } from './private-to-private/transport.js'
|
|
2
2
|
import { WebRTCDirectTransport, type WebRTCTransportDirectInit, type WebRTCDirectTransportComponents } from './private-to-public/transport.js'
|
|
3
3
|
import type { WebRTCTransportComponents, WebRTCTransportInit } from './private-to-private/transport.js'
|
|
4
|
-
import type { Transport } from '@libp2p/interface
|
|
4
|
+
import type { Transport } from '@libp2p/interface/transport'
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* @param {WebRTCTransportDirectInit} init - WebRTC direct transport configuration
|
package/src/maconn.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { logger } from '@libp2p/logger'
|
|
2
2
|
import { nopSink, nopSource } from './util.js'
|
|
3
|
-
import type { MultiaddrConnection, MultiaddrConnectionTimeline } from '@libp2p/interface
|
|
4
|
-
import type { CounterGroup } from '@libp2p/interface
|
|
5
|
-
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
3
|
+
import type { MultiaddrConnection, MultiaddrConnectionTimeline } from '@libp2p/interface/connection'
|
|
4
|
+
import type { CounterGroup } from '@libp2p/interface/metrics'
|
|
5
|
+
import type { AbortOptions, Multiaddr } from '@multiformats/multiaddr'
|
|
6
6
|
import type { Source, Sink } from 'it-stream-types'
|
|
7
7
|
|
|
8
8
|
const log = logger('libp2p:webrtc:connection')
|
|
@@ -72,14 +72,19 @@ export class WebRTCMultiaddrConnection implements MultiaddrConnection {
|
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
async close (
|
|
76
|
-
if (err !== undefined) {
|
|
77
|
-
log.error('error closing connection', err)
|
|
78
|
-
}
|
|
75
|
+
async close (options?: AbortOptions): Promise<void> {
|
|
79
76
|
log.trace('closing connection')
|
|
80
77
|
|
|
81
|
-
this.timeline.close = Date.now()
|
|
82
78
|
this.peerConnection.close()
|
|
79
|
+
this.timeline.close = Date.now()
|
|
83
80
|
this.metrics?.increment({ close: true })
|
|
84
81
|
}
|
|
82
|
+
|
|
83
|
+
abort (err: Error): void {
|
|
84
|
+
log.error('closing connection due to error', err)
|
|
85
|
+
|
|
86
|
+
this.peerConnection.close()
|
|
87
|
+
this.timeline.close = Date.now()
|
|
88
|
+
this.metrics?.increment({ abort: true })
|
|
89
|
+
}
|
|
85
90
|
}
|
package/src/muxer.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { createStream } from './stream.js'
|
|
2
2
|
import { nopSink, nopSource } from './util.js'
|
|
3
3
|
import type { DataChannelOpts } from './stream.js'
|
|
4
|
-
import type { Stream } from '@libp2p/interface
|
|
5
|
-
import type { CounterGroup } from '@libp2p/interface
|
|
6
|
-
import type { StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface
|
|
4
|
+
import type { Stream } from '@libp2p/interface/connection'
|
|
5
|
+
import type { CounterGroup } from '@libp2p/interface/metrics'
|
|
6
|
+
import type { StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface/stream-muxer'
|
|
7
|
+
import type { AbortOptions } from '@multiformats/multiaddr'
|
|
7
8
|
import type { Source, Sink } from 'it-stream-types'
|
|
8
9
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
9
10
|
|
|
@@ -93,9 +94,14 @@ export class DataChannelMuxer implements StreamMuxer {
|
|
|
93
94
|
private readonly metrics?: CounterGroup
|
|
94
95
|
|
|
95
96
|
/**
|
|
96
|
-
*
|
|
97
|
+
* Gracefully close all tracked streams and stop the muxer
|
|
97
98
|
*/
|
|
98
|
-
close: (
|
|
99
|
+
close: (options?: AbortOptions) => Promise<void> = async () => { }
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Abort all tracked streams and stop the muxer
|
|
103
|
+
*/
|
|
104
|
+
abort: (err: Error) => void = () => { }
|
|
99
105
|
|
|
100
106
|
/**
|
|
101
107
|
* The stream source, a no-op as the transport natively supports multiplexing
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { logger } from '@libp2p/logger'
|
|
2
2
|
import { abortableDuplex } from 'abortable-iterator'
|
|
3
|
-
import { pbStream } from 'it-
|
|
3
|
+
import { pbStream } from 'it-protobuf-stream'
|
|
4
4
|
import pDefer, { type DeferredPromise } from 'p-defer'
|
|
5
5
|
import { DataChannelMuxerFactory } from '../muxer.js'
|
|
6
6
|
import { Message } from './pb/message.js'
|
|
7
7
|
import { readCandidatesUntilConnected, resolveOnConnected } from './util.js'
|
|
8
8
|
import type { DataChannelOpts } from '../stream.js'
|
|
9
|
-
import type { Stream } from '@libp2p/interface
|
|
10
|
-
import type {
|
|
11
|
-
import type {
|
|
9
|
+
import type { Stream } from '@libp2p/interface/connection'
|
|
10
|
+
import type { StreamMuxerFactory } from '@libp2p/interface/stream-muxer'
|
|
11
|
+
import type { IncomingStreamData } from '@libp2p/interface-internal/registrar'
|
|
12
12
|
|
|
13
13
|
const DEFAULT_TIMEOUT = 30 * 1000
|
|
14
14
|
|
|
@@ -28,8 +28,8 @@ export async function handleIncomingStream ({ rtcConfiguration, dataChannelOptio
|
|
|
28
28
|
// candidate callbacks
|
|
29
29
|
pc.onicecandidate = ({ candidate }) => {
|
|
30
30
|
answerSentPromise.promise.then(
|
|
31
|
-
() => {
|
|
32
|
-
stream.write({
|
|
31
|
+
async () => {
|
|
32
|
+
await stream.write({
|
|
33
33
|
type: Message.Type.ICE_CANDIDATE,
|
|
34
34
|
data: (candidate != null) ? JSON.stringify(candidate.toJSON()) : ''
|
|
35
35
|
})
|
|
@@ -64,7 +64,7 @@ export async function handleIncomingStream ({ rtcConfiguration, dataChannelOptio
|
|
|
64
64
|
throw new Error('Failed to create answer')
|
|
65
65
|
})
|
|
66
66
|
// write the answer to the remote
|
|
67
|
-
stream.write({ type: Message.Type.SDP_ANSWER, data: answer.sdp })
|
|
67
|
+
await stream.write({ type: Message.Type.SDP_ANSWER, data: answer.sdp })
|
|
68
68
|
|
|
69
69
|
await pc.setLocalDescription(answer).catch(err => {
|
|
70
70
|
log.error('could not execute setLocalDescription', err)
|
|
@@ -107,15 +107,18 @@ export async function initiateConnection ({ rtcConfiguration, dataChannelOptions
|
|
|
107
107
|
// setup callback to write ICE candidates to the remote
|
|
108
108
|
// peer
|
|
109
109
|
pc.onicecandidate = ({ candidate }) => {
|
|
110
|
-
stream.write({
|
|
110
|
+
void stream.write({
|
|
111
111
|
type: Message.Type.ICE_CANDIDATE,
|
|
112
112
|
data: (candidate != null) ? JSON.stringify(candidate.toJSON()) : ''
|
|
113
113
|
})
|
|
114
|
+
.catch(err => {
|
|
115
|
+
log.error('error sending ICE candidate', err)
|
|
116
|
+
})
|
|
114
117
|
}
|
|
115
118
|
// create an offer
|
|
116
119
|
const offerSdp = await pc.createOffer()
|
|
117
120
|
// write the offer to the stream
|
|
118
|
-
stream.write({ type: Message.Type.SDP_OFFER, data: offerSdp.sdp })
|
|
121
|
+
await stream.write({ type: Message.Type.SDP_OFFER, data: offerSdp.sdp })
|
|
119
122
|
// set offer as local description
|
|
120
123
|
await pc.setLocalDescription(offerSdp).catch(err => {
|
|
121
124
|
log.error('could not execute setLocalDescription', err)
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { EventEmitter } from '@libp2p/
|
|
1
|
+
import { EventEmitter } from '@libp2p/interface/events'
|
|
2
2
|
import { Circuit } from '@multiformats/mafmt'
|
|
3
|
-
import type { PeerId } from '@libp2p/interface
|
|
4
|
-
import type { ListenerEvents, Listener
|
|
3
|
+
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
4
|
+
import type { ListenerEvents, Listener } from '@libp2p/interface/transport'
|
|
5
|
+
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
|
|
5
6
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
6
7
|
|
|
7
8
|
export interface ListenerOptions {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { CodeError } from '@libp2p/interface/errors'
|
|
2
|
+
import { type CreateListenerOptions, type DialOptions, symbol, type Transport, type Listener, type Upgrader } from '@libp2p/interface/transport'
|
|
3
3
|
import { logger } from '@libp2p/logger'
|
|
4
4
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
5
5
|
import { multiaddr, type Multiaddr, protocols } from '@multiformats/multiaddr'
|
|
@@ -8,10 +8,11 @@ import { WebRTCMultiaddrConnection } from '../maconn.js'
|
|
|
8
8
|
import { initiateConnection, handleIncomingStream } from './handler.js'
|
|
9
9
|
import { WebRTCPeerListener } from './listener.js'
|
|
10
10
|
import type { DataChannelOpts } from '../stream.js'
|
|
11
|
-
import type { Connection } from '@libp2p/interface
|
|
12
|
-
import type { PeerId } from '@libp2p/interface
|
|
13
|
-
import type {
|
|
14
|
-
import type {
|
|
11
|
+
import type { Connection } from '@libp2p/interface/connection'
|
|
12
|
+
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
13
|
+
import type { Startable } from '@libp2p/interface/startable'
|
|
14
|
+
import type { IncomingStreamData, Registrar } from '@libp2p/interface-internal/registrar'
|
|
15
|
+
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
|
|
15
16
|
|
|
16
17
|
const log = logger('libp2p:webrtc:peer')
|
|
17
18
|
|
|
@@ -113,11 +114,11 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
113
114
|
)
|
|
114
115
|
|
|
115
116
|
// close the stream if SDP has been exchanged successfully
|
|
116
|
-
signalingStream.close()
|
|
117
|
+
await signalingStream.close()
|
|
117
118
|
return result
|
|
118
|
-
} catch (err) {
|
|
119
|
+
} catch (err: any) {
|
|
119
120
|
// reset the stream in case of any error
|
|
120
|
-
signalingStream.
|
|
121
|
+
signalingStream.abort(err)
|
|
121
122
|
throw err
|
|
122
123
|
} finally {
|
|
123
124
|
// Close the signaling connection
|
|
@@ -143,8 +144,8 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
143
144
|
skipProtection: true,
|
|
144
145
|
muxerFactory
|
|
145
146
|
})
|
|
146
|
-
} catch (err) {
|
|
147
|
-
stream.
|
|
147
|
+
} catch (err: any) {
|
|
148
|
+
stream.abort(err)
|
|
148
149
|
throw err
|
|
149
150
|
} finally {
|
|
150
151
|
// Close the signaling connection
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CreateListenerOptions, DialOptions } from '@libp2p/interface
|
|
1
|
+
import type { CreateListenerOptions, DialOptions } from '@libp2p/interface/transport'
|
|
2
2
|
|
|
3
3
|
export interface WebRTCListenerOptions extends CreateListenerOptions {}
|
|
4
4
|
export interface WebRTCDialOptions extends DialOptions {}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { noise as Noise } from '@chainsafe/libp2p-noise'
|
|
2
|
-
import { type CreateListenerOptions,
|
|
2
|
+
import { type CreateListenerOptions, symbol, type Transport, type Listener } from '@libp2p/interface/transport'
|
|
3
3
|
import { logger } from '@libp2p/logger'
|
|
4
4
|
import * as p from '@libp2p/peer-id'
|
|
5
5
|
import { protocols } from '@multiformats/multiaddr'
|
|
@@ -15,9 +15,9 @@ import * as sdp from './sdp.js'
|
|
|
15
15
|
import { genUfrag } from './util.js'
|
|
16
16
|
import type { WebRTCDialOptions } from './options.js'
|
|
17
17
|
import type { DataChannelOpts } from '../stream.js'
|
|
18
|
-
import type { Connection } from '@libp2p/interface
|
|
19
|
-
import type { CounterGroup, Metrics } from '@libp2p/interface
|
|
20
|
-
import type { PeerId } from '@libp2p/interface
|
|
18
|
+
import type { Connection } from '@libp2p/interface/connection'
|
|
19
|
+
import type { CounterGroup, Metrics } from '@libp2p/interface/metrics'
|
|
20
|
+
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
21
21
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
22
22
|
|
|
23
23
|
const log = logger('libp2p:webrtc:transport')
|
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
|
}
|