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
11 changes: 11 additions & 0 deletions lib/web/websocket/stream/websocketstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ class WebSocketStream {
}

#write (chunk) {
// See /websockets/stream/tentative/write.any.html
chunk = webidl.converters.WebSocketStreamWrite(chunk)

// 1. Let promise be a new promise created in stream ’s relevant realm .
const promise = createDeferredPromise()

Expand Down Expand Up @@ -484,4 +487,12 @@ webidl.converters.WebSocketCloseInfo = webidl.dictionaryConverter([
}
])

webidl.converters.WebSocketStreamWrite = function (V) {
if (typeof V === 'string') {
return webidl.converters.USVString(V)
}

return webidl.converters.BufferSource(V)
Copy link
Contributor

Choose a reason for hiding this comment

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

The only minor thing with this approach is that the failure message on passing an invalid type will only list ArrayBuffer and ArrayBufferView as valid types, not DOMString.

}

module.exports = { WebSocketStream }
6 changes: 2 additions & 4 deletions test/web-platform-tests/expectation.json
Original file line number Diff line number Diff line change
Expand Up @@ -39837,13 +39837,11 @@
},
{
"name": "writing a resizable ArrayBuffer should be rejected",
"success": false,
"message": "assert_unreached: Should have rejected: write() should reject Reached unreachable code"
"success": true
},
{
"name": "writing a view on a shared buffer should be rejected",
"success": false,
"message": "promise_rejects_js: write() should reject function \"function() { throw e; }\" threw object \"RangeError: Invalid string length\" (\"RangeError\") expected instance of function \"function TypeError() { [native code] }\" (\"TypeError\")"
"success": true
},
{
"name": "Garbage collecting a WebSocket stream doesn't crash while write promise is pending",
Expand Down
2 changes: 2 additions & 0 deletions types/webidl.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ interface WebidlConverters {
*/
EventHandlerNonNull (V: unknown): Function | null

WebSocketStreamWrite (V: unknown): ArrayBuffer | NodeJS.TypedArray | string

[Key: string]: (...args: any[]) => unknown
}

Expand Down
Loading