@@ -100,20 +100,47 @@ Parser.prototype.append = function append(chunk) {
100
100
return ;
101
101
}
102
102
103
- var buffer = chunk ;
103
+ // Calculate slice ranges
104
104
var sliceEnd = this . _buffer . length ;
105
105
var sliceStart = this . _packetOffset === null
106
106
? this . _offset
107
107
: this . _packetOffset ;
108
108
var sliceLength = sliceEnd - sliceStart ;
109
109
110
+ // Get chunk data
111
+ var buffer = null ;
112
+ var chunks = ! ( chunk instanceof Array || Array . isArray ( chunk ) ) ? [ chunk ] : chunk ;
113
+ var length = 0 ;
114
+ var offset = 0 ;
115
+
116
+ for ( var i = 0 ; i < chunks . length ; i ++ ) {
117
+ length += chunks [ i ] . length ;
118
+ }
119
+
110
120
if ( sliceLength !== 0 ) {
111
121
// Create a new Buffer
112
- buffer = new Buffer ( sliceLength + chunk . length ) ;
122
+ buffer = new Buffer ( sliceLength + length ) ;
123
+ offset = 0 ;
124
+
125
+ // Copy data slice
126
+ offset += this . _buffer . copy ( buffer , 0 , sliceStart , sliceEnd ) ;
113
127
114
- // Copy data
115
- this . _buffer . copy ( buffer , 0 , sliceStart , sliceEnd ) ;
116
- chunk . copy ( buffer , sliceLength ) ;
128
+ // Copy chunks
129
+ for ( var i = 0 ; i < chunks . length ; i ++ ) {
130
+ offset += chunks [ i ] . copy ( buffer , offset ) ;
131
+ }
132
+ } else if ( chunks . length > 1 ) {
133
+ // Create a new Buffer
134
+ buffer = new Buffer ( length ) ;
135
+ offset = 0 ;
136
+
137
+ // Copy chunks
138
+ for ( var i = 0 ; i < chunks . length ; i ++ ) {
139
+ offset += chunks [ i ] . copy ( buffer , offset ) ;
140
+ }
141
+ } else {
142
+ // Buffer is the only chunk
143
+ buffer = chunks [ 0 ] ;
117
144
}
118
145
119
146
// Adjust data-tracking pointers
@@ -384,19 +411,27 @@ Parser.prototype.packetLength = function packetLength() {
384
411
} ;
385
412
386
413
Parser . prototype . _combineNextBuffers = function _combineNextBuffers ( bytes ) {
387
- if ( ( this . _buffer . length - this . _offset ) >= bytes ) {
414
+ var length = this . _buffer . length - this . _offset ;
415
+
416
+ if ( length >= bytes ) {
388
417
return true ;
389
418
}
390
419
391
- if ( ! this . _nextBuffers . size ) {
420
+ if ( ( length + this . _nextBuffers . size ) < bytes ) {
392
421
return false ;
393
422
}
394
423
395
- while ( this . _nextBuffers . size && ( this . _buffer . length - this . _offset ) < bytes ) {
396
- this . append ( this . _nextBuffers . shift ( ) ) ;
424
+ var buffers = [ ] ;
425
+ var bytesNeeded = bytes - length ;
426
+
427
+ while ( bytesNeeded > 0 ) {
428
+ var buffer = this . _nextBuffers . shift ( ) ;
429
+ buffers . push ( buffer ) ;
430
+ bytesNeeded -= buffer . length ;
397
431
}
398
432
399
- return ( this . _buffer . length - this . _offset ) >= bytes ;
433
+ this . append ( buffers ) ;
434
+ return true ;
400
435
} ;
401
436
402
437
Parser . prototype . _combineLongPacketBuffers = function _combineLongPacketBuffers ( ) {
0 commit comments