diff --git a/lib/packets/packet.js b/lib/packets/packet.js index ccf3a8458cf..fc4d9c5388c 100644 --- a/lib/packets/packet.js +++ b/lib/packets/packet.js @@ -222,10 +222,9 @@ class Packet { return word1 * 0x100000000 + word0; } res = new Long(word0, word1, !signed); // Long need unsigned - const resNumber = res.toNumber(); const resString = res.toString(); - res = resNumber.toString() === resString ? resNumber : resString; - return bigNumberStrings ? resString : res; + const safeResNumberPreferred = res.greaterThan(Number.MAX_SAFE_INTEGER) || res.lessThan(Number.MIN_SAFE_INTEGER) ? resString : res.toNumber(); + return bigNumberStrings ? resString : safeResNumberPreferred; } // eslint-disable-next-line no-console console.trace(); @@ -425,7 +424,7 @@ class Packet { return StringParser.decode( this.buffer, encoding, - this.offset - len, + this.offset - len, this.offset ); } @@ -450,21 +449,18 @@ class Packet { this.offset++; sign = -1; } - // max precise int is 9007199254740992 + // max precise int is Number.MAX_SAFE_INTEGER 9007199254740991 let str; const numDigits = end - this.offset; if (supportBigNumbers) { if (numDigits >= 15) { str = this.readString(end - this.offset, 'binary'); - result = parseInt(str, 10); - if (result.toString() === str) { - return sign * result; - } - return sign === -1 ? `-${str}` : str; - } - if (numDigits > 16) { - str = this.readString(end - this.offset); - return sign === -1 ? `-${str}` : str; + str = sign === -1 ? `-${str}` : str; + result = Long.fromString(str, false, 10); + if (result.greaterThan(Number.MAX_SAFE_INTEGER) || result.lessThan(Number.MIN_SAFE_INTEGER)) { + return result.toString(); + } + return result.toNumber(); } } if (this.buffer[this.offset] === plus) { diff --git a/test/integration/connection/test-insert-bigint.js b/test/integration/connection/test-insert-bigint.js index b4e8f68a693..ecaf8d1786c 100644 --- a/test/integration/connection/test-insert-bigint.js +++ b/test/integration/connection/test-insert-bigint.js @@ -26,7 +26,7 @@ connection.query("INSERT INTO bigs SET title='test1'", (err, result) => { connection.query("INSERT INTO bigs SET title='test2'", (err, result) => { assert.strictEqual(result.insertId, 123456790); // big int - connection.query("INSERT INTO bigs SET title='test', id=9007199254740992"); + connection.query("INSERT INTO bigs SET title='test', id=9007199254740991"); connection.query("INSERT INTO bigs SET title='test3'", (err, result) => { assert.strictEqual( Long.fromString('9007199254740993').compare(result.insertId), @@ -51,7 +51,7 @@ connection.query("INSERT INTO bigs SET title='test1'", (err, result) => { assert.strictEqual(result[1].id, 124); assert.strictEqual(result[2].id, 123456789); assert.strictEqual(result[3].id, 123456790); - assert.strictEqual(result[4].id, 9007199254740992); + assert.strictEqual(result[4].id, 9007199254740991); assert.strictEqual(result[5].id, '9007199254740993'); assert.strictEqual(result[6].id, '90071992547409924'); assert.strictEqual(result[7].id, '90071992547409925');