Skip to content

Commit 1aa486e

Browse files
committed
Fix allocation errors receiving many result rows
fixes #918 fixes #1265 fixes #1324 fixes #1415
1 parent 1100f90 commit 1aa486e

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

Changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ you spot any mistakes.
1010
* Add `POOL_CONNLIMIT` code to "No connections available." error #1332
1111
* Bind underlying connections in pool to same domain as pool #1242
1212
* Bind underlying socket to same domain as connection #1243
13+
* Fix allocation errors receiving many result rows #918 #1265 #1324 #1415
1314
* Fix edge cases constructing long stack traces #1387
1415
* Fix handshake inactivity timeout on Node.js v4.2.0 #1223 #1236 #1239 #1240 #1241 #1252
1516
* Fix Query stream to emit close after ending #1349 #1350

lib/protocol/Parser.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function Parser(options) {
99

1010
this._supportBigNumbers = options.config && options.config.supportBigNumbers;
1111
this._buffer = new Buffer(0);
12+
this._nextBuffers = [];
1213
this._longPacketBuffers = [];
1314
this._offset = 0;
1415
this._packetEnd = null;
@@ -21,12 +22,12 @@ function Parser(options) {
2122
this._paused = false;
2223
}
2324

24-
Parser.prototype.write = function(buffer) {
25-
this.append(buffer);
25+
Parser.prototype.write = function write(chunk) {
26+
this._nextBuffers.push(chunk);
2627

2728
while (!this._paused) {
2829
if (!this._packetHeader) {
29-
if (this._bytesRemaining() < 4) {
30+
if (!this._combineNextBuffers(4)) {
3031
break;
3132
}
3233

@@ -50,7 +51,7 @@ Parser.prototype.write = function(buffer) {
5051
this.incrementPacketNumber();
5152
}
5253

53-
if (this._bytesRemaining() < this._packetHeader.length) {
54+
if (!this._combineNextBuffers(this._packetHeader.length)) {
5455
break;
5556
}
5657

@@ -141,7 +142,7 @@ Parser.prototype.peak = function() {
141142
return this._buffer[this._offset];
142143
};
143144

144-
Parser.prototype.parseUnsignedNumber = function(bytes) {
145+
Parser.prototype.parseUnsignedNumber = function parseUnsignedNumber(bytes) {
145146
if (bytes === 1) {
146147
return this._buffer[this._offset++];
147148
}
@@ -362,10 +363,6 @@ Parser.prototype.reachedPacketEnd = function() {
362363
return this._offset === this._packetEnd;
363364
};
364365

365-
Parser.prototype._bytesRemaining = function() {
366-
return this._buffer.length - this._offset;
367-
};
368-
369366
Parser.prototype.incrementPacketNumber = function() {
370367
var currentPacketNumber = this._nextPacketNumber;
371368
this._nextPacketNumber = (this._nextPacketNumber + 1) % 256;
@@ -383,7 +380,23 @@ Parser.prototype.packetLength = function() {
383380
}, this._packetHeader.length);
384381
};
385382

386-
Parser.prototype._combineLongPacketBuffers = function() {
383+
Parser.prototype._combineNextBuffers = function _combineNextBuffers(bytes) {
384+
if ((this._buffer.length - this._offset) >= bytes) {
385+
return true;
386+
}
387+
388+
if (!this._nextBuffers.length) {
389+
return false;
390+
}
391+
392+
while (this._nextBuffers.length && (this._buffer.length - this._offset) < bytes) {
393+
this.append(this._nextBuffers.shift());
394+
}
395+
396+
return (this._buffer.length - this._offset) >= bytes;
397+
};
398+
399+
Parser.prototype._combineLongPacketBuffers = function _combineLongPacketBuffers() {
387400
if (!this._longPacketBuffers.length) {
388401
return;
389402
}
@@ -392,7 +405,7 @@ Parser.prototype._combineLongPacketBuffers = function() {
392405

393406
var length = this._longPacketBuffers.reduce(function(length, buffer) {
394407
return length + buffer.length;
395-
}, this._bytesRemaining());
408+
}, (this._buffer.length - this._offset));
396409

397410
var combinedBuffer = new Buffer(length);
398411

0 commit comments

Comments
 (0)