Skip to content

Commit

Permalink
fix: comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Jan 12, 2023
1 parent 3887429 commit 56e348d
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/bson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
11 changes: 11 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
4 changes: 2 additions & 2 deletions src/extended_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions 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 { BSONError } from '../error';
import { BSONVersionError } from '../error';
import * as constants from '../constants';
import { ByteUtils } from '../utils/byte_utils';
import { isAnyArrayBuffer, isDate, isRegExp } from './utils';
Expand Down Expand Up @@ -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' ||
Expand Down
8 changes: 4 additions & 4 deletions src/parser/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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') {
Expand Down
6 changes: 3 additions & 3 deletions test/node/cross_compat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ 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/);
expect(() => BSON.serialize(new Map([['type', type]]))).to.throw(/Unsupported BSON version/);
});

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/);
Expand Down
12 changes: 11 additions & 1 deletion test/node/error.test.ts
Original file line number Diff line number Diff line change
@@ -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__;

Expand Down Expand Up @@ -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');
});
});
});
1 change: 1 addition & 0 deletions test/node/exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const EXPECTED_EXPORTS = [

'BSONType',
'BSONValue',
'BSONVersionError',
'EJSON',
'Code',
'BSONSymbol',
Expand Down

0 comments on commit 56e348d

Please sign in to comment.