@@ -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
0 commit comments