@libp2p/webrtc 4.0.34 → 4.1.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.
@@ -1,4 +1,4 @@
1
- import { CodeError, setMaxListeners } from '@libp2p/interface'
1
+ import { CodeError, serviceCapabilities, serviceDependencies, setMaxListeners } from '@libp2p/interface'
2
2
  import { type CreateListenerOptions, type DialOptions, transportSymbol, type Transport, type Listener, type Upgrader, type ComponentLogger, type Logger, type Connection, type PeerId, type CounterGroup, type Metrics, type Startable } from '@libp2p/interface'
3
3
  import { peerIdFromString } from '@libp2p/peer-id'
4
4
  import { multiaddr, type Multiaddr } from '@multiformats/multiaddr'
@@ -19,7 +19,7 @@ export const SIGNALING_PROTO_ID = '/webrtc-signaling/0.0.1'
19
19
  const INBOUND_CONNECTION_TIMEOUT = 30 * 1000
20
20
 
21
21
  export interface WebRTCTransportInit {
22
- rtcConfiguration?: RTCConfiguration
22
+ rtcConfiguration?: RTCConfiguration | (() => RTCConfiguration | Promise<RTCConfiguration>)
23
23
  dataChannel?: DataChannelOptions
24
24
 
25
25
  /**
@@ -72,6 +72,19 @@ export class WebRTCTransport implements Transport, Startable {
72
72
  }
73
73
  }
74
74
 
75
+ readonly [transportSymbol] = true
76
+
77
+ readonly [Symbol.toStringTag] = '@libp2p/webrtc'
78
+
79
+ readonly [serviceCapabilities]: string[] = [
80
+ '@libp2p/transport'
81
+ ]
82
+
83
+ readonly [serviceDependencies]: string[] = [
84
+ '@libp2p/identify',
85
+ '@libp2p/circuit-relay-v2-transport'
86
+ ]
87
+
75
88
  isStarted (): boolean {
76
89
  return this._started
77
90
  }
@@ -96,10 +109,6 @@ export class WebRTCTransport implements Transport, Startable {
96
109
  })
97
110
  }
98
111
 
99
- readonly [Symbol.toStringTag] = '@libp2p/webrtc'
100
-
101
- readonly [transportSymbol] = true
102
-
103
112
  /**
104
113
  * Filter check for all Multiaddrs that this transport can listen on
105
114
  */
@@ -124,20 +133,16 @@ export class WebRTCTransport implements Transport, Startable {
124
133
  async dial (ma: Multiaddr, options: DialOptions): Promise<Connection> {
125
134
  this.log.trace('dialing address: %a', ma)
126
135
 
127
- const peerConnection = new RTCPeerConnection(this.init.rtcConfiguration)
128
- const muxerFactory = new DataChannelMuxerFactory(this.components, {
129
- peerConnection,
130
- dataChannelOptions: this.init.dataChannel
131
- })
132
-
133
- const { remoteAddress } = await initiateConnection({
134
- peerConnection,
136
+ const { remoteAddress, peerConnection, muxerFactory } = await initiateConnection({
137
+ rtcConfiguration: typeof this.init.rtcConfiguration === 'function' ? await this.init.rtcConfiguration() : this.init.rtcConfiguration,
138
+ dataChannel: this.init.dataChannel,
135
139
  multiaddr: ma,
136
140
  dataChannelOptions: this.init.dataChannel,
137
141
  signal: options.signal,
138
142
  connectionManager: this.components.connectionManager,
139
143
  transportManager: this.components.transportManager,
140
- log: this.log
144
+ log: this.log,
145
+ logger: this.components.logger
141
146
  })
142
147
 
143
148
  const webRTCConn = new WebRTCMultiaddrConnection(this.components, {
@@ -161,7 +166,7 @@ export class WebRTCTransport implements Transport, Startable {
161
166
 
162
167
  async _onProtocol ({ connection, stream }: IncomingStreamData): Promise<void> {
163
168
  const signal = AbortSignal.timeout(this.init.inboundConnectionTimeout ?? INBOUND_CONNECTION_TIMEOUT)
164
- const peerConnection = new RTCPeerConnection(this.init.rtcConfiguration)
169
+ const peerConnection = new RTCPeerConnection(typeof this.init.rtcConfiguration === 'function' ? await this.init.rtcConfiguration() : this.init.rtcConfiguration)
165
170
  const muxerFactory = new DataChannelMuxerFactory(this.components, {
166
171
  peerConnection,
167
172
  dataChannelOptions: this.init.dataChannel
@@ -176,6 +181,11 @@ export class WebRTCTransport implements Transport, Startable {
176
181
  log: this.log
177
182
  })
178
183
 
184
+ // close the stream if SDP messages have been exchanged successfully
185
+ await stream.close({
186
+ signal
187
+ })
188
+
179
189
  const webRTCConn = new WebRTCMultiaddrConnection(this.components, {
180
190
  peerConnection,
181
191
  timeline: { open: (new Date()).getTime() },
@@ -183,20 +193,18 @@ export class WebRTCTransport implements Transport, Startable {
183
193
  metrics: this.metrics?.listenerEvents
184
194
  })
185
195
 
186
- // close the connection on shut down
187
- this._closeOnShutdown(peerConnection, webRTCConn)
188
-
189
196
  await this.components.upgrader.upgradeInbound(webRTCConn, {
190
197
  skipEncryption: true,
191
198
  skipProtection: true,
192
199
  muxerFactory
193
200
  })
194
201
 
195
- // close the stream if SDP messages have been exchanged successfully
196
- await stream.close({
197
- signal
198
- })
202
+ // close the connection on shut down
203
+ this._closeOnShutdown(peerConnection, webRTCConn)
199
204
  } catch (err: any) {
205
+ this.log.error('incoming signalling error', err)
206
+
207
+ peerConnection.close()
200
208
  stream.abort(err)
201
209
  throw err
202
210
  }
@@ -23,11 +23,14 @@ export const readCandidatesUntilConnected = async (pc: RTCPeerConnection, stream
23
23
  connectedPromise.promise,
24
24
  stream.read({
25
25
  signal: options.signal
26
- })
26
+ }).catch(() => {})
27
27
  ])
28
28
 
29
29
  // stream ended or we became connected
30
30
  if (message == null) {
31
+ // throw if we timed out
32
+ options.signal?.throwIfAborted()
33
+
31
34
  break
32
35
  }
33
36
 
@@ -48,7 +51,7 @@ export const readCandidatesUntilConnected = async (pc: RTCPeerConnection, stream
48
51
 
49
52
  const candidate = new RTCIceCandidate(candidateInit)
50
53
 
51
- options.log.trace('%s received new ICE candidate', options.direction, candidate)
54
+ options.log.trace('%s received new ICE candidate %o', options.direction, candidateInit)
52
55
 
53
56
  try {
54
57
  await pc.addIceCandidate(candidate)
@@ -1,5 +1,5 @@
1
1
  import { noise } from '@chainsafe/libp2p-noise'
2
- import { type CreateListenerOptions, transportSymbol, type Transport, type Listener, type ComponentLogger, type Logger, type Connection, type CounterGroup, type Metrics, type PeerId } from '@libp2p/interface'
2
+ import { transportSymbol, serviceCapabilities } from '@libp2p/interface'
3
3
  import * as p from '@libp2p/peer-id'
4
4
  import { protocols } from '@multiformats/multiaddr'
5
5
  import { WebRTCDirect } from '@multiformats/multiaddr-matcher'
@@ -16,6 +16,7 @@ import * as sdp from './sdp.js'
16
16
  import { genUfrag } from './util.js'
17
17
  import type { WebRTCDialOptions } from './options.js'
18
18
  import type { DataChannelOptions } from '../index.js'
19
+ import type { CreateListenerOptions, Transport, Listener, ComponentLogger, Logger, Connection, CounterGroup, Metrics, PeerId } from '@libp2p/interface'
19
20
  import type { Multiaddr } from '@multiformats/multiaddr'
20
21
 
21
22
  /**
@@ -51,6 +52,7 @@ export interface WebRTCMetrics {
51
52
  }
52
53
 
53
54
  export interface WebRTCTransportDirectInit {
55
+ rtcConfiguration?: RTCConfiguration | (() => RTCConfiguration | Promise<RTCConfiguration>)
54
56
  dataChannel?: DataChannelOptions
55
57
  }
56
58
 
@@ -73,6 +75,14 @@ export class WebRTCDirectTransport implements Transport {
73
75
  }
74
76
  }
75
77
 
78
+ readonly [transportSymbol] = true
79
+
80
+ readonly [Symbol.toStringTag] = '@libp2p/webrtc-direct'
81
+
82
+ readonly [serviceCapabilities]: string[] = [
83
+ '@libp2p/transport'
84
+ ]
85
+
76
86
  /**
77
87
  * Dial a given multiaddr
78
88
  */
@@ -103,16 +113,6 @@ export class WebRTCDirectTransport implements Transport {
103
113
  return this.listenFilter(multiaddrs)
104
114
  }
105
115
 
106
- /**
107
- * Implement toString() for WebRTCTransport
108
- */
109
- readonly [Symbol.toStringTag] = '@libp2p/webrtc-direct'
110
-
111
- /**
112
- * Symbol.for('@libp2p/transport')
113
- */
114
- readonly [transportSymbol] = true
115
-
116
116
  /**
117
117
  * Connect to a peer using a multiaddr
118
118
  */
@@ -138,7 +138,10 @@ export class WebRTCDirectTransport implements Transport {
138
138
  hash: sdp.toSupportedHashFunction(remoteCerthash.name)
139
139
  } as any)
140
140
 
141
- const peerConnection = new RTCPeerConnection({ certificates: [certificate] })
141
+ const peerConnection = new RTCPeerConnection({
142
+ ...(typeof this.init.rtcConfiguration === 'function' ? await this.init.rtcConfiguration() : this.init.rtcConfiguration ?? {}),
143
+ certificates: [certificate]
144
+ })
142
145
 
143
146
  try {
144
147
  // create data channel for running the noise handshake. Once the data channel is opened,