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 on BigInt type values #397

Merged
merged 8 commits into from
Sep 28, 2020
Merged

fix: Throw on BigInt type values #397

merged 8 commits into from
Sep 28, 2020

Conversation

nbbeeken
Copy link
Contributor

Given JavaScript's new BigInt type the serializer will attempt to fit the value in the smallest possible type. In the following order: Int32, Int64, Decimal128. Any values larger than Decimal128's maximum representable range will result in a thrown TypeError

NODE-2529

There's no logic in here to deserialize to big int only to accept it and serialize it to one of our BSON types. In terms of platform support I only used the BigInt constructor not the 23n syntax so the code path will just go untravelled on older JS engines.

src/long.ts Outdated Show resolved Hide resolved
src/long.ts Outdated Show resolved Hide resolved
src/parser/serializer.ts Outdated Show resolved Hide resolved
Copy link
Member

@mbroadst mbroadst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧅 on wording, feel free to ignore and merge, but I felt compelled to leave a note

@@ -806,6 +806,8 @@ export function serializeInto(
index = serializeString(buffer, key, value, index, true);
} else if (typeof value === 'number') {
index = serializeNumber(buffer, key, value, index, true);
} else if (typeof value === 'bigint') {
throw new TypeError('Do not know how to serialize a BigInt, please use Decimal128');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new TypeError('Do not know how to serialize a BigInt, please use Decimal128');
throw new TypeError('Unsupported type BigInt, please use Decimal128');

this is just a deep nitpick, I think we do know how to serialize them, we just don't support them being passed in raw for now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just matches JSON's error precisely
But I think I agree and I'll change this

Given JavaScript's new BigInt type the serializer will attempt to fit the value in the smallest possible type. In the following order: Int32, Int64, Decimal128. Any values larger than Decimal128's maximum representable range will result in a thrown TypeError

NODE-2529
In order to ensure users get consistent and expected behavoir BigInts should be manually translated to a type which can fit the precision of the value
@nbbeeken nbbeeken changed the title feat: Add support for serializing BigInt fix: Throw on BigInt type values Sep 28, 2020
@nbbeeken nbbeeken merged commit 2dd54e5 into master Sep 28, 2020
@nbbeeken nbbeeken deleted the NODE-2529/bigint branch September 28, 2020 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants