Skip to content

Commit 1a5594f

Browse files
committed
fix(bson): bigint with isNaN checks
1 parent e4f22dd commit 1a5594f

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

packages/bson/src/bson-deserializer-templates.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ export function deserializeAny(type: Type, state: TemplateState) {
7878
const numberParsers = createParserLookup(() => 0, [
7979
[BSONType.INT, parser => parser.parseInt()],
8080
[BSONType.NUMBER, parser => parser.parseNumber()],
81-
[BSONType.LONG, parser => parser.parseLong()],
82-
[BSONType.TIMESTAMP, parser => parser.parseLong()],
81+
[BSONType.LONG, parser => Number(parser.parseLong())],
82+
[BSONType.TIMESTAMP, parser => Number(parser.parseLong())],
8383
[BSONType.BOOLEAN, parser => parser.parseBoolean() ? 1 : 0],
8484
[BSONType.BINARY, parser => Number(parser.parseBinaryBigInt())],
8585
[BSONType.STRING, parser => Number(parser.parseString())],

packages/bson/src/bson-serializer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ function sizerNumber(type: Type, state: TemplateState) {
909909
state.setContext({ getValueSize });
910910
//per default bigint will be serialized as long, to be compatible with default mongo driver and mongo database.
911911
//We should add a new annotation, maybe like `bigint & Binary` to make it binary (unlimited size)
912-
sizerPropertyNameAware(type, state, `(typeof ${state.accessor} === 'number' || typeof ${state.accessor} === 'bigint') && !Number.isNaN(${state.accessor})`, `
912+
sizerPropertyNameAware(type, state, `typeof ${state.accessor} === 'bigint' || (typeof ${state.accessor} === 'number' && !Number.isNaN(${state.accessor}))`, `
913913
state.size += getValueSize(${state.accessor});
914914
`);
915915
}
@@ -986,7 +986,7 @@ function sizerBigInt(type: TypeBigInt, state: TemplateState) {
986986
const bigIntSize = binaryBigInt === BinaryBigIntType.unsigned ? 'getBinaryBigIntSize' : 'getSignedBinaryBigIntSize';
987987
//per default bigint will be serialized as long, to be compatible with default mongo driver and mongo database.
988988
//We should add a new annotation, maybe like `bigint & Binary` to make it binary (unlimited size)
989-
sizerPropertyNameAware(type, state, `(typeof ${state.accessor} === 'number' || typeof ${state.accessor} === 'bigint') && !Number.isNaN(${state.accessor})`, `
989+
sizerPropertyNameAware(type, state, `typeof ${state.accessor} === 'bigint' || (typeof ${state.accessor} === 'number' && !Number.isNaN(${state.accessor}))`, `
990990
state.size += ${bigIntSize}(${state.accessor});
991991
`);
992992
} else {
@@ -1000,7 +1000,7 @@ function serializeBigInt(type: TypeBigInt, state: TemplateState) {
10001000
if (binaryBigInt !== undefined) {
10011001
const writeBigInt = binaryBigInt === BinaryBigIntType.unsigned ? 'writeBigIntBinary' : 'writeSignedBigIntBinary';
10021002
state.addCode(`
1003-
if (('bigint' === typeof ${state.accessor} || 'number' === typeof ${state.accessor}) && !Number.isNaN(${state.accessor})) {
1003+
if ('bigint' === typeof ${state.accessor} || ('number' === typeof ${state.accessor}) && !Number.isNaN(${state.accessor}))) {
10041004
//long
10051005
state.writer.writeType(${BSONType.BINARY});
10061006
state.writer.${writeBigInt}(${state.accessor});

0 commit comments

Comments
 (0)