Skip to content

Commit 13523fd

Browse files
authored
websocket: pre-calculated length (#3284)
1 parent 064b08d commit 13523fd

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/web/websocket/receiver.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
failWebsocketConnection,
1212
websocketMessageReceived,
1313
utf8Decode,
14+
bufferConcat,
1415
isControlFrame,
1516
isTextBinaryFrame,
1617
isContinuationFrame
@@ -33,6 +34,7 @@ class ByteParser extends Writable {
3334

3435
#info = {}
3536
#fragments = []
37+
#fragmentsBytes = 0
3638

3739
/** @type {Map<string, PerMessageDeflate>} */
3840
#extensions
@@ -208,15 +210,17 @@ class ByteParser extends Writable {
208210
} else {
209211
if (!this.#info.compressed) {
210212
this.#fragments.push(body)
213+
this.#fragmentsBytes += body.length
211214

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

222226
this.#state = parserStates.INFO
@@ -236,12 +240,13 @@ class ByteParser extends Writable {
236240
return
237241
}
238242

239-
websocketMessageReceived(this.ws, this.#info.binaryType, Buffer.concat(this.#fragments))
243+
websocketMessageReceived(this.ws, this.#info.binaryType, bufferConcat(this.#fragments, this.#fragmentsBytes))
240244

241245
this.#loop = true
242246
this.#state = parserStates.INFO
243247
this.run(callback)
244248
this.#fragments.length = 0
249+
this.#fragmentsBytes = 0
245250
})
246251

247252
this.#loop = false

lib/web/websocket/util.js

+24
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,29 @@ const utf8Decode = hasIntl
294294
throw new TypeError('Invalid utf-8 received.')
295295
}
296296

297+
function bufferConcat (buffers, length) {
298+
if (buffers.length === 0 || length === 0) {
299+
return Buffer.allocUnsafe(0)
300+
}
301+
let offset = 0
302+
if (length === undefined) {
303+
length = 0
304+
for (let i = 0; i < buffers.length; ++i) {
305+
length += buffers[i].length
306+
}
307+
}
308+
const output = Buffer.allocUnsafe(length)
309+
for (let i = 0; i < buffers.length; ++i) {
310+
const buffer = buffers[i]
311+
output.set(buffer, offset)
312+
offset += buffer.length
313+
}
314+
if (offset < length) {
315+
output.fill(0, offset, length)
316+
}
317+
return output
318+
}
319+
297320
module.exports = {
298321
isConnecting,
299322
isEstablished,
@@ -305,6 +328,7 @@ module.exports = {
305328
failWebsocketConnection,
306329
websocketMessageReceived,
307330
utf8Decode,
331+
bufferConcat,
308332
isControlFrame,
309333
isContinuationFrame,
310334
isTextBinaryFrame,

0 commit comments

Comments
 (0)