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: accept preconfigured PeerConnection as polyfill arg #260

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion polyfill/RTCPeerConnection.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/// <reference lib="dom" />

import { PeerConnection } from '../lib/index.js';

export interface _RTCConfiguration extends RTCConfiguration {
peerConnection?: PeerConnection;
}

export default class _RTCPeerConnection extends EventTarget implements RTCPeerConnection {
constructor(config?: RTCConfiguration);
constructor(config?: _RTCConfiguration);

// events
onconnectionstatechange: ((this: RTCPeerConnection, ev: Event) => any) | null;
Expand Down
36 changes: 19 additions & 17 deletions polyfill/RTCPeerConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,25 @@ export default class _RTCPeerConnection extends EventTarget {
this.#dataChannels = new Set();
this.#canTrickleIceCandidates = null;

this.#peerConnection = new NodeDataChannel.PeerConnection(init?.peerIdentity ?? `peer-${getRandomString(7)}`, {
...init,
iceServers:
init?.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 =
init.peerConnection ??
new NodeDataChannel.PeerConnection(init?.peerIdentity ?? `peer-${getRandomString(7)}`, {
...init,
iceServers:
init?.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() ?? [],
});

// forward peerConnection events
this.#peerConnection.onStateChange(() => {
Expand Down
25 changes: 24 additions & 1 deletion test/jest-tests/polyfill.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
import { expect } from '@jest/globals';
import { expect, jest } from '@jest/globals';
import polyfill from '../../polyfill/index.js';
import { PeerConnection } from '../../lib';

describe('polyfill', () => {
test('generateCertificate should throw', () => {
expect(async () => {
await polyfill.RTCPeerConnection.generateCertificate({});
}).rejects.toEqual(new DOMException('Not implemented'));
});

test('it should accept a preconfigured PeerConnection', () => {
const peerConnection = new PeerConnection('Peer', {
iceServers: [],
});

// have to override write-only method in order to spy on it
const originalFunc = peerConnection.state.bind(peerConnection);
Object.defineProperty(peerConnection, 'state', {
value: originalFunc,
writable: true,
enumerable: true,
});

const spy = jest.spyOn(peerConnection, 'state');
const rtcPeerConnection = new polyfill.RTCPeerConnection({
peerConnection,
});
const connectionState = rtcPeerConnection.connectionState;
expect(spy).toHaveBeenCalled();
expect(connectionState).toEqual(originalFunc());
});
});
Loading