@@ -2,15 +2,16 @@ var MAX_PACKET_LENGTH = Math.pow(2, 24) - 1;
2
2
var MUL_32BIT = Math . pow ( 2 , 32 ) ;
3
3
var PacketHeader = require ( './PacketHeader' ) ;
4
4
var BigNumber = require ( 'bignumber.js' ) ;
5
+ var BufferList = require ( './BufferList' ) ;
5
6
6
7
module . exports = Parser ;
7
8
function Parser ( options ) {
8
9
options = options || { } ;
9
10
10
11
this . _supportBigNumbers = options . config && options . config . supportBigNumbers ;
11
12
this . _buffer = new Buffer ( 0 ) ;
12
- this . _nextBuffers = [ ] ;
13
- this . _longPacketBuffers = [ ] ;
13
+ this . _nextBuffers = new BufferList ( ) ;
14
+ this . _longPacketBuffers = new BufferList ( ) ;
14
15
this . _offset = 0 ;
15
16
this . _packetEnd = null ;
16
17
this . _packetHeader = null ;
@@ -374,53 +375,56 @@ Parser.prototype.resetPacketNumber = function() {
374
375
this . _nextPacketNumber = 0 ;
375
376
} ;
376
377
377
- Parser . prototype . packetLength = function ( ) {
378
- return this . _longPacketBuffers . reduce ( function ( length , buffer ) {
379
- return length + buffer . length ;
380
- } , this . _packetHeader . length ) ;
378
+ Parser . prototype . packetLength = function packetLength ( ) {
379
+ if ( ! this . _packetHeader ) {
380
+ return null ;
381
+ }
382
+
383
+ return this . _packetHeader . length + this . _longPacketBuffers . size ;
381
384
} ;
382
385
383
386
Parser . prototype . _combineNextBuffers = function _combineNextBuffers ( bytes ) {
384
387
if ( ( this . _buffer . length - this . _offset ) >= bytes ) {
385
388
return true ;
386
389
}
387
390
388
- if ( ! this . _nextBuffers . length ) {
391
+ if ( ! this . _nextBuffers . size ) {
389
392
return false ;
390
393
}
391
394
392
- while ( this . _nextBuffers . length && ( this . _buffer . length - this . _offset ) < bytes ) {
395
+ while ( this . _nextBuffers . size && ( this . _buffer . length - this . _offset ) < bytes ) {
393
396
this . append ( this . _nextBuffers . shift ( ) ) ;
394
397
}
395
398
396
399
return ( this . _buffer . length - this . _offset ) >= bytes ;
397
400
} ;
398
401
399
402
Parser . prototype . _combineLongPacketBuffers = function _combineLongPacketBuffers ( ) {
400
- if ( ! this . _longPacketBuffers . length ) {
403
+ if ( ! this . _longPacketBuffers . size ) {
401
404
return ;
402
405
}
403
406
407
+ // Calculate bytes
408
+ var remainingBytes = this . _buffer . length - this . _offset ;
404
409
var trailingPacketBytes = this . _buffer . length - this . _packetEnd ;
405
410
406
- var length = this . _longPacketBuffers . reduce ( function ( length , buffer ) {
407
- return length + buffer . length ;
408
- } , ( this . _buffer . length - this . _offset ) ) ;
409
-
410
- var combinedBuffer = new Buffer ( length ) ;
411
+ // Create buffer
412
+ var buf = null ;
413
+ var buffer = new Buffer ( remainingBytes + this . _longPacketBuffers . size ) ;
414
+ var offset = 0 ;
411
415
412
- var offset = this . _longPacketBuffers . reduce ( function ( offset , buffer ) {
413
- buffer . copy ( combinedBuffer , offset ) ;
414
- return offset + buffer . length ;
415
- } , 0 ) ;
416
+ // Copy long buffers
417
+ while ( ( buf = this . _longPacketBuffers . shift ( ) ) ) {
418
+ offset += buf . copy ( buffer , offset ) ;
419
+ }
416
420
417
- this . _buffer . copy ( combinedBuffer , offset , this . _offset ) ;
421
+ // Copy remaining bytes
422
+ this . _buffer . copy ( buffer , offset , this . _offset ) ;
418
423
419
- this . _buffer = combinedBuffer ;
420
- this . _longPacketBuffers = [ ] ;
421
- this . _offset = 0 ;
422
- this . _packetEnd = this . _buffer . length - trailingPacketBytes ;
423
- this . _packetOffset = 0 ;
424
+ this . _buffer = buffer ;
425
+ this . _offset = 0 ;
426
+ this . _packetEnd = this . _buffer . length - trailingPacketBytes ;
427
+ this . _packetOffset = 0 ;
424
428
} ;
425
429
426
430
Parser . prototype . _advanceToNextPacket = function ( ) {
0 commit comments