bson 6.8.0 → 6.9.0

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.rn.cjs CHANGED
@@ -1,25 +1,53 @@
1
1
  'use strict';
2
2
 
3
+ const map = new WeakMap();
4
+ const TYPES = {
5
+ ArrayBuffer: '[object ArrayBuffer]',
6
+ SharedArrayBuffer: '[object SharedArrayBuffer]',
7
+ Uint8Array: '[object Uint8Array]',
8
+ BigInt64Array: '[object BigInt64Array]',
9
+ BigUint64Array: '[object BigUint64Array]',
10
+ RegExp: '[object RegExp]',
11
+ Map: '[object Map]',
12
+ Date: '[object Date]'
13
+ };
14
+ function getPrototypeString(value) {
15
+ let str = map.get(value);
16
+ if (!str) {
17
+ str = Object.prototype.toString.call(value);
18
+ if (value !== null && typeof value === 'object') {
19
+ map.set(value, str);
20
+ }
21
+ }
22
+ return str;
23
+ }
3
24
  function isAnyArrayBuffer(value) {
4
- return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(Object.prototype.toString.call(value));
25
+ const type = getPrototypeString(value);
26
+ return type === TYPES.ArrayBuffer || type === TYPES.SharedArrayBuffer;
5
27
  }
6
28
  function isUint8Array(value) {
7
- return Object.prototype.toString.call(value) === '[object Uint8Array]';
29
+ const type = getPrototypeString(value);
30
+ return type === TYPES.Uint8Array;
8
31
  }
9
32
  function isBigInt64Array(value) {
10
- return Object.prototype.toString.call(value) === '[object BigInt64Array]';
33
+ const type = getPrototypeString(value);
34
+ return type === TYPES.BigInt64Array;
11
35
  }
12
36
  function isBigUInt64Array(value) {
13
- return Object.prototype.toString.call(value) === '[object BigUint64Array]';
37
+ const type = getPrototypeString(value);
38
+ return type === TYPES.BigUint64Array;
14
39
  }
15
40
  function isRegExp(d) {
16
- return Object.prototype.toString.call(d) === '[object RegExp]';
41
+ const type = getPrototypeString(d);
42
+ return type === TYPES.RegExp;
17
43
  }
18
44
  function isMap(d) {
19
- return Object.prototype.toString.call(d) === '[object Map]';
45
+ const type = getPrototypeString(d);
46
+ return type === TYPES.Map;
20
47
  }
21
48
  function isDate(d) {
22
- return Object.prototype.toString.call(d) === '[object Date]';
49
+ const type = getPrototypeString(d);
50
+ return type === TYPES.Date;
23
51
  }
