Skip to content

Commit

Permalink
perf(NODE-6356): Improve serialization performance (#709)
Browse files Browse the repository at this point in the history
Co-authored-by: Warren James <[email protected]>
  • Loading branch information
SeanReece and W-A-James authored Sep 16, 2024
1 parent e584fbb commit 61537f5
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 202 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
2 changes: 1 addition & 1 deletion src/decimal128.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class Decimal128 extends BSONValue {
super();
if (typeof bytes === 'string') {
this.bytes = Decimal128.fromString(bytes).bytes;
} else if (isUint8Array(bytes)) {
} else if (bytes instanceof Uint8Array || isUint8Array(bytes)) {
if (bytes.byteLength !== 16) {
throw new BSONError('Decimal128 must take a Buffer of 16 bytes');
}
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 61537f5

Please sign in to comment.