diff --git a/lib/web/websocket/constants.js b/lib/web/websocket/constants.js index 2019b5b67a7..66f27245246 100644 --- a/lib/web/websocket/constants.js +++ b/lib/web/websocket/constants.js @@ -47,7 +47,7 @@ const parserStates = { const emptyBuffer = Buffer.allocUnsafe(0) const sendHints = { - string: 1, + text: 1, typedArray: 2, arrayBuffer: 3, blob: 4 diff --git a/lib/web/websocket/sender.js b/lib/web/websocket/sender.js index 1b1468d4ab9..1691854a5ae 100644 --- a/lib/web/websocket/sender.js +++ b/lib/web/websocket/sender.js @@ -4,9 +4,6 @@ const { WebsocketFrameSend } = require('./frame') const { opcodes, sendHints } = require('./constants') const FixedQueue = require('../../dispatcher/fixed-queue') -/** @type {typeof Uint8Array} */ -const FastBuffer = Buffer[Symbol.species] - /** * @typedef {object} SendQueueNode * @property {Promise | null} promise @@ -86,18 +83,17 @@ class SendQueue { } function createFrame (data, hint) { - return new WebsocketFrameSend(toBuffer(data, hint)).createFrame(hint === sendHints.string ? opcodes.TEXT : opcodes.BINARY) + return new WebsocketFrameSend(toBuffer(data, hint)).createFrame(hint === sendHints.text ? opcodes.TEXT : opcodes.BINARY) } function toBuffer (data, hint) { switch (hint) { - case sendHints.string: - return Buffer.from(data) + case sendHints.text: + case sendHints.typedArray: + return new Uint8Array(data.buffer, data.byteOffset, data.byteLength) case sendHints.arrayBuffer: case sendHints.blob: - return new FastBuffer(data) - case sendHints.typedArray: - return new FastBuffer(data.buffer, data.byteOffset, data.byteLength) + return new Uint8Array(data) } } diff --git a/lib/web/websocket/websocket.js b/lib/web/websocket/websocket.js index 109d7be2e2f..59de73bfac9 100644 --- a/lib/web/websocket/websocket.js +++ b/lib/web/websocket/websocket.js @@ -234,12 +234,12 @@ class WebSocket extends EventTarget { // the bufferedAmount attribute by the number of bytes needed to // express the argument as UTF-8. - const length = Buffer.byteLength(data) + const buffer = Buffer.from(data) - this.#bufferedAmount += length - this.#sendQueue.add(data, () => { - this.#bufferedAmount -= length - }, sendHints.string) + this.#bufferedAmount += buffer.byteLength + this.#sendQueue.add(buffer, () => { + this.#bufferedAmount -= buffer.byteLength + }, sendHints.text) } else if (types.isArrayBuffer(data)) { // If the WebSocket connection is established, and the WebSocket // closing handshake has not yet started, then the user agent must