Skip to content

Commit

Permalink
Another rewrite of the patch
Browse files Browse the repository at this point in the history
  • Loading branch information
Slava committed Mar 20, 2020
1 parent df1e4b3 commit 3789061
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions src/network/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit 3789061

Please sign in to comment.