Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

websocket: pre-calculated length #3284

Merged
merged 1 commit into from
May 21, 2024
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
9 changes: 7 additions & 2 deletions lib/web/websocket/receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
failWebsocketConnection,
websocketMessageReceived,
utf8Decode,
bufferConcat,
isControlFrame,
isTextBinaryFrame,
isContinuationFrame
Expand All @@ -33,6 +34,7 @@ class ByteParser extends Writable {

#info = {}
#fragments = []
#fragmentsBytes = 0

/** @type {Map<string, PerMessageDeflate>} */
#extensions
Expand Down Expand Up @@ -208,15 +210,17 @@ class ByteParser extends Writable {
} else {
if (!this.#info.compressed) {
this.#fragments.push(body)
this.#fragmentsBytes += body.length

// If the frame is not fragmented, a message has been received.
// If the frame is fragmented, it will terminate with a fin bit set
// and an opcode of 0 (continuation), therefore we handle that when
// parsing continuation frames, not here.
if (!this.#info.fragmented && this.#info.fin) {
const fullMessage = Buffer.concat(this.#fragments)
const fullMessage = bufferConcat(this.#fragments, this.#fragmentsBytes)
websocketMessageReceived(this.ws, this.#info.binaryType, fullMessage)
this.#fragments.length = 0
this.#fragmentsBytes = 0
}

this.#state = parserStates.INFO
Expand All @@ -236,12 +240,13 @@ class ByteParser extends Writable {
return
}

websocketMessageReceived(this.ws, this.#info.binaryType, Buffer.concat(this.#fragments))
websocketMessageReceived(this.ws, this.#info.binaryType, bufferConcat(this.#fragments, this.#fragmentsBytes))

this.#loop = true
this.#state = parserStates.INFO
this.run(callback)
this.#fragments.length = 0
this.#fragmentsBytes = 0
})

this.#loop = false
Expand Down
24 changes: 24 additions & 0 deletions lib/web/websocket/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,29 @@ const utf8Decode = hasIntl
throw new TypeError('Invalid utf-8 received.')
}

function bufferConcat (buffers, length) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Avoid instance assertion in Buffer.concat.

if (buffers.length === 0 || length === 0) {
return Buffer.allocUnsafe(0)
}
let offset = 0
if (length === undefined) {
length = 0
for (let i = 0; i < buffers.length; ++i) {
length += buffers[i].length
}
}
const output = Buffer.allocUnsafe(length)
for (let i = 0; i < buffers.length; ++i) {
const buffer = buffers[i]
output.set(buffer, offset)
offset += buffer.length
}
if (offset < length) {
output.fill(0, offset, length)
}
return output
}

module.exports = {
isConnecting,
isEstablished,
Expand All @@ -305,6 +328,7 @@ module.exports = {
failWebsocketConnection,
websocketMessageReceived,
utf8Decode,
bufferConcat,
isControlFrame,
isContinuationFrame,
isTextBinaryFrame,
Expand Down
Loading