-
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 on BigInt type values (#397)
Presently BigInt values were being lost in serialization silently. Add case and error on BigInt values to avoid loss of data. NODE-2529
- Loading branch information
Showing
5 changed files
with
110 additions
and
3 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
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,58 @@ | ||
/* globals BigInt */ | ||
'use strict'; | ||
|
||
const BSON = require('../register-bson'); | ||
|
||
describe('BSON BigInt Support', function () { | ||
before(function () { | ||
try { | ||
BigInt(0); | ||
} catch (_) { | ||
this.skip('JS VM does not support BigInt'); | ||
} | ||
}); | ||
it('Should serialize an int that fits in int32', function () { | ||
const testDoc = { b: BigInt(32) }; | ||
expect(() => BSON.serialize(testDoc)).to.throw(TypeError); | ||
|
||
// const serializedDoc = BSON.serialize(testDoc); | ||
// // prettier-ignore | ||
// const resultBuffer = Buffer.from([0x0C, 0x00, 0x00, 0x00, 0x10, 0x62, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00]); | ||
// const resultDoc = BSON.deserialize(serializedDoc); | ||
// expect(Array.from(serializedDoc)).to.have.members(Array.from(resultBuffer)); | ||
// expect(BigInt(resultDoc.b)).to.equal(testDoc.b); | ||
}); | ||
|
||
it('Should serialize an int that fits in int64', function () { | ||
const testDoc = { b: BigInt(0x1ffffffff) }; | ||
expect(() => BSON.serialize(testDoc)).to.throw(TypeError); | ||
|
||
// const serializedDoc = BSON.serialize(testDoc); | ||
// // prettier-ignore | ||
// const resultBuffer = Buffer.from([0x10, 0x00, 0x00, 0x00, 0x12, 0x62, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00]); | ||
// const resultDoc = BSON.deserialize(serializedDoc); | ||
// expect(Array.from(serializedDoc)).to.have.members(Array.from(resultBuffer)); | ||
// expect(BigInt(resultDoc.b)).to.equal(testDoc.b); | ||
}); | ||
|
||
it('Should serialize an int that fits in decimal128', function () { | ||
const testDoc = { b: BigInt('9223372036854776001') }; // int64 max + 1 | ||
expect(() => BSON.serialize(testDoc)).to.throw(TypeError); | ||
|
||
// const serializedDoc = BSON.serialize(testDoc); | ||
// // prettier-ignore | ||
// const resultBuffer = Buffer.from([0x18, 0x00, 0x00, 0x00, 0x13, 0x62, 0x00, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x30, 0x00]); | ||
// const resultDoc = BSON.deserialize(serializedDoc); | ||
// expect(Array.from(serializedDoc)).to.have.members(Array.from(resultBuffer)); | ||
// expect(resultDoc.b._bsontype).to.equal('Decimal128'); | ||
// expect(BigInt(resultDoc.b.toString())).to.equal(testDoc.b); | ||
}); | ||
|
||
it('Should throw if BigInt is too large to serialize', function () { | ||
const testDoc = { | ||
b: BigInt('9'.repeat(35)) | ||
}; // decimal 128 can only encode 34 digits of precision | ||
expect(() => BSON.serialize(testDoc)).to.throw(TypeError); | ||
// expect(() => BSON.serialize(testDoc)).to.throw(); | ||
}); | ||
}); |
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