Skip to content

fix: polyfill W3C compatibility #324

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

Open
wants to merge 8 commits 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
30 changes: 29 additions & 1 deletion package-lock.json

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

226 changes: 114 additions & 112 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,117 +1,119 @@
{
"name": "node-datachannel",
"version": "0.26.0",
"description": "WebRTC For Node.js and Electron. libdatachannel node bindings.",
"main": "./dist/cjs/lib/index.cjs",
"module": "./dist/esm/lib/index.mjs",
"types": "./dist/types/lib/index.d.ts",
"typesVersions": {
"*": {
"*": [
"dist/types/lib/index.d.ts"
],
"polyfill": [
"dist/types/polyfill/index.d.ts"
]
}
},
"exports": {
".": {
"types": "./dist/types/lib/index.d.ts",
"require": "./dist/cjs/lib/index.cjs",
"import": "./dist/esm/lib/index.mjs",
"default": "./dist/lib/esm/index.mjs"
"name": "node-datachannel",
"version": "0.26.0",
"description": "WebRTC For Node.js and Electron. libdatachannel node bindings.",
"main": "./dist/cjs/lib/index.cjs",
"module": "./dist/esm/lib/index.mjs",
"types": "./dist/types/lib/index.d.ts",
"typesVersions": {
"*": {
"*": [
"dist/types/lib/index.d.ts"
],
"polyfill": [
"dist/types/polyfill/index.d.ts"
]
}
},
"./polyfill": {
"types": "./dist/types/polyfill/index.d.ts",
"require": "./dist/cjs/polyfill/index.cjs",
"import": "./dist/esm/polyfill/index.mjs",
"default": "./dist/polyfill/esm/index.mjs"
}
},
"engines": {
"node": ">=18.20.0"
},
"scripts": {
"install": "prebuild-install -r napi || (npm install --ignore-scripts --production=false && npm run _prebuild)",
"install:nice": "npm run clean && npm install --ignore-scripts --production=false && cmake-js configure --CDUSE_NICE=1 && cmake-js build",
"install:gnu": "npm run clean && npm install --ignore-scripts --production=false && cmake-js configure --CDUSE_GNUTLS=1 && cmake-js build",
"build": "npm run compile && npm run build:tsc",
"compile": "cmake-js build",
"compile:debug": "cmake-js build -D",
"build:tsc": "rimraf dist && rollup -c",
"build:tsc:watch": "rollup -c -w",
"clean": "rimraf dist build",
"lint": "eslint . --ext .ts --ext .mts",
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch",
"test:wpt": "npm run run:wpt:server & (sleep 8 && (npm run run:wpt:test | tee test/wpt-tests/last-test-results.md) )",
"wpt:server": "cd test/wpt-tests/wpt && ./wpt serve",
"wpt:test": "ts-node test/wpt-tests/index.ts",
"_prebuild": "prebuild -r napi --backend cmake-js",
"prepack": "npm run build:tsc"
},
"binary": {
"napi_versions": [
8
]
},
"repository": {
"type": "git",
"url": "git+https://github.com/murat-dogan/node-datachannel.git"
},
"keywords": [
"libdatachannel",
"webrtc",
"p2p",
"peer-to-peer",
"datachannel",
"data channel",
"websocket"
],
"contributors": [
{
"name": "Murat Doğan",
"url": "https://github.com/murat-dogan"
"exports": {
".": {
"types": "./dist/types/lib/index.d.ts",
"require": "./dist/cjs/lib/index.cjs",
"import": "./dist/esm/lib/index.mjs",
"default": "./dist/lib/esm/index.mjs"
},
"./polyfill": {
"types": "./dist/types/polyfill/index.d.ts",
"require": "./dist/cjs/polyfill/index.cjs",
"import": "./dist/esm/polyfill/index.mjs",
"default": "./dist/polyfill/esm/index.mjs"
}
},
"engines": {
"node": ">=18.20.0"
},
"scripts": {
"install": "prebuild-install -r napi || (npm install --ignore-scripts --production=false && npm run _prebuild)",
"install:nice": "npm run clean && npm install --ignore-scripts --production=false && cmake-js configure --CDUSE_NICE=1 && cmake-js build",
"install:gnu": "npm run clean && npm install --ignore-scripts --production=false && cmake-js configure --CDUSE_GNUTLS=1 && cmake-js build",
"build": "npm run compile && npm run build:tsc",
"compile": "cmake-js build",
"compile:debug": "cmake-js build -D",
"build:tsc": "rimraf dist && rollup -c",
"build:tsc:watch": "rollup -c -w",
"clean": "rimraf dist build",
"lint": "eslint . --ext .ts --ext .mts",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
"test:watch": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --watch",
"test:wpt": "npm run run:wpt:server & (sleep 8 && (npm run run:wpt:test | tee test/wpt-tests/last-test-results.md) )",
"wpt:server": "cd test/wpt-tests/wpt && ./wpt serve",
"wpt:test": "ts-node test/wpt-tests/index.ts",
"_prebuild": "prebuild -r napi --backend cmake-js",
"prepack": "npm run build:tsc"
},
"binary": {
"napi_versions": [
8
]
},
"repository": {
"type": "git",
"url": "git+https://github.com/murat-dogan/node-datachannel.git"
},
"keywords": [
"libdatachannel",
"webrtc",
"p2p",
"peer-to-peer",
"datachannel",
"data channel",
"websocket"
],
"contributors": [
{
"name": "Murat Doğan",
"url": "https://github.com/murat-dogan"
},
{
"name": "Paul-Louis Ageneau",
"url": "https://github.com/paullouisageneau"
}
],
"license": "MPL 2.0",
"bugs": {
"url": "https://github.com/murat-dogan/node-datachannel/issues"
},
"homepage": "https://github.com/murat-dogan/node-datachannel#readme",
"devDependencies": {
"@rollup/plugin-esm-shim": "^0.1.7",
"@rollup/plugin-replace": "^6.0.1",
"@types/jest": "^29.5.12",
"@types/node": "^20.6.1",
"@types/webrtc": "^0.0.44",
"@typescript-eslint/eslint-plugin": "^7.17.0",
"@typescript-eslint/parser": "^7.17.0",
"cmake-js": "^7.3.0",
"cross-env": "^7.0.3",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-prettier": "^5.2.1",
"jest": "^29.7.0",
"jsdom": "^24.1.1",
"node-addon-api": "^7.0.0",
"prebuild": "^13.0.1",
"prettier": "^3.3.3",
"puppeteer": "^22.14.0",
"rimraf": "^5.0.9",
"rollup": "^4.22.5",
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-esbuild": "^6.1.1",
"ts-api-utils": "^1.3.0",
"ts-jest": "^29.2.3",
"ts-node": "^10.9.2",
"typescript": "5.4"
},
{
"name": "Paul-Louis Ageneau",
"url": "https://github.com/paullouisageneau"
"dependencies": {
"prebuild-install": "^7.1.3"
}
],
"license": "MPL 2.0",
"bugs": {
"url": "https://github.com/murat-dogan/node-datachannel/issues"
},
"homepage": "https://github.com/murat-dogan/node-datachannel#readme",
"devDependencies": {
"@rollup/plugin-esm-shim": "^0.1.7",
"@rollup/plugin-replace": "^6.0.1",
"@types/jest": "^29.5.12",
"@types/node": "^20.6.1",
"@typescript-eslint/eslint-plugin": "^7.17.0",
"@typescript-eslint/parser": "^7.17.0",
"cmake-js": "^7.3.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-prettier": "^5.2.1",
"jest": "^29.7.0",
"jsdom": "^24.1.1",
"node-addon-api": "^7.0.0",
"prebuild": "^13.0.1",
"prettier": "^3.3.3",
"puppeteer": "^22.14.0",
"rimraf": "^5.0.9",
"rollup": "^4.22.5",
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-esbuild": "^6.1.1",
"ts-api-utils": "^1.3.0",
"ts-jest": "^29.2.3",
"ts-node": "^10.9.2",
"typescript": "5.4"
},
"dependencies": {
"prebuild-install": "^7.1.3"
}
}
56 changes: 51 additions & 5 deletions src/polyfill/Events.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import RTCDataChannel from "./RTCDataChannel";
import RTCError from "./RTCError";

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

Expand All @@ -10,20 +13,63 @@ export class RTCPeerConnectionIceEvent extends Event implements globalThis.RTCPe
get candidate(): globalThis.RTCIceCandidate {
return this.#candidate;
}

get url (): string {
return '' // TODO ?
}
}

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

constructor(type: string, eventInitDict: globalThis.RTCDataChannelEventInit) {
super(type);
// type is defined as a consturctor, but always overwritten, interesting spec
// eslint-disable-next-line @typescript-eslint/no-unused-vars
constructor(_type: string = 'datachannel', init: globalThis.RTCDataChannelEventInit) {
if (arguments.length === 0) throw new TypeError(`Failed to construct 'RTCDataChannelEvent': 2 arguments required, but only ${arguments.length} present.`)
if (typeof init !== 'object') throw new TypeError("Failed to construct 'RTCDataChannelEvent': The provided value is not of type 'RTCDataChannelEventInit'.")
if (!init.channel) throw new TypeError("Failed to construct 'RTCDataChannelEvent': Failed to read the 'channel' property from 'RTCDataChannelEventInit': Required member is undefined.")
if (init.channel.constructor !== RTCDataChannel) throw new TypeError("Failed to construct 'RTCDataChannelEvent': Failed to read the 'channel' property from 'RTCDataChannelEventInit': Failed to convert value to 'RTCDataChannel'.")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

required by spec

super('datachannel')

if (type && !eventInitDict.channel) throw new TypeError('channel member is required');

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

get channel(): globalThis.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
}
}

export class MediaStreamTrackEvent extends Event implements globalThis.MediaStreamTrackEvent {
#track: MediaStreamTrack

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

super(type)

this.#track = init.track
}

get track (): MediaStreamTrack {
return this.#track
}
}
13 changes: 6 additions & 7 deletions src/polyfill/RTCCertificate.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
export default class RTCCertificate implements globalThis.RTCCertificate {
#expires: number;
#fingerprints: globalThis.RTCDtlsFingerprint[];

constructor() {
this.#expires = null;
this.#fingerprints = [];
}
#expires: number = 0;
#fingerprints: globalThis.RTCDtlsFingerprint[] = [];

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

getAlgorithm (): string {
return ''
}
}
Loading