bson 6.8.1 → 6.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "vendor"
15
15
  ],
16
16
  "types": "bson.d.ts",
17
- "version": "6.8.1",
17
+ "version": "6.9.1",
18
18
  "author": {
19
19
  "name": "The MongoDB NodeJS Team",
20
20
  "email": "dbx-node@mongodb.com"
@@ -27,41 +27,41 @@
27
27
  },
28
28
  "devDependencies": {
29
29
  "@istanbuljs/nyc-config-typescript": "^1.0.2",
30
- "@microsoft/api-extractor": "^7.43.1",
30
+ "@microsoft/api-extractor": "^7.47.5",
31
31
  "@rollup/plugin-node-resolve": "^15.2.3",
32
32
  "@rollup/plugin-typescript": "^11.1.6",
33
- "@types/chai": "^4.3.14",
34
- "@types/mocha": "^10.0.6",
35
- "@types/node": "^20.12.7",
33
+ "@types/chai": "^4.3.17",
34
+ "@types/mocha": "^10.0.7",
35
+ "@types/node": "^22.1.0",
36
36
  "@types/sinon": "^17.0.3",
37
37
  "@types/sinon-chai": "^3.2.12",
38
- "@typescript-eslint/eslint-plugin": "^7.7.0",
39
- "@typescript-eslint/parser": "^7.7.0",
38
+ "@typescript-eslint/eslint-plugin": "^8.0.1",
39
+ "@typescript-eslint/parser": "^8.0.1",
40
40
  "benchmark": "^2.1.4",
41
41
  "chai": "^4.4.1",
42
42
  "chalk": "^5.3.0",
43
43
  "dbx-js-tools": "github:mongodb-js/dbx-js-tools",
44
- "eslint": "^8.57.0",
44
+ "eslint": "^9.8.0",
45
45
  "eslint-config-prettier": "^9.1.0",
46
46
  "eslint-plugin-no-bigint-usage": "file:etc/eslint/no-bigint-usage",
47
- "eslint-plugin-prettier": "^5.1.3",
48
- "eslint-plugin-tsdoc": "^0.2.17",
49
- "magic-string": "^0.30.10",
50
- "mocha": "^10.4.0",
47
+ "eslint-plugin-prettier": "^5.2.1",
48
+ "eslint-plugin-tsdoc": "^0.3.0",
49
+ "magic-string": "^0.30.11",
50
+ "mocha": "^10.7.0",
51
51
  "node-fetch": "^3.3.2",
52
52
  "nyc": "^15.1.0",
53
- "prettier": "^3.2.5",
54
- "rollup": "^4.14.3",
55
- "sinon": "^17.0.1",
53
+ "prettier": "^3.3.3",
54
+ "rollup": "^4.20.0",
55
+ "sinon": "^18.0.0",
56
56
  "sinon-chai": "^3.7.0",
57
57
  "source-map-support": "^0.5.21",
58
58
  "standard-version": "^9.5.0",
59
- "tar": "^7.0.1",
59
+ "tar": "^7.4.3",
60
60
  "ts-node": "^10.9.2",
61
- "tsd": "^0.31.0",
62
- "typescript": "5.3",
61
+ "tsd": "^0.31.1",
62
+ "typescript": "^5.5.4",
63
63
  "typescript-cached-transpile": "0.0.6",
64
- "uuid": "^9.0.1"
64
+ "uuid": "^10.0.0"
65
65
  },
