Skip to content

Commit c15d418

Browse files
committed
Trial for Parser state fix
1 parent 8eda615 commit c15d418

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

lib/protocol/Parser.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,25 +98,36 @@ Parser.prototype.write = function(buffer) {
9898
}
9999
};
100100

101-
Parser.prototype.append = function(newBuffer) {
102-
// If resume() is called, we don't pass a buffer to write()
103-
if (!newBuffer) {
101+
Parser.prototype.append = function append(chunk) {
102+
if (!chunk || chunk.length === 0) {
104103
return;
105104
}
106105

107-
var oldBuffer = this._buffer;
108-
var bytesRemaining = this._bytesRemaining();
109-
var newLength = bytesRemaining + newBuffer.length;
106+
var buffer = chunk;
107+
var sliceEnd = this._buffer.length;
108+
var sliceStart = this._packetOffset === null
109+
? this._offset
110+
: this._packetOffset;
111+
var sliceLength = sliceEnd - sliceStart;
110112

111-
var combinedBuffer = (this._offset > newLength)
112-
? oldBuffer.slice(0, newLength)
113-
: new Buffer(newLength);
113+
if (sliceLength !== 0) {
114+
// Create a new Buffer
115+
buffer = new Buffer(sliceLength + chunk.length);
114116

115-
oldBuffer.copy(combinedBuffer, 0, this._offset);
116-
newBuffer.copy(combinedBuffer, bytesRemaining);
117+
// Copy data
118+
this._buffer.copy(buffer, 0, sliceStart, sliceEnd);
119+
chunk.copy(buffer, sliceLength);
120+
}
117121

118-
this._buffer = combinedBuffer;
119-
this._offset = 0;
122+
// Adjust data-tracking pointers
123+
this._buffer = buffer;
124+
this._offset = this._offset - sliceStart;
125+
this._packetEnd = this._packetEnd !== null
126+
? this._packetEnd - sliceStart
127+
: null;
128+
this._packetOffset = this._packetOffset !== null
129+
? this._packetOffset - sliceStart
130+
: null;
120131
};
121132

122133
Parser.prototype.pause = function() {

0 commit comments

Comments
 (0)