-
-
Notifications
You must be signed in to change notification settings - Fork 71
fix: RTCDataChannel readyState race condition and improve typings #359
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
base: master
Are you sure you want to change the base?
Changes from 7 commits
13dd568
5863b40
6e22bd1
37e5283
c62b15c
05381db
5100ad9
0ec58d2
ddce92f
b291be2
2168c78
f3c71ec
dc6a887
0081313
9cbbf63
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,8 +16,6 @@ export default class RTCDataChannel extends EventTarget implements globalThis.RT | |
| #label: string; | ||
| #protocol: string; | ||
|
|
||
| #closeRequested = false; | ||
|
|
||
| // events | ||
| onbufferedamountlow: globalThis.RTCDataChannel['onbufferedamountlow'] = null; | ||
| onclose: globalThis.RTCDataChannel['onclose'] = null; | ||
|
|
@@ -49,14 +47,18 @@ export default class RTCDataChannel extends EventTarget implements globalThis.RT | |
|
|
||
| this.#dataChannel.onClosed(() => { | ||
| // Simulate closing event | ||
| if (!this.#closeRequested) { | ||
| if (this.#readyState === 'closed') return; | ||
|
|
||
| if (this.#readyState !== 'closing') { | ||
| this.#readyState = 'closing'; | ||
| this.dispatchEvent(new Event('closing')); | ||
| } | ||
|
|
||
| setImmediate(() => { | ||
| this.#readyState = 'closed'; | ||
| this.dispatchEvent(new Event('close')); | ||
| if (this.#readyState !== 'closed') { | ||
| this.#readyState = 'closed'; | ||
| this.dispatchEvent(new Event('close')); | ||
| } | ||
| }); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to use |
||
| }); | ||
|
|
||
|
|
@@ -79,32 +81,22 @@ export default class RTCDataChannel extends EventTarget implements globalThis.RT | |
|
|
||
| this.#dataChannel.onMessage((data) => { | ||
| if (ArrayBuffer.isView(data)) { | ||
| if (this.binaryType == 'arraybuffer') data = data.buffer; | ||
| else data = Buffer.from(data.buffer); | ||
| data = | ||
| this.binaryType === 'arraybuffer' | ||
| ? (data.buffer as ArrayBuffer) | ||
| : Buffer.from(data.buffer); | ||
| } | ||
|
|
||
| this.dispatchEvent(new MessageEvent('message', { data })); | ||
| }); | ||
|
|
||
| // forward events to properties | ||
| this.addEventListener('message', (e) => { | ||
| if (this.onmessage) this.onmessage(e as MessageEvent); | ||
| }); | ||
| this.addEventListener('bufferedamountlow', (e) => { | ||
| if (this.onbufferedamountlow) this.onbufferedamountlow(e); | ||
| }); | ||
| this.addEventListener('error', (e) => { | ||
| if (this.onerror) this.onerror(e as RTCErrorEvent); | ||
| }); | ||
| this.addEventListener('close', (e) => { | ||
| if (this.onclose) this.onclose(e); | ||
| }); | ||
| this.addEventListener('closing', (e) => { | ||
| if (this.onclosing) this.onclosing(e); | ||
| }); | ||
| this.addEventListener('open', (e) => { | ||
| if (this.onopen) this.onopen(e); | ||
| }); | ||
| this.addEventListener('open', (e) => this.onopen?.(e)); | ||
| this.addEventListener('message', (e) => this.onmessage?.(e as MessageEvent)); | ||
| this.addEventListener('error', (e) => this.onerror?.(e as RTCErrorEvent)); | ||
| this.addEventListener('close', (e) => this.onclose?.(e)); | ||
| this.addEventListener('closing', (e) => this.onclosing?.(e)); | ||
| this.addEventListener('bufferedamountlow', (e) => this.onbufferedamountlow?.(e)); | ||
| } | ||
|
|
||
| set binaryType(type) { | ||
|
|
@@ -168,7 +160,7 @@ export default class RTCDataChannel extends EventTarget implements globalThis.RT | |
| return this.#readyState; | ||
| } | ||
|
|
||
| send(data): void { | ||
| send(data: string | Blob | ArrayBuffer | ArrayBufferView): void { | ||
| if (this.#readyState !== 'open') { | ||
| throw new exceptions.InvalidStateError( | ||
| "Failed to execute 'send' on 'RTCDataChannel': RTCDataChannel.readyState is not 'open'", | ||
|
|
@@ -190,15 +182,19 @@ export default class RTCDataChannel extends EventTarget implements globalThis.RT | |
| this.#dataChannel.sendMessageBinary(data); | ||
| } else { | ||
| if (process?.versions?.bun) { | ||
| this.#dataChannel.sendMessageBinary(Buffer.from(data)); | ||
| this.#dataChannel.sendMessageBinary(Buffer.from(data as ArrayBuffer)); | ||
| } else { | ||
| this.#dataChannel.sendMessageBinary(new Uint8Array(data)); | ||
| this.#dataChannel.sendMessageBinary(new Uint8Array(data as ArrayBuffer)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| close(): void { | ||
| this.#closeRequested = true; | ||
| if (this.#readyState === 'closing' || this.#readyState === 'closed') return; | ||
|
|
||
| this.#readyState = 'closing'; | ||
| this.dispatchEvent(new Event('closing')); | ||
|
|
||
| setImmediate(() => { | ||
| this.#dataChannel.close(); | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -354,6 +354,13 @@ export default class RTCPeerConnection extends EventTarget implements globalThis | |
| } | ||
|
|
||
| close(): void { | ||
| for (const dc of this.#dataChannels) { | ||
| if (dc.readyState !== 'closed') { | ||
| dc.close(); | ||
|
||
| } | ||
| } | ||
| this.#dataChannels.clear(); | ||
|
|
||
| this.#peerConnection.close(); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic was incorrect. Why would we simulate a "closing" event when the data channel is already closed? Instead, we start the "closing" state at the beginning of the
<RTCDataChannel>.close()method, not here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a gap in the libdatachannel API. Step 4 of 6.2.4 says:
We don't get a "closing" notification from libdatachannel before the channel is actually closed so this is the best we can do in order to emit the events required by the spec.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So how are we gonna handle both cases?
Step 5 of https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close
NOTE
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The linked case is when the RTCPeerConnection is explicitly closed by the user so set a flag or a status and use that to decide whether or not to emit the event(s)?