Skip to content

Commit

Permalink
fix: don't convert BigInt type to Number before writing to buffer to …
Browse files Browse the repository at this point in the history
…prevent RangeError (#1642)
  • Loading branch information
paulish authored Jul 4, 2024
1 parent 3cd7e86 commit fc4ec02
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/data-types/bigint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},

Expand Down
16 changes: 15 additions & 1 deletion test/unit/int-data-type.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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);
});
});
});
});

0 comments on commit fc4ec02

Please sign in to comment.