Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Throw error on bigint usage and add helpers to Long #426

Merged
merged 1 commit into from Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/bson/long.js
Expand Up @@ -77,6 +77,11 @@ Long.prototype.toNumber = function() {
return this.high_ * Long.TWO_PWR_32_DBL_ + this.getLowBitsUnsigned();
};

/** Converts the Long to a BigInt (arbitrary precision). */
Long.prototype.toBigInt = function () {
return BigInt(this.toString());
}

/**
* Return the JSON value.
*
Expand Down Expand Up @@ -711,6 +716,15 @@ Long.fromNumber = function(value) {
}
};

/**
* Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
* @param {bigint} value - The number in question
* @returns {Long} The corresponding Long value
*/
Long.fromBigInt = function(value) {
return Long.fromString(value.toString(10), 10);
}

/**
* Returns a Long representing the 64-bit integer that comes by concatenating the given high and low bits. Each is assumed to use 32 bits.
*
Expand Down
6 changes: 6 additions & 0 deletions lib/bson/parser/serializer.js
Expand Up @@ -709,6 +709,8 @@ var serializeInto = function serializeInto(
index = serializeString(buffer, key, value, index, true);
} else if (type === 'number') {
index = serializeNumber(buffer, key, value, index, true);
} else if(type === 'bigint') {
throw new TypeError('Unsupported type BigInt, please use Decimal128');
} else if (type === 'boolean') {
index = serializeBoolean(buffer, key, value, index, true);
} else if (value instanceof Date || isDate(value)) {
Expand Down Expand Up @@ -820,6 +822,8 @@ var serializeInto = function serializeInto(
index = serializeString(buffer, key, value, index);
} else if (type === 'number') {
index = serializeNumber(buffer, key, value, index);
} else if(type === 'bigint') {
throw new TypeError('Unsupported type BigInt, please use Decimal128');
} else if (type === 'boolean') {
index = serializeBoolean(buffer, key, value, index);
} else if (value instanceof Date || isDate(value)) {
Expand Down Expand Up @@ -923,6 +927,8 @@ var serializeInto = function serializeInto(
index = serializeString(buffer, key, value, index);
} else if (type === 'number') {
index = serializeNumber(buffer, key, value, index);
} else if(type === 'bigint') {
throw new TypeError('Unsupported type BigInt, please use Decimal128');
} else if (type === 'boolean') {
index = serializeBoolean(buffer, key, value, index);
} else if (value instanceof Date || isDate(value)) {
Expand Down
59 changes: 59 additions & 0 deletions test/node/bigint_test.js
@@ -0,0 +1,59 @@
/* globals BigInt */
'use strict';

var createBSON = require('../utils');
var BSON = require('../..');
var bson = createBSON();

try {
BigInt(0);

// will throw on the line above if BigInt is not supported in the runtime

exports['Should error on serialize bigint'] = function (test) {
var testDoc = { b: BigInt(32) };
try {
bson.serialize(testDoc)
test.ok(false);
} catch (error) {
test.ok(error instanceof TypeError);
test.ok(error.message === 'Unsupported type BigInt, please use Decimal128');
}
test.done();
};

exports['Should error on serialize bigint inside array'] = function (test) {
var testDoc = { b: [0, 1, BigInt(0x1ffffffff)] };
try {
bson.serialize(testDoc)
test.ok(false);
} catch (error) {
test.ok(error instanceof TypeError);
test.ok(error.message === 'Unsupported type BigInt, please use Decimal128');
}
test.done();
};

exports['Should error on serialize bigint inside subdocument'] = function (test) {
var testDoc = { b: { a: BigInt(0x1ffffffff) } };
try {
bson.serialize(testDoc)
test.ok(false);
} catch (error) {
test.ok(error instanceof TypeError);
test.ok(error.message === 'Unsupported type BigInt, please use Decimal128');
}
test.done();
};

exports['Should support conversion on Long type'] = function (test) {
var long = BSON.Long.fromBigInt(BigInt(200));
test.ok(long._bsontype === 'Long');
test.ok(long.toNumber() === 200);
test.ok(long.toBigInt() === BigInt(200));
test.done();
}

} catch (_) {
// 'JS VM does not support BigInt'
}