Skip to content

Commit 3419272

Browse files
committed
feat: allow overriding PeerConnection creation
Adds a protected method that can be overidden by extending classes to supply a `PeerConnection` with different configuration. Refs: murat-dogan#260 (comment)
1 parent b0caa62 commit 3419272

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

src/polyfill/RTCPeerConnection.ts

+25-20
Original file line numberDiff line numberDiff line change
@@ -120,26 +120,7 @@ export default class RTCPeerConnection extends EventTarget implements globalThis
120120
this.#canTrickleIceCandidates = null;
121121

122122
try {
123-
const peerIdentity = (config as any)?.peerIdentity ?? `peer-${getRandomString(7)}`;
124-
this.#peerConnection = new PeerConnection(peerIdentity,
125-
{
126-
...config,
127-
iceServers:
128-
config?.iceServers
129-
?.map((server) => {
130-
const urls = Array.isArray(server.urls) ? server.urls : [server.urls];
131-
132-
return urls.map((url) => {
133-
if (server.username && server.credential) {
134-
const [protocol, rest] = url.split(/:(.*)/);
135-
return `${protocol}:${server.username}:${server.credential}@${rest}`;
136-
}
137-
return url;
138-
});
139-
})
140-
.flat() ?? [],
141-
},
142-
);
123+
this.#peerConnection = this.createPeerConnection(config);
143124
} catch (error) {
144125
if (!error || !error.message) throw new exceptions.NotFoundError('Unknown error');
145126
throw new exceptions.SyntaxError(error.message);
@@ -230,6 +211,30 @@ export default class RTCPeerConnection extends EventTarget implements globalThis
230211
});
231212
}
232213

214+
protected createPeerConnection (config: RTCConfiguration): PeerConnection {
215+
const peerIdentity = config.peerIdentity ?? `peer-${getRandomString(7)}`;
216+
217+
return new PeerConnection(peerIdentity,
218+
{
219+
...config,
220+
iceServers:
221+
config?.iceServers
222+
?.map((server) => {
223+
const urls = Array.isArray(server.urls) ? server.urls : [server.urls];
224+
225+
return urls.map((url) => {
226+
if (server.username && server.credential) {
227+
const [protocol, rest] = url.split(/:(.*)/);
228+
return `${protocol}:${server.username}:${server.credential}@${rest}`;
229+
}
230+
return url;
231+
});
232+
})
233+
.flat() ?? [],
234+
},
235+
);
236+
}
237+
233238
get canTrickleIceCandidates(): boolean | null {
234239
return this.#canTrickleIceCandidates;
235240
}

0 commit comments

Comments
 (0)