@@ -89,15 +89,15 @@ export class Parser {
8989
9090 public parse ( buffer : Buffer , callback : MessageCallback ) {
9191 let combinedBuffer = buffer
92- let combinedBufferOffset = 0
9392 let combinedBufferLength = buffer . byteLength
94- let remainingBufferNotEmpty = this . remainingBufferLength > 0
95- if ( remainingBufferNotEmpty ) {
96- const newRealLength = this . remainingBufferLength + combinedBufferLength
97- const newLength = newRealLength + this . remainingBufferOffset
98- if ( newLength > this . remainingBuffer . byteLength ) {
93+ let combinedBufferOffset = 0
94+ let reuseRemainingBuffer = this . remainingBufferLength > 0
95+ if ( reuseRemainingBuffer ) {
96+ const newLength = this . remainingBufferLength + combinedBufferLength
97+ const newFullLength = newLength + this . remainingBufferOffset
98+ if ( newFullLength > this . remainingBuffer . byteLength ) {
9999 let newBufferLength = this . remainingBuffer . byteLength * 2
100- while ( newRealLength >= newBufferLength ) {
100+ while ( newLength >= newBufferLength ) {
101101 newBufferLength *= 2
102102 }
103103 const newBuffer = Buffer . allocUnsafe ( newBufferLength )
@@ -112,12 +112,12 @@ export class Parser {
112112 }
113113 buffer . copy ( this . remainingBuffer , this . remainingBufferOffset + this . remainingBufferLength )
114114 combinedBuffer = this . remainingBuffer
115- combinedBufferLength = this . remainingBufferLength = newRealLength
115+ combinedBufferLength = this . remainingBufferLength = newLength
116116 combinedBufferOffset = this . remainingBufferOffset
117117 }
118- const realLength = combinedBufferOffset + combinedBufferLength
118+ const fullLength = combinedBufferOffset + combinedBufferLength
119119 let offset = combinedBufferOffset
120- while ( offset + HEADER_LENGTH <= realLength ) {
120+ while ( offset + HEADER_LENGTH <= fullLength ) {
121121 // code is 1 byte long - it identifies the message type
122122 const code = combinedBuffer [ offset ]
123123
@@ -126,7 +126,7 @@ export class Parser {
126126
127127 const fullMessageLength = CODE_LENGTH + length
128128
129- if ( fullMessageLength + offset <= realLength ) {
129+ if ( fullMessageLength + offset <= fullLength ) {
130130 const message = this . handlePacket ( offset + HEADER_LENGTH , code , length , combinedBuffer )
131131 callback ( message )
132132 offset += fullMessageLength
@@ -135,14 +135,19 @@ export class Parser {
135135 }
136136 }
137137
138- if ( offset === realLength ) {
138+ if ( offset === fullLength ) {
139139 this . remainingBuffer = emptyBuffer
140140 this . remainingBufferLength = 0
141141 this . remainingBufferOffset = 0
142142 } else {
143- this . remainingBuffer = remainingBufferNotEmpty ? combinedBuffer : combinedBuffer . slice ( )
144- this . remainingBufferLength = combinedBufferLength - offset
145- this . remainingBufferOffset += offset
143+ if ( reuseRemainingBuffer ) {
144+ this . remainingBufferLength = combinedBufferLength - offset
145+ this . remainingBufferOffset += offset
146+ } else {
147+ this . remainingBuffer = combinedBuffer . slice ( offset )
148+ this . remainingBufferLength = this . remainingBuffer . byteLength
149+ this . remainingBufferOffset = 0
150+ }
146151 }
147152 }
148153
0 commit comments