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/lib/bson.mjs CHANGED
@@ -1,17 +1,43 @@
1
+ const map = new WeakMap();
2
+ const TYPES = {
3
+ ArrayBuffer: '[object ArrayBuffer]',
4
+ SharedArrayBuffer: '[object SharedArrayBuffer]',
5
+ Uint8Array: '[object Uint8Array]',
6
+ BigInt64Array: '[object BigInt64Array]',
7
+ BigUint64Array: '[object BigUint64Array]',
8
+ RegExp: '[object RegExp]',
9
+ Map: '[object Map]',
10
+ Date: '[object Date]'
11
+ };
12
+ function getPrototypeString(value) {
13
+ let str = map.get(value);
14
+ if (!str) {
15
+ str = Object.prototype.toString.call(value);
16
+ if (value !== null && typeof value === 'object') {
17
+ map.set(value, str);
18
+ }
19
+ }
20
+ return str;
21
+ }
1
22
  function isAnyArrayBuffer(value) {
2
- return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(Object.prototype.toString.call(value));
23
+ const type = getPrototypeString(value);
24
+ return type === TYPES.ArrayBuffer || type === TYPES.SharedArrayBuffer;
3
25
  }
4
26
  function isUint8Array(value) {
5
- return Object.prototype.toString.call(value) === '[object Uint8Array]';
27
+ const type = getPrototypeString(value);
28
+ return type === TYPES.Uint8Array;
6
29
  }
7
30
  function isRegExp(d) {
8
- return Object.prototype.toString.call(d) === '[object RegExp]';
31
+ const type = getPrototypeString(d);
32
+ return type === TYPES.RegExp;
9
33
  }
10
34
  function isMap(d) {
11
- return Object.prototype.toString.call(d) === '[object Map]';
35
+ const type = getPrototypeString(d);
36
+ return type === TYPES.Map;
12
37
  }
13
38
  function isDate(d) {
14
- return Object.prototype.toString.call(d) === '[object Date]';
39
+ const type = getPrototypeString(d);
40
+ return type === TYPES.Date;
15
41
  }
