Skip to content

Commit

Permalink
feat: allow overriding PeerConnection creation
Browse files Browse the repository at this point in the history
Adds a protected method that can be overidden by extending classes
to supply a `PeerConnection` with different configuration.

Refs: murat-dogan#260 (comment)
  • Loading branch information
achingbrain committed Jan 27, 2025
1 parent b0caa62 commit 3419272
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions src/polyfill/RTCPeerConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,7 @@ export default class RTCPeerConnection extends EventTarget implements globalThis
this.#canTrickleIceCandidates = null;

try {
const peerIdentity = (config as any)?.peerIdentity ?? `peer-${getRandomString(7)}`;
this.#peerConnection = new PeerConnection(peerIdentity,
{
...config,
iceServers:
config?.iceServers
?.map((server) => {
const urls = Array.isArray(server.urls) ? server.urls : [server.urls];

return urls.map((url) => {
if (server.username && server.credential) {
const [protocol, rest] = url.split(/:(.*)/);
return `${protocol}:${server.username}:${server.credential}@${rest}`;
}
return url;
});
})
.flat() ?? [],
},
);
this.#peerConnection = this.createPeerConnection(config);
} catch (error) {
if (!error || !error.message) throw new exceptions.NotFoundError('Unknown error');
throw new exceptions.SyntaxError(error.message);
Expand Down Expand Up @@ -230,6 +211,30 @@ export default class RTCPeerConnection extends EventTarget implements globalThis
});
}

protected createPeerConnection (config: RTCConfiguration): PeerConnection {
const peerIdentity = config.peerIdentity ?? `peer-${getRandomString(7)}`;

return new PeerConnection(peerIdentity,
{
...config,
iceServers:
config?.iceServers
?.map((server) => {
const urls = Array.isArray(server.urls) ? server.urls : [server.urls];

return urls.map((url) => {
if (server.username && server.credential) {
const [protocol, rest] = url.split(/:(.*)/);
return `${protocol}:${server.username}:${server.credential}@${rest}`;
}
return url;
});
})
.flat() ?? [],
},
);
}

get canTrickleIceCandidates(): boolean | null {
return this.#canTrickleIceCandidates;
}
Expand Down

0 comments on commit 3419272

Please sign in to comment.