24
52
  function defaultInspect(x, _options) {
25
53
  return JSON.stringify(x, (k, v) => {
@@ -43,6 +71,7 @@ function getStylizeFunction(options) {
43
71
  }
44
72
 
45
73
  const BSON_MAJOR_VERSION = 6;
74
+ const BSON_VERSION_SYMBOL = Symbol.for('@@mdb.bson.version');
46
75
  const BSON_INT32_MAX = 0x7fffffff;
47
76
  const BSON_INT32_MIN = -0x80000000;
48
77
  const BSON_INT64_MAX = Math.pow(2, 63) - 1;
@@ -428,7 +457,7 @@ const hasGlobalBuffer = typeof Buffer === 'function' && Buffer.prototype?._isBuf
428
457
  const ByteUtils = hasGlobalBuffer ? nodeJsByteUtils : webByteUtils;
429
458
 
430
459
  class BSONValue {
431
- get [Symbol.for('@@mdb.bson.version')]() {
460
+ get [BSON_VERSION_SYMBOL]() {
432
461
  return BSON_MAJOR_VERSION;
433
462
  }
434
463
  [Symbol.for('nodejs.util.inspect.custom')](depth, options, inspect) {
@@ -1605,7 +1634,7 @@ class Decimal128 extends BSONValue {
1605
1634
  if (typeof bytes === 'string') {
1606
1635
  this.bytes = Decimal128.fromString(bytes).bytes;
1607
1636
  }
1608
- else if (isUint8Array(bytes)) {
1637
+ else if (bytes instanceof Uint8Array || isUint8Array(bytes)) {
1609
1638
  if (bytes.byteLength !== 16) {
1610
1639
  throw new BSONError('Decimal128 must take a Buffer of 16 bytes');
1611
1640
  }
@@ -2296,7 +2325,7 @@ const NumberUtils = {
2296
2325
  return 4;
2297
2326
  },
2298
2327
  setBigInt64LE(destination, offset, value) {
2299
- const mask32bits = BigInt(4294967295);
2328
+ const mask32bits = BigInt(0xffff_ffff);
2300
2329
  let lo = Number(value & mask32bits);
2301
2330
  destination[offset] = lo;
2302
2331
  lo >>= 8;
@@ -2342,7 +2371,6 @@ const NumberUtils = {
2342
2371
  }
2343
2372
  };
2344
2373
 
2345
- const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
2346
2374
  let PROCESS_UNIQUE = null;
2347
2375
  class ObjectId extends BSONValue {
2348
2376
  get _bsontype() {
@@ -2372,7 +2400,7 @@ class ObjectId extends BSONValue {
2372
2400
  this.buffer = ByteUtils.toLocalBufferType(workingId);
2373
2401
  }
2374
2402
  else if (typeof workingId === 'string') {
2375
- if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
2403
+ if (ObjectId.validateHexString(workingId)) {
2376
2404
  this.buffer = ByteUtils.fromHex(workingId);
2377
2405
  }
2378
2406
  else {
@@ -2395,6 +2423,20 @@ class ObjectId extends BSONValue {
2395
2423
  this.__id = ByteUtils.toHex(value);
2396
2424
  }
2397
2425
  }
2426
+ static validateHexString(string) {
2427
+ if (string?.length !== 24)
2428
+ return false;
2429
+ for (let i = 0; i < 24; i++) {
2430
+ const char = string.charCodeAt(i);
2431
+ if ((char >= 48 && char <= 57) ||
2432
+ (char >= 97 && char <= 102) ||
2433
+ (char >= 65 && char <= 70)) {
2434
+ continue;
2435
+ }
2436
+ return false;
2437
+ }
2438
+ return true;
2439
+ }
2398
2440
  toHexString() {
2399
2441
  if (ObjectId.cacheHexString && this.__id) {
2400
2442
  return this.__id;
@@ -2507,6 +2549,8 @@ class ObjectId extends BSONValue {
2507
2549
  static isValid(id) {
2508
2550
  if (id == null)
2509
2551
  return false;
2552
+ if (typeof id === 'string')
2553
+ return ObjectId.validateHexString(id);
2510
2554
  try {
2511
2555
  new ObjectId(id);
2512
2556
  return true;
@@ -2577,7 +2621,7 @@ function calculateElement(name, value, serializeFunctions = false, isArray = fal
2577
2621
  case 'object':
2578
2622
  if (value != null &&
2579
2623
  typeof value._bsontype === 'string' &&
2580
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
2624
+ value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
2581
2625
  throw new BSONVersionError();
2582
2626
  }
2583
2627
  else if (value == null || value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
@@ -2781,6 +2825,12 @@ class Timestamp extends LongWithoutOverridesClass {
2781
2825
  get _bsontype() {
2782
2826
  return 'Timestamp';
2783
2827
  }
2828
+ get i() {
2829
+ return this.low >>> 0;
2830
+ }
2831
+ get t() {
2832
+ return this.high >>> 0;
2833
+ }
2784
2834
  constructor(low) {
2785
2835
  if (low == null) {
2786
2836
  super(0, 0, true);
@@ -2806,10 +2856,10 @@ class Timestamp extends LongWithoutOverridesClass {
2806
2856
  if (i < 0 || Number.isNaN(i)) {
2807
2857
  throw new BSONError('Timestamp constructed from { t, i } must provide a positive i');
2808
2858
  }
2809
- if (t > 4294967295) {
2859
+ if (t > 0xffff_ffff) {
2810
2860
  throw new BSONError('Timestamp constructed from { t, i } must provide t equal or less than uint32 max');
2811
2861
  }
2812
- if (i > 4294967295) {
2862
+ if (i > 0xffff_ffff) {
2813
2863
  throw new BSONError('Timestamp constructed from { t, i } must provide i equal or less than uint32 max');
2814
2864
  }
2815
2865
  super(i, t, true);
@@ -2836,7 +2886,7 @@ class Timestamp extends LongWithoutOverridesClass {
2836
2886
  return new Timestamp(Long.fromString(str, true, optRadix));
2837
2887
  }
2838
2888
  toExtendedJSON() {
2839
- return { $timestamp: { t: this.high >>> 0, i: this.low >>> 0 } };
2889
+ return { $timestamp: { t: this.t, i: this.i } };
2840
2890
  }
2841
2891
  static fromExtendedJSON(doc) {
2842
2892
  const i = Long.isLong(doc.$timestamp.i)
@@ -2849,8 +2899,8 @@ class Timestamp extends LongWithoutOverridesClass {
2849
2899
  }
2850
2900
  inspect(depth, options, inspect) {
2851
2901
  inspect ??= defaultInspect;
2852
- const t = inspect(this.high >>> 0, options);
2853
- const i = inspect(this.low >>> 0, options);
2902
+ const t = inspect(this.t, options);
2903
+ const i = inspect(this.i, options);
2854
2904
  return `new Timestamp({ t: ${t}, i: ${i} })`;
2855
2905
  }
2856
2906
  }
@@ -3619,79 +3669,83 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3619
3669
  if (typeof value?.toBSON === 'function') {
3620
3670
  value = value.toBSON();
3621
3671
  }
3622
- if (typeof value === 'string') {
3623
- index = serializeString(buffer, key, value, index);
3624
- }
3625
- else if (typeof value === 'number') {
3626
- index = serializeNumber(buffer, key, value, index);
3627
- }
3628
- else if (typeof value === 'bigint') {
3629
- index = serializeBigInt(buffer, key, value, index);
3630
- }
3631
- else if (typeof value === 'boolean') {
3632
- index = serializeBoolean(buffer, key, value, index);
3633
- }
3634
- else if (value instanceof Date || isDate(value)) {
3635
- index = serializeDate(buffer, key, value, index);
3636
- }
3637
- else if (value === undefined) {
3672
+ const type = typeof value;
3673
+ if (value === undefined) {
3638
3674
  index = serializeNull(buffer, key, value, index);
3639
3675
  }
3640
3676
  else if (value === null) {
3641
3677
  index = serializeNull(buffer, key, value, index);
3642
3678
  }
3643
- else if (isUint8Array(value)) {
3644
- index = serializeBuffer(buffer, key, value, index);
3645
- }
3646
- else if (value instanceof RegExp || isRegExp(value)) {
3647
- index = serializeRegExp(buffer, key, value, index);
3648
- }
3649
- else if (typeof value === 'object' && value._bsontype == null) {
3650
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3679
+ else if (type === 'string') {
3680
+ index = serializeString(buffer, key, value, index);
3651
3681
  }
3652
- else if (typeof value === 'object' &&
3653
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3654
- throw new BSONVersionError();
3682
+ else if (type === 'number') {
3683
+ index = serializeNumber(buffer, key, value, index);
3655
3684
  }
3656
- else if (value._bsontype === 'ObjectId') {
3657
- index = serializeObjectId(buffer, key, value, index);
3685
+ else if (type === 'bigint') {
3686
+ index = serializeBigInt(buffer, key, value, index);
3658
3687
  }
3659
- else if (value._bsontype === 'Decimal128') {
3660
- index = serializeDecimal128(buffer, key, value, index);
3688
+ else if (type === 'boolean') {
3689
+ index = serializeBoolean(buffer, key, value, index);
3661
3690
  }
3662
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3663
- index = serializeLong(buffer, key, value, index);
3691
+ else if (type === 'object' && value._bsontype == null) {
3692
+ if (value instanceof Date || isDate(value)) {
3693
+ index = serializeDate(buffer, key, value, index);
3694
+ }
3695
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3696
+ index = serializeBuffer(buffer, key, value, index);
3697
+ }
3698
+ else if (value instanceof RegExp || isRegExp(value)) {
3699
+ index = serializeRegExp(buffer, key, value, index);
3700
+ }
3701
+ else {
3702
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3703
+ }
3664
3704
  }
3665
- else if (value._bsontype === 'Double') {
3666
- index = serializeDouble(buffer, key, value, index);
3705
+ else if (type === 'object') {
3706
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3707
+ throw new BSONVersionError();
3708
+ }
3709
+ else if (value._bsontype === 'ObjectId') {
3710
+ index = serializeObjectId(buffer, key, value, index);
3711
+ }
3712
+ else if (value._bsontype === 'Decimal128') {
3713
+ index = serializeDecimal128(buffer, key, value, index);
3714
+ }
3715
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3716
+ index = serializeLong(buffer, key, value, index);
3717
+ }
3718
+ else if (value._bsontype === 'Double') {
3719
+ index = serializeDouble(buffer, key, value, index);
3720
+ }
3721
+ else if (value._bsontype === 'Code') {
3722
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3723
+ }
3724
+ else if (value._bsontype === 'Binary') {
3725
+ index = serializeBinary(buffer, key, value, index);
3726
+ }
3727
+ else if (value._bsontype === 'BSONSymbol') {
3728
+ index = serializeSymbol(buffer, key, value, index);
3729
+ }
3730
+ else if (value._bsontype === 'DBRef') {
3731
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3732
+ }
3733
+ else if (value._bsontype === 'BSONRegExp') {
3734
+ index = serializeBSONRegExp(buffer, key, value, index);
3735
+ }
3736
+ else if (value._bsontype === 'Int32') {
3737
+ index = serializeInt32(buffer, key, value, index);
3738
+ }
3739
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3740
+ index = serializeMinMax(buffer, key, value, index);
3741
+ }
3742
+ else if (typeof value._bsontype !== 'undefined') {
3743
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3744
+ }
3667
3745
  }
3668
- else if (typeof value === 'function' && serializeFunctions) {
3746
+ else if (type === 'function' && serializeFunctions) {
3669
3747
  index = serializeFunction(buffer, key, value, index);
3670
3748
  }
3671
- else if (value._bsontype === 'Code') {
3672
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3673
- }
3674
- else if (value._bsontype === 'Binary') {
3675
- index = serializeBinary(buffer, key, value, index);
3676
- }
3677
- else if (value._bsontype === 'BSONSymbol') {
3678
- index = serializeSymbol(buffer, key, value, index);
3679
- }
3680
- else if (value._bsontype === 'DBRef') {
3681
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3682
- }
3683
- else if (value._bsontype === 'BSONRegExp') {
3684
- index = serializeBSONRegExp(buffer, key, value, index);
3685
- }
3686
- else if (value._bsontype === 'Int32') {
3687
- index = serializeInt32(buffer, key, value, index);
3688
- }
3689
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3690
- index = serializeMinMax(buffer, key, value, index);
3691
- }
3692
- else if (typeof value._bsontype !== 'undefined') {
3693
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3694
- }
3695
3749
  }
3696
3750
  }
3697
3751
  else if (object instanceof Map || isMap(object)) {
@@ -3721,7 +3775,14 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3721
3775
  }
3722
3776
  }
3723
3777
  }
3724
- if (type === 'string') {
3778
+ if (value === undefined) {
3779
+ if (ignoreUndefined === false)
3780
+ index = serializeNull(buffer, key, value, index);
3781
+ }
3782
+ else if (value === null) {
3783
+ index = serializeNull(buffer, key, value, index);
3784
+ }
3785
+ else if (type === 'string') {
3725
3786
  index = serializeString(buffer, key, value, index);
3726
3787
  }
3727
3788
  else if (type === 'number') {
@@ -3733,64 +3794,64 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3733
3794
  else if (type === 'boolean') {
3734
3795
  index = serializeBoolean(buffer, key, value, index);
3735
3796
  }
3736
- else if (value instanceof Date || isDate(value)) {
3737
- index = serializeDate(buffer, key, value, index);
3738
- }
3739
- else if (value === null || (value === undefined && ignoreUndefined === false)) {
3740
- index = serializeNull(buffer, key, value, index);
3741
- }
3742
- else if (isUint8Array(value)) {
3743
- index = serializeBuffer(buffer, key, value, index);
3744
- }
3745
- else if (value instanceof RegExp || isRegExp(value)) {
3746
- index = serializeRegExp(buffer, key, value, index);
3747
- }
3748
3797
  else if (type === 'object' && value._bsontype == null) {
3749
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3750
- }
3751
- else if (typeof value === 'object' &&
3752
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3753
- throw new BSONVersionError();
3754
- }
3755
- else if (value._bsontype === 'ObjectId') {
3756
- index = serializeObjectId(buffer, key, value, index);
3757
- }
3758
- else if (type === 'object' && value._bsontype === 'Decimal128') {
3759
- index = serializeDecimal128(buffer, key, value, index);
3760
- }
3761
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3762
- index = serializeLong(buffer, key, value, index);
3763
- }
3764
- else if (value._bsontype === 'Double') {
3765
- index = serializeDouble(buffer, key, value, index);
3798
+ if (value instanceof Date || isDate(value)) {
3799
+ index = serializeDate(buffer, key, value, index);
3800
+ }
3801
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3802
+ index = serializeBuffer(buffer, key, value, index);
3803
+ }
3804
+ else if (value instanceof RegExp || isRegExp(value)) {
3805
+ index = serializeRegExp(buffer, key, value, index);
3806
+ }
3807
+ else {
3808
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3809
+ }
3766
3810
  }
3767
- else if (value._bsontype === 'Code') {
3768
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3811
+ else if (type === 'object') {
3812
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3813
+ throw new BSONVersionError();
3814
+ }
3815
+ else if (value._bsontype === 'ObjectId') {
3816
+ index = serializeObjectId(buffer, key, value, index);
3817
+ }
3818
+ else if (value._bsontype === 'Decimal128') {
3819
+ index = serializeDecimal128(buffer, key, value, index);
3820
+ }
3821
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3822
+ index = serializeLong(buffer, key, value, index);
3823
+ }
3824
+ else if (value._bsontype === 'Double') {
3825
+ index = serializeDouble(buffer, key, value, index);
3826
+ }
3827
+ else if (value._bsontype === 'Code') {
3828
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3829
+ }
3830
+ else if (value._bsontype === 'Binary') {
3831
+ index = serializeBinary(buffer, key, value, index);
3832
+ }
3833
+ else if (value._bsontype === 'BSONSymbol') {
3834
+ index = serializeSymbol(buffer, key, value, index);
3835
+ }
3836
+ else if (value._bsontype === 'DBRef') {
3837
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3838
+ }
3839
+ else if (value._bsontype === 'BSONRegExp') {
3840
+ index = serializeBSONRegExp(buffer, key, value, index);
3841
+ }
3842
+ else if (value._bsontype === 'Int32') {
3843
+ index = serializeInt32(buffer, key, value, index);
3844
+ }
3845
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3846
+ index = serializeMinMax(buffer, key, value, index);
3847
+ }
3848
+ else if (typeof value._bsontype !== 'undefined') {
3849
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3850
+ }
3769
3851
  }
3770
- else if (typeof value === 'function' && serializeFunctions) {
3852
+ else if (type === 'function' && serializeFunctions) {
3771
3853
  index = serializeFunction(buffer, key, value, index);
3772
3854
  }
3773
- else if (value._bsontype === 'Binary') {
3774
- index = serializeBinary(buffer, key, value, index);
3775
- }
3776
- else if (value._bsontype === 'BSONSymbol') {
3777
- index = serializeSymbol(buffer, key, value, index);
3778
- }
3779
- else if (value._bsontype === 'DBRef') {
3780
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3781
- }
3782
- else if (value._bsontype === 'BSONRegExp') {
3783
- index = serializeBSONRegExp(buffer, key, value, index);
3784
- }
3785
- else if (value._bsontype === 'Int32') {
3786
- index = serializeInt32(buffer, key, value, index);
3787
- }
3788
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3789
- index = serializeMinMax(buffer, key, value, index);
3790
- }
3791
- else if (typeof value._bsontype !== 'undefined') {
3792
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3793
- }
3794
3855
  }
3795
3856
  }
3796
3857
  else {
@@ -3819,7 +3880,14 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3819
3880
  }
3820
3881
  }
3821
3882
  }
3822
- if (type === 'string') {
3883
+ if (value === undefined) {
3884
+ if (ignoreUndefined === false)
3885
+ index = serializeNull(buffer, key, value, index);
3886
+ }
3887
+ else if (value === null) {
3888
+ index = serializeNull(buffer, key, value, index);
3889
+ }
3890
+ else if (type === 'string') {
3823
3891
  index = serializeString(buffer, key, value, index);
3824
3892
  }
3825
3893
  else if (type === 'number') {
@@ -3831,68 +3899,64 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3831
3899
  else if (type === 'boolean') {
3832
3900
  index = serializeBoolean(buffer, key, value, index);
3833
3901
  }
3834
- else if (value instanceof Date || isDate(value)) {
3835
- index = serializeDate(buffer, key, value, index);
3836
- }
3837
- else if (value === undefined) {
3838
- if (ignoreUndefined === false)
3839
- index = serializeNull(buffer, key, value, index);
3840
- }
3841
- else if (value === null) {
3842
- index = serializeNull(buffer, key, value, index);
3843
- }
3844
- else if (isUint8Array(value)) {
3845
- index = serializeBuffer(buffer, key, value, index);
3846
- }
3847
- else if (value instanceof RegExp || isRegExp(value)) {
3848
- index = serializeRegExp(buffer, key, value, index);
3849
- }
3850
3902
  else if (type === 'object' && value._bsontype == null) {
3851
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3852
- }
3853
- else if (typeof value === 'object' &&
3854
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3855
- throw new BSONVersionError();
3856
- }
3857
- else if (value._bsontype === 'ObjectId') {
3858
- index = serializeObjectId(buffer, key, value, index);
3859
- }
3860
- else if (type === 'object' && value._bsontype === 'Decimal128') {
3861
- index = serializeDecimal128(buffer, key, value, index);
3862
- }
3863
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3864
- index = serializeLong(buffer, key, value, index);
3865
- }
3866
- else if (value._bsontype === 'Double') {
3867
- index = serializeDouble(buffer, key, value, index);
3903
+ if (value instanceof Date || isDate(value)) {
3904
+ index = serializeDate(buffer, key, value, index);
3905
+ }
3906
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3907
+ index = serializeBuffer(buffer, key, value, index);
3908
+ }
3909
+ else if (value instanceof RegExp || isRegExp(value)) {
3910
+ index = serializeRegExp(buffer, key, value, index);
3911
+ }
3912
+ else {
3913
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3914
+ }
3868
3915
  }
3869
- else if (value._bsontype === 'Code') {
3870
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3916
+ else if (type === 'object') {
3917
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3918
+ throw new BSONVersionError();
3919
+ }
3920
+ else if (value._bsontype === 'ObjectId') {
3921
+ index = serializeObjectId(buffer, key, value, index);
3922
+ }
3923
+ else if (value._bsontype === 'Decimal128') {
3924
+ index = serializeDecimal128(buffer, key, value, index);
3925
+ }
3926
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3927
+ index = serializeLong(buffer, key, value, index);
3928
+ }
3929
+ else if (value._bsontype === 'Double') {
3930
+ index = serializeDouble(buffer, key, value, index);
3931
+ }
3932
+ else if (value._bsontype === 'Code') {
3933
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3934
+ }
3935
+ else if (value._bsontype === 'Binary') {
3936
+ index = serializeBinary(buffer, key, value, index);
3937
+ }
3938
+ else if (value._bsontype === 'BSONSymbol') {
3939
+ index = serializeSymbol(buffer, key, value, index);
3940
+ }
3941
+ else if (value._bsontype === 'DBRef') {
3942
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3943
+ }
3944
+ else if (value._bsontype === 'BSONRegExp') {
3945
+ index = serializeBSONRegExp(buffer, key, value, index);
3946
+ }
3947
+ else if (value._bsontype === 'Int32') {
3948
+ index = serializeInt32(buffer, key, value, index);
3949
+ }
3950
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3951
+ index = serializeMinMax(buffer, key, value, index);
3952
+ }
3953
+ else if (typeof value._bsontype !== 'undefined') {
3954
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3955
+ }
3871
3956
  }
3872
- else if (typeof value === 'function' && serializeFunctions) {
3957
+ else if (type === 'function' && serializeFunctions) {
3873
3958
  index = serializeFunction(buffer, key, value, index);
3874
3959
  }
3875
- else if (value._bsontype === 'Binary') {
3876
- index = serializeBinary(buffer, key, value, index);
3877
- }
3878
- else if (value._bsontype === 'BSONSymbol') {
3879
- index = serializeSymbol(buffer, key, value, index);
3880
- }
3881
- else if (value._bsontype === 'DBRef') {
3882
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3883
- }
3884
- else if (value._bsontype === 'BSONRegExp') {
3885
- index = serializeBSONRegExp(buffer, key, value, index);
3886
- }
3887
- else if (value._bsontype === 'Int32') {
3888
- index = serializeInt32(buffer, key, value, index);
3889
- }
3890
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3891
- index = serializeMinMax(buffer, key, value, index);
3892
- }
3893
- else if (typeof value._bsontype !== 'undefined') {
3894
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3895
- }
3896
3960
  }
3897
3961
  }
3898
3962
  path.delete(object);
@@ -4144,7 +4208,7 @@ function serializeDocument(doc, options) {
4144
4208
  else if (doc != null &&
4145
4209
  typeof doc === 'object' &&
4146
4210
  typeof doc._bsontype === 'string' &&
4147
- doc[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
4211
+ doc[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
4148
4212
  throw new BSONVersionError();
4149
4213
  }
4150
4214
  else if (isBSONType(doc)) {