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(NODE-6608): calculateObjectSize returns the wrong value for bigint #742

Merged
merged 9 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .evergreen/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,10 @@ tasks:
- func: fetch source
vars:
NODE_LTS_VERSION: 16
NPM_VERSION: 9
- func: install dependencies
vars:
NODE_LTS_VERSION: 16
NPM_VERSION: 9
- func: run tests
vars:
Expand All @@ -143,6 +145,7 @@ tasks:
- func: fetch source
vars:
NODE_LTS_VERSION: 18
NPM_VERSION: 10
- func: install dependencies
- func: run tests
vars:
Expand Down Expand Up @@ -246,6 +249,7 @@ tasks:
vars:
# This needs to stay pinned at Node v18.16.0 for consistency across perf runs.
NODE_LTS_VERSION: v18.16.0
NPM_VERSION: 9
- func: install dependencies
vars:
NPM_VERSION: 9
Expand All @@ -262,6 +266,7 @@ tasks:
vars:
# This needs to stay pinned at Node v18.16.0 for consistency across perf runs.
NODE_LTS_VERSION: v18.16.0
NPM_VERSION: 9
- func: install dependencies
vars:
NPM_VERSION: 9
Expand All @@ -275,6 +280,7 @@ tasks:
vars:
# This needs to stay pinned at Node v18.16.0 for consistency across perf runs.
NODE_LTS_VERSION: v18.16.0
NPM_VERSION: 9
- func: install dependencies
vars:
NPM_VERSION: 9
Expand Down
2 changes: 2 additions & 0 deletions .evergreen/prepare-shell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ cat <<EOT > expansion.yml
CURRENT_VERSION: "$CURRENT_VERSION"
PROJECT_DIRECTORY: "$PROJECT_DIRECTORY"
NODE_LTS_VERSION: "$NODE_LTS_VERSION"
NPM_VERSION: "$NPM_VERSION"
DRIVERS_TOOLS: "$DRIVERS_TOOLS"
PREPARE_SHELL: |
set -o errexit
set -o xtrace
export PROJECT_DIRECTORY="$PROJECT_DIRECTORY"
export NODE_LTS_VERSION="$NODE_LTS_VERSION"
export NPM_VERSION="$NPM_VERSION"
export DRIVERS_TOOLS="$DRIVERS_TOOLS"
EOT
# See what we've done
Expand Down
9 changes: 8 additions & 1 deletion src/parser/calculate_size.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Binary } from '../binary';
import type { Document } from '../bson';
import { BSONVersionError } from '../error';
import { BSONError, BSONVersionError } from '../error';
import * as constants from '../constants';
import { ByteUtils } from '../utils/byte_utils';
import { isAnyArrayBuffer, isDate, isRegExp } from './utils';
Expand Down Expand Up @@ -205,6 +205,13 @@ function calculateElement(
1
);
}
return 0;
case 'bigint':
return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (8 + 1);
case 'symbol':
return 0;
default:
throw new BSONError(`Unrecognized JS type: ${typeof value}`);
}

return 0;
Expand Down
22 changes: 22 additions & 0 deletions test/node/parser/calculate_size.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,26 @@ describe('calculateSize()', () => {
})
).to.throw(BSONVersionError, /Unsupported BSON version/i);
});

describe('when given a bigint value with a single character key', function () {
beforeEach(function () {
if (BSON.__noBigInt__) {
this.currentTest?.skip();
}
});

it('returns 8 bytes (+4 bytes for document size + 1 type byte + 1 byte for "a" + 2 null terminators)', function () {
const doc = { a: BigInt(1) };
expect(BSON.calculateObjectSize(doc)).to.equal(8 + 4 + 1 + 1 + 1 + 1);
expect(BSON.calculateObjectSize(doc)).to.equal(BSON.serialize(doc).byteLength);
});
});

describe('when given a symbol value with a single character key', function () {
it('returns 0 bytes (+4 bytes for document size + 1 null terminator)', function () {
const doc = { a: Symbol() };
expect(BSON.calculateObjectSize(doc)).to.equal(4 + 1);
expect(BSON.calculateObjectSize(doc)).to.equal(BSON.serialize(doc).byteLength);
});
});
});
Loading