diff --git a/lib/packets/packet.js b/lib/packets/packet.js index ccf3a8458cf..dc39cdbe341 100644 --- a/lib/packets/packet.js +++ b/lib/packets/packet.js @@ -222,9 +222,8 @@ 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; + res = res.greaterThan(Number.MAX_SAFE_INTEGER) ? res.toNumber() : resString; return bigNumberStrings ? resString : res; } // eslint-disable-next-line no-console @@ -425,7 +424,7 @@ class Packet { return StringParser.decode( this.buffer, encoding, - this.offset - len, + this.offset - len, this.offset ); } @@ -456,15 +455,12 @@ class Packet { 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; + } + return result.toNumber(); } } if (this.buffer[this.offset] === plus) {