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

fix: polyfill W3C compatibility #324

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,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",
Expand All @@ -104,4 +105,4 @@
"dependencies": {
"prebuild-install": "^7.1.2"
}
}
}
53 changes: 48 additions & 5 deletions src/polyfill/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,63 @@ export class RTCPeerConnectionIceEvent extends Event implements globalThis.RTCPe
get candidate(): RTCIceCandidate {
return this.#candidate;
}

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

export class RTCDataChannelEvent extends Event implements globalThis.RTCDataChannelEvent {
#channel: 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 RTCDataChannel;
this.#channel = init.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
}
}

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
}
}
177 changes: 177 additions & 0 deletions src/polyfill/MediaStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import { Readable } from 'node:stream'
import { MediaStreamTrackEvent } from './Events.js'
import { Track } from '../lib/index.js'

export class MediaStreamTrack extends EventTarget implements globalThis.MediaStreamTrack {
media
track: Track
stream = new Readable({ read: () => {} })
#kind: string
#label: string
#id = crypto.randomUUID()
contentHint = ''

onmute
onunmute
onended

constructor ({ kind, label }: { kind: string, label: string }) {
super()
if (!kind) throw new TypeError("Failed to construct 'MediaStreamTrack': Failed to read the 'kind' property from 'MediaStreamTrackInit': Required member is undefined.")
this.#kind = kind
this.#label = label

this.addEventListener('ended', e => {
this.onended?.(e)
this.track?.close()
this.stream.destroy()
})
this.stream.on('close', () => {
this.stop()
})
}

async applyConstraints (): Promise<void> {
console.warn('Constraints unsupported, ignored')
}

stop (): void {
this.track?.close()
this.stream.destroy()
this.dispatchEvent(new Event('ended'))
}

getSettings (): globalThis.MediaTrackSettings {
console.warn('Settings upsupported, ignored')
return {}
}

getConstraints (): globalThis.MediaTrackConstraints {
console.warn('Constraints unsupported, ignored')
return {}
}

getCapabilities (): globalThis.MediaTrackCapabilities {
console.warn('Capabilities unsupported, ignored')
return {}
}

clone (): this {
console.warn('Track clonning is unsupported, returned this instance')
return this
}

get kind (): string {
return this.#kind
}

get enabled (): boolean | null {
return this.track?.isOpen()
}

set enabled (_) {
console.warn('Track enabling and disabling is unsupported, ignored')
}

get muted (): boolean {
return false
}

get id (): string {
return this.#id
}

get label (): string {
return this.#label
}

get readyState (): 'ended' | 'live' {
return this.track?.isClosed() ? 'ended' : 'live'
}
}

/**
* @class
* @implements {globalThis.MediaStream}
*/
export class MediaStream extends EventTarget {
#active = true
#id = crypto.randomUUID()
#tracks = new Set<MediaStreamTrack>()
onaddtrack
onremovetrack
onactive
oninactive

constructor (streamOrTracks) {
super()
if (streamOrTracks instanceof MediaStream) {
for (const track of streamOrTracks.getTracks()) {
this.addTrack(track)
}
} else if (Array.isArray(streamOrTracks)) {
for (const track of streamOrTracks) {
this.addTrack(track)
}
}
this.addEventListener('active', e => {
this.onactive?.(e)
})
this.addEventListener('inactive', e => {
this.oninactive?.(e)
})
this.addEventListener('removetrack', e => {
this.onremovetrack?.(e)
})
this.addEventListener('addtrack', e => {
this.onaddtrack?.(e)
})
this.dispatchEvent(new Event('active'))
}

get active (): boolean {
return this.#active
}

get id (): string {
return this.#id
}

addTrack (track) {
this.#tracks.add(track)
this.dispatchEvent(new MediaStreamTrackEvent('addtrack', { track }))
}

getTracks (): MediaStreamTrack[] {
return [...this.#tracks]
}

getVideoTracks (): MediaStreamTrack[] {
return [...this.#tracks].filter(({ kind }) => kind === 'video')
}

getAudioTracks (): MediaStreamTrack[] {
return [...this.#tracks].filter(({ kind }) => kind === 'audio')
}

getTrackById (id): MediaStreamTrack {
return [...this.#tracks].find(track => track.id === id) ?? null
}

removeTrack (track): void {
this.#tracks.delete(track)
this.dispatchEvent(new MediaStreamTrackEvent('removetrack', { track }))
}

clone (): MediaStream {
return new MediaStream([...this.getTracks()])
}

stop ():void {
for (const track of this.getTracks()) {
track.stop()
}
this.#active = false
this.dispatchEvent(new Event('inactive'))
}
}
4 changes: 4 additions & 0 deletions src/polyfill/RTCCertificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ export default class RTCCertificate implements globalThis.RTCCertificate {
getFingerprints(): globalThis.RTCDtlsFingerprint[] {
return this.#fingerprints;
}

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