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

feat(NODE-4892)!: error on bson types not from this version #543

Merged
merged 26 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c95f4c1
feat(NODE-4892)!: error on bson types not from this version
nbbeeken Dec 15, 2022
c0ce081
chore(release): 5.0.0-alpha.0
nbbeeken Dec 19, 2022
5b38dd8
fix: BSONSymbol issue
nbbeeken Dec 19, 2022
6557ebc
fix: use common constant for versioning
nbbeeken Dec 19, 2022
7596086
chore(release): 5.0.0-alpha.1
nbbeeken Dec 19, 2022
1cd6a31
Merge branch 'main' into NODE-4892-version-tag
durran Dec 20, 2022
9132939
Merge branch 'main' into NODE-4892-version-tag
nbbeeken Dec 30, 2022
c4715c1
Merge branch 'main' into NODE-4892-version-tag
nbbeeken Jan 4, 2023
90d2467
Merge branch 'main' into NODE-4892-version-tag
nbbeeken Jan 5, 2023
02f26dd
fix: calculate object size and test for error case
nbbeeken Jan 5, 2023
9e6a976
Merge branch 'main' into NODE-4892-version-tag
nbbeeken Jan 10, 2023
ff249e9
chore(release): 5.0.0-alpha.2
nbbeeken Jan 10, 2023
60bf3d4
feat: add BSONValue super class with version getter
nbbeeken Jan 11, 2023
40ed19e
test: name fix
nbbeeken Jan 11, 2023
120d026
fix: rm getter from uuid
nbbeeken Jan 11, 2023
3860c8c
fix: lint
nbbeeken Jan 12, 2023
b7c0e01
feat: add other common apis
nbbeeken Jan 12, 2023
3887429
fix: add access annotations
nbbeeken Jan 12, 2023
56e348d
fix: comments
nbbeeken Jan 12, 2023
11159bb
fix: revert history file
nbbeeken Jan 12, 2023
06911d1
fix: lint
nbbeeken Jan 12, 2023
b11e13d
fix tests checking for BSONVersionError
nbbeeken Jan 12, 2023
ee1c500
docs: migrate
nbbeeken Jan 12, 2023
c3b4cc2
docs: add jira ticket
nbbeeken Jan 12, 2023
814f52f
address comments
nbbeeken Jan 12, 2023
7614863
docs: update guide
nbbeeken Jan 12, 2023
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
75 changes: 75 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,81 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [5.0.0-alpha.2](https://github.com/mongodb/js-bson/compare/v5.0.0-alpha.1...v5.0.0-alpha.2) (2023-01-10)


### ⚠ BREAKING CHANGES

* **NODE-4890:** make all thrown errors into BSONErrors (#545)

### Features

* **NODE-4870:** Support BigInt serialization ([#541](https://github.com/mongodb/js-bson/issues/541)) ([e9e40a2](https://github.com/mongodb/js-bson/commit/e9e40a2cceb6392065a4f217d17fc4211aa1bbd7))
* **NODE-4871:** Add support for int64 deserialization to BigInt ([#542](https://github.com/mongodb/js-bson/issues/542)) ([9ff60ba](https://github.com/mongodb/js-bson/commit/9ff60baedf2c3fcd5faf3760d4df03235531c608))
* **NODE-4890:** make all thrown errors into BSONErrors ([#545](https://github.com/mongodb/js-bson/issues/545)) ([5b837a9](https://github.com/mongodb/js-bson/commit/5b837a9e5019016529a83700f3ba3065d5e53e80))
* **NODE-4927:** exports in package.json for react native and document how to polyfill for BSON ([#550](https://github.com/mongodb/js-bson/issues/550)) ([3b4b61e](https://github.com/mongodb/js-bson/commit/3b4b61ed8dfbe7e7f8635e4584e1b17a91c1c4df))


### Bug Fixes

* calculate object size and test for error case ([02f26dd](https://github.com/mongodb/js-bson/commit/02f26dda765ac95aab41c9e622caf4429a49ba83))
* **NODE-4887:** serializeInto does not check for the presence of a toBSON method for values in Map entries ([#555](https://github.com/mongodb/js-bson/issues/555)) ([ebc1c76](https://github.com/mongodb/js-bson/commit/ebc1c765276e83c9e8f073efdf41b27d4e5e7d7b))
* **NODE-4905:** double precision accuracy in canonical EJSON ([#548](https://github.com/mongodb/js-bson/issues/548)) ([e0dbb17](https://github.com/mongodb/js-bson/commit/e0dbb17d0a13f3aca9879f08cba3573b2a84fd8d))
* **NODE-4932:** remove .0 suffix from double extended json values ([#554](https://github.com/mongodb/js-bson/issues/554)) ([946866d](https://github.com/mongodb/js-bson/commit/946866d1a1b265f13c0e6c542f54b9786726fa85))

### [5.0.0-alpha.1](https://github.com/mongodb/js-bson/compare/v5.0.0-alpha.0...v5.0.0-alpha.1) (2022-12-19)

### Bug Fixes

* fix: BSONSymbol serialize not recognized issue ([5b38dd8](https://github.com/mongodb/js-bson/commit/5b38dd84027ac072c67fbaee6cf08e2a44be2980))


## [5.0.0-alpha.0](https://github.com/mongodb/js-bson/compare/v4.7.0...v5.0.0-alpha.0) (2022-12-16)


### ⚠ BREAKING CHANGES

* **NODE-4892:** error on bson types not from this version
* **NODE-4713:** modernize bundling (#534)
* **NODE-1921:** validate serializer root input (#537)
* **NODE-4711:** remove evalFunctions option (#539)
* **NODE-4706:** validate Timestamp ctor argument (#536)
* **NODE-4710:** remove capital D ObjectID export (#528)
* **NODE-4862:** add BSONType enum and remove internal constants from export (#532)
* **NODE-4410:** only enumerate own properties (#527)
* **NODE-4850:** serialize negative zero to double (#529)
* **NODE-4704:** remove deprecated ObjectId methods (#525)
* **NODE-4461:** remove Decimal128 toObject transformer (#526)
* **NODE-4712:** remove unused Map polyfill (#523)
* **NODE-4440:** bump TS target version to es2020 (#520)
* **NODE-4802:** Refactor BSON to work with cross platform JS APIs (#518)
* **NODE-4435:** drop support for nodejs versions below 14 (#517)

### Features

* **NODE-4892:** error on bson types not from this version ([c95f4c1](https://github.com/mongodb/js-bson/commit/c95f4c11fbdfe7d55cb19198d68a17327d6d260d))
* **NODE-4713:** modernize bundling ([#534](https://github.com/mongodb/js-bson/issues/534)) ([28ce4d5](https://github.com/mongodb/js-bson/commit/28ce4d584ce399d99c41c5f5fd022b6e16f7b913))
* **NODE-1921:** validate serializer root input ([#537](https://github.com/mongodb/js-bson/issues/537)) ([95d5edf](https://github.com/mongodb/js-bson/commit/95d5edf5c4f8a1cd6c23deaac3a446959fde779e))
* **NODE-4711:** remove evalFunctions option ([#539](https://github.com/mongodb/js-bson/issues/539)) ([0427eb5](https://github.com/mongodb/js-bson/commit/0427eb588073a07d9e88ddf6155d2c1891ae5ad1))
* **NODE-4706:** validate Timestamp ctor argument ([#536](https://github.com/mongodb/js-bson/issues/536)) ([f90bcc3](https://github.com/mongodb/js-bson/commit/f90bcc3))
* **NODE-4710:** remove capital D ObjectID export ([#528](https://github.com/mongodb/js-bson/issues/528)) ([8511225](https://github.com/mongodb/js-bson/commit/8511225))
* **NODE-4862:** add BSONType enum and remove internal constants from export ([#532](https://github.com/mongodb/js-bson/issues/532)) ([196f9f8](https://github.com/mongodb/js-bson/commit/196f9f8))
* **NODE-4410:** only enumerate own properties ([#527](https://github.com/mongodb/js-bson/issues/527)) ([5103e4d](https://github.com/mongodb/js-bson/commit/5103e4d))
* **NODE-4850:** serialize negative zero to double ([#529](https://github.com/mongodb/js-bson/issues/529)) ([be74b30](https://github.com/mongodb/js-bson/commit/be74b30))
* **NODE-4704:** remove deprecated ObjectId methods ([#525](https://github.com/mongodb/js-bson/issues/525)) ([f1cccf2](https://github.com/mongodb/js-bson/commit/f1cccf2))
* **NODE-4461:** remove Decimal128 toObject transformer ([#526](https://github.com/mongodb/js-bson/issues/526)) ([14a7473](https://github.com/mongodb/js-bson/commit/14a7473))
* **NODE-4712:** remove unused Map polyfill ([#523](https://github.com/mongodb/js-bson/issues/523)) ([1fb6dc6](https://github.com/mongodb/js-bson/commit/1fb6dc6))
* **NODE-4440:** bump TS target version to es2020 ([#520](https://github.com/mongodb/js-bson/issues/520)) ([491d8b7](https://github.com/mongodb/js-bson/commit/491d8b7))
* **NODE-4802:** Refactor BSON to work with cross platform JS APIs ([#518](https://github.com/mongodb/js-bson/issues/518)) ([3d3d0dc](https://github.com/mongodb/js-bson/commit/3d3d0dc))
* **NODE-4435:** drop support for nodejs versions below 14 ([#517](https://github.com/mongodb/js-bson/issues/517)) ([027ffb7](https://github.com/mongodb/js-bson/commit/027ffb7))


### Bug Fixes

* **NODE-4464:** stringify and parse negative zero to and from $numberDouble: -0.0 ([#531](https://github.com/mongodb/js-bson/issues/531)) ([a469e91](https://github.com/mongodb/js-bson/commit/a469e91))
* **NODE-4771:** serializeFunctions breaks function names outside of basic latin ([#538](https://github.com/mongodb/js-bson/issues/538)) ([35a9234](https://github.com/mongodb/js-bson/commit/35a92341c0860fb41cbd5761250c565154ce1353))


## [4.7.0](https://github.com/mongodb/js-bson/compare/v4.6.5...v4.7.0) (2022-08-18)


Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"etc/prepare.js"
],
"types": "bson.d.ts",
"version": "4.7.0",
"version": "5.0.0-alpha.2",
dariakp marked this conversation as resolved.
Show resolved Hide resolved
"author": {
"name": "The MongoDB NodeJS Team",
"email": "dbx-node@mongodb.com"
Expand Down
4 changes: 3 additions & 1 deletion src/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { EJSONOptions } from './extended_json';
import { BSONError } from './error';
import { BSON_BINARY_SUBTYPE_UUID_NEW } from './constants';
import { ByteUtils } from './utils/byte_utils';
import { BSONValue } from './bson_value';

/** @public */
export type BinarySequence = Uint8Array | number[];
Expand All @@ -27,7 +28,7 @@ export interface BinaryExtended {
* @public
* @category BSONType
*/
export class Binary {
export class Binary extends BSONValue {
get _bsontype(): 'Binary' {
return 'Binary';
}
Expand Down Expand Up @@ -75,6 +76,7 @@ export class Binary {
* @param subType - the option binary type.
*/
constructor(buffer?: string | BinarySequence, subType?: number) {
super();
if (
!(buffer == null) &&
!(typeof buffer === 'string') &&
Expand Down
1 change: 1 addition & 0 deletions src/bson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export {
BSONRegExp,
Decimal128
};
export { BSONValue } from './bson_value';
export { BSONError } from './error';
export { BSONType } from './constants';
export { EJSON } from './extended_json';
Expand Down
18 changes: 18 additions & 0 deletions src/bson_value.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { BSON_MAJOR_VERSION } from './constants';

/** @public */
export abstract class BSONValue {
dariakp marked this conversation as resolved.
Show resolved Hide resolved
/** @public */
public abstract get _bsontype(): string;

/** @internal */
get [Symbol.for('@@mdb.bson.version')](): BSON_MAJOR_VERSION {
return BSON_MAJOR_VERSION;
}

/** @public */
public abstract inspect(): string;

/** @internal */
abstract toExtendedJSON(): unknown;
}
4 changes: 3 additions & 1 deletion src/code.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Document } from './bson';
import { BSONValue } from './bson_value';

/** @public */
export interface CodeExtended {
Expand All @@ -11,7 +12,7 @@ export interface CodeExtended {
* @public
* @category BSONType
*/
export class Code {
export class Code extends BSONValue {
get _bsontype(): 'Code' {
return 'Code';
}
Expand All @@ -27,6 +28,7 @@ export class Code {
* @param scope - an optional scope for the function.
*/
constructor(code: string | Function, scope?: Document | null) {
super();
this.code = code.toString();
this.scope = scope ?? null;
}
Expand Down
5 changes: 5 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/** @internal */
export type BSON_MAJOR_VERSION = 5;
/** @internal */
export const BSON_MAJOR_VERSION: BSON_MAJOR_VERSION = 5;
dariakp marked this conversation as resolved.
Show resolved Hide resolved

/** @internal */
export const BSON_INT32_MAX = 0x7fffffff;
/** @internal */
Expand Down
4 changes: 3 additions & 1 deletion src/db_ref.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Document } from './bson';
import { BSONValue } from './bson_value';
import type { EJSONOptions } from './extended_json';
import type { ObjectId } from './objectid';

Expand Down Expand Up @@ -28,7 +29,7 @@ export function isDBRefLike(value: unknown): value is DBRefLike {
* @public
* @category BSONType
*/
export class DBRef {
export class DBRef extends BSONValue {
get _bsontype(): 'DBRef' {
return 'DBRef';
}
Expand All @@ -44,6 +45,7 @@ export class DBRef {
* @param db - optional db name, if omitted the reference is local to the current db.
*/
constructor(collection: string, oid: ObjectId, db?: string, fields?: Document) {
super();
// check if namespace has been provided
const parts = collection.split('.');
if (parts.length === 2) {
Expand Down
4 changes: 3 additions & 1 deletion src/decimal128.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BSONValue } from './bson_value';
import { BSONError } from './error';
import { Long } from './long';
import { isUint8Array } from './parser/utils';
Expand Down Expand Up @@ -126,7 +127,7 @@ export interface Decimal128Extended {
* @public
* @category BSONType
*/
export class Decimal128 {
export class Decimal128 extends BSONValue {
get _bsontype(): 'Decimal128' {
return 'Decimal128';
}
Expand All @@ -138,6 +139,7 @@ export class Decimal128 {
* or a string representation as returned by .toString()
*/
constructor(bytes: Uint8Array | string) {
super();
if (typeof bytes === 'string') {
this.bytes = Decimal128.fromString(bytes).bytes;
} else if (isUint8Array(bytes)) {
Expand Down
4 changes: 3 additions & 1 deletion src/double.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BSONValue } from './bson_value';
import type { EJSONOptions } from './extended_json';

/** @public */
Expand All @@ -10,7 +11,7 @@ export interface DoubleExtended {
* @public
* @category BSONType
*/
export class Double {
export class Double extends BSONValue {
get _bsontype(): 'Double' {
return 'Double';
}
Expand All @@ -22,6 +23,7 @@ export class Double {
* @param value - the number we want to represent as a double.
*/
constructor(value: number) {
super();
if ((value as unknown) instanceof Number) {
value = value.valueOf();
}
Expand Down
21 changes: 15 additions & 6 deletions src/extended_json.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Binary } from './binary';
import type { Document } from './bson';
import { Code } from './code';
import { BSON_INT32_MAX, BSON_INT32_MIN, BSON_INT64_MAX, BSON_INT64_MIN } from './constants';
import {
BSON_INT32_MAX,
BSON_INT32_MIN,
BSON_INT64_MAX,
BSON_INT64_MIN,
BSON_MAJOR_VERSION
} from './constants';
import { DBRef, isDBRefLike } from './db_ref';
import { Decimal128 } from './decimal128';
import { Double } from './double';
Expand Down Expand Up @@ -273,13 +279,9 @@ const BSON_TYPE_MAPPINGS = {
),
MaxKey: () => new MaxKey(),
MinKey: () => new MinKey(),
ObjectID: (o: ObjectId) => new ObjectId(o),
// The _bsontype for ObjectId is spelled with a capital "D", to the mapping above will be used (most of the time)
// specifically BSON versions 4.0.0 and 4.0.1 the _bsontype was changed to "ObjectId" so we keep this mapping to support
// those version of BSON
ObjectId: (o: ObjectId) => new ObjectId(o),
BSONRegExp: (o: BSONRegExp) => new BSONRegExp(o.pattern, o.options),
Symbol: (o: BSONSymbol) => new BSONSymbol(o.value),
BSONSymbol: (o: BSONSymbol) => new BSONSymbol(o.value),
Timestamp: (o: Timestamp) => Timestamp.fromBits(o.low, o.high)
} as const;

Expand Down Expand Up @@ -310,6 +312,13 @@ function serializeDocument(doc: any, options: EJSONSerializeOptions) {
}
}
return _doc;
} else if (
doc != null &&
typeof doc === 'object' &&
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');
} 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: 3 additions & 1 deletion src/int_32.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BSONValue } from './bson_value';
import type { EJSONOptions } from './extended_json';

/** @public */
Expand All @@ -10,7 +11,7 @@ export interface Int32Extended {
* @public
* @category BSONType
*/
export class Int32 {
export class Int32 extends BSONValue {
get _bsontype(): 'Int32' {
return 'Int32';
}
Expand All @@ -22,6 +23,7 @@ export class Int32 {
* @param value - the number we want to represent as an int32.
*/
constructor(value: number | string) {
super();
if ((value as unknown) instanceof Number) {
value = value.valueOf();
}
Expand Down
4 changes: 3 additions & 1 deletion src/long.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BSONValue } from './bson_value';
import { BSONError } from './error';
import type { EJSONOptions } from './extended_json';
import type { Timestamp } from './timestamp';
Expand Down Expand Up @@ -99,7 +100,7 @@ export interface LongExtended {
* case would often result in infinite recursion.
* Common constant values ZERO, ONE, NEG_ONE, etc. are found as static properties on this class.
*/
export class Long {
export class Long extends BSONValue {
get _bsontype(): 'Long' {
return 'Long';
}
Expand Down Expand Up @@ -138,6 +139,7 @@ export class Long {
* @param unsigned - Whether unsigned or not, defaults to signed
*/
constructor(low: number | bigint | string = 0, high?: number | boolean, unsigned?: boolean) {
super();
if (typeof low === 'bigint') {
Object.assign(this, Long.fromBigInt(low, !!high));
} else if (typeof low === 'string') {
Expand Down
4 changes: 3 additions & 1 deletion src/max_key.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BSONValue } from './bson_value';

/** @public */
export interface MaxKeyExtended {
$maxKey: 1;
Expand All @@ -8,7 +10,7 @@ export interface MaxKeyExtended {
* @public
* @category BSONType
*/
export class MaxKey {
export class MaxKey extends BSONValue {
dariakp marked this conversation as resolved.
Show resolved Hide resolved
get _bsontype(): 'MaxKey' {
return 'MaxKey';
}
Expand Down
4 changes: 3 additions & 1 deletion src/min_key.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BSONValue } from './bson_value';

/** @public */
export interface MinKeyExtended {
$minKey: 1;
Expand All @@ -8,7 +10,7 @@ export interface MinKeyExtended {
* @public
* @category BSONType
*/
export class MinKey {
export class MinKey extends BSONValue {
dariakp marked this conversation as resolved.
Show resolved Hide resolved
get _bsontype(): 'MinKey' {
return 'MinKey';
}
Expand Down
Loading