From 1e052def6c0ccc2ba41b183f5b33ba7cc594e571 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 14 Jun 2024 16:08:16 +0100 Subject: [PATCH] chore: allow function based config --- packages/transport-webrtc/src/private-to-public/listener.ts | 2 +- .../transport-webrtc/src/private-to-public/transport.ts | 2 +- .../src/private-to-public/utils/get-rtcpeerconnection.ts | 6 ++++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/transport-webrtc/src/private-to-public/listener.ts b/packages/transport-webrtc/src/private-to-public/listener.ts index 668ac49439..cb42259fc1 100644 --- a/packages/transport-webrtc/src/private-to-public/listener.ts +++ b/packages/transport-webrtc/src/private-to-public/listener.ts @@ -34,7 +34,7 @@ export interface WebRTCDirectListenerInit { certificates?: TransportCertificate[] maxInboundStreams?: number dataChannel?: DataChannelOptions - rtcConfiguration?: RTCConfiguration + rtcConfiguration?: RTCConfiguration | (() => RTCConfiguration | Promise) useLibjuice?: boolean } diff --git a/packages/transport-webrtc/src/private-to-public/transport.ts b/packages/transport-webrtc/src/private-to-public/transport.ts index 1c3ac3a15e..3266126072 100644 --- a/packages/transport-webrtc/src/private-to-public/transport.ts +++ b/packages/transport-webrtc/src/private-to-public/transport.ts @@ -49,7 +49,7 @@ export interface WebRTCMetrics { } export interface WebRTCTransportDirectInit { - rtcConfiguration?: RTCConfiguration + rtcConfiguration?: RTCConfiguration | (() => RTCConfiguration | Promise) dataChannel?: DataChannelOptions certificates?: TransportCertificate[] useLibjuice?: boolean diff --git a/packages/transport-webrtc/src/private-to-public/utils/get-rtcpeerconnection.ts b/packages/transport-webrtc/src/private-to-public/utils/get-rtcpeerconnection.ts index d4b82fe4d1..69854dd6b1 100644 --- a/packages/transport-webrtc/src/private-to-public/utils/get-rtcpeerconnection.ts +++ b/packages/transport-webrtc/src/private-to-public/utils/get-rtcpeerconnection.ts @@ -78,7 +78,7 @@ export class DirectRTCPeerConnection extends RTCPeerConnection { } } -export async function createDialerRTCPeerConnection (name: string, ufrag: string, rtcConfiguration?: RTCConfiguration, certificate?: TransportCertificate): Promise { +export async function createDialerRTCPeerConnection (name: string, ufrag: string, rtcConfiguration?: RTCConfiguration | (() => RTCConfiguration | Promise), certificate?: TransportCertificate): Promise { if (certificate == null) { const keyPair = await crypto.subtle.generateKey({ name: 'ECDSA', @@ -90,6 +90,8 @@ export async function createDialerRTCPeerConnection (name: string, ufrag: string }) } + const rtcConfig = typeof rtcConfiguration === 'function' ? await rtcConfiguration() : rtcConfiguration + // https://github.com/libp2p/specs/blob/master/webrtc/webrtc-direct.md#browser-to-public-server const peerConnection = new PeerConnection(name, { disableFingerprintVerification: true, @@ -97,7 +99,7 @@ export async function createDialerRTCPeerConnection (name: string, ufrag: string certificatePemFile: certificate.pem, keyPemFile: certificate.privateKey, maxMessageSize: 16384, - iceServers: toLibdatachannelIceServers(rtcConfiguration?.iceServers) ?? DEFAULT_STUN_SERVERS + iceServers: toLibdatachannelIceServers(rtcConfig?.iceServers) ?? DEFAULT_STUN_SERVERS }) return new DirectRTCPeerConnection({