-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Throw error on bigint usage and add helpers to Long (#426)
Adds helpers toBigInt and fromBigInt to the Long class to help support 64bit values. Throws an error when bigint is attempted to be serialized. NODE-2378
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
} |