From 89b165e6ee26cfc18b6651672e596bce5af971d2 Mon Sep 17 00:00:00 2001 From: Khafra Date: Thu, 12 Jan 2023 04:12:49 -0500 Subject: [PATCH] fix(websocket): simplify typedarray copying (#1854) * fix(websocket): simplify typedarray copying * fix: simplify even further * and again --- lib/websocket/websocket.js | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/lib/websocket/websocket.js b/lib/websocket/websocket.js index 366b6aa3e27..79c9be439c1 100644 --- a/lib/websocket/websocket.js +++ b/lib/websocket/websocket.js @@ -309,23 +309,14 @@ class WebSocket extends EventTarget { // not throw an exception must increase the bufferedAmount attribute // by the length of data’s buffer in bytes. - const ab = new ArrayBuffer(data.byteLength) + const ab = Buffer.from(data, data.byteOffset, data.byteLength) - if (Buffer.isBuffer(data)) { - // new Buffer signature is deprecated - Buffer.from(ab).set(data) - } else { - new data.constructor(ab).set(data) - } - - const value = Buffer.from(ab) - - const frame = new WebsocketFrameSend(value) + const frame = new WebsocketFrameSend(ab) const buffer = frame.createFrame(opcodes.BINARY) - this.#bufferedAmount += value.byteLength + this.#bufferedAmount += ab.byteLength socket.write(buffer, () => { - this.#bufferedAmount -= value.byteLength + this.#bufferedAmount -= ab.byteLength }) } else if (isBlobLike(data)) { // If the WebSocket connection is established, and the WebSocket