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: WebRTC-Direct support for Node.js #2583

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
chore: allow function based config
  • Loading branch information
achingbrain committed Jun 14, 2024
commit 1e052def6c0ccc2ba41b183f5b33ba7cc594e571
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
Loading