From d58078ce62ae61b3230d7da5e397efbbad5cffbd Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Sat, 6 Jul 2024 17:18:14 +0900 Subject: [PATCH 1/2] websocket: avoid using Buffer.byteLength --- lib/web/websocket/sender.js | 9 +++------ lib/web/websocket/websocket.js | 8 ++++---- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/lib/web/websocket/sender.js b/lib/web/websocket/sender.js index 1b1468d4ab9..130024f2d58 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 @@ -92,12 +89,12 @@ function createFrame (data, hint) { function toBuffer (data, hint) { switch (hint) { case sendHints.string: - return Buffer.from(data) + return typeof data === 'string' ? Buffer.from(data) : data case sendHints.arrayBuffer: case sendHints.blob: - return new FastBuffer(data) + return new Uint8Array(data) case sendHints.typedArray: - return new FastBuffer(data.buffer, data.byteOffset, data.byteLength) + return new Uint8Array(data.buffer, data.byteOffset, data.byteLength) } } diff --git a/lib/web/websocket/websocket.js b/lib/web/websocket/websocket.js index 109d7be2e2f..9756c47449e 100644 --- a/lib/web/websocket/websocket.js +++ b/lib/web/websocket/websocket.js @@ -234,11 +234,11 @@ 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 + this.#bufferedAmount += buffer.byteLength + this.#sendQueue.add(buffer, () => { + this.#bufferedAmount -= buffer.byteLength }, sendHints.string) } else if (types.isArrayBuffer(data)) { // If the WebSocket connection is established, and the WebSocket From 8fb8f0f1f8af4f7aadfce8762ebccdf21abbe9e0 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Sun, 7 Jul 2024 13:28:54 +0900 Subject: [PATCH 2/2] rename sendHints.string to sendHints.text --- lib/web/websocket/constants.js | 2 +- lib/web/websocket/sender.js | 9 ++++----- lib/web/websocket/websocket.js | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) 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 130024f2d58..1691854a5ae 100644 --- a/lib/web/websocket/sender.js +++ b/lib/web/websocket/sender.js @@ -83,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 typeof data === 'string' ? Buffer.from(data) : data + case sendHints.text: + case sendHints.typedArray: + return new Uint8Array(data.buffer, data.byteOffset, data.byteLength) case sendHints.arrayBuffer: case sendHints.blob: return new Uint8Array(data) - case sendHints.typedArray: - return new Uint8Array(data.buffer, data.byteOffset, data.byteLength) } } diff --git a/lib/web/websocket/websocket.js b/lib/web/websocket/websocket.js index 9756c47449e..59de73bfac9 100644 --- a/lib/web/websocket/websocket.js +++ b/lib/web/websocket/websocket.js @@ -239,7 +239,7 @@ class WebSocket extends EventTarget { this.#bufferedAmount += buffer.byteLength this.#sendQueue.add(buffer, () => { this.#bufferedAmount -= buffer.byteLength - }, sendHints.string) + }, 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