Skip to content
Merged
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
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"@rollup/plugin-replace": "^6.0.1",
"@types/jest": "^29.5.12",
"@types/node": "^20.6.1",
"@types/webrtc": "^0.0.46",
"@typescript-eslint/eslint-plugin": "^7.17.0",
"@typescript-eslint/parser": "^7.17.0",
"cmake-js": "^7.3.0",
Expand Down
59 changes: 52 additions & 7 deletions src/polyfill/Events.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import RTCDataChannel from './RTCDataChannel';
import RTCError from './RTCError';

export class RTCPeerConnectionIceEvent
extends Event
implements globalThis.RTCPeerConnectionIceEvent
{
export class RTCPeerConnectionIceEvent extends Event implements globalThis.RTCPeerConnectionIceEvent {
#candidate: globalThis.RTCIceCandidate;

constructor(candidate: globalThis.RTCIceCandidate) {
Expand All @@ -15,20 +13,67 @@ export class RTCPeerConnectionIceEvent
get candidate(): globalThis.RTCIceCandidate {
return this.#candidate;
}

get url(): string {
return '';
}
}

export class RTCDataChannelEvent extends Event implements globalThis.RTCDataChannelEvent {
#channel: RTCDataChannel;

constructor(type: string, eventInitDict: globalThis.RTCDataChannelEventInit) {
constructor(type: string = 'datachannel', eventInitDict: globalThis.RTCDataChannelEventInit) {
super(type);

if (type && !eventInitDict.channel) throw new TypeError('channel member is required');
if (arguments.length === 0)
throw new TypeError(
`Failed to construct 'RTCDataChannelEvent': 2 arguments required, but only ${arguments.length} present.`,
);
if (typeof eventInitDict !== 'object')
throw new TypeError(
"Failed to construct 'RTCDataChannelEvent': The provided value is not of type 'RTCDataChannelEventInit'.",
);
if (!eventInitDict.channel)
throw new TypeError(
"Failed to construct 'RTCDataChannelEvent': Failed to read the 'channel' property from 'RTCDataChannelEventInit': Required member is undefined.",
);
if (eventInitDict.channel.constructor !== RTCDataChannel)
throw new TypeError(
"Failed to construct 'RTCDataChannelEvent': Failed to read the 'channel' property from 'RTCDataChannelEventInit': Failed to convert value to 'RTCDataChannel'.",
);

this.#channel = eventInitDict?.channel as RTCDataChannel;
this.#channel = eventInitDict?.channel;
}

get channel(): RTCDataChannel {
return this.#channel;
}
}

export class RTCErrorEvent extends Event implements globalThis.RTCErrorEvent {
#error: RTCError;
constructor(type: string, init: globalThis.RTCErrorEventInit) {
if (arguments.length < 2)
throw new TypeError(
`Failed to construct 'RTCErrorEvent': 2 arguments required, but only ${arguments.length} present.`,
);
if (typeof init !== 'object')
throw new TypeError(
"Failed to construct 'RTCErrorEvent': The provided value is not of type 'RTCErrorEventInit'.",
);
if (!init.error)
throw new TypeError(
"Failed to construct 'RTCErrorEvent': Failed to read the 'error' property from 'RTCErrorEventInit': Required member is undefined.",
);
if (init.error.constructor !== RTCError)
throw new TypeError(
"Failed to construct 'RTCErrorEvent': Failed to read the 'error' property from 'RTCErrorEventInit': Failed to convert value to 'RTCError'.",
);
super(type || 'error');
this.#error = init.error;
}

get error(): RTCError {
return this.#error;
}
}
8 changes: 6 additions & 2 deletions src/polyfill/RTCCertificate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default class RTCCertificate implements globalThis.RTCCertificate {
#expires: number;
#fingerprints: globalThis.RTCDtlsFingerprint[];
#expires: number = 0;
#fingerprints: globalThis.RTCDtlsFingerprint[] = [];

constructor() {
this.#expires = null;
Expand All @@ -14,4 +14,8 @@ export default class RTCCertificate implements globalThis.RTCCertificate {
getFingerprints(): globalThis.RTCDtlsFingerprint[] {
return this.#fingerprints;
}

getAlgorithm(): string {
return '';
}
}
5 changes: 3 additions & 2 deletions src/polyfill/RTCDataChannel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as exceptions from './Exception';
import { DataChannel } from '../lib/index';
import { RTCErrorEvent } from './Events';

export default class RTCDataChannel extends EventTarget implements globalThis.RTCDataChannel {
#dataChannel: DataChannel;
Expand Down Expand Up @@ -61,7 +62,7 @@ export default class RTCDataChannel extends EventTarget implements globalThis.RT

this.#dataChannel.onError((msg) => {
this.dispatchEvent(
new globalThis.RTCErrorEvent('error', {
new RTCErrorEvent('error', {
error: new RTCError(
{
errorDetail: 'data-channel-failure',
Expand Down Expand Up @@ -93,7 +94,7 @@ export default class RTCDataChannel extends EventTarget implements globalThis.RT
if (this.onbufferedamountlow) this.onbufferedamountlow(e);
});
this.addEventListener('error', (e) => {
if (this.onerror) this.onerror(e);
if (this.onerror) this.onerror(e as RTCErrorEvent);
});
this.addEventListener('close', (e) => {
if (this.onclose) this.onclose(e);
Expand Down
8 changes: 4 additions & 4 deletions src/polyfill/RTCDtlsTransport.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import RTCIceTransport from './RTCIceTransport';
import RTCPeerConnection from './RTCPeerConnection';

export default class RTCDtlsTransport extends EventTarget implements globalThis.RTCDtlsTransport {
#pc: globalThis.RTCPeerConnection = null;
#pc: RTCPeerConnection = null;
#iceTransport = null;

onstatechange: globalThis.RTCDtlsTransport['onstatechange'] = null;
onerror: globalThis.RTCDtlsTransport['onstatechange'] = null;

constructor(init: { pc: globalThis.RTCPeerConnection; extraFunctions }) {
constructor(init: { pc: RTCPeerConnection }) {
super();
this.#pc = init.pc;

this.#iceTransport = new RTCIceTransport({
pc: init.pc,
extraFunctions: init.extraFunctions,
pc: init.pc
});

// forward peerConnection events
Expand Down
6 changes: 6 additions & 0 deletions src/polyfill/RTCError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default class RTCError extends DOMException implements globalThis.RTCErro
#sctpCauseCode: number | null;
#sdpLineNumber: number | null;
#sentAlert: number | null;
#httpRequestStatusCode: number | null;

constructor(init: globalThis.RTCErrorInit, message?: string) {
super(message, 'OperationError');
Expand All @@ -28,6 +29,7 @@ export default class RTCError extends DOMException implements globalThis.RTCErro
this.#sctpCauseCode = init.sctpCauseCode ?? null;
this.#sdpLineNumber = init.sdpLineNumber ?? null;
this.#sentAlert = init.sentAlert ?? null;
this.#httpRequestStatusCode = init.httpRequestStatusCode ?? null;
}

get errorDetail(): globalThis.RTCErrorDetailType {
Expand All @@ -54,6 +56,10 @@ export default class RTCError extends DOMException implements globalThis.RTCErro
throw new TypeError('Cannot set sctpCauseCode, it is read-only');
}

get httpRequestStatusCode(): number | null {
return this.#httpRequestStatusCode;
}

get sdpLineNumber(): number | null {
return this.#sdpLineNumber;
}
Expand Down
37 changes: 25 additions & 12 deletions src/polyfill/RTCIceTransport.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import RTCIceCandidate from './RTCIceCandidate';
import RTCPeerConnection from './RTCPeerConnection';

export default class RTCIceTransport extends EventTarget implements globalThis.RTCIceTransport {
#pc: globalThis.RTCPeerConnection = null;
#extraFunctions = null;
#pc: RTCPeerConnection = null;

ongatheringstatechange: globalThis.RTCIceTransport['ongatheringstatechange'] = null;
onselectedcandidatepairchange: globalThis.RTCIceTransport['onselectedcandidatepairchange'] = null;
onstatechange: globalThis.RTCIceTransport['onstatechange'] = null;

constructor(init: { pc: globalThis.RTCPeerConnection; extraFunctions }) {
constructor(init: { pc: RTCPeerConnection }) {
super();
this.#pc = init.pc;
this.#extraFunctions = init.extraFunctions;

this.#pc.addEventListener('icegatheringstatechange', () => {
const e = new Event('gatheringstatechange');
Expand All @@ -28,40 +27,45 @@ export default class RTCIceTransport extends EventTarget implements globalThis.R

get component(): globalThis.RTCIceComponent {
const cp = this.getSelectedCandidatePair();
if (!cp) return null;
if (!cp?.local) return null;
return cp.local.component;
}

get gatheringState(): globalThis.RTCIceGatheringState {
return this.#pc ? this.#pc.iceGatheringState : 'new';
}

get role(): string {
return this.#pc.localDescription.type == 'offer' ? 'controlling' : 'controlled';
get role(): globalThis.RTCIceRole {
return this.#pc.localDescription!.type == 'offer' ? 'controlling' : 'controlled';
}

get state(): globalThis.RTCIceTransportState {
return this.#pc ? this.#pc.iceConnectionState : 'new';
}

getLocalCandidates(): globalThis.RTCIceCandidate[] {
return this.#pc ? this.#extraFunctions.localCandidates() : [];
return this.#pc?.ext_localCandidates ?? [];
}

getLocalParameters(): any {
/** */
getLocalParameters(): RTCIceParameters | null {
return new RTCIceParameters(
new RTCIceCandidate({
candidate: this.#pc.selectedCandidatePair()!.local.candidate,
sdpMLineIndex: 0,
}),
);
}

getRemoteCandidates(): globalThis.RTCIceCandidate[] {
return this.#pc ? this.#extraFunctions.remoteCandidates() : [];
return this.#pc?.ext_remoteCandidates ?? [];
}

getRemoteParameters(): any {
/** */
}

getSelectedCandidatePair(): globalThis.RTCIceCandidatePair | null {
const cp = this.#extraFunctions.selectedCandidatePair();
const cp = this.#pc?.selectedCandidatePair();
if (!cp) return null;
return {
local: new RTCIceCandidate({
Expand All @@ -75,3 +79,12 @@ export default class RTCIceTransport extends EventTarget implements globalThis.R
};
}
}

export class RTCIceParameters implements globalThis.RTCIceParameters {
usernameFragment = '';
password = '';
constructor({ usernameFragment, password = '' }) {
this.usernameFragment = usernameFragment;
this.password = password;
}
}
Loading