Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow passing a function for rtcConfiguration #2590

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading