Skip to content

Commit

Permalink
chore: allow function based config
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Jun 14, 2024
1 parent 03044e0 commit 1e052de
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface WebRTCDirectListenerInit {
certificates?: TransportCertificate[]
maxInboundStreams?: number
dataChannel?: DataChannelOptions
rtcConfiguration?: RTCConfiguration
rtcConfiguration?: RTCConfiguration | (() => RTCConfiguration | Promise<RTCConfiguration>)
useLibjuice?: boolean
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface WebRTCMetrics {
}

export interface WebRTCTransportDirectInit {
rtcConfiguration?: RTCConfiguration
rtcConfiguration?: RTCConfiguration | (() => RTCConfiguration | Promise<RTCConfiguration>)
dataChannel?: DataChannelOptions
certificates?: TransportCertificate[]
useLibjuice?: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class DirectRTCPeerConnection extends RTCPeerConnection {
}
}

export async function createDialerRTCPeerConnection (name: string, ufrag: string, rtcConfiguration?: RTCConfiguration, certificate?: TransportCertificate): Promise<DirectRTCPeerConnection> {
export async function createDialerRTCPeerConnection (name: string, ufrag: string, rtcConfiguration?: RTCConfiguration | (() => RTCConfiguration | Promise<RTCConfiguration>), certificate?: TransportCertificate): Promise<DirectRTCPeerConnection> {
if (certificate == null) {
const keyPair = await crypto.subtle.generateKey({
name: 'ECDSA',
Expand All @@ -90,14 +90,16 @@ 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,
disableAutoNegotiation: true,
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({
Expand Down

0 comments on commit 1e052de

Please sign in to comment.