diff --git a/src/network/connection.js b/src/network/connection.js index ce53cd6d4..bb6e0993d 100644 --- a/src/network/connection.js +++ b/src/network/connection.js @@ -378,19 +378,11 @@ module.exports = class Connection { this.chunks.push(rawData) this.bytesBuffered += Buffer.byteLength(rawData) - // Return early if more bytes are needed to read the next messzge - if (this.bytesNeeded > this.bytesBuffered) { - return - } - - const buffer = Buffer.concat(this.chunks) - this.chunks = [] - this.bytesBuffered = 0 - this.bytesNeeded = Decoder.int32Size() - // Process data if there are enough bytes to read the expected response size, // otherwise keep buffering - while (Buffer.byteLength(buffer) > Decoder.int32Size()) { + while (this.bytesNeeded <= this.bytesBuffered) { + const buffer = this.chunks.length > 1 ? Buffer.concat(this.chunks) : this.chunks[0] + // create another view of the buffer that can be mutated const data = Buffer.from(buffer) const decoder = new Decoder(data) const expectedResponseSize = decoder.readInt32() @@ -409,7 +401,7 @@ module.exports = class Connection { const remainderBuffer = decoder.readAll() this.chunks = [remainderBuffer] this.bytesBuffered = Buffer.byteLength(remainderBuffer) - this.bytesNeeded = Decoder.int32Size() + expectedResponseSize + this.bytesNeeded = Decoder.int32Size() if (this.authHandlers) { const rawResponseSize = Decoder.int32Size() + expectedResponseSize