@@ -89,15 +89,15 @@ export class Parser {
89
89
90
90
public parse ( buffer : Buffer , callback : MessageCallback ) {
91
91
let combinedBuffer = buffer
92
- let combinedBufferOffset = 0
93
92
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 ) {
99
99
let newBufferLength = this . remainingBuffer . byteLength * 2
100
- while ( newRealLength >= newBufferLength ) {
100
+ while ( newLength >= newBufferLength ) {
101
101
newBufferLength *= 2
102
102
}
103
103
const newBuffer = Buffer . allocUnsafe ( newBufferLength )
@@ -112,12 +112,12 @@ export class Parser {
112
112
}
113
113
buffer . copy ( this . remainingBuffer , this . remainingBufferOffset + this . remainingBufferLength )
114
114
combinedBuffer = this . remainingBuffer
115
- combinedBufferLength = this . remainingBufferLength = newRealLength
115
+ combinedBufferLength = this . remainingBufferLength = newLength
116
116
combinedBufferOffset = this . remainingBufferOffset
117
117
}
118
- const realLength = combinedBufferOffset + combinedBufferLength
118
+ const fullLength = combinedBufferOffset + combinedBufferLength
119
119
let offset = combinedBufferOffset
120
- while ( offset + HEADER_LENGTH <= realLength ) {
120
+ while ( offset + HEADER_LENGTH <= fullLength ) {
121
121
// code is 1 byte long - it identifies the message type
122
122
const code = combinedBuffer [ offset ]
123
123
@@ -126,7 +126,7 @@ export class Parser {
126
126
127
127
const fullMessageLength = CODE_LENGTH + length
128
128
129
- if ( fullMessageLength + offset <= realLength ) {
129
+ if ( fullMessageLength + offset <= fullLength ) {
130
130
const message = this . handlePacket ( offset + HEADER_LENGTH , code , length , combinedBuffer )
131
131
callback ( message )
132
132
offset += fullMessageLength
@@ -135,14 +135,19 @@ export class Parser {
135
135
}
136
136
}
137
137
138
- if ( offset === realLength ) {
138
+ if ( offset === fullLength ) {
139
139
this . remainingBuffer = emptyBuffer
140
140
this . remainingBufferLength = 0
141
141
this . remainingBufferOffset = 0
142
142
} 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
+ }
146
151
}
147
152
}
148
153
0 commit comments