@@ -9,6 +9,7 @@ function Parser(options) {
9
9
10
10
this . _supportBigNumbers = options . config && options . config . supportBigNumbers ;
11
11
this . _buffer = new Buffer ( 0 ) ;
12
+ this . _nextBuffers = [ ] ;
12
13
this . _longPacketBuffers = [ ] ;
13
14
this . _offset = 0 ;
14
15
this . _packetEnd = null ;
@@ -21,12 +22,12 @@ function Parser(options) {
21
22
this . _paused = false ;
22
23
}
23
24
24
- Parser . prototype . write = function ( buffer ) {
25
- this . append ( buffer ) ;
25
+ Parser . prototype . write = function write ( chunk ) {
26
+ this . _nextBuffers . push ( chunk ) ;
26
27
27
28
while ( ! this . _paused ) {
28
29
if ( ! this . _packetHeader ) {
29
- if ( this . _bytesRemaining ( ) < 4 ) {
30
+ if ( ! this . _combineNextBuffers ( 4 ) ) {
30
31
break ;
31
32
}
32
33
@@ -50,7 +51,7 @@ Parser.prototype.write = function(buffer) {
50
51
this . incrementPacketNumber ( ) ;
51
52
}
52
53
53
- if ( this . _bytesRemaining ( ) < this . _packetHeader . length ) {
54
+ if ( ! this . _combineNextBuffers ( this . _packetHeader . length ) ) {
54
55
break ;
55
56
}
56
57
@@ -141,7 +142,7 @@ Parser.prototype.peak = function() {
141
142
return this . _buffer [ this . _offset ] ;
142
143
} ;
143
144
144
- Parser . prototype . parseUnsignedNumber = function ( bytes ) {
145
+ Parser . prototype . parseUnsignedNumber = function parseUnsignedNumber ( bytes ) {
145
146
if ( bytes === 1 ) {
146
147
return this . _buffer [ this . _offset ++ ] ;
147
148
}
@@ -362,10 +363,6 @@ Parser.prototype.reachedPacketEnd = function() {
362
363
return this . _offset === this . _packetEnd ;
363
364
} ;
364
365
365
- Parser . prototype . _bytesRemaining = function ( ) {
366
- return this . _buffer . length - this . _offset ;
367
- } ;
368
-
369
366
Parser . prototype . incrementPacketNumber = function ( ) {
370
367
var currentPacketNumber = this . _nextPacketNumber ;
371
368
this . _nextPacketNumber = ( this . _nextPacketNumber + 1 ) % 256 ;
@@ -383,7 +380,23 @@ Parser.prototype.packetLength = function() {
383
380
} , this . _packetHeader . length ) ;
384
381
} ;
385
382
386
- Parser . prototype . _combineLongPacketBuffers = function ( ) {
383
+ Parser . prototype . _combineNextBuffers = function _combineNextBuffers ( bytes ) {
384
+ if ( ( this . _buffer . length - this . _offset ) >= bytes ) {
385
+ return true ;
386
+ }
387
+
388
+ if ( ! this . _nextBuffers . length ) {
389
+ return false ;
390
+ }
391
+
392
+ while ( this . _nextBuffers . length && ( this . _buffer . length - this . _offset ) < bytes ) {
393
+ this . append ( this . _nextBuffers . shift ( ) ) ;
394
+ }
395
+
396
+ return ( this . _buffer . length - this . _offset ) >= bytes ;
397
+ } ;
398
+
399
+ Parser . prototype . _combineLongPacketBuffers = function _combineLongPacketBuffers ( ) {
387
400
if ( ! this . _longPacketBuffers . length ) {
388
401
return ;
389
402
}
@@ -392,7 +405,7 @@ Parser.prototype._combineLongPacketBuffers = function() {
392
405
393
406
var length = this . _longPacketBuffers . reduce ( function ( length , buffer ) {
394
407
return length + buffer . length ;
395
- } , this . _bytesRemaining ( ) ) ;
408
+ } , ( this . _buffer . length - this . _offset ) ) ;
396
409
397
410
var combinedBuffer = new Buffer ( length ) ;
398
411
0 commit comments