From fc4ec0273d96b3f467d14c7a30cc011e930c7430 Mon Sep 17 00:00:00 2001 From: Paul Ishenin Date: Fri, 5 Jul 2024 01:57:52 +0700 Subject: [PATCH] fix: don't convert BigInt type to Number before writing to buffer to prevent RangeError (#1642) --- src/data-types/bigint.ts | 2 +- test/unit/int-data-type.js | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/data-types/bigint.ts b/src/data-types/bigint.ts index c57ead905..cf28bbf2c 100644 --- a/src/data-types/bigint.ts +++ b/src/data-types/bigint.ts @@ -34,7 +34,7 @@ const BigInt: DataType = { } const buffer = new WritableTrackingBuffer(8); - buffer.writeInt64LE(Number(parameter.value)); + buffer.writeBigInt64LE(typeof parameter.value === 'bigint' ? parameter.value : globalThis.BigInt(parameter.value)); yield buffer.data; }, diff --git a/test/unit/int-data-type.js b/test/unit/int-data-type.js index a31ffbc38..676af8edf 100644 --- a/test/unit/int-data-type.js +++ b/test/unit/int-data-type.js @@ -1,5 +1,5 @@ const { assert } = require('chai'); -const { typeByName: { Int, SmallInt, TinyInt } } = require('../../src/data-type'); +const { typeByName: { Int, SmallInt, TinyInt, BigInt } } = require('../../src/data-type'); describe('integer-data-types', function() { describe('int data type test', function() { @@ -46,4 +46,18 @@ describe('integer-data-types', function() { }); }); }); + + describe('big int data type test', function() { + const params = [ + { param: { value: 9223372036854775807n }, expected: 9223372036854775807n }, + { param: { value: -9223372036854775808n }, expected: -9223372036854775808n } + ]; + + params.forEach(function(item) { + it('test valid parameter values', function() { + const buffer = Buffer.concat([...BigInt.generateParameterData(item.param, {})]); + assert.equal(buffer.readBigInt64LE(0), item.expected); + }); + }); + }); });