Skip to content

Commit

Permalink
Improve serialization performance
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanReece committed Aug 22, 2024
1 parent ede8357 commit 98dec41
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 201 deletions.
3 changes: 2 additions & 1 deletion src/bson_value.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { BSON_MAJOR_VERSION } from './constants';
import { type InspectFn } from './parser/utils';
import { BSON_VERSION_SYMBOL } from './constants';

/** @public */
export abstract class BSONValue {
/** @public */
public abstract get _bsontype(): string;

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

Expand Down
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/** @internal */
export const BSON_MAJOR_VERSION = 6;

/** @internal */
export const BSON_VERSION_SYMBOL = Symbol.for('@@mdb.bson.version');

/** @internal */
export const BSON_INT32_MAX = 0x7fffffff;
/** @internal */
Expand Down
5 changes: 3 additions & 2 deletions src/extended_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
BSON_INT32_MIN,
BSON_INT64_MAX,
BSON_INT64_MIN,
BSON_MAJOR_VERSION
BSON_MAJOR_VERSION,
BSON_VERSION_SYMBOL
} from './constants';
import { DBRef, isDBRefLike } from './db_ref';
import { Decimal128 } from './decimal128';
Expand Down Expand Up @@ -358,7 +359,7 @@ function serializeDocument(doc: any, options: EJSONSerializeOptions) {
doc != null &&
typeof doc === 'object' &&
typeof doc._bsontype === 'string' &&
doc[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION
doc[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION
) {
throw new BSONVersionError();
} else if (isBSONType(doc)) {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/calculate_size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function calculateElement(
if (
value != null &&
typeof value._bsontype === 'string' &&
value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
value[constants.BSON_VERSION_SYMBOL] !== constants.BSON_MAJOR_VERSION
) {
throw new BSONVersionError();
} else if (value == null || value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
Expand Down
Loading

0 comments on commit 98dec41

Please sign in to comment.