16
42
  function defaultInspect(x, _options) {
17
43
  return JSON.stringify(x, (k, v) => {
@@ -35,6 +61,7 @@ function getStylizeFunction(options) {
35
61
  }
36
62
 
37
63
  const BSON_MAJOR_VERSION = 6;
64
+ const BSON_VERSION_SYMBOL = Symbol.for('@@mdb.bson.version');
38
65
  const BSON_INT32_MAX = 0x7fffffff;
39
66
  const BSON_INT32_MIN = -0x80000000;
40
67
  const BSON_INT64_MAX = Math.pow(2, 63) - 1;
@@ -409,7 +436,7 @@ const hasGlobalBuffer = typeof Buffer === 'function' && Buffer.prototype?._isBuf
409
436
  const ByteUtils = hasGlobalBuffer ? nodeJsByteUtils : webByteUtils;
410
437
 
411
438
  class BSONValue {
412
- get [Symbol.for('@@mdb.bson.version')]() {
439
+ get [BSON_VERSION_SYMBOL]() {
413
440
  return BSON_MAJOR_VERSION;
414
441
  }
415
442
  [Symbol.for('nodejs.util.inspect.custom')](depth, options, inspect) {
@@ -1586,7 +1613,7 @@ class Decimal128 extends BSONValue {
1586
1613
  if (typeof bytes === 'string') {
1587
1614
  this.bytes = Decimal128.fromString(bytes).bytes;
1588
1615
  }
1589
- else if (isUint8Array(bytes)) {
1616
+ else if (bytes instanceof Uint8Array || isUint8Array(bytes)) {
1590
1617
  if (bytes.byteLength !== 16) {
1591
1618
  throw new BSONError('Decimal128 must take a Buffer of 16 bytes');
1592
1619
  }
@@ -2283,7 +2310,7 @@ const NumberUtils = {
2283
2310
  return 4;
2284
2311
  },
2285
2312
  setBigInt64LE(destination, offset, value) {
2286
- const mask32bits = BigInt(4294967295);
2313
+ const mask32bits = BigInt(0xffff_ffff);
2287
2314
  let lo = Number(value & mask32bits);
2288
2315
  destination[offset] = lo;
2289
2316
  lo >>= 8;
@@ -2329,7 +2356,6 @@ const NumberUtils = {
2329
2356
  }
2330
2357
  };
2331
2358
 
2332
- const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
2333
2359
  let PROCESS_UNIQUE = null;
2334
2360
  class ObjectId extends BSONValue {
2335
2361
  get _bsontype() {
@@ -2359,7 +2385,7 @@ class ObjectId extends BSONValue {
2359
2385
  this.buffer = ByteUtils.toLocalBufferType(workingId);
2360
2386
  }
2361
2387
  else if (typeof workingId === 'string') {
2362
- if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
2388
+ if (ObjectId.validateHexString(workingId)) {
2363
2389
  this.buffer = ByteUtils.fromHex(workingId);
2364
2390
  }
2365
2391
  else {
@@ -2382,6 +2408,20 @@ class ObjectId extends BSONValue {
2382
2408
  this.__id = ByteUtils.toHex(value);
2383
2409
  }
2384
2410
  }
2411
+ static validateHexString(string) {
2412
+ if (string?.length !== 24)
2413
+ return false;
2414
+ for (let i = 0; i < 24; i++) {
2415
+ const char = string.charCodeAt(i);
2416
+ if ((char >= 48 && char <= 57) ||
2417
+ (char >= 97 && char <= 102) ||
2418
+ (char >= 65 && char <= 70)) {
2419
+ continue;
2420
+ }
2421
+ return false;
2422
+ }
2423
+ return true;
2424
+ }
2385
2425
  toHexString() {
2386
2426
  if (ObjectId.cacheHexString && this.__id) {
2387
2427
  return this.__id;
@@ -2494,6 +2534,8 @@ class ObjectId extends BSONValue {
2494
2534
  static isValid(id) {
2495
2535
  if (id == null)
2496
2536
  return false;
2537
+ if (typeof id === 'string')
2538
+ return ObjectId.validateHexString(id);
2497
2539
  try {
2498
2540
  new ObjectId(id);
2499
2541
  return true;
@@ -2564,7 +2606,7 @@ function calculateElement(name, value, serializeFunctions = false, isArray = fal
2564
2606
  case 'object':
2565
2607
  if (value != null &&
2566
2608
  typeof value._bsontype === 'string' &&
2567
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
2609
+ value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
2568
2610
  throw new BSONVersionError();
2569
2611
  }
2570
2612
  else if (value == null || value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
@@ -2768,6 +2810,12 @@ class Timestamp extends LongWithoutOverridesClass {
2768
2810
  get _bsontype() {
2769
2811
  return 'Timestamp';
2770
2812
  }
2813
+ get i() {
2814
+ return this.low >>> 0;
2815
+ }
2816
+ get t() {
2817
+ return this.high >>> 0;
2818
+ }
2771
2819
  constructor(low) {
2772
2820
  if (low == null) {
2773
2821
  super(0, 0, true);
@@ -2793,10 +2841,10 @@ class Timestamp extends LongWithoutOverridesClass {
2793
2841
  if (i < 0 || Number.isNaN(i)) {
2794
2842
  throw new BSONError('Timestamp constructed from { t, i } must provide a positive i');
2795
2843
  }
2796
- if (t > 4294967295) {
2844
+ if (t > 0xffff_ffff) {
2797
2845
  throw new BSONError('Timestamp constructed from { t, i } must provide t equal or less than uint32 max');
2798
2846
  }
2799
- if (i > 4294967295) {
2847
+ if (i > 0xffff_ffff) {
2800
2848
  throw new BSONError('Timestamp constructed from { t, i } must provide i equal or less than uint32 max');
2801
2849
  }
2802
2850
  super(i, t, true);
@@ -2823,7 +2871,7 @@ class Timestamp extends LongWithoutOverridesClass {
2823
2871
  return new Timestamp(Long.fromString(str, true, optRadix));
2824
2872
  }
2825
2873
  toExtendedJSON() {
2826
- return { $timestamp: { t: this.high >>> 0, i: this.low >>> 0 } };
2874
+ return { $timestamp: { t: this.t, i: this.i } };
2827
2875
  }
2828
2876
  static fromExtendedJSON(doc) {
2829
2877
  const i = Long.isLong(doc.$timestamp.i)
@@ -2836,8 +2884,8 @@ class Timestamp extends LongWithoutOverridesClass {
2836
2884
  }
2837
2885
  inspect(depth, options, inspect) {
2838
2886
  inspect ??= defaultInspect;
2839
- const t = inspect(this.high >>> 0, options);
2840
- const i = inspect(this.low >>> 0, options);
2887
+ const t = inspect(this.t, options);
2888
+ const i = inspect(this.i, options);
2841
2889
  return `new Timestamp({ t: ${t}, i: ${i} })`;
2842
2890
  }
2843
2891
  }
@@ -3606,79 +3654,83 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3606
3654
  if (typeof value?.toBSON === 'function') {
3607
3655
  value = value.toBSON();
3608
3656
  }
3609
- if (typeof value === 'string') {
3610
- index = serializeString(buffer, key, value, index);
3611
- }
3612
- else if (typeof value === 'number') {
3613
- index = serializeNumber(buffer, key, value, index);
3614
- }
3615
- else if (typeof value === 'bigint') {
3616
- index = serializeBigInt(buffer, key, value, index);
3617
- }
3618
- else if (typeof value === 'boolean') {
3619
- index = serializeBoolean(buffer, key, value, index);
3620
- }
3621
- else if (value instanceof Date || isDate(value)) {
3622
- index = serializeDate(buffer, key, value, index);
3623
- }
3624
- else if (value === undefined) {
3657
+ const type = typeof value;
3658
+ if (value === undefined) {
3625
3659
  index = serializeNull(buffer, key, value, index);
3626
3660
  }
3627
3661
  else if (value === null) {
3628
3662
  index = serializeNull(buffer, key, value, index);
3629
3663
  }
3630
- else if (isUint8Array(value)) {
3631
- index = serializeBuffer(buffer, key, value, index);
3632
- }
3633
- else if (value instanceof RegExp || isRegExp(value)) {
3634
- index = serializeRegExp(buffer, key, value, index);
3635
- }
3636
- else if (typeof value === 'object' && value._bsontype == null) {
3637
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3664
+ else if (type === 'string') {
3665
+ index = serializeString(buffer, key, value, index);
3638
3666
  }
3639
- else if (typeof value === 'object' &&
3640
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3641
- throw new BSONVersionError();
3667
+ else if (type === 'number') {
3668
+ index = serializeNumber(buffer, key, value, index);
3642
3669
  }
3643
- else if (value._bsontype === 'ObjectId') {
3644
- index = serializeObjectId(buffer, key, value, index);
3670
+ else if (type === 'bigint') {
3671
+ index = serializeBigInt(buffer, key, value, index);
3645
3672
  }
3646
- else if (value._bsontype === 'Decimal128') {
3647
- index = serializeDecimal128(buffer, key, value, index);
3673
+ else if (type === 'boolean') {
3674
+ index = serializeBoolean(buffer, key, value, index);
3648
3675
  }
3649
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3650
- index = serializeLong(buffer, key, value, index);
3676
+ else if (type === 'object' && value._bsontype == null) {
3677
+ if (value instanceof Date || isDate(value)) {
3678
+ index = serializeDate(buffer, key, value, index);
3679
+ }
3680
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3681
+ index = serializeBuffer(buffer, key, value, index);
3682
+ }
3683
+ else if (value instanceof RegExp || isRegExp(value)) {
3684
+ index = serializeRegExp(buffer, key, value, index);
3685
+ }
3686
+ else {
3687
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3688
+ }
3651
3689
  }
3652
- else if (value._bsontype === 'Double') {
3653
- index = serializeDouble(buffer, key, value, index);
3690
+ else if (type === 'object') {
3691
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3692
+ throw new BSONVersionError();
3693
+ }
3694
+ else if (value._bsontype === 'ObjectId') {
3695
+ index = serializeObjectId(buffer, key, value, index);
3696
+ }
3697
+ else if (value._bsontype === 'Decimal128') {
3698
+ index = serializeDecimal128(buffer, key, value, index);
3699
+ }
3700
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3701
+ index = serializeLong(buffer, key, value, index);
3702
+ }
3703
+ else if (value._bsontype === 'Double') {
3704
+ index = serializeDouble(buffer, key, value, index);
3705
+ }
3706
+ else if (value._bsontype === 'Code') {
3707
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3708
+ }
3709
+ else if (value._bsontype === 'Binary') {
3710
+ index = serializeBinary(buffer, key, value, index);
3711
+ }
3712
+ else if (value._bsontype === 'BSONSymbol') {
3713
+ index = serializeSymbol(buffer, key, value, index);
3714
+ }
3715
+ else if (value._bsontype === 'DBRef') {
3716
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3717
+ }
3718
+ else if (value._bsontype === 'BSONRegExp') {
3719
+ index = serializeBSONRegExp(buffer, key, value, index);
3720
+ }
3721
+ else if (value._bsontype === 'Int32') {
3722
+ index = serializeInt32(buffer, key, value, index);
3723
+ }
3724
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3725
+ index = serializeMinMax(buffer, key, value, index);
3726
+ }
3727
+ else if (typeof value._bsontype !== 'undefined') {
3728
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3729
+ }
3654
3730
  }
3655
- else if (typeof value === 'function' && serializeFunctions) {
3731
+ else if (type === 'function' && serializeFunctions) {
3656
3732
  index = serializeFunction(buffer, key, value, index);
3657
3733
  }
3658
- else if (value._bsontype === 'Code') {
3659
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3660
- }
3661
- else if (value._bsontype === 'Binary') {
3662
- index = serializeBinary(buffer, key, value, index);
3663
- }
3664
- else if (value._bsontype === 'BSONSymbol') {
3665
- index = serializeSymbol(buffer, key, value, index);
3666
- }
3667
- else if (value._bsontype === 'DBRef') {
3668
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3669
- }
3670
- else if (value._bsontype === 'BSONRegExp') {
3671
- index = serializeBSONRegExp(buffer, key, value, index);
3672
- }
3673
- else if (value._bsontype === 'Int32') {
3674
- index = serializeInt32(buffer, key, value, index);
3675
- }
3676
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3677
- index = serializeMinMax(buffer, key, value, index);
3678
- }
3679
- else if (typeof value._bsontype !== 'undefined') {
3680
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3681
- }
3682
3734
  }
3683
3735
  }
3684
3736
  else if (object instanceof Map || isMap(object)) {
@@ -3708,7 +3760,14 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3708
3760
  }
3709
3761
  }
3710
3762
  }
3711
- if (type === 'string') {
3763
+ if (value === undefined) {
3764
+ if (ignoreUndefined === false)
3765
+ index = serializeNull(buffer, key, value, index);
3766
+ }
3767
+ else if (value === null) {
3768
+ index = serializeNull(buffer, key, value, index);
3769
+ }
3770
+ else if (type === 'string') {
3712
3771
  index = serializeString(buffer, key, value, index);
3713
3772
  }
3714
3773
  else if (type === 'number') {
@@ -3720,64 +3779,64 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3720
3779
  else if (type === 'boolean') {
3721
3780
  index = serializeBoolean(buffer, key, value, index);
3722
3781
  }
3723
- else if (value instanceof Date || isDate(value)) {
3724
- index = serializeDate(buffer, key, value, index);
3725
- }
3726
- else if (value === null || (value === undefined && ignoreUndefined === false)) {
3727
- index = serializeNull(buffer, key, value, index);
3728
- }
3729
- else if (isUint8Array(value)) {
3730
- index = serializeBuffer(buffer, key, value, index);
3731
- }
3732
- else if (value instanceof RegExp || isRegExp(value)) {
3733
- index = serializeRegExp(buffer, key, value, index);
3734
- }
3735
3782
  else if (type === 'object' && value._bsontype == null) {
3736
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3737
- }
3738
- else if (typeof value === 'object' &&
3739
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3740
- throw new BSONVersionError();
3741
- }
3742
- else if (value._bsontype === 'ObjectId') {
3743
- index = serializeObjectId(buffer, key, value, index);
3744
- }
3745
- else if (type === 'object' && value._bsontype === 'Decimal128') {
3746
- index = serializeDecimal128(buffer, key, value, index);
3747
- }
3748
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3749
- index = serializeLong(buffer, key, value, index);
3750
- }
3751
- else if (value._bsontype === 'Double') {
3752
- index = serializeDouble(buffer, key, value, index);
3783
+ if (value instanceof Date || isDate(value)) {
3784
+ index = serializeDate(buffer, key, value, index);
3785
+ }
3786
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3787
+ index = serializeBuffer(buffer, key, value, index);
3788
+ }
3789
+ else if (value instanceof RegExp || isRegExp(value)) {
3790
+ index = serializeRegExp(buffer, key, value, index);
3791
+ }
3792
+ else {
3793
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3794
+ }
3753
3795
  }
3754
- else if (value._bsontype === 'Code') {
3755
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3796
+ else if (type === 'object') {
3797
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3798
+ throw new BSONVersionError();
3799
+ }
3800
+ else if (value._bsontype === 'ObjectId') {
3801
+ index = serializeObjectId(buffer, key, value, index);
3802
+ }
3803
+ else if (value._bsontype === 'Decimal128') {
3804
+ index = serializeDecimal128(buffer, key, value, index);
3805
+ }
3806
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3807
+ index = serializeLong(buffer, key, value, index);
3808
+ }
3809
+ else if (value._bsontype === 'Double') {
3810
+ index = serializeDouble(buffer, key, value, index);
3811
+ }
3812
+ else if (value._bsontype === 'Code') {
3813
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3814
+ }
3815
+ else if (value._bsontype === 'Binary') {
3816
+ index = serializeBinary(buffer, key, value, index);
3817
+ }
3818
+ else if (value._bsontype === 'BSONSymbol') {
3819
+ index = serializeSymbol(buffer, key, value, index);
3820
+ }
3821
+ else if (value._bsontype === 'DBRef') {
3822
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3823
+ }
3824
+ else if (value._bsontype === 'BSONRegExp') {
3825
+ index = serializeBSONRegExp(buffer, key, value, index);
3826
+ }
3827
+ else if (value._bsontype === 'Int32') {
3828
+ index = serializeInt32(buffer, key, value, index);
3829
+ }
3830
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3831
+ index = serializeMinMax(buffer, key, value, index);
3832
+ }
3833
+ else if (typeof value._bsontype !== 'undefined') {
3834
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3835
+ }
3756
3836
  }
3757
- else if (typeof value === 'function' && serializeFunctions) {
3837
+ else if (type === 'function' && serializeFunctions) {
3758
3838
  index = serializeFunction(buffer, key, value, index);
3759
3839
  }
3760
- else if (value._bsontype === 'Binary') {
3761
- index = serializeBinary(buffer, key, value, index);
3762
- }
3763
- else if (value._bsontype === 'BSONSymbol') {
3764
- index = serializeSymbol(buffer, key, value, index);
3765
- }
3766
- else if (value._bsontype === 'DBRef') {
3767
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3768
- }
3769
- else if (value._bsontype === 'BSONRegExp') {
3770
- index = serializeBSONRegExp(buffer, key, value, index);
3771
- }
3772
- else if (value._bsontype === 'Int32') {
3773
- index = serializeInt32(buffer, key, value, index);
3774
- }
3775
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3776
- index = serializeMinMax(buffer, key, value, index);
3777
- }
3778
- else if (typeof value._bsontype !== 'undefined') {
3779
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3780
- }
3781
3840
  }
3782
3841
  }
3783
3842
  else {
@@ -3806,7 +3865,14 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3806
3865
  }
3807
3866
  }
3808
3867
  }
3809
- if (type === 'string') {
3868
+ if (value === undefined) {
3869
+ if (ignoreUndefined === false)
3870
+ index = serializeNull(buffer, key, value, index);
3871
+ }
3872
+ else if (value === null) {
3873
+ index = serializeNull(buffer, key, value, index);
3874
+ }
3875
+ else if (type === 'string') {
3810
3876
  index = serializeString(buffer, key, value, index);
3811
3877
  }
3812
3878
  else if (type === 'number') {
@@ -3818,68 +3884,64 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3818
3884
  else if (type === 'boolean') {
3819
3885
  index = serializeBoolean(buffer, key, value, index);
3820
3886
  }
3821
- else if (value instanceof Date || isDate(value)) {
3822
- index = serializeDate(buffer, key, value, index);
3823
- }
3824
- else if (value === undefined) {
3825
- if (ignoreUndefined === false)
3826
- index = serializeNull(buffer, key, value, index);
3827
- }
3828
- else if (value === null) {
3829
- index = serializeNull(buffer, key, value, index);
3830
- }
3831
- else if (isUint8Array(value)) {
3832
- index = serializeBuffer(buffer, key, value, index);
3833
- }
3834
- else if (value instanceof RegExp || isRegExp(value)) {
3835
- index = serializeRegExp(buffer, key, value, index);
3836
- }
3837
3887
  else if (type === 'object' && value._bsontype == null) {
3838
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3839
- }
3840
- else if (typeof value === 'object' &&
3841
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3842
- throw new BSONVersionError();
3843
- }
3844
- else if (value._bsontype === 'ObjectId') {
3845
- index = serializeObjectId(buffer, key, value, index);
3846
- }
3847
- else if (type === 'object' && value._bsontype === 'Decimal128') {
3848
- index = serializeDecimal128(buffer, key, value, index);
3849
- }
3850
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3851
- index = serializeLong(buffer, key, value, index);
3852
- }
3853
- else if (value._bsontype === 'Double') {
3854
- index = serializeDouble(buffer, key, value, index);
3888
+ if (value instanceof Date || isDate(value)) {
3889
+ index = serializeDate(buffer, key, value, index);
3890
+ }
3891
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3892
+ index = serializeBuffer(buffer, key, value, index);
3893
+ }
3894
+ else if (value instanceof RegExp || isRegExp(value)) {
3895
+ index = serializeRegExp(buffer, key, value, index);
3896
+ }
3897
+ else {
3898
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3899
+ }
3855
3900
  }
3856
- else if (value._bsontype === 'Code') {
3857
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3901
+ else if (type === 'object') {
3902
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3903
+ throw new BSONVersionError();
3904
+ }
3905
+ else if (value._bsontype === 'ObjectId') {
3906
+ index = serializeObjectId(buffer, key, value, index);
3907
+ }
3908
+ else if (value._bsontype === 'Decimal128') {
3909
+ index = serializeDecimal128(buffer, key, value, index);
3910
+ }
3911
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3912
+ index = serializeLong(buffer, key, value, index);
3913
+ }
3914
+ else if (value._bsontype === 'Double') {
3915
+ index = serializeDouble(buffer, key, value, index);
3916
+ }
3917
+ else if (value._bsontype === 'Code') {
3918
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3919
+ }
3920
+ else if (value._bsontype === 'Binary') {
3921
+ index = serializeBinary(buffer, key, value, index);
3922
+ }
3923
+ else if (value._bsontype === 'BSONSymbol') {
3924
+ index = serializeSymbol(buffer, key, value, index);
3925
+ }
3926
+ else if (value._bsontype === 'DBRef') {
3927
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3928
+ }
3929
+ else if (value._bsontype === 'BSONRegExp') {
3930
+ index = serializeBSONRegExp(buffer, key, value, index);
3931
+ }
3932
+ else if (value._bsontype === 'Int32') {
3933
+ index = serializeInt32(buffer, key, value, index);
3934
+ }
3935
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3936
+ index = serializeMinMax(buffer, key, value, index);
3937
+ }
3938
+ else if (typeof value._bsontype !== 'undefined') {
3939
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3940
+ }
3858
3941
  }
3859
- else if (typeof value === 'function' && serializeFunctions) {
3942
+ else if (type === 'function' && serializeFunctions) {
3860
3943
  index = serializeFunction(buffer, key, value, index);
3861
3944
  }
3862
- else if (value._bsontype === 'Binary') {
3863
- index = serializeBinary(buffer, key, value, index);
3864
- }
3865
- else if (value._bsontype === 'BSONSymbol') {
3866
- index = serializeSymbol(buffer, key, value, index);
3867
- }
3868
- else if (value._bsontype === 'DBRef') {
3869
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3870
- }
3871
- else if (value._bsontype === 'BSONRegExp') {
3872
- index = serializeBSONRegExp(buffer, key, value, index);
3873
- }
3874
- else if (value._bsontype === 'Int32') {
3875
- index = serializeInt32(buffer, key, value, index);
3876
- }
3877
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3878
- index = serializeMinMax(buffer, key, value, index);
3879
- }
3880
- else if (typeof value._bsontype !== 'undefined') {
3881
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3882
- }
3883
3945
  }
3884
3946
  }
3885
3947
  path.delete(object);
@@ -4131,7 +4193,7 @@ function serializeDocument(doc, options) {
4131
4193
  else if (doc != null &&
4132
4194
  typeof doc === 'object' &&
4133
4195
  typeof doc._bsontype === 'string' &&
4134
- doc[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
4196
+ doc[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
4135
4197
  throw new BSONVersionError();
4136
4198
  }
4137
4199
  else if (isBSONType(doc)) {