diff --git a/lib/web/websocket/util.js b/lib/web/websocket/util.js index 1354e07ac48..b023e1f1f60 100644 --- a/lib/web/websocket/util.js +++ b/lib/web/websocket/util.js @@ -104,7 +104,7 @@ function websocketMessageReceived (ws, type, data) { // -> type indicates that the data is Binary and binary type is "arraybuffer" // a new ArrayBuffer object, created in the relevant Realm of the // WebSocket object, whose contents are data - dataForEvent = new Uint8Array(data).buffer + dataForEvent = toArrayBuffer(data) } } @@ -117,6 +117,13 @@ function websocketMessageReceived (ws, type, data) { }) } +function toArrayBuffer (buffer) { + if (buffer.byteLength === buffer.buffer.byteLength) { + return buffer.buffer + } + return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength) +} + /** * @see https://datatracker.ietf.org/doc/html/rfc6455 * @see https://datatracker.ietf.org/doc/html/rfc2616