-
-
Notifications
You must be signed in to change notification settings - Fork 527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix a bug when parsing multiple responses #115
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -304,43 +304,43 @@ module.exports = class Connection { | |
|
||
this.buffer = Buffer.concat([this.buffer, rawData]) | ||
|
||
// Not enough bytes to read the expected response size, keep buffering | ||
if (Buffer.byteLength(this.buffer) <= Decoder.int32Size()) { | ||
return | ||
} | ||
|
||
const data = Buffer.from(this.buffer) | ||
const decoder = new Decoder(data) | ||
const expectedResponseSize = decoder.readInt32() | ||
// Process data if there are enough bytes to read the expected response size, | ||
// otherwise keep buffering | ||
while (Buffer.byteLength(this.buffer) > Decoder.int32Size()) { | ||
const data = Buffer.from(this.buffer) | ||
const decoder = new Decoder(data) | ||
const expectedResponseSize = decoder.readInt32() | ||
|
||
if (!decoder.canReadBytes(expectedResponseSize)) { | ||
return | ||
} | ||
|
||
if (!decoder.canReadBytes(expectedResponseSize)) { | ||
return | ||
} | ||
const response = new Decoder(decoder.readBytes(expectedResponseSize)) | ||
// Reset the buffer as the rest of the bytes | ||
this.buffer = decoder.readAll() | ||
|
||
// The full payload is loaded, erase the temporary buffer | ||
this.buffer = Buffer.alloc(0) | ||
if (this.authHandlers) { | ||
return this.authHandlers.onSuccess(data.slice(0, this.offset + byteLength)) | ||
} | ||
|
||
if (this.authHandlers) { | ||
return this.authHandlers.onSuccess(data) | ||
} | ||
const correlationId = response.readInt32() | ||
const payload = response.readAll() | ||
|
||
const correlationId = decoder.readInt32() | ||
const payload = decoder.readAll() | ||
const entry = this.pendingQueue[correlationId] | ||
delete this.pendingQueue[correlationId] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should remove the previous data from the buffer, I fear to end up leaking memory. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we are okey, since the buffer is reset using |
||
|
||
const entry = this.pendingQueue[correlationId] | ||
delete this.pendingQueue[correlationId] | ||
if (!entry) { | ||
this.logDebug(`Response without match`, { correlationId }) | ||
return | ||
} | ||
|
||
if (!entry) { | ||
this.logDebug(`Response without match`, { correlationId }) | ||
return | ||
entry.resolve({ | ||
size: expectedResponseSize, | ||
correlationId, | ||
entry, | ||
payload, | ||
}) | ||
} | ||
|
||
entry.resolve({ | ||
size: expectedResponseSize, | ||
correlationId, | ||
entry, | ||
payload, | ||
}) | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
byteLength
isn't defined, this is breaking the testsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch! copy-paste bug...