@@ -77,6 +77,7 @@ class Receiver extends Writable {
7777
7878 this . _totalPayloadLength = 0 ;
7979 this . _messageLength = 0 ;
80+ this . _numFragments = 0 ;
8081 this . _fragments = [ ] ;
8182
8283 this . _errored = false ;
@@ -500,29 +501,26 @@ class Receiver extends Writable {
500501 return ;
501502 }
502503
504+ if ( this . _maxFragments > 0 && ++ this . _numFragments > this . _maxFragments ) {
505+ const error = this . createError (
506+ RangeError ,
507+ 'Too many message fragments' ,
508+ false ,
509+ 1008 ,
510+ 'WS_ERR_TOO_MANY_BUFFERED_PARTS'
511+ ) ;
512+
513+ cb ( error ) ;
514+ return ;
515+ }
516+
503517 if ( this . _compressed ) {
504518 this . _state = INFLATING ;
505519 this . decompress ( data , cb ) ;
506520 return ;
507521 }
508522
509523 if ( data . length ) {
510- if (
511- this . _maxFragments > 0 &&
512- this . _fragments . length >= this . _maxFragments
513- ) {
514- const error = this . createError (
515- RangeError ,
516- 'Too many message fragments' ,
517- false ,
518- 1008 ,
519- 'WS_ERR_TOO_MANY_BUFFERED_PARTS'
520- ) ;
521-
522- cb ( error ) ;
523- return ;
524- }
525-
526524 //
527525 // This message is not compressed so its length is the sum of the payload
528526 // length of all fragments.
@@ -562,22 +560,6 @@ class Receiver extends Writable {
562560 return ;
563561 }
564562
565- if (
566- this . _maxFragments > 0 &&
567- this . _fragments . length >= this . _maxFragments
568- ) {
569- const error = this . createError (
570- RangeError ,
571- 'Too many message fragments' ,
572- false ,
573- 1008 ,
574- 'WS_ERR_TOO_MANY_BUFFERED_PARTS'
575- ) ;
576-
577- cb ( error ) ;
578- return ;
579- }
580-
581563 this . _fragments . push ( buf ) ;
582564 }
583565
@@ -604,6 +586,7 @@ class Receiver extends Writable {
604586 this . _totalPayloadLength = 0 ;
605587 this . _messageLength = 0 ;
606588 this . _fragmented = 0 ;
589+ this . _numFragments = 0 ;
607590 this . _fragments = [ ] ;
608591
609592 if ( this . _opcode === 2 ) {
0 commit comments