-
Notifications
You must be signed in to change notification settings - Fork 254
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
Conversation
There was a problem hiding this 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
src/parser/serializer.ts
Outdated
@@ -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'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
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
823f711
to
f6a51b2
Compare
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.