Skip to content

Commit 1fed073

Browse files
authored
fix(NODE-6608): calculateObjectSize returns the wrong value for bigint (#742)
1 parent 26549d9 commit 1fed073

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

Diff for: .evergreen/config.yml

+6
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,10 @@ tasks:
131131
- func: fetch source
132132
vars:
133133
NODE_LTS_VERSION: 16
134+
NPM_VERSION: 9
134135
- func: install dependencies
135136
vars:
137+
NODE_LTS_VERSION: 16
136138
NPM_VERSION: 9
137139
- func: run tests
138140
vars:
@@ -143,6 +145,7 @@ tasks:
143145
- func: fetch source
144146
vars:
145147
NODE_LTS_VERSION: 18
148+
NPM_VERSION: 10
146149
- func: install dependencies
147150
- func: run tests
148151
vars:
@@ -246,6 +249,7 @@ tasks:
246249
vars:
247250
# This needs to stay pinned at Node v18.16.0 for consistency across perf runs.
248251
NODE_LTS_VERSION: v18.16.0
252+
NPM_VERSION: 9
249253
- func: install dependencies
250254
vars:
251255
NPM_VERSION: 9
@@ -262,6 +266,7 @@ tasks:
262266
vars:
263267
# This needs to stay pinned at Node v18.16.0 for consistency across perf runs.
264268
NODE_LTS_VERSION: v18.16.0
269+
NPM_VERSION: 9
265270
- func: install dependencies
266271
vars:
267272
NPM_VERSION: 9
@@ -275,6 +280,7 @@ tasks:
275280
vars:
276281
# This needs to stay pinned at Node v18.16.0 for consistency across perf runs.
277282
NODE_LTS_VERSION: v18.16.0
283+
NPM_VERSION: 9
278284
- func: install dependencies
279285
vars:
280286
NPM_VERSION: 9

Diff for: .evergreen/prepare-shell.sh

+2
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ cat <<EOT > expansion.yml
3333
CURRENT_VERSION: "$CURRENT_VERSION"
3434
PROJECT_DIRECTORY: "$PROJECT_DIRECTORY"
3535
NODE_LTS_VERSION: "$NODE_LTS_VERSION"
36+
NPM_VERSION: "$NPM_VERSION"
3637
DRIVERS_TOOLS: "$DRIVERS_TOOLS"
3738
PREPARE_SHELL: |
3839
set -o errexit
3940
set -o xtrace
4041
export PROJECT_DIRECTORY="$PROJECT_DIRECTORY"
4142
export NODE_LTS_VERSION="$NODE_LTS_VERSION"
43+
export NPM_VERSION="$NPM_VERSION"
4244
export DRIVERS_TOOLS="$DRIVERS_TOOLS"
4345
EOT
4446
# See what we've done

Diff for: src/parser/calculate_size.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Binary } from '../binary';
22
import type { Document } from '../bson';
3-
import { BSONVersionError } from '../error';
3+
import { BSONError, BSONVersionError } from '../error';
44
import * as constants from '../constants';
55
import { ByteUtils } from '../utils/byte_utils';
66
import { isAnyArrayBuffer, isDate, isRegExp } from './utils';
@@ -205,6 +205,13 @@ function calculateElement(
205205
1
206206
);
207207
}
208+
return 0;
209+
case 'bigint':
210+
return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (8 + 1);
211+
case 'symbol':
212+
return 0;
213+
default:
214+
throw new BSONError(`Unrecognized JS type: ${typeof value}`);
208215
}
209216

210217
return 0;

Diff for: test/node/parser/calculate_size.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,26 @@ describe('calculateSize()', () => {
2424
})
2525
).to.throw(BSONVersionError, /Unsupported BSON version/i);
2626
});
27+
28+
describe('when given a bigint value with a single character key', function () {
29+
beforeEach(function () {
30+
if (BSON.__noBigInt__) {
31+
this.currentTest?.skip();
32+
}
33+
});
34+
35+
it('returns 8 bytes (+4 bytes for document size + 1 type byte + 1 byte for "a" + 2 null terminators)', function () {
36+
const doc = { a: BigInt(1) };
37+
expect(BSON.calculateObjectSize(doc)).to.equal(8 + 4 + 1 + 1 + 1 + 1);
38+
expect(BSON.calculateObjectSize(doc)).to.equal(BSON.serialize(doc).byteLength);
39+
});
40+
});
41+
42+
describe('when given a symbol value with a single character key', function () {
43+
it('returns 0 bytes (+4 bytes for document size + 1 null terminator)', function () {
44+
const doc = { a: Symbol() };
45+
expect(BSON.calculateObjectSize(doc)).to.equal(4 + 1);
46+
expect(BSON.calculateObjectSize(doc)).to.equal(BSON.serialize(doc).byteLength);
47+
});
48+
});
2749
});

0 commit comments

Comments
 (0)