66
66
  "tsd": {
67
67
  "directory": "test/types",
@@ -105,13 +105,14 @@
105
105
  "check:web-no-bigint": "WEB=true NO_BIGINT=true mocha test/node",
106
106
  "check:granular-bench": "npm run build:bench && node ./test/bench/etc/run_granular_benchmarks.js",
107
107
  "check:spec-bench": "npm run build:bench && node ./test/bench/lib/spec/bsonBench.js",
108
+ "check:custom-bench": "npm run build && node ./test/bench/custom/main.mjs",
108
109
  "build:bench": "cd test/bench && npx tsc",
109
110
  "build:ts": "node ./node_modules/typescript/bin/tsc",
110
111
  "build:dts": "npm run build:ts && api-extractor run --typescript-compiler-folder node_modules/typescript --local && node etc/clean_definition_files.cjs",
111
112
  "build:bundle": "rollup -c rollup.config.mjs",
112
113
  "build": "npm run build:dts && npm run build:bundle",
113
- "check:lint": "eslint -v && eslint --ext '.js,.ts' --max-warnings=0 src test && npm run build:dts && npm run check:tsd",
114
- "format": "eslint --ext '.js,.ts' src test --fix",
114
+ "check:lint": "ESLINT_USE_FLAT_CONFIG=false eslint -v && ESLINT_USE_FLAT_CONFIG=false eslint --ext '.js,.ts' --max-warnings=0 src test && npm run build:dts && npm run check:tsd",
115
+ "format": "ESLINT_USE_FLAT_CONFIG=false eslint --ext '.js,.ts' src test --fix",
115
116
  "check:coverage": "nyc --check-coverage npm run check:node",
116
117
  "prepare": "node etc/prepare.js",
117
118
  "release": "standard-version -i HISTORY.md"
package/src/bson_value.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { BSON_MAJOR_VERSION } from './constants';
2
2
  import { type InspectFn } from './parser/utils';
3
+ import { BSON_VERSION_SYMBOL } from './constants';
3
4
 
4
5
  /** @public */
5
6
  export abstract class BSONValue {
@@ -7,7 +8,7 @@ export abstract class BSONValue {
7
8
  public abstract get _bsontype(): string;
8
9
 
9
10
  /** @internal */
10
- get [Symbol.for('@@mdb.bson.version')](): typeof BSON_MAJOR_VERSION {
11
+ get [BSON_VERSION_SYMBOL](): typeof BSON_MAJOR_VERSION {
11
12
  return BSON_MAJOR_VERSION;
12
13
  }
13
14
 
package/src/constants.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  /** @internal */
2
2
  export const BSON_MAJOR_VERSION = 6;
3
3
 
4
+ /** @internal */
5
+ export const BSON_VERSION_SYMBOL = Symbol.for('@@mdb.bson.version');
6
+
4
7
  /** @internal */
5
8
  export const BSON_INT32_MAX = 0x7fffffff;
6
9
  /** @internal */
package/src/decimal128.ts CHANGED
@@ -142,7 +142,7 @@ export class Decimal128 extends BSONValue {
142
142
  super();
143
143
  if (typeof bytes === 'string') {
144
144
  this.bytes = Decimal128.fromString(bytes).bytes;
145
- } else if (isUint8Array(bytes)) {
145
+ } else if (bytes instanceof Uint8Array || isUint8Array(bytes)) {
146
146
  if (bytes.byteLength !== 16) {
147
147
  throw new BSONError('Decimal128 must take a Buffer of 16 bytes');
148
148
  }
@@ -6,7 +6,8 @@ import {
6
6
  BSON_INT32_MIN,
7
7
  BSON_INT64_MAX,
8
8
  BSON_INT64_MIN,
9
- BSON_MAJOR_VERSION
9
+ BSON_MAJOR_VERSION,
10
+ BSON_VERSION_SYMBOL
10
11
  } from './constants';
11
12
  import { DBRef, isDBRefLike } from './db_ref';
12
13
  import { Decimal128 } from './decimal128';
@@ -358,7 +359,7 @@ function serializeDocument(doc: any, options: EJSONSerializeOptions) {
358
359
  doc != null &&
359
360
  typeof doc === 'object' &&
360
361
  typeof doc._bsontype === 'string' &&
361
- doc[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION
362
+ doc[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION
362
363
  ) {
363
364
  throw new BSONVersionError();
364
365
  } else if (isBSONType(doc)) {
package/src/long.ts CHANGED
@@ -1163,7 +1163,6 @@ export class Long extends BSONValue {
1163
1163
  // eslint-disable-next-line @typescript-eslint/no-this-alias
1164
1164
  let rem: Long = this;
1165
1165
  let result = '';
1166
- // eslint-disable-next-line no-constant-condition
1167
1166
  while (true) {
1168
1167
  const remDiv = rem.div(radixToPower);
1169
1168
  const intval = rem.sub(remDiv.mul(radixToPower)).toInt() >>> 0;
package/src/objectid.ts CHANGED
@@ -4,9 +4,6 @@ import { type InspectFn, defaultInspect } from './parser/utils';
4
4
  import { ByteUtils } from './utils/byte_utils';
5
5
  import { NumberUtils } from './utils/number_utils';
6
6
 
7
- // Regular expression that checks for hex value
8
- const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
9
-
10
7
  // Unique sequence for the current process (initialized on first use)
11
8
  let PROCESS_UNIQUE: Uint8Array | null = null;
12
9
 
@@ -112,7 +109,7 @@ export class ObjectId extends BSONValue {
112
109
  // If intstanceof matches we can escape calling ensure buffer in Node.js environments
113
110
  this.buffer = ByteUtils.toLocalBufferType(workingId);
114
111
  } else if (typeof workingId === 'string') {
115
- if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
112
+ if (ObjectId.validateHexString(workingId)) {
116
113
  this.buffer = ByteUtils.fromHex(workingId);
117
114
  } else {
118
115
  throw new BSONError(
@@ -143,6 +140,29 @@ export class ObjectId extends BSONValue {
143
140
  }
144
141
  }
145
142
 
143
+ /**
144
+ * @internal
145
+ * Validates the input string is a valid hex representation of an ObjectId.
146
+ */
147
+ private static validateHexString(string: string): boolean {
148
+ if (string?.length !== 24) return false;
149
+ for (let i = 0; i < 24; i++) {
150
+ const char = string.charCodeAt(i);
151
+ if (
152
+ // Check for ASCII 0-9
153
+ (char >= 48 && char <= 57) ||
154
+ // Check for ASCII a-f
155
+ (char >= 97 && char <= 102) ||
156
+ // Check for ASCII A-F
157
+ (char >= 65 && char <= 70)
158
+ ) {
159
+ continue;
160
+ }
161
+ return false;
162
+ }
163
+ return true;
164
+ }
165
+
146
166
  /** Returns the ObjectId id as a 24 lowercase character hex string representation */
147
167
  toHexString(): string {
148
168
  if (ObjectId.cacheHexString && this.__id) {
@@ -329,6 +349,7 @@ export class ObjectId extends BSONValue {
329
349
  */
330
350
  static isValid(id: string | number | ObjectId | ObjectIdLike | Uint8Array): boolean {
331
351
  if (id == null) return false;
352
+ if (typeof id === 'string') return ObjectId.validateHexString(id);
332
353
 
333
354
  try {
334
355
  new ObjectId(id);
@@ -81,7 +81,7 @@ function calculateElement(
81
81
  if (
82
82
  value != null &&
83
83
  typeof value._bsontype === 'string' &&
84
- value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
84
+ value[constants.BSON_VERSION_SYMBOL] !== constants.BSON_MAJOR_VERSION
85
85
  ) {
86
86
  throw new BSONVersionError();
87
87
  } else if (value == null || value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
@@ -633,77 +633,81 @@ export function serializeInto(
633
633
  value = value.toBSON();
634
634
  }
635
635
 
636
- if (typeof value === 'string') {
636
+ // Check the type of the value
637
+ const type = typeof value;
638
+
639
+ if (value === undefined) {
640
+ index = serializeNull(buffer, key, value, index);
641
+ } else if (value === null) {
642
+ index = serializeNull(buffer, key, value, index);
643
+ } else if (type === 'string') {
637
644
  index = serializeString(buffer, key, value, index);
638
- } else if (typeof value === 'number') {
645
+ } else if (type === 'number') {
639
646
  index = serializeNumber(buffer, key, value, index);
640
- } else if (typeof value === 'bigint') {
647
+ } else if (type === 'bigint') {
641
648
  index = serializeBigInt(buffer, key, value, index);
642
- } else if (typeof value === 'boolean') {
649
+ } else if (type === 'boolean') {
643
650
  index = serializeBoolean(buffer, key, value, index);
644
- } else if (value instanceof Date || isDate(value)) {
645
- index = serializeDate(buffer, key, value, index);
646
- } else if (value === undefined) {
647
- index = serializeNull(buffer, key, value, index);
648
- } else if (value === null) {
649
- index = serializeNull(buffer, key, value, index);
650
- } else if (isUint8Array(value)) {
651
- index = serializeBuffer(buffer, key, value, index);
652
- } else if (value instanceof RegExp || isRegExp(value)) {
653
- index = serializeRegExp(buffer, key, value, index);
654
- } else if (typeof value === 'object' && value._bsontype == null) {
655
- index = serializeObject(
656
- buffer,
657
- key,
658
- value,
659
- index,
660
- checkKeys,
661
- depth,
662
- serializeFunctions,
663
- ignoreUndefined,
664
- path
665
- );
666
- } else if (
667
- typeof value === 'object' &&
668
- value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
669
- ) {
670
- throw new BSONVersionError();
671
- } else if (value._bsontype === 'ObjectId') {
672
- index = serializeObjectId(buffer, key, value, index);
673
- } else if (value._bsontype === 'Decimal128') {
674
- index = serializeDecimal128(buffer, key, value, index);
675
- } else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
676
- index = serializeLong(buffer, key, value, index);
677
- } else if (value._bsontype === 'Double') {
678
- index = serializeDouble(buffer, key, value, index);
679
- } else if (typeof value === 'function' && serializeFunctions) {
651
+ } else if (type === 'object' && value._bsontype == null) {
652
+ if (value instanceof Date || isDate(value)) {
653
+ index = serializeDate(buffer, key, value, index);
654
+ } else if (value instanceof Uint8Array || isUint8Array(value)) {
655
+ index = serializeBuffer(buffer, key, value, index);
656
+ } else if (value instanceof RegExp || isRegExp(value)) {
657
+ index = serializeRegExp(buffer, key, value, index);
658
+ } else {
659
+ index = serializeObject(
660
+ buffer,
661
+ key,
662
+ value,
663
+ index,
664
+ checkKeys,
665
+ depth,
666
+ serializeFunctions,
667
+ ignoreUndefined,
668
+ path
669
+ );
670
+ }
671
+ } else if (type === 'object') {
672
+ if (value[constants.BSON_VERSION_SYMBOL] !== constants.BSON_MAJOR_VERSION) {
673
+ throw new BSONVersionError();
674
+ } else if (value._bsontype === 'ObjectId') {
675
+ index = serializeObjectId(buffer, key, value, index);
676
+ } else if (value._bsontype === 'Decimal128') {
677
+ index = serializeDecimal128(buffer, key, value, index);
678
+ } else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
679
+ index = serializeLong(buffer, key, value, index);
680
+ } else if (value._bsontype === 'Double') {
681
+ index = serializeDouble(buffer, key, value, index);
682
+ } else if (value._bsontype === 'Code') {
683
+ index = serializeCode(
684
+ buffer,
685
+ key,
686
+ value,
687
+ index,
688
+ checkKeys,
689
+ depth,
690
+ serializeFunctions,
691
+ ignoreUndefined,
692
+ path
693
+ );
694
+ } else if (value._bsontype === 'Binary') {
695
+ index = serializeBinary(buffer, key, value, index);
696
+ } else if (value._bsontype === 'BSONSymbol') {
697
+ index = serializeSymbol(buffer, key, value, index);
698
+ } else if (value._bsontype === 'DBRef') {
699
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
700
+ } else if (value._bsontype === 'BSONRegExp') {
701
+ index = serializeBSONRegExp(buffer, key, value, index);
702
+ } else if (value._bsontype === 'Int32') {
703
+ index = serializeInt32(buffer, key, value, index);
704
+ } else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
705
+ index = serializeMinMax(buffer, key, value, index);
706
+ } else if (typeof value._bsontype !== 'undefined') {
707
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
708
+ }
709
+ } else if (type === 'function' && serializeFunctions) {
680
710
  index = serializeFunction(buffer, key, value, index);
681
- } else if (value._bsontype === 'Code') {
682
- index = serializeCode(
683
- buffer,
684
- key,
685
- value,
686
- index,
687
- checkKeys,
688
- depth,
689
- serializeFunctions,
690
- ignoreUndefined,
691
- path
692
- );
693
- } else if (value._bsontype === 'Binary') {
694
- index = serializeBinary(buffer, key, value, index);
695
- } else if (value._bsontype === 'BSONSymbol') {
696
- index = serializeSymbol(buffer, key, value, index);
697
- } else if (value._bsontype === 'DBRef') {
698
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
699
- } else if (value._bsontype === 'BSONRegExp') {
700
- index = serializeBSONRegExp(buffer, key, value, index);
701
- } else if (value._bsontype === 'Int32') {
702
- index = serializeInt32(buffer, key, value, index);
703
- } else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
704
- index = serializeMinMax(buffer, key, value, index);
705
- } else if (typeof value._bsontype !== 'undefined') {
706
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
707
711
  }
708
712
  }
709
713
  } else if (object instanceof Map || isMap(object)) {
@@ -745,7 +749,11 @@ export function serializeInto(
745
749
  }
746
750
  }
747
751
 
748
- if (type === 'string') {
752
+ if (value === undefined) {
753
+ if (ignoreUndefined === false) index = serializeNull(buffer, key, value, index);
754
+ } else if (value === null) {
755
+ index = serializeNull(buffer, key, value, index);
756
+ } else if (type === 'string') {
749
757
  index = serializeString(buffer, key, value, index);
750
758
  } else if (type === 'number') {
751
759
  index = serializeNumber(buffer, key, value, index);
@@ -753,67 +761,66 @@ export function serializeInto(
753
761
  index = serializeBigInt(buffer, key, value, index);
754
762
  } else if (type === 'boolean') {
755
763
  index = serializeBoolean(buffer, key, value, index);
756
- } else if (value instanceof Date || isDate(value)) {
757
- index = serializeDate(buffer, key, value, index);
758
- } else if (value === null || (value === undefined && ignoreUndefined === false)) {
759
- index = serializeNull(buffer, key, value, index);
760
- } else if (isUint8Array(value)) {
761
- index = serializeBuffer(buffer, key, value, index);
762
- } else if (value instanceof RegExp || isRegExp(value)) {
763
- index = serializeRegExp(buffer, key, value, index);
764
764
  } else if (type === 'object' && value._bsontype == null) {
765
- index = serializeObject(
766
- buffer,
767
- key,
768
- value,
769
- index,
770
- checkKeys,
771
- depth,
772
- serializeFunctions,
773
- ignoreUndefined,
774
- path
775
- );
776
- } else if (
777
- typeof value === 'object' &&
778
- value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
779
- ) {
780
- throw new BSONVersionError();
781
- } else if (value._bsontype === 'ObjectId') {
782
- index = serializeObjectId(buffer, key, value, index);
783
- } else if (type === 'object' && value._bsontype === 'Decimal128') {
784
- index = serializeDecimal128(buffer, key, value, index);
785
- } else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
786
- index = serializeLong(buffer, key, value, index);
787
- } else if (value._bsontype === 'Double') {
788
- index = serializeDouble(buffer, key, value, index);
789
- } else if (value._bsontype === 'Code') {
790
- index = serializeCode(
791
- buffer,
792
- key,
793
- value,
794
- index,
795
- checkKeys,
796
- depth,
797
- serializeFunctions,
798
- ignoreUndefined,
799
- path
800
- );
801
- } else if (typeof value === 'function' && serializeFunctions) {
765
+ if (value instanceof Date || isDate(value)) {
766
+ index = serializeDate(buffer, key, value, index);
767
+ } else if (value instanceof Uint8Array || isUint8Array(value)) {
768
+ index = serializeBuffer(buffer, key, value, index);
769
+ } else if (value instanceof RegExp || isRegExp(value)) {
770
+ index = serializeRegExp(buffer, key, value, index);
771
+ } else {
772
+ index = serializeObject(
773
+ buffer,
774
+ key,
775
+ value,
776
+ index,
777
+ checkKeys,
778
+ depth,
779
+ serializeFunctions,
780
+ ignoreUndefined,
781
+ path
782
+ );
783
+ }
784
+ } else if (type === 'object') {
785
+ if (value[constants.BSON_VERSION_SYMBOL] !== constants.BSON_MAJOR_VERSION) {
786
+ throw new BSONVersionError();
787
+ } else if (value._bsontype === 'ObjectId') {
788
+ index = serializeObjectId(buffer, key, value, index);
789
+ } else if (value._bsontype === 'Decimal128') {
790
+ index = serializeDecimal128(buffer, key, value, index);
791
+ } else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
792
+ index = serializeLong(buffer, key, value, index);
793
+ } else if (value._bsontype === 'Double') {
794
+ index = serializeDouble(buffer, key, value, index);
795
+ } else if (value._bsontype === 'Code') {
796
+ index = serializeCode(
797
+ buffer,
798
+ key,
799
+ value,
800
+ index,
801
+ checkKeys,
802
+ depth,
803
+ serializeFunctions,
804
+ ignoreUndefined,
805
+ path
806
+ );
807
+ } else if (value._bsontype === 'Binary') {
808
+ index = serializeBinary(buffer, key, value, index);
809
+ } else if (value._bsontype === 'BSONSymbol') {
810
+ index = serializeSymbol(buffer, key, value, index);
811
+ } else if (value._bsontype === 'DBRef') {
812
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
813
+ } else if (value._bsontype === 'BSONRegExp') {
814
+ index = serializeBSONRegExp(buffer, key, value, index);
815
+ } else if (value._bsontype === 'Int32') {
816
+ index = serializeInt32(buffer, key, value, index);
817
+ } else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
818
+ index = serializeMinMax(buffer, key, value, index);
819
+ } else if (typeof value._bsontype !== 'undefined') {
820
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
821
+ }
822
+ } else if (type === 'function' && serializeFunctions) {
802
823
  index = serializeFunction(buffer, key, value, index);
803
- } else if (value._bsontype === 'Binary') {
804
- index = serializeBinary(buffer, key, value, index);
805
- } else if (value._bsontype === 'BSONSymbol') {
806
- index = serializeSymbol(buffer, key, value, index);
807
- } else if (value._bsontype === 'DBRef') {
808
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
809
- } else if (value._bsontype === 'BSONRegExp') {
810
- index = serializeBSONRegExp(buffer, key, value, index);
811
- } else if (value._bsontype === 'Int32') {
812
- index = serializeInt32(buffer, key, value, index);
813
- } else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
814
- index = serializeMinMax(buffer, key, value, index);
815
- } else if (typeof value._bsontype !== 'undefined') {
816
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
817
824
  }
818
825
  }
819
826
  } else {
@@ -853,7 +860,11 @@ export function serializeInto(
853
860
  }
854
861
  }
855
862
 
856
- if (type === 'string') {
863
+ if (value === undefined) {
864
+ if (ignoreUndefined === false) index = serializeNull(buffer, key, value, index);
865
+ } else if (value === null) {
866
+ index = serializeNull(buffer, key, value, index);
867
+ } else if (type === 'string') {
857
868
  index = serializeString(buffer, key, value, index);
858
869
  } else if (type === 'number') {
859
870
  index = serializeNumber(buffer, key, value, index);
@@ -861,69 +872,66 @@ export function serializeInto(
861
872
  index = serializeBigInt(buffer, key, value, index);
862
873
  } else if (type === 'boolean') {
863
874
  index = serializeBoolean(buffer, key, value, index);
864
- } else if (value instanceof Date || isDate(value)) {
865
- index = serializeDate(buffer, key, value, index);
866
- } else if (value === undefined) {
867
- if (ignoreUndefined === false) index = serializeNull(buffer, key, value, index);
868
- } else if (value === null) {
869
- index = serializeNull(buffer, key, value, index);
870
- } else if (isUint8Array(value)) {
871
- index = serializeBuffer(buffer, key, value, index);
872
- } else if (value instanceof RegExp || isRegExp(value)) {
873
- index = serializeRegExp(buffer, key, value, index);
874
875
  } else if (type === 'object' && value._bsontype == null) {
875
- index = serializeObject(
876
- buffer,
877
- key,
878
- value,
879
- index,
880
- checkKeys,
881
- depth,
882
- serializeFunctions,
883
- ignoreUndefined,
884
- path
885
- );
886
- } else if (
887
- typeof value === 'object' &&
888
- value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
889
- ) {
890
- throw new BSONVersionError();
891
- } else if (value._bsontype === 'ObjectId') {
892
- index = serializeObjectId(buffer, key, value, index);
893
- } else if (type === 'object' && value._bsontype === 'Decimal128') {
894
- index = serializeDecimal128(buffer, key, value, index);
895
- } else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
896
- index = serializeLong(buffer, key, value, index);
897
- } else if (value._bsontype === 'Double') {
898
- index = serializeDouble(buffer, key, value, index);
899
- } else if (value._bsontype === 'Code') {
900
- index = serializeCode(
901
- buffer,
902
- key,
903
- value,
904
- index,
905
- checkKeys,
906
- depth,
907
- serializeFunctions,
908
- ignoreUndefined,
909
- path
910
- );
911
- } else if (typeof value === 'function' && serializeFunctions) {
876
+ if (value instanceof Date || isDate(value)) {
877
+ index = serializeDate(buffer, key, value, index);
878
+ } else if (value instanceof Uint8Array || isUint8Array(value)) {
879
+ index = serializeBuffer(buffer, key, value, index);
880
+ } else if (value instanceof RegExp || isRegExp(value)) {
881
+ index = serializeRegExp(buffer, key, value, index);
882
+ } else {
883
+ index = serializeObject(
884
+ buffer,
885
+ key,
886
+ value,
887
+ index,
888
+ checkKeys,
889
+ depth,
890
+ serializeFunctions,
891
+ ignoreUndefined,
892
+ path
893
+ );
894
+ }
895
+ } else if (type === 'object') {
896
+ if (value[constants.BSON_VERSION_SYMBOL] !== constants.BSON_MAJOR_VERSION) {
897
+ throw new BSONVersionError();
898
+ } else if (value._bsontype === 'ObjectId') {
899
+ index = serializeObjectId(buffer, key, value, index);
900
+ } else if (value._bsontype === 'Decimal128') {
901
+ index = serializeDecimal128(buffer, key, value, index);
902
+ } else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
903
+ index = serializeLong(buffer, key, value, index);
904
+ } else if (value._bsontype === 'Double') {
905
+ index = serializeDouble(buffer, key, value, index);
906
+ } else if (value._bsontype === 'Code') {
907
+ index = serializeCode(
908
+ buffer,
909
+ key,
910
+ value,
911
+ index,
912
+ checkKeys,
913
+ depth,
914
+ serializeFunctions,
915
+ ignoreUndefined,
916
+ path
917
+ );
918
+ } else if (value._bsontype === 'Binary') {
919
+ index = serializeBinary(buffer, key, value, index);
920
+ } else if (value._bsontype === 'BSONSymbol') {
921
+ index = serializeSymbol(buffer, key, value, index);
922
+ } else if (value._bsontype === 'DBRef') {
923
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
924
+ } else if (value._bsontype === 'BSONRegExp') {
925
+ index = serializeBSONRegExp(buffer, key, value, index);
926
+ } else if (value._bsontype === 'Int32') {
927
+ index = serializeInt32(buffer, key, value, index);
928
+ } else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
929
+ index = serializeMinMax(buffer, key, value, index);
930
+ } else if (typeof value._bsontype !== 'undefined') {
931
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
932
+ }
933
+ } else if (type === 'function' && serializeFunctions) {
912
934
  index = serializeFunction(buffer, key, value, index);
913
- } else if (value._bsontype === 'Binary') {
914
- index = serializeBinary(buffer, key, value, index);
915
- } else if (value._bsontype === 'BSONSymbol') {
916
- index = serializeSymbol(buffer, key, value, index);
917
- } else if (value._bsontype === 'DBRef') {
918
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
919
- } else if (value._bsontype === 'BSONRegExp') {
920
- index = serializeBSONRegExp(buffer, key, value, index);
921
- } else if (value._bsontype === 'Int32') {
922
- index = serializeInt32(buffer, key, value, index);
923
- } else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
924
- index = serializeMinMax(buffer, key, value, index);
925
- } else if (typeof value._bsontype !== 'undefined') {
926
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
927
935
  }
928
936
  }
929
937
  }