From 56e348df784d561c318f35fa90cb79ab995073db Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Thu, 12 Jan 2023 12:19:31 -0500 Subject: [PATCH] fix: comments --- src/bson.ts | 2 +- src/error.ts | 11 +++++++++++ src/extended_json.ts | 4 ++-- src/parser/calculate_size.ts | 4 ++-- src/parser/serializer.ts | 8 ++++---- test/node/cross_compat.test.ts | 6 +++--- test/node/error.test.ts | 12 +++++++++++- test/node/exports.test.ts | 1 + 8 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/bson.ts b/src/bson.ts index 574220d2..8ec644bf 100644 --- a/src/bson.ts +++ b/src/bson.ts @@ -50,7 +50,7 @@ export { Decimal128 }; export { BSONValue } from './bson_value'; -export { BSONError } from './error'; +export { BSONError, BSONVersionError } from './error'; export { BSONType } from './constants'; export { EJSON } from './extended_json'; diff --git a/src/error.ts b/src/error.ts index 88e16b3e..e4e108ad 100644 --- a/src/error.ts +++ b/src/error.ts @@ -43,3 +43,14 @@ export class BSONError extends Error { ); } } + +/** @public */ +export class BSONVersionError extends BSONError { + get name(): 'BSONVersionError' { + return 'BSONVersionError'; + } + + constructor() { + super('Unsupported BSON version, bson types must be from bson 5.0 or later'); + } +} diff --git a/src/extended_json.ts b/src/extended_json.ts index 7c32e577..e3f07c61 100644 --- a/src/extended_json.ts +++ b/src/extended_json.ts @@ -11,7 +11,7 @@ import { import { DBRef, isDBRefLike } from './db_ref'; import { Decimal128 } from './decimal128'; import { Double } from './double'; -import { BSONError } from './error'; +import { BSONError, BSONVersionError } from './error'; import { Int32 } from './int_32'; import { Long } from './long'; import { MaxKey } from './max_key'; @@ -318,7 +318,7 @@ function serializeDocument(doc: any, options: EJSONSerializeOptions) { typeof doc._bsontype === 'string' && doc[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION ) { - throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later'); + throw new BSONVersionError(); } else if (isBSONType(doc)) { // the "document" is really just a BSON type object // eslint-disable-next-line @typescript-eslint/no-explicit-any diff --git a/src/parser/calculate_size.ts b/src/parser/calculate_size.ts index 2794802f..4ac9fe42 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 { BSONError } from '../error'; +import { BSONVersionError } from '../error'; import * as constants from '../constants'; import { ByteUtils } from '../utils/byte_utils'; import { isAnyArrayBuffer, isDate, isRegExp } from './utils'; @@ -83,7 +83,7 @@ function calculateElement( typeof value._bsontype === 'string' && value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION ) { - throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later'); + throw new BSONVersionError(); } else if ( value == null || value['_bsontype'] === 'MinKey' || diff --git a/src/parser/serializer.ts b/src/parser/serializer.ts index 7cfa9a2e..b59a1b77 100644 --- a/src/parser/serializer.ts +++ b/src/parser/serializer.ts @@ -5,7 +5,7 @@ import * as constants from '../constants'; import type { DBRefLike } from '../db_ref'; import type { Decimal128 } from '../decimal128'; import type { Double } from '../double'; -import { BSONError } from '../error'; +import { BSONError, BSONVersionError } from '../error'; import type { Int32 } from '../int_32'; import { Long } from '../long'; import type { MinKey } from '../min_key'; @@ -710,7 +710,7 @@ export function serializeInto( typeof value === 'object' && value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION ) { - throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later'); + throw new BSONVersionError(); } else if (value._bsontype === 'ObjectId') { index = serializeObjectId(buffer, key, value, index); } else if (value._bsontype === 'Decimal128') { @@ -820,7 +820,7 @@ export function serializeInto( typeof value === 'object' && value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION ) { - throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later'); + throw new BSONVersionError(); } else if (value._bsontype === 'ObjectId') { index = serializeObjectId(buffer, key, value, index); } else if (type === 'object' && value._bsontype === 'Decimal128') { @@ -930,7 +930,7 @@ export function serializeInto( typeof value === 'object' && value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION ) { - throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later'); + throw new BSONVersionError(); } else if (value._bsontype === 'ObjectId') { index = serializeObjectId(buffer, key, value, index); } else if (type === 'object' && value._bsontype === 'Decimal128') { diff --git a/test/node/cross_compat.test.ts b/test/node/cross_compat.test.ts index 88d9c103..b5abd03d 100644 --- a/test/node/cross_compat.test.ts +++ b/test/node/cross_compat.test.ts @@ -64,9 +64,9 @@ const BSONTypeClasses = { }; describe('Prevent previous major versions from working with BSON v5 serialize and stringify', function () { - for (const [typeName, typeMaker] of Object.entries(BSONTypeClasses)) { + for (const [typeName, typeFactory] of Object.entries(BSONTypeClasses)) { it(`serialize throws if ${typeName} is missing a version symbol`, () => { - const type = typeMaker(); + const type = typeFactory(); Object.defineProperty(type, Symbol.for('@@mdb.bson.version'), { value: null }); // set an own property that overrides the getter expect(() => BSON.serialize({ type })).to.throw(/Unsupported BSON version/); expect(() => BSON.serialize({ a: [type] })).to.throw(/Unsupported BSON version/); @@ -74,7 +74,7 @@ describe('Prevent previous major versions from working with BSON v5 serialize an }); it(`stringify throws if ${typeName} is missing a version symbol`, () => { - const type = typeMaker(); + const type = typeFactory(); Object.defineProperty(type, Symbol.for('@@mdb.bson.version'), { value: null }); // set an own property that overrides the getter expect(() => EJSON.stringify({ type })).to.throw(/Unsupported BSON version/); expect(() => EJSON.stringify({ a: [type] })).to.throw(/Unsupported BSON version/); diff --git a/test/node/error.test.ts b/test/node/error.test.ts index 126e7f2b..8e1e7289 100644 --- a/test/node/error.test.ts +++ b/test/node/error.test.ts @@ -1,7 +1,7 @@ import { expect } from 'chai'; import { loadESModuleBSON } from '../load_bson'; -import { __isWeb__, BSONError } from '../register-bson'; +import { __isWeb__, BSONError, BSONVersionError } from '../register-bson'; const instanceOfChecksWork = !__isWeb__; @@ -82,4 +82,14 @@ describe('BSONError', function () { expect(bsonErr.name).equals('BSONError'); expect(bsonErr.message).equals('This is a BSONError message'); }); + + describe('class BSONVersionError', () => { + it('is a BSONError instance', () => { + expect(BSONError.isBSONError(new BSONVersionError())).to.be.true; + }); + + it('has a name property equal to "BSONVersionError"', () => { + expect(new BSONVersionError()).to.have.property('name', 'BSONVersionError'); + }); + }); }); diff --git a/test/node/exports.test.ts b/test/node/exports.test.ts index 399af139..d4d87f74 100644 --- a/test/node/exports.test.ts +++ b/test/node/exports.test.ts @@ -8,6 +8,7 @@ const EXPECTED_EXPORTS = [ 'BSONType', 'BSONValue', + 'BSONVersionError', 'EJSON', 'Code', 'BSONSymbol',