Skip to content

Commit

Permalink
feat: allow passing a function for rtcConfiguration (#2590)
Browse files Browse the repository at this point in the history
In order to allow refreshing STUN/TURN credentials between dials,
allow passing a function that returns rtc config.

Fixes #2554
  • Loading branch information
achingbrain authored Jun 18, 2024
1 parent 8e4fdcd commit 9e02366
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/transport-webrtc/src/private-to-private/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const SIGNALING_PROTO_ID = '/webrtc-signaling/0.0.1'
const INBOUND_CONNECTION_TIMEOUT = 30 * 1000

export interface WebRTCTransportInit {
rtcConfiguration?: RTCConfiguration
rtcConfiguration?: RTCConfiguration | (() => RTCConfiguration | Promise<RTCConfiguration>)
dataChannel?: DataChannelOptions

/**
Expand Down Expand Up @@ -134,7 +134,7 @@ export class WebRTCTransport implements Transport, Startable {
this.log.trace('dialing address: %a', ma)

const { remoteAddress, peerConnection, muxerFactory } = await initiateConnection({
rtcConfiguration: this.init.rtcConfiguration,
rtcConfiguration: typeof this.init.rtcConfiguration === 'function' ? await this.init.rtcConfiguration() : this.init.rtcConfiguration,
dataChannel: this.init.dataChannel,
multiaddr: ma,
dataChannelOptions: this.init.dataChannel,
Expand Down Expand Up @@ -166,7 +166,7 @@ export class WebRTCTransport implements Transport, Startable {

async _onProtocol ({ connection, stream }: IncomingStreamData): Promise<void> {
const signal = AbortSignal.timeout(this.init.inboundConnectionTimeout ?? INBOUND_CONNECTION_TIMEOUT)
const peerConnection = new RTCPeerConnection(this.init.rtcConfiguration)
const peerConnection = new RTCPeerConnection(typeof this.init.rtcConfiguration === 'function' ? await this.init.rtcConfiguration() : this.init.rtcConfiguration)
const muxerFactory = new DataChannelMuxerFactory(this.components, {
peerConnection,
dataChannelOptions: this.init.dataChannel
Expand Down
6 changes: 5 additions & 1 deletion packages/transport-webrtc/src/private-to-public/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface WebRTCMetrics {
}

export interface WebRTCTransportDirectInit {
rtcConfiguration?: RTCConfiguration | (() => RTCConfiguration | Promise<RTCConfiguration>)
dataChannel?: DataChannelOptions
}

Expand Down Expand Up @@ -137,7 +138,10 @@ export class WebRTCDirectTransport implements Transport {
hash: sdp.toSupportedHashFunction(remoteCerthash.name)
} as any)

const peerConnection = new RTCPeerConnection({ certificates: [certificate] })
const peerConnection = new RTCPeerConnection({
...(typeof this.init.rtcConfiguration === 'function' ? await this.init.rtcConfiguration() : this.init.rtcConfiguration ?? {}),
certificates: [certificate]
})

try {
// create data channel for running the noise handshake. Once the data channel is opened,
Expand Down

0 comments on commit 9e02366

Please sign in to comment.