diff --git a/src/parser/calculate_size.ts b/src/parser/calculate_size.ts index da62ae6c..557d15a8 100644 --- a/src/parser/calculate_size.ts +++ b/src/parser/calculate_size.ts @@ -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'; @@ -208,6 +208,10 @@ function calculateElement( 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; diff --git a/test/node/parser/calculate_size.test.ts b/test/node/parser/calculate_size.test.ts index e421c28f..d7d3387c 100644 --- a/test/node/parser/calculate_size.test.ts +++ b/test/node/parser/calculate_size.test.ts @@ -25,9 +25,15 @@ describe('calculateSize()', () => { ).to.throw(BSONVersionError, /Unsupported BSON version/i); }); - it('returns the correct size for a bigint value', function () { + it('returns 8 bytes (+7 meta bytes) size for a bigint value', function () { const doc = { a: BigInt(1) }; expect(BSON.calculateObjectSize(doc)).to.equal(16); expect(BSON.calculateObjectSize(doc)).to.equal(BSON.serialize(doc).byteLength); }); + + it('returns 0 bytes (+5 meta bytes) for a symbol value', function () { + const doc = { a: Symbol() }; + expect(BSON.calculateObjectSize(doc)).to.equal(5); + expect(BSON.calculateObjectSize(doc)).to.equal(BSON.serialize(doc).byteLength); + }); });