Skip to content

Commit fcfa4db

Browse files
authored
revert: "websocket: pre-calculated length" (#3290)
* revert: "websocket: pre-calculated length" This reverts 13523fd which broke permessage-deflate support. See #3289 for why the tests did not catch this. * rerun autobahn
1 parent 0564b46 commit fcfa4db

2 files changed

Lines changed: 3 additions & 32 deletions

File tree

lib/web/websocket/receiver.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const {
1111
failWebsocketConnection,
1212
websocketMessageReceived,
1313
utf8Decode,
14-
bufferConcat,
1514
isControlFrame,
1615
isTextBinaryFrame,
1716
isContinuationFrame
@@ -34,7 +33,6 @@ class ByteParser extends Writable {
3433

3534
#info = {}
3635
#fragments = []
37-
#fragmentsBytes = 0
3836

3937
/** @type {Map<string, PerMessageDeflate>} */
4038
#extensions
@@ -210,17 +208,15 @@ class ByteParser extends Writable {
210208
} else {
211209
if (!this.#info.compressed) {
212210
this.#fragments.push(body)
213-
this.#fragmentsBytes += body.length
214211

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

226222
this.#state = parserStates.INFO
@@ -240,13 +236,12 @@ class ByteParser extends Writable {
240236
return
241237
}
242238

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

245241
this.#loop = true
246242
this.#state = parserStates.INFO
247-
this.run(callback)
248243
this.#fragments.length = 0
249-
this.#fragmentsBytes = 0
244+
this.run(callback)
250245
})
251246

252247
this.#loop = false

lib/web/websocket/util.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -294,29 +294,6 @@ 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-
320297
module.exports = {
321298
isConnecting,
322299
isEstablished,
@@ -328,7 +305,6 @@ module.exports = {
328305
failWebsocketConnection,
329306
websocketMessageReceived,
330307
utf8Decode,
331-
bufferConcat,
332308
isControlFrame,
333309
isContinuationFrame,
334310
isTextBinaryFrame,

0 commit comments

Comments
 (0)