@libp2p/webrtc 3.2.11 → 4.0.0
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 -12
- 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 +33 -3
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/maconn.d.ts +8 -4
- package/dist/src/maconn.d.ts.map +1 -1
- package/dist/src/maconn.js +6 -6
- package/dist/src/maconn.js.map +1 -1
- package/dist/src/muxer.d.ts +13 -5
- package/dist/src/muxer.d.ts.map +1 -1
- package/dist/src/muxer.js +36 -7
- package/dist/src/muxer.js.map +1 -1
- package/dist/src/pb/message.js.map +1 -1
- package/dist/src/private-to-private/initiate-connection.d.ts +4 -5
- package/dist/src/private-to-private/initiate-connection.d.ts.map +1 -1
- package/dist/src/private-to-private/initiate-connection.js +4 -5
- package/dist/src/private-to-private/initiate-connection.js.map +1 -1
- package/dist/src/private-to-private/listener.d.ts +3 -4
- 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/pb/message.js.map +1 -1
- package/dist/src/private-to-private/signaling-stream-handler.d.ts +4 -2
- package/dist/src/private-to-private/signaling-stream-handler.d.ts.map +1 -1
- package/dist/src/private-to-private/signaling-stream-handler.js +4 -5
- package/dist/src/private-to-private/signaling-stream-handler.js.map +1 -1
- package/dist/src/private-to-private/transport.d.ts +6 -27
- package/dist/src/private-to-private/transport.d.ts.map +1 -1
- package/dist/src/private-to-private/transport.js +15 -14
- package/dist/src/private-to-private/transport.js.map +1 -1
- package/dist/src/private-to-private/util.d.ts +2 -2
- package/dist/src/private-to-private/util.d.ts.map +1 -1
- package/dist/src/private-to-private/util.js +26 -41
- package/dist/src/private-to-private/util.js.map +1 -1
- package/dist/src/private-to-public/options.d.ts +1 -1
- package/dist/src/private-to-public/options.d.ts.map +1 -1
- package/dist/src/private-to-public/sdp.d.ts +2 -1
- package/dist/src/private-to-public/sdp.d.ts.map +1 -1
- package/dist/src/private-to-public/sdp.js +3 -6
- package/dist/src/private-to-public/sdp.js.map +1 -1
- package/dist/src/private-to-public/transport.d.ts +5 -16
- package/dist/src/private-to-public/transport.d.ts.map +1 -1
- package/dist/src/private-to-public/transport.js +28 -17
- package/dist/src/private-to-public/transport.js.map +1 -1
- package/dist/src/stream.d.ts +10 -4
- package/dist/src/stream.d.ts.map +1 -1
- package/dist/src/stream.js +19 -33
- package/dist/src/stream.js.map +1 -1
- package/dist/src/util.d.ts +2 -1
- package/dist/src/util.d.ts.map +1 -1
- package/dist/src/util.js +4 -6
- package/dist/src/util.js.map +1 -1
- package/dist/src/webrtc/rtc-data-channel.js.map +1 -1
- package/dist/src/webrtc/rtc-ice-candidate.js.map +1 -1
- package/dist/src/webrtc/rtc-peer-connection.js.map +1 -1
- package/dist/typedoc-urls.json +8 -0
- package/package.json +25 -20
- package/src/error.ts +2 -2
- package/src/index.ts +40 -3
- package/src/maconn.ts +14 -10
- package/src/muxer.ts +57 -11
- package/src/private-to-private/initiate-connection.ts +7 -11
- package/src/private-to-private/listener.ts +3 -4
- package/src/private-to-private/signaling-stream-handler.ts +7 -7
- package/src/private-to-private/transport.ts +18 -43
- package/src/private-to-private/util.ts +30 -46
- package/src/private-to-public/options.ts +1 -1
- package/src/private-to-public/sdp.ts +4 -8
- package/src/private-to-public/transport.ts +30 -34
- package/src/stream.ts +27 -39
- package/src/util.ts +5 -7
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/webrtc",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "A libp2p transport using WebRTC connections",
|
|
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/main/packages/transport-webrtc#readme",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "git+https://github.com/libp2p/js-libp2p.git"
|
|
@@ -11,6 +11,10 @@
|
|
|
11
11
|
"bugs": {
|
|
12
12
|
"url": "https://github.com/libp2p/js-libp2p/issues"
|
|
13
13
|
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public",
|
|
16
|
+
"provenance": true
|
|
17
|
+
},
|
|
14
18
|
"type": "module",
|
|
15
19
|
"types": "./dist/src/index.d.ts",
|
|
16
20
|
"files": [
|
|
@@ -35,7 +39,7 @@
|
|
|
35
39
|
"scripts": {
|
|
36
40
|
"generate": "protons src/private-to-private/pb/message.proto src/pb/message.proto",
|
|
37
41
|
"build": "aegir build",
|
|
38
|
-
"test": "aegir test -t node -t browser
|
|
42
|
+
"test": "aegir test -t node -t browser -- --exit",
|
|
39
43
|
"test:node": "aegir test -t node --cov -- --exit",
|
|
40
44
|
"test:chrome": "aegir test -t browser --cov",
|
|
41
45
|
"test:firefox": "aegir test -t browser -- --browser firefox",
|
|
@@ -46,23 +50,23 @@
|
|
|
46
50
|
},
|
|
47
51
|
"dependencies": {
|
|
48
52
|
"@chainsafe/libp2p-noise": "^13.0.0",
|
|
49
|
-
"@libp2p/interface": "^0.
|
|
50
|
-
"@libp2p/interface-internal": "^0.1.
|
|
51
|
-
"@libp2p/logger": "^
|
|
52
|
-
"@libp2p/peer-id": "^
|
|
53
|
-
"@
|
|
54
|
-
"@multiformats/
|
|
55
|
-
"@multiformats/multiaddr
|
|
56
|
-
"
|
|
53
|
+
"@libp2p/interface": "^1.0.0",
|
|
54
|
+
"@libp2p/interface-internal": "^0.1.10",
|
|
55
|
+
"@libp2p/logger": "^4.0.0",
|
|
56
|
+
"@libp2p/peer-id": "^4.0.0",
|
|
57
|
+
"@libp2p/utils": "^5.0.0",
|
|
58
|
+
"@multiformats/mafmt": "^12.1.6",
|
|
59
|
+
"@multiformats/multiaddr": "^12.1.10",
|
|
60
|
+
"@multiformats/multiaddr-matcher": "^1.1.0",
|
|
57
61
|
"any-signal": "^4.1.1",
|
|
58
62
|
"detect-browser": "^5.3.0",
|
|
59
|
-
"it-length-prefixed": "^9.0.
|
|
63
|
+
"it-length-prefixed": "^9.0.3",
|
|
60
64
|
"it-pipe": "^3.0.1",
|
|
61
|
-
"it-protobuf-stream": "^1.0.
|
|
62
|
-
"it-pushable": "^3.2.
|
|
65
|
+
"it-protobuf-stream": "^1.0.2",
|
|
66
|
+
"it-pushable": "^3.2.1",
|
|
63
67
|
"it-stream-types": "^2.0.1",
|
|
64
68
|
"it-to-buffer": "^4.0.2",
|
|
65
|
-
"multiformats": "^12.
|
|
69
|
+
"multiformats": "^12.1.3",
|
|
66
70
|
"multihashes": "^4.0.3",
|
|
67
71
|
"node-datachannel": "^0.5.0-dev",
|
|
68
72
|
"p-defer": "^4.0.0",
|
|
@@ -75,9 +79,10 @@
|
|
|
75
79
|
},
|
|
76
80
|
"devDependencies": {
|
|
77
81
|
"@chainsafe/libp2p-yamux": "^5.0.0",
|
|
78
|
-
"@libp2p/
|
|
79
|
-
"@libp2p/
|
|
80
|
-
"@libp2p/
|
|
82
|
+
"@libp2p/circuit-relay-v2": "^1.0.0",
|
|
83
|
+
"@libp2p/interface-compliance-tests": "^5.0.0",
|
|
84
|
+
"@libp2p/peer-id-factory": "^3.0.9",
|
|
85
|
+
"@libp2p/websockets": "^8.0.0",
|
|
81
86
|
"@types/sinon": "^17.0.0",
|
|
82
87
|
"aegir": "^41.0.2",
|
|
83
88
|
"delay": "^6.0.0",
|
|
@@ -85,9 +90,9 @@
|
|
|
85
90
|
"it-length": "^3.0.2",
|
|
86
91
|
"it-map": "^3.0.3",
|
|
87
92
|
"it-pair": "^2.0.6",
|
|
88
|
-
"libp2p": "^0.
|
|
93
|
+
"libp2p": "^1.0.0",
|
|
89
94
|
"p-retry": "^6.1.0",
|
|
90
|
-
"protons": "^7.0
|
|
95
|
+
"protons": "^7.3.0",
|
|
91
96
|
"sinon": "^17.0.0",
|
|
92
97
|
"sinon-ts": "^2.0.0"
|
|
93
98
|
},
|
package/src/error.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { CodeError } from '@libp2p/interface
|
|
2
|
-
import type { Direction } from '@libp2p/interface
|
|
1
|
+
import { CodeError } from '@libp2p/interface'
|
|
2
|
+
import type { Direction } from '@libp2p/interface'
|
|
3
3
|
|
|
4
4
|
export enum codes {
|
|
5
5
|
ERR_ALREADY_ABORTED = 'ERR_ALREADY_ABORTED',
|
package/src/index.ts
CHANGED
|
@@ -30,9 +30,9 @@
|
|
|
30
30
|
*/
|
|
31
31
|
|
|
32
32
|
import { WebRTCTransport } from './private-to-private/transport.js'
|
|
33
|
-
import { WebRTCDirectTransport
|
|
34
|
-
import type {
|
|
35
|
-
import type {
|
|
33
|
+
import { WebRTCDirectTransport } from './private-to-public/transport.js'
|
|
34
|
+
import type { ComponentLogger, Metrics, PeerId, Transport, Upgrader } from '@libp2p/interface'
|
|
35
|
+
import type { ConnectionManager, Registrar, TransportManager } from '@libp2p/interface-internal'
|
|
36
36
|
|
|
37
37
|
export interface DataChannelOptions {
|
|
38
38
|
/**
|
|
@@ -66,6 +66,22 @@ export interface DataChannelOptions {
|
|
|
66
66
|
* controls how long we wait for the acknowledgement in ms (default: 5s)
|
|
67
67
|
*/
|
|
68
68
|
closeTimeout?: number
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* When sending the first data message, if the channel is not in the "open"
|
|
72
|
+
* state, wait this long for the "open" event to fire.
|
|
73
|
+
*/
|
|
74
|
+
openTimeout?: number
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface WebRTCDirectTransportComponents {
|
|
78
|
+
peerId: PeerId
|
|
79
|
+
metrics?: Metrics
|
|
80
|
+
logger: ComponentLogger
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface WebRTCTransportDirectInit {
|
|
84
|
+
dataChannel?: DataChannelOptions
|
|
69
85
|
}
|
|
70
86
|
|
|
71
87
|
/**
|
|
@@ -80,6 +96,27 @@ function webRTCDirect (init?: WebRTCTransportDirectInit): (components: WebRTCDir
|
|
|
80
96
|
return (components: WebRTCDirectTransportComponents) => new WebRTCDirectTransport(components, init)
|
|
81
97
|
}
|
|
82
98
|
|
|
99
|
+
export interface WebRTCTransportInit {
|
|
100
|
+
rtcConfiguration?: RTCConfiguration
|
|
101
|
+
dataChannel?: DataChannelOptions
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Inbound connections must complete the upgrade within this many ms
|
|
105
|
+
* (default: 30s)
|
|
106
|
+
*/
|
|
107
|
+
inboundConnectionTimeout?: number
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export interface WebRTCTransportComponents {
|
|
111
|
+
peerId: PeerId
|
|
112
|
+
registrar: Registrar
|
|
113
|
+
upgrader: Upgrader
|
|
114
|
+
transportManager: TransportManager
|
|
115
|
+
connectionManager: ConnectionManager
|
|
116
|
+
metrics?: Metrics
|
|
117
|
+
logger: ComponentLogger
|
|
118
|
+
}
|
|
119
|
+
|
|
83
120
|
/**
|
|
84
121
|
* @param {WebRTCTransportInit} init - WebRTC transport configuration
|
|
85
122
|
* @param {RTCConfiguration} init.rtcConfiguration - RTCConfiguration
|
package/src/maconn.ts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
import { logger } from '@libp2p/logger'
|
|
2
1
|
import { nopSink, nopSource } from './util.js'
|
|
3
|
-
import type { MultiaddrConnection, MultiaddrConnectionTimeline } from '@libp2p/interface
|
|
4
|
-
import type { CounterGroup } from '@libp2p/interface/metrics'
|
|
2
|
+
import type { ComponentLogger, Logger, MultiaddrConnection, MultiaddrConnectionTimeline, CounterGroup } from '@libp2p/interface'
|
|
5
3
|
import type { AbortOptions, Multiaddr } from '@multiformats/multiaddr'
|
|
6
4
|
import type { Source, Sink } from 'it-stream-types'
|
|
7
|
-
|
|
8
|
-
const log = logger('libp2p:webrtc:maconn')
|
|
5
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
9
6
|
|
|
10
7
|
interface WebRTCMultiaddrConnectionInit {
|
|
11
8
|
/**
|
|
@@ -29,7 +26,13 @@ interface WebRTCMultiaddrConnectionInit {
|
|
|
29
26
|
metrics?: CounterGroup
|
|
30
27
|
}
|
|
31
28
|
|
|
29
|
+
export interface WebRTCMultiaddrConnectionComponents {
|
|
30
|
+
logger: ComponentLogger
|
|
31
|
+
}
|
|
32
|
+
|
|
32
33
|
export class WebRTCMultiaddrConnection implements MultiaddrConnection {
|
|
34
|
+
readonly log: Logger
|
|
35
|
+
|
|
33
36
|
/**
|
|
34
37
|
* WebRTC Peer Connection
|
|
35
38
|
*/
|
|
@@ -58,9 +61,10 @@ export class WebRTCMultiaddrConnection implements MultiaddrConnection {
|
|
|
58
61
|
/**
|
|
59
62
|
* The stream destination, a no-op as the transport natively supports multiplexing
|
|
60
63
|
*/
|
|
61
|
-
sink: Sink<Source<Uint8Array>, Promise<void>> = nopSink
|
|
64
|
+
sink: Sink<Source<Uint8Array | Uint8ArrayList>, Promise<void>> = nopSink
|
|
62
65
|
|
|
63
|
-
constructor (init: WebRTCMultiaddrConnectionInit) {
|
|
66
|
+
constructor (components: WebRTCMultiaddrConnectionComponents, init: WebRTCMultiaddrConnectionInit) {
|
|
67
|
+
this.log = components.logger.forComponent('libp2p:webrtc:maconn')
|
|
64
68
|
this.remoteAddr = init.remoteAddr
|
|
65
69
|
this.timeline = init.timeline
|
|
66
70
|
this.peerConnection = init.peerConnection
|
|
@@ -68,7 +72,7 @@ export class WebRTCMultiaddrConnection implements MultiaddrConnection {
|
|
|
68
72
|
const initialState = this.peerConnection.connectionState
|
|
69
73
|
|
|
70
74
|
this.peerConnection.onconnectionstatechange = () => {
|
|
71
|
-
log.trace('peer connection state change', this.peerConnection.connectionState, 'initial state', initialState)
|
|
75
|
+
this.log.trace('peer connection state change', this.peerConnection.connectionState, 'initial state', initialState)
|
|
72
76
|
|
|
73
77
|
if (this.peerConnection.connectionState === 'disconnected' || this.peerConnection.connectionState === 'failed' || this.peerConnection.connectionState === 'closed') {
|
|
74
78
|
// nothing else to do but close the connection
|
|
@@ -78,7 +82,7 @@ export class WebRTCMultiaddrConnection implements MultiaddrConnection {
|
|
|
78
82
|
}
|
|
79
83
|
|
|
80
84
|
async close (options?: AbortOptions): Promise<void> {
|
|
81
|
-
log.trace('closing connection')
|
|
85
|
+
this.log.trace('closing connection')
|
|
82
86
|
|
|
83
87
|
this.peerConnection.close()
|
|
84
88
|
this.timeline.close = Date.now()
|
|
@@ -86,7 +90,7 @@ export class WebRTCMultiaddrConnection implements MultiaddrConnection {
|
|
|
86
90
|
}
|
|
87
91
|
|
|
88
92
|
abort (err: Error): void {
|
|
89
|
-
log.error('closing connection due to error', err)
|
|
93
|
+
this.log.error('closing connection due to error', err)
|
|
90
94
|
|
|
91
95
|
this.peerConnection.close()
|
|
92
96
|
this.timeline.close = Date.now()
|
package/src/muxer.ts
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
|
-
import { logger } from '@libp2p/logger'
|
|
2
1
|
import { createStream } from './stream.js'
|
|
3
2
|
import { drainAndClose, nopSink, nopSource } from './util.js'
|
|
4
3
|
import type { DataChannelOptions } from './index.js'
|
|
5
|
-
import type { Stream } from '@libp2p/interface
|
|
6
|
-
import type { CounterGroup } from '@libp2p/interface/metrics'
|
|
7
|
-
import type { StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface/stream-muxer'
|
|
4
|
+
import type { ComponentLogger, Logger, Stream, CounterGroup, StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface'
|
|
8
5
|
import type { AbortOptions } from '@multiformats/multiaddr'
|
|
9
6
|
import type { Source, Sink } from 'it-stream-types'
|
|
10
7
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
11
8
|
|
|
12
|
-
const log = logger('libp2p:webrtc:muxer')
|
|
13
|
-
|
|
14
9
|
const PROTOCOL = '/webrtc'
|
|
15
10
|
|
|
16
11
|
export interface DataChannelMuxerFactoryInit {
|
|
@@ -32,6 +27,10 @@ export interface DataChannelMuxerFactoryInit {
|
|
|
32
27
|
dataChannelOptions?: DataChannelOptions
|
|
33
28
|
}
|
|
34
29
|
|
|
30
|
+
export interface DataChannelMuxerFactoryComponents {
|
|
31
|
+
logger: ComponentLogger
|
|
32
|
+
}
|
|
33
|
+
|
|
35
34
|
interface BufferedStream {
|
|
36
35
|
stream: Stream
|
|
37
36
|
channel: RTCDataChannel
|
|
@@ -48,15 +47,29 @@ export class DataChannelMuxerFactory implements StreamMuxerFactory {
|
|
|
48
47
|
private bufferedStreams: BufferedStream[] = []
|
|
49
48
|
private readonly metrics?: CounterGroup
|
|
50
49
|
private readonly dataChannelOptions?: DataChannelOptions
|
|
50
|
+
private readonly components: DataChannelMuxerFactoryComponents
|
|
51
|
+
private readonly log: Logger
|
|
51
52
|
|
|
52
|
-
constructor (init: DataChannelMuxerFactoryInit) {
|
|
53
|
+
constructor (components: DataChannelMuxerFactoryComponents, init: DataChannelMuxerFactoryInit) {
|
|
54
|
+
this.components = components
|
|
53
55
|
this.peerConnection = init.peerConnection
|
|
54
56
|
this.metrics = init.metrics
|
|
55
57
|
this.protocol = init.protocol ?? PROTOCOL
|
|
56
58
|
this.dataChannelOptions = init.dataChannelOptions ?? {}
|
|
59
|
+
this.log = components.logger.forComponent('libp2p:webrtc:datachannelmuxerfactory')
|
|
57
60
|
|
|
58
61
|
// store any datachannels opened before upgrade has been completed
|
|
59
62
|
this.peerConnection.ondatachannel = ({ channel }) => {
|
|
63
|
+
this.log.trace('incoming early datachannel with channel id %d and label "%s"', channel.id)
|
|
64
|
+
|
|
65
|
+
// 'init' channel is only used during connection establishment
|
|
66
|
+
if (channel.label === 'init') {
|
|
67
|
+
this.log.trace('closing early init channel')
|
|
68
|
+
channel.close()
|
|
69
|
+
|
|
70
|
+
return
|
|
71
|
+
}
|
|
72
|
+
|
|
60
73
|
// @ts-expect-error fields are set below
|
|
61
74
|
const bufferedStream: BufferedStream = {}
|
|
62
75
|
|
|
@@ -66,6 +79,7 @@ export class DataChannelMuxerFactory implements StreamMuxerFactory {
|
|
|
66
79
|
onEnd: (err) => {
|
|
67
80
|
bufferedStream.onEnd(err)
|
|
68
81
|
},
|
|
82
|
+
logger: components.logger,
|
|
69
83
|
...this.dataChannelOptions
|
|
70
84
|
})
|
|
71
85
|
|
|
@@ -80,7 +94,7 @@ export class DataChannelMuxerFactory implements StreamMuxerFactory {
|
|
|
80
94
|
}
|
|
81
95
|
|
|
82
96
|
createStreamMuxer (init?: StreamMuxerInit): StreamMuxer {
|
|
83
|
-
return new DataChannelMuxer({
|
|
97
|
+
return new DataChannelMuxer(this.components, {
|
|
84
98
|
...init,
|
|
85
99
|
peerConnection: this.peerConnection,
|
|
86
100
|
dataChannelOptions: this.dataChannelOptions,
|
|
@@ -95,6 +109,10 @@ export interface DataChannelMuxerInit extends DataChannelMuxerFactoryInit, Strea
|
|
|
95
109
|
streams: BufferedStream[]
|
|
96
110
|
}
|
|
97
111
|
|
|
112
|
+
export interface DataChannelMuxerComponents {
|
|
113
|
+
logger: ComponentLogger
|
|
114
|
+
}
|
|
115
|
+
|
|
98
116
|
/**
|
|
99
117
|
* A libp2p data channel stream muxer
|
|
100
118
|
*/
|
|
@@ -105,11 +123,15 @@ export class DataChannelMuxer implements StreamMuxer {
|
|
|
105
123
|
public streams: Stream[]
|
|
106
124
|
public protocol: string
|
|
107
125
|
|
|
126
|
+
private readonly log: Logger
|
|
108
127
|
private readonly peerConnection: RTCPeerConnection
|
|
109
128
|
private readonly dataChannelOptions: DataChannelOptions
|
|
110
129
|
private readonly metrics?: CounterGroup
|
|
130
|
+
private readonly logger: ComponentLogger
|
|
111
131
|
|
|
112
|
-
constructor (readonly init: DataChannelMuxerInit) {
|
|
132
|
+
constructor (components: DataChannelMuxerComponents, readonly init: DataChannelMuxerInit) {
|
|
133
|
+
this.log = components.logger.forComponent('libp2p:webrtc:muxer')
|
|
134
|
+
this.logger = components.logger
|
|
113
135
|
this.streams = init.streams.map(s => s.stream)
|
|
114
136
|
this.peerConnection = init.peerConnection
|
|
115
137
|
this.protocol = init.protocol ?? PROTOCOL
|
|
@@ -123,12 +145,24 @@ export class DataChannelMuxer implements StreamMuxer {
|
|
|
123
145
|
* {@link https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/datachannel_event}
|
|
124
146
|
*/
|
|
125
147
|
this.peerConnection.ondatachannel = ({ channel }) => {
|
|
148
|
+
this.log.trace('incoming datachannel with channel id %d', channel.id)
|
|
149
|
+
|
|
150
|
+
// 'init' channel is only used during connection establishment
|
|
151
|
+
if (channel.label === 'init') {
|
|
152
|
+
this.log.trace('closing init channel')
|
|
153
|
+
channel.close()
|
|
154
|
+
|
|
155
|
+
return
|
|
156
|
+
}
|
|
157
|
+
|
|
126
158
|
const stream = createStream({
|
|
127
159
|
channel,
|
|
128
160
|
direction: 'inbound',
|
|
129
161
|
onEnd: () => {
|
|
162
|
+
this.log('incoming channel %s ended with state %s', channel.id, channel.readyState)
|
|
130
163
|
this.#onStreamEnd(stream, channel)
|
|
131
164
|
},
|
|
165
|
+
logger: this.logger,
|
|
132
166
|
...this.dataChannelOptions
|
|
133
167
|
})
|
|
134
168
|
|
|
@@ -147,6 +181,7 @@ export class DataChannelMuxer implements StreamMuxer {
|
|
|
147
181
|
queueMicrotask(() => {
|
|
148
182
|
this.init.streams.forEach(bufferedStream => {
|
|
149
183
|
bufferedStream.onEnd = () => {
|
|
184
|
+
this.log('incoming early channel %s ended with state %s', bufferedStream.channel.id, bufferedStream.channel.readyState)
|
|
150
185
|
this.#onStreamEnd(bufferedStream.stream, bufferedStream.channel)
|
|
151
186
|
}
|
|
152
187
|
|
|
@@ -158,8 +193,14 @@ export class DataChannelMuxer implements StreamMuxer {
|
|
|
158
193
|
}
|
|
159
194
|
|
|
160
195
|
#onStreamEnd (stream: Stream, channel: RTCDataChannel): void {
|
|
161
|
-
log.trace('stream %s %s %s onEnd', stream.direction, stream.id, stream.protocol)
|
|
162
|
-
drainAndClose(
|
|
196
|
+
this.log.trace('stream %s %s %s onEnd', stream.direction, stream.id, stream.protocol)
|
|
197
|
+
drainAndClose(
|
|
198
|
+
channel,
|
|
199
|
+
`${stream.direction} ${stream.id} ${stream.protocol}`,
|
|
200
|
+
this.dataChannelOptions.drainTimeout, {
|
|
201
|
+
log: this.log
|
|
202
|
+
}
|
|
203
|
+
)
|
|
163
204
|
this.streams = this.streams.filter(s => s.id !== stream.id)
|
|
164
205
|
this.metrics?.increment({ stream_end: true })
|
|
165
206
|
this.init?.onStreamEnd?.(stream)
|
|
@@ -200,12 +241,17 @@ export class DataChannelMuxer implements StreamMuxer {
|
|
|
200
241
|
newStream (): Stream {
|
|
201
242
|
// The spec says the label SHOULD be an empty string: https://github.com/libp2p/specs/blob/master/webrtc/README.md#rtcdatachannel-label
|
|
202
243
|
const channel = this.peerConnection.createDataChannel('')
|
|
244
|
+
|
|
245
|
+
this.log.trace('opened outgoing datachannel with channel id %s', channel.id)
|
|
246
|
+
|
|
203
247
|
const stream = createStream({
|
|
204
248
|
channel,
|
|
205
249
|
direction: 'outbound',
|
|
206
250
|
onEnd: () => {
|
|
251
|
+
this.log('outgoing channel %s ended with state %s', channel.id, channel.readyState)
|
|
207
252
|
this.#onStreamEnd(stream, channel)
|
|
208
253
|
},
|
|
254
|
+
logger: this.logger,
|
|
209
255
|
...this.dataChannelOptions
|
|
210
256
|
})
|
|
211
257
|
this.streams.push(stream)
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { CodeError } from '@libp2p/interface
|
|
2
|
-
import { logger } from '@libp2p/logger'
|
|
1
|
+
import { CodeError } from '@libp2p/interface'
|
|
3
2
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
4
3
|
import { pbStream } from 'it-protobuf-stream'
|
|
5
4
|
import pDefer, { type DeferredPromise } from 'p-defer'
|
|
@@ -8,21 +7,17 @@ import { Message } from './pb/message.js'
|
|
|
8
7
|
import { SIGNALING_PROTO_ID, splitAddr, type WebRTCTransportMetrics } from './transport.js'
|
|
9
8
|
import { readCandidatesUntilConnected, resolveOnConnected } from './util.js'
|
|
10
9
|
import type { DataChannelOptions } from '../index.js'
|
|
11
|
-
import type { Connection } from '@libp2p/interface
|
|
12
|
-
import type { ConnectionManager } from '@libp2p/interface-internal
|
|
13
|
-
import type { IncomingStreamData } from '@libp2p/interface-internal/registrar'
|
|
14
|
-
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
|
|
10
|
+
import type { LoggerOptions, Connection } from '@libp2p/interface'
|
|
11
|
+
import type { ConnectionManager, IncomingStreamData, TransportManager } from '@libp2p/interface-internal'
|
|
15
12
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
16
13
|
|
|
17
|
-
const log = logger('libp2p:webrtc:initiate-connection')
|
|
18
|
-
|
|
19
14
|
export interface IncomingStreamOpts extends IncomingStreamData {
|
|
20
15
|
rtcConfiguration?: RTCConfiguration
|
|
21
16
|
dataChannelOptions?: Partial<DataChannelOptions>
|
|
22
17
|
signal: AbortSignal
|
|
23
18
|
}
|
|
24
19
|
|
|
25
|
-
export interface ConnectOptions {
|
|
20
|
+
export interface ConnectOptions extends LoggerOptions {
|
|
26
21
|
peerConnection: RTCPeerConnection
|
|
27
22
|
multiaddr: Multiaddr
|
|
28
23
|
connectionManager: ConnectionManager
|
|
@@ -32,7 +27,7 @@ export interface ConnectOptions {
|
|
|
32
27
|
metrics?: WebRTCTransportMetrics
|
|
33
28
|
}
|
|
34
29
|
|
|
35
|
-
export async function initiateConnection ({ peerConnection, signal, metrics, multiaddr: ma, connectionManager, transportManager }: ConnectOptions): Promise<{ remoteAddress: Multiaddr }> {
|
|
30
|
+
export async function initiateConnection ({ peerConnection, signal, metrics, multiaddr: ma, connectionManager, transportManager, log }: ConnectOptions): Promise<{ remoteAddress: Multiaddr }> {
|
|
36
31
|
const { baseAddr } = splitAddr(ma)
|
|
37
32
|
|
|
38
33
|
metrics?.dialerEvents.increment({ open: true })
|
|
@@ -147,7 +142,8 @@ export async function initiateConnection ({ peerConnection, signal, metrics, mul
|
|
|
147
142
|
|
|
148
143
|
await readCandidatesUntilConnected(connectedPromise, peerConnection, messageStream, {
|
|
149
144
|
direction: 'initiator',
|
|
150
|
-
signal
|
|
145
|
+
signal,
|
|
146
|
+
log
|
|
151
147
|
})
|
|
152
148
|
|
|
153
149
|
log.trace('initiator connected, closing init channel')
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { TypedEventEmitter } from '@libp2p/interface
|
|
1
|
+
import { TypedEventEmitter } from '@libp2p/interface'
|
|
2
2
|
import { Circuit } from '@multiformats/mafmt'
|
|
3
|
-
import type { PeerId } from '@libp2p/interface
|
|
4
|
-
import type {
|
|
5
|
-
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
|
|
3
|
+
import type { PeerId, ListenerEvents, Listener } from '@libp2p/interface'
|
|
4
|
+
import type { TransportManager } from '@libp2p/interface-internal'
|
|
6
5
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
7
6
|
|
|
8
7
|
export interface WebRTCPeerListenerComponents {
|
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
import { CodeError } from '@libp2p/interface
|
|
2
|
-
import { logger } from '@libp2p/logger'
|
|
1
|
+
import { CodeError } from '@libp2p/interface'
|
|
3
2
|
import { multiaddr, type Multiaddr } from '@multiformats/multiaddr'
|
|
4
3
|
import { pbStream } from 'it-protobuf-stream'
|
|
5
4
|
import pDefer, { type DeferredPromise } from 'p-defer'
|
|
6
5
|
import { type RTCPeerConnection, RTCSessionDescription } from '../webrtc/index.js'
|
|
7
6
|
import { Message } from './pb/message.js'
|
|
8
7
|
import { readCandidatesUntilConnected, resolveOnConnected } from './util.js'
|
|
9
|
-
import type {
|
|
10
|
-
|
|
11
|
-
const log = logger('libp2p:webrtc:signaling-stream-handler')
|
|
8
|
+
import type { Logger } from '@libp2p/interface'
|
|
9
|
+
import type { IncomingStreamData } from '@libp2p/interface-internal'
|
|
12
10
|
|
|
13
11
|
export interface IncomingStreamOpts extends IncomingStreamData {
|
|
14
12
|
peerConnection: RTCPeerConnection
|
|
15
13
|
signal: AbortSignal
|
|
14
|
+
log: Logger
|
|
16
15
|
}
|
|
17
16
|
|
|
18
|
-
export async function handleIncomingStream ({ peerConnection, stream, signal, connection }: IncomingStreamOpts): Promise<{ remoteAddress: Multiaddr }> {
|
|
17
|
+
export async function handleIncomingStream ({ peerConnection, stream, signal, connection, log }: IncomingStreamOpts): Promise<{ remoteAddress: Multiaddr }> {
|
|
19
18
|
log.trace('new inbound signaling stream')
|
|
20
19
|
|
|
21
20
|
const messageStream = pbStream(stream).pb(Message)
|
|
@@ -104,7 +103,8 @@ export async function handleIncomingStream ({ peerConnection, stream, signal, co
|
|
|
104
103
|
// wait until candidates are connected
|
|
105
104
|
await readCandidatesUntilConnected(connectedPromise, peerConnection, messageStream, {
|
|
106
105
|
direction: 'recipient',
|
|
107
|
-
signal
|
|
106
|
+
signal,
|
|
107
|
+
log
|
|
108
108
|
})
|
|
109
109
|
|
|
110
110
|
log.trace('recipient connected, closing signaling stream')
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import { CodeError } from '@libp2p/interface
|
|
2
|
-
import { type CreateListenerOptions, type DialOptions, symbol, type Transport, type Listener, type Upgrader } from '@libp2p/interface/transport'
|
|
3
|
-
import { logger } from '@libp2p/logger'
|
|
1
|
+
import { CodeError, transportSymbol } from '@libp2p/interface'
|
|
4
2
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
5
3
|
import { multiaddr, type Multiaddr } from '@multiformats/multiaddr'
|
|
6
4
|
import { WebRTC } from '@multiformats/multiaddr-matcher'
|
|
@@ -11,48 +9,22 @@ import { cleanup, RTCPeerConnection } from '../webrtc/index.js'
|
|
|
11
9
|
import { initiateConnection } from './initiate-connection.js'
|
|
12
10
|
import { WebRTCPeerListener } from './listener.js'
|
|
13
11
|
import { handleIncomingStream } from './signaling-stream-handler.js'
|
|
14
|
-
import type {
|
|
15
|
-
import type { Connection } from '@libp2p/interface
|
|
16
|
-
import type {
|
|
17
|
-
import type { CounterGroup, Metrics } from '@libp2p/interface/src/metrics/index.js'
|
|
18
|
-
import type { Startable } from '@libp2p/interface/startable'
|
|
19
|
-
import type { IncomingStreamData, Registrar } from '@libp2p/interface-internal/registrar'
|
|
20
|
-
import type { ConnectionManager } from '@libp2p/interface-internal/src/connection-manager/index.js'
|
|
21
|
-
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
|
|
22
|
-
|
|
23
|
-
const log = logger('libp2p:webrtc:peer')
|
|
12
|
+
import type { WebRTCTransportComponents, WebRTCTransportInit } from '../index.js'
|
|
13
|
+
import type { Logger, Connection, CounterGroup, PeerId, Startable, CreateListenerOptions, DialOptions, Transport, Listener } from '@libp2p/interface'
|
|
14
|
+
import type { IncomingStreamData } from '@libp2p/interface-internal'
|
|
24
15
|
|
|
25
16
|
const WEBRTC_TRANSPORT = '/webrtc'
|
|
26
17
|
const CIRCUIT_RELAY_TRANSPORT = '/p2p-circuit'
|
|
27
18
|
export const SIGNALING_PROTO_ID = '/webrtc-signaling/0.0.1'
|
|
28
19
|
const INBOUND_CONNECTION_TIMEOUT = 30 * 1000
|
|
29
20
|
|
|
30
|
-
export interface WebRTCTransportInit {
|
|
31
|
-
rtcConfiguration?: RTCConfiguration
|
|
32
|
-
dataChannel?: DataChannelOptions
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Inbound connections must complete the upgrade within this many ms
|
|
36
|
-
* (default: 30s)
|
|
37
|
-
*/
|
|
38
|
-
inboundConnectionTimeout?: number
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export interface WebRTCTransportComponents {
|
|
42
|
-
peerId: PeerId
|
|
43
|
-
registrar: Registrar
|
|
44
|
-
upgrader: Upgrader
|
|
45
|
-
transportManager: TransportManager
|
|
46
|
-
connectionManager: ConnectionManager
|
|
47
|
-
metrics?: Metrics
|
|
48
|
-
}
|
|
49
|
-
|
|
50
21
|
export interface WebRTCTransportMetrics {
|
|
51
22
|
dialerEvents: CounterGroup
|
|
52
23
|
listenerEvents: CounterGroup
|
|
53
24
|
}
|
|
54
25
|
|
|
55
26
|
export class WebRTCTransport implements Transport, Startable {
|
|
27
|
+
private readonly log: Logger
|
|
56
28
|
private _started = false
|
|
57
29
|
private readonly metrics?: WebRTCTransportMetrics
|
|
58
30
|
private readonly shutdownController: AbortController
|
|
@@ -61,6 +33,7 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
61
33
|
private readonly components: WebRTCTransportComponents,
|
|
62
34
|
private readonly init: WebRTCTransportInit = {}
|
|
63
35
|
) {
|
|
36
|
+
this.log = components.logger.forComponent('libp2p:webrtc')
|
|
64
37
|
this.shutdownController = new AbortController()
|
|
65
38
|
|
|
66
39
|
if (components.metrics != null) {
|
|
@@ -83,7 +56,7 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
83
56
|
|
|
84
57
|
async start (): Promise<void> {
|
|
85
58
|
await this.components.registrar.handle(SIGNALING_PROTO_ID, (data: IncomingStreamData) => {
|
|
86
|
-
this._onProtocol(data).catch(err => { log.error('failed to handle incoming connect from %p', data.connection.remotePeer, err) })
|
|
59
|
+
this._onProtocol(data).catch(err => { this.log.error('failed to handle incoming connect from %p', data.connection.remotePeer, err) })
|
|
87
60
|
}, {
|
|
88
61
|
runOnTransientConnection: true
|
|
89
62
|
})
|
|
@@ -104,7 +77,7 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
104
77
|
|
|
105
78
|
readonly [Symbol.toStringTag] = '@libp2p/webrtc'
|
|
106
79
|
|
|
107
|
-
readonly [
|
|
80
|
+
readonly [transportSymbol] = true
|
|
108
81
|
|
|
109
82
|
filter (multiaddrs: Multiaddr[]): Multiaddr[] {
|
|
110
83
|
return multiaddrs.filter(WebRTC.exactMatch)
|
|
@@ -118,10 +91,10 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
118
91
|
* <relay address>/p2p/<relay-peer>/p2p-circuit/webrtc/p2p/<destination-peer>
|
|
119
92
|
*/
|
|
120
93
|
async dial (ma: Multiaddr, options: DialOptions): Promise<Connection> {
|
|
121
|
-
log.trace('dialing address: %a', ma)
|
|
94
|
+
this.log.trace('dialing address: %a', ma)
|
|
122
95
|
|
|
123
96
|
const peerConnection = new RTCPeerConnection(this.init.rtcConfiguration)
|
|
124
|
-
const muxerFactory = new DataChannelMuxerFactory({
|
|
97
|
+
const muxerFactory = new DataChannelMuxerFactory(this.components, {
|
|
125
98
|
peerConnection,
|
|
126
99
|
dataChannelOptions: this.init.dataChannel
|
|
127
100
|
})
|
|
@@ -132,10 +105,11 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
132
105
|
dataChannelOptions: this.init.dataChannel,
|
|
133
106
|
signal: options.signal,
|
|
134
107
|
connectionManager: this.components.connectionManager,
|
|
135
|
-
transportManager: this.components.transportManager
|
|
108
|
+
transportManager: this.components.transportManager,
|
|
109
|
+
log: this.log
|
|
136
110
|
})
|
|
137
111
|
|
|
138
|
-
const webRTCConn = new WebRTCMultiaddrConnection({
|
|
112
|
+
const webRTCConn = new WebRTCMultiaddrConnection(this.components, {
|
|
139
113
|
peerConnection,
|
|
140
114
|
timeline: { open: Date.now() },
|
|
141
115
|
remoteAddr: remoteAddress,
|
|
@@ -157,7 +131,7 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
157
131
|
async _onProtocol ({ connection, stream }: IncomingStreamData): Promise<void> {
|
|
158
132
|
const signal = AbortSignal.timeout(this.init.inboundConnectionTimeout ?? INBOUND_CONNECTION_TIMEOUT)
|
|
159
133
|
const peerConnection = new RTCPeerConnection(this.init.rtcConfiguration)
|
|
160
|
-
const muxerFactory = new DataChannelMuxerFactory({
|
|
134
|
+
const muxerFactory = new DataChannelMuxerFactory(this.components, {
|
|
161
135
|
peerConnection,
|
|
162
136
|
dataChannelOptions: this.init.dataChannel
|
|
163
137
|
})
|
|
@@ -167,10 +141,11 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
167
141
|
peerConnection,
|
|
168
142
|
connection,
|
|
169
143
|
stream,
|
|
170
|
-
signal
|
|
144
|
+
signal,
|
|
145
|
+
log: this.log
|
|
171
146
|
})
|
|
172
147
|
|
|
173
|
-
const webRTCConn = new WebRTCMultiaddrConnection({
|
|
148
|
+
const webRTCConn = new WebRTCMultiaddrConnection(this.components, {
|
|
174
149
|
peerConnection,
|
|
175
150
|
timeline: { open: (new Date()).getTime() },
|
|
176
151
|
remoteAddr: remoteAddress,
|
|
@@ -201,7 +176,7 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
201
176
|
const shutDownListener = (): void => {
|
|
202
177
|
webRTCConn.close()
|
|
203
178
|
.catch(err => {
|
|
204
|
-
log.error('could not close WebRTCMultiaddrConnection', err)
|
|
179
|
+
this.log.error('could not close WebRTCMultiaddrConnection', err)
|
|
205
180
|
})
|
|
206
181
|
}
|
|
207
182
|
|