@@ -2,15 +2,16 @@ var MAX_PACKET_LENGTH = Math.pow(2, 24) - 1;
22var MUL_32BIT = Math . pow ( 2 , 32 ) ;
33var PacketHeader = require ( './PacketHeader' ) ;
44var BigNumber = require ( 'bignumber.js' ) ;
5+ var BufferList = require ( './BufferList' ) ;
56
67module . exports = Parser ;
78function Parser ( options ) {
89 options = options || { } ;
910
1011 this . _supportBigNumbers = options . config && options . config . supportBigNumbers ;
1112 this . _buffer = new Buffer ( 0 ) ;
12- this . _nextBuffers = [ ] ;
13- this . _longPacketBuffers = [ ] ;
13+ this . _nextBuffers = new BufferList ( ) ;
14+ this . _longPacketBuffers = new BufferList ( ) ;
1415 this . _offset = 0 ;
1516 this . _packetEnd = null ;
1617 this . _packetHeader = null ;
@@ -374,53 +375,56 @@ Parser.prototype.resetPacketNumber = function() {
374375 this . _nextPacketNumber = 0 ;
375376} ;
376377
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 ;
381384} ;
382385
383386Parser . prototype . _combineNextBuffers = function _combineNextBuffers ( bytes ) {
384387 if ( ( this . _buffer . length - this . _offset ) >= bytes ) {
385388 return true ;
386389 }
387390
388- if ( ! this . _nextBuffers . length ) {
391+ if ( ! this . _nextBuffers . size ) {
389392 return false ;
390393 }
391394
392- while ( this . _nextBuffers . length && ( this . _buffer . length - this . _offset ) < bytes ) {
395+ while ( this . _nextBuffers . size && ( this . _buffer . length - this . _offset ) < bytes ) {
393396 this . append ( this . _nextBuffers . shift ( ) ) ;
394397 }
395398
396399 return ( this . _buffer . length - this . _offset ) >= bytes ;
397400} ;
398401
399402Parser . prototype . _combineLongPacketBuffers = function _combineLongPacketBuffers ( ) {
400- if ( ! this . _longPacketBuffers . length ) {
403+ if ( ! this . _longPacketBuffers . size ) {
401404 return ;
402405 }
403406
407+ // Calculate bytes
408+ var remainingBytes = this . _buffer . length - this . _offset ;
404409 var trailingPacketBytes = this . _buffer . length - this . _packetEnd ;
405410
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 ;
411415
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+ }
416420
417- this . _buffer . copy ( combinedBuffer , offset , this . _offset ) ;
421+ // Copy remaining bytes
422+ this . _buffer . copy ( buffer , offset , this . _offset ) ;
418423
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 ;
424428} ;
425429
426430Parser . prototype . _advanceToNextPacket = function ( ) {
0 commit comments