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.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
  }
@@ -2302,7 +2331,7 @@ const NumberUtils = {
2302
2331
  return 4;
2303
2332
  },
2304
2333
  setBigInt64LE(destination, offset, value) {
2305
- const mask32bits = BigInt(4294967295);
2334
+ const mask32bits = BigInt(0xffff_ffff);
2306
2335
  let lo = Number(value & mask32bits);
2307
2336
  destination[offset] = lo;
2308
2337
  lo >>= 8;
@@ -2348,7 +2377,6 @@ const NumberUtils = {
2348
2377
  }
2349
2378
  };
2350
2379
 
2351
- const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
2352
2380
  let PROCESS_UNIQUE = null;
2353
2381
  class ObjectId extends BSONValue {
2354
2382
  get _bsontype() {
@@ -2378,7 +2406,7 @@ class ObjectId extends BSONValue {
2378
2406
  this.buffer = ByteUtils.toLocalBufferType(workingId);
2379
2407
  }
2380
2408
  else if (typeof workingId === 'string') {
2381
- if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
2409
+ if (ObjectId.validateHexString(workingId)) {
2382
2410
  this.buffer = ByteUtils.fromHex(workingId);
2383
2411
  }
2384
2412
  else {
@@ -2401,6 +2429,20 @@ class ObjectId extends BSONValue {
2401
2429
  this.__id = ByteUtils.toHex(value);
2402
2430
  }
2403
2431
  }
2432
+ static validateHexString(string) {
2433
+ if (string?.length !== 24)
2434
+ return false;
2435
+ for (let i = 0; i < 24; i++) {
2436
+ const char = string.charCodeAt(i);
2437
+ if ((char >= 48 && char <= 57) ||
2438
+ (char >= 97 && char <= 102) ||
2439
+ (char >= 65 && char <= 70)) {
2440
+ continue;
2441
+ }
2442
+ return false;
2443
+ }
2444
+ return true;
2445
+ }
2404
2446
  toHexString() {
2405
2447
  if (ObjectId.cacheHexString && this.__id) {
2406
2448
  return this.__id;
@@ -2513,6 +2555,8 @@ class ObjectId extends BSONValue {
2513
2555
  static isValid(id) {
2514
2556
  if (id == null)
2515
2557
  return false;
2558
+ if (typeof id === 'string')
2559
+ return ObjectId.validateHexString(id);
2516
2560
  try {
2517
2561
  new ObjectId(id);
2518
2562
  return true;
@@ -2583,7 +2627,7 @@ function calculateElement(name, value, serializeFunctions = false, isArray = fal
2583
2627
  case 'object':
2584
2628
  if (value != null &&
2585
2629
  typeof value._bsontype === 'string' &&
2586
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
2630
+ value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
2587
2631
  throw new BSONVersionError();
2588
2632
  }
2589
2633
  else if (value == null || value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
@@ -2787,6 +2831,12 @@ class Timestamp extends LongWithoutOverridesClass {
2787
2831
  get _bsontype() {
2788
2832
  return 'Timestamp';
2789
2833
  }
2834
+ get i() {
2835
+ return this.low >>> 0;
2836
+ }
2837
+ get t() {
2838
+ return this.high >>> 0;
2839
+ }
2790
2840
  constructor(low) {
2791
2841
  if (low == null) {
2792
2842
  super(0, 0, true);
@@ -2812,10 +2862,10 @@ class Timestamp extends LongWithoutOverridesClass {
2812
2862
  if (i < 0 || Number.isNaN(i)) {
2813
2863
  throw new BSONError('Timestamp constructed from { t, i } must provide a positive i');
2814
2864
  }
2815
- if (t > 4294967295) {
2865
+ if (t > 0xffff_ffff) {
2816
2866
  throw new BSONError('Timestamp constructed from { t, i } must provide t equal or less than uint32 max');
2817
2867
  }
2818
- if (i > 4294967295) {
2868
+ if (i > 0xffff_ffff) {
2819
2869
  throw new BSONError('Timestamp constructed from { t, i } must provide i equal or less than uint32 max');
2820
2870
  }
2821
2871
  super(i, t, true);
@@ -2842,7 +2892,7 @@ class Timestamp extends LongWithoutOverridesClass {
2842
2892
  return new Timestamp(Long.fromString(str, true, optRadix));
2843
2893
  }
2844
2894
  toExtendedJSON() {
2845
- return { $timestamp: { t: this.high >>> 0, i: this.low >>> 0 } };
2895
+ return { $timestamp: { t: this.t, i: this.i } };
2846
2896
  }
2847
2897
  static fromExtendedJSON(doc) {
2848
2898
  const i = Long.isLong(doc.$timestamp.i)
@@ -2855,8 +2905,8 @@ class Timestamp extends LongWithoutOverridesClass {
2855
2905
  }
2856
2906
  inspect(depth, options, inspect) {
2857
2907
  inspect ??= defaultInspect;
2858
- const t = inspect(this.high >>> 0, options);
2859
- const i = inspect(this.low >>> 0, options);
2908
+ const t = inspect(this.t, options);
2909
+ const i = inspect(this.i, options);
2860
2910
  return `new Timestamp({ t: ${t}, i: ${i} })`;
2861
2911
  }
2862
2912
  }
@@ -3625,79 +3675,83 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3625
3675
  if (typeof value?.toBSON === 'function') {
3626
3676
  value = value.toBSON();
3627
3677
  }
3628
- if (typeof value === 'string') {
3629
- index = serializeString(buffer, key, value, index);
3630
- }
3631
- else if (typeof value === 'number') {
3632
- index = serializeNumber(buffer, key, value, index);
3633
- }
3634
- else if (typeof value === 'bigint') {
3635
- index = serializeBigInt(buffer, key, value, index);
3636
- }
3637
- else if (typeof value === 'boolean') {
3638
- index = serializeBoolean(buffer, key, value, index);
3639
- }
3640
- else if (value instanceof Date || isDate(value)) {
3641
- index = serializeDate(buffer, key, value, index);
3642
- }
3643
- else if (value === undefined) {
3678
+ const type = typeof value;
3679
+ if (value === undefined) {
3644
3680
  index = serializeNull(buffer, key, value, index);
3645
3681
  }
3646
3682
  else if (value === null) {
3647
3683
  index = serializeNull(buffer, key, value, index);
3648
3684
  }
3649
- else if (isUint8Array(value)) {
3650
- index = serializeBuffer(buffer, key, value, index);
3651
- }
3652
- else if (value instanceof RegExp || isRegExp(value)) {
3653
- index = serializeRegExp(buffer, key, value, index);
3654
- }
3655
- else if (typeof value === 'object' && value._bsontype == null) {
3656
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3685
+ else if (type === 'string') {
3686
+ index = serializeString(buffer, key, value, index);
3657
3687
  }
3658
- else if (typeof value === 'object' &&
3659
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3660
- throw new BSONVersionError();
3688
+ else if (type === 'number') {
3689
+ index = serializeNumber(buffer, key, value, index);
3661
3690
  }
3662
- else if (value._bsontype === 'ObjectId') {
3663
- index = serializeObjectId(buffer, key, value, index);
3691
+ else if (type === 'bigint') {
3692
+ index = serializeBigInt(buffer, key, value, index);
3664
3693
  }
3665
- else if (value._bsontype === 'Decimal128') {
3666
- index = serializeDecimal128(buffer, key, value, index);
3694
+ else if (type === 'boolean') {
3695
+ index = serializeBoolean(buffer, key, value, index);
3667
3696
  }
3668
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3669
- index = serializeLong(buffer, key, value, index);
3697
+ else if (type === 'object' && value._bsontype == null) {
3698
+ if (value instanceof Date || isDate(value)) {
3699
+ index = serializeDate(buffer, key, value, index);
3700
+ }
3701
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3702
+ index = serializeBuffer(buffer, key, value, index);
3703
+ }
3704
+ else if (value instanceof RegExp || isRegExp(value)) {
3705
+ index = serializeRegExp(buffer, key, value, index);
3706
+ }
3707
+ else {
3708
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3709
+ }
3670
3710
  }
3671
- else if (value._bsontype === 'Double') {
3672
- index = serializeDouble(buffer, key, value, index);
3711
+ else if (type === 'object') {
3712
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3713
+ throw new BSONVersionError();
3714
+ }
3715
+ else if (value._bsontype === 'ObjectId') {
3716
+ index = serializeObjectId(buffer, key, value, index);
3717
+ }
3718
+ else if (value._bsontype === 'Decimal128') {
3719
+ index = serializeDecimal128(buffer, key, value, index);
3720
+ }
3721
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3722
+ index = serializeLong(buffer, key, value, index);
3723
+ }
3724
+ else if (value._bsontype === 'Double') {
3725
+ index = serializeDouble(buffer, key, value, index);
3726
+ }
3727
+ else if (value._bsontype === 'Code') {
3728
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3729
+ }
3730
+ else if (value._bsontype === 'Binary') {
3731
+ index = serializeBinary(buffer, key, value, index);
3732
+ }
3733
+ else if (value._bsontype === 'BSONSymbol') {
3734
+ index = serializeSymbol(buffer, key, value, index);
3735
+ }
3736
+ else if (value._bsontype === 'DBRef') {
3737
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3738
+ }
3739
+ else if (value._bsontype === 'BSONRegExp') {
3740
+ index = serializeBSONRegExp(buffer, key, value, index);
3741
+ }
3742
+ else if (value._bsontype === 'Int32') {
3743
+ index = serializeInt32(buffer, key, value, index);
3744
+ }
3745
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3746
+ index = serializeMinMax(buffer, key, value, index);
3747
+ }
3748
+ else if (typeof value._bsontype !== 'undefined') {
3749
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3750
+ }
3673
3751
  }
3674
- else if (typeof value === 'function' && serializeFunctions) {
3752
+ else if (type === 'function' && serializeFunctions) {
3675
3753
  index = serializeFunction(buffer, key, value, index);
3676
3754
  }
3677
- else if (value._bsontype === 'Code') {
3678
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3679
- }
3680
- else if (value._bsontype === 'Binary') {
3681
- index = serializeBinary(buffer, key, value, index);
3682
- }
3683
- else if (value._bsontype === 'BSONSymbol') {
3684
- index = serializeSymbol(buffer, key, value, index);
3685
- }
3686
- else if (value._bsontype === 'DBRef') {
3687
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3688
- }
3689
- else if (value._bsontype === 'BSONRegExp') {
3690
- index = serializeBSONRegExp(buffer, key, value, index);
3691
- }
3692
- else if (value._bsontype === 'Int32') {
3693
- index = serializeInt32(buffer, key, value, index);
3694
- }
3695
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3696
- index = serializeMinMax(buffer, key, value, index);
3697
- }
3698
- else if (typeof value._bsontype !== 'undefined') {
3699
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3700
- }
3701
3755
  }
3702
3756
  }
3703
3757
  else if (object instanceof Map || isMap(object)) {
@@ -3727,7 +3781,14 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3727
3781
  }
3728
3782
  }
3729
3783
  }
3730
- if (type === 'string') {
3784
+ if (value === undefined) {
3785
+ if (ignoreUndefined === false)
3786
+ index = serializeNull(buffer, key, value, index);
3787
+ }
3788
+ else if (value === null) {
3789
+ index = serializeNull(buffer, key, value, index);
3790
+ }
3791
+ else if (type === 'string') {
3731
3792
  index = serializeString(buffer, key, value, index);
3732
3793
  }
3733
3794
  else if (type === 'number') {
@@ -3739,64 +3800,64 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3739
3800
  else if (type === 'boolean') {
3740
3801
  index = serializeBoolean(buffer, key, value, index);
3741
3802
  }
3742
- else if (value instanceof Date || isDate(value)) {
3743
- index = serializeDate(buffer, key, value, index);
3744
- }
3745
- else if (value === null || (value === undefined && ignoreUndefined === false)) {
3746
- index = serializeNull(buffer, key, value, index);
3747
- }
3748
- else if (isUint8Array(value)) {
3749
- index = serializeBuffer(buffer, key, value, index);
3750
- }
3751
- else if (value instanceof RegExp || isRegExp(value)) {
3752
- index = serializeRegExp(buffer, key, value, index);
3753
- }
3754
3803
  else if (type === 'object' && value._bsontype == null) {
3755
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3756
- }
3757
- else if (typeof value === 'object' &&
3758
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3759
- throw new BSONVersionError();
3760
- }
3761
- else if (value._bsontype === 'ObjectId') {
3762
- index = serializeObjectId(buffer, key, value, index);
3763
- }
3764
- else if (type === 'object' && value._bsontype === 'Decimal128') {
3765
- index = serializeDecimal128(buffer, key, value, index);
3766
- }
3767
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3768
- index = serializeLong(buffer, key, value, index);
3769
- }
3770
- else if (value._bsontype === 'Double') {
3771
- index = serializeDouble(buffer, key, value, index);
3804
+ if (value instanceof Date || isDate(value)) {
3805
+ index = serializeDate(buffer, key, value, index);
3806
+ }
3807
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3808
+ index = serializeBuffer(buffer, key, value, index);
3809
+ }
3810
+ else if (value instanceof RegExp || isRegExp(value)) {
3811
+ index = serializeRegExp(buffer, key, value, index);
3812
+ }
3813
+ else {
3814
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3815
+ }
3772
3816
  }
3773
- else if (value._bsontype === 'Code') {
3774
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3817
+ else if (type === 'object') {
3818
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3819
+ throw new BSONVersionError();
3820
+ }
3821
+ else if (value._bsontype === 'ObjectId') {
3822
+ index = serializeObjectId(buffer, key, value, index);
3823
+ }
3824
+ else if (value._bsontype === 'Decimal128') {
3825
+ index = serializeDecimal128(buffer, key, value, index);
3826
+ }
3827
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3828
+ index = serializeLong(buffer, key, value, index);
3829
+ }
3830
+ else if (value._bsontype === 'Double') {
3831
+ index = serializeDouble(buffer, key, value, index);
3832
+ }
3833
+ else if (value._bsontype === 'Code') {
3834
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3835
+ }
3836
+ else if (value._bsontype === 'Binary') {
3837
+ index = serializeBinary(buffer, key, value, index);
3838
+ }
3839
+ else if (value._bsontype === 'BSONSymbol') {
3840
+ index = serializeSymbol(buffer, key, value, index);
3841
+ }
3842
+ else if (value._bsontype === 'DBRef') {
3843
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3844
+ }
3845
+ else if (value._bsontype === 'BSONRegExp') {
3846
+ index = serializeBSONRegExp(buffer, key, value, index);
3847
+ }
3848
+ else if (value._bsontype === 'Int32') {
3849
+ index = serializeInt32(buffer, key, value, index);
3850
+ }
3851
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3852
+ index = serializeMinMax(buffer, key, value, index);
3853
+ }
3854
+ else if (typeof value._bsontype !== 'undefined') {
3855
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3856
+ }
3775
3857
  }
3776
- else if (typeof value === 'function' && serializeFunctions) {
3858
+ else if (type === 'function' && serializeFunctions) {
3777
3859
  index = serializeFunction(buffer, key, value, index);
3778
3860
  }
3779
- else if (value._bsontype === 'Binary') {
3780
- index = serializeBinary(buffer, key, value, index);
3781
- }
3782
- else if (value._bsontype === 'BSONSymbol') {
3783
- index = serializeSymbol(buffer, key, value, index);
3784
- }
3785
- else if (value._bsontype === 'DBRef') {
3786
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3787
- }
3788
- else if (value._bsontype === 'BSONRegExp') {
3789
- index = serializeBSONRegExp(buffer, key, value, index);
3790
- }
3791
- else if (value._bsontype === 'Int32') {
3792
- index = serializeInt32(buffer, key, value, index);
3793
- }
3794
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3795
- index = serializeMinMax(buffer, key, value, index);
3796
- }
3797
- else if (typeof value._bsontype !== 'undefined') {
3798
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3799
- }
3800
3861
  }
3801
3862
  }
3802
3863
  else {
@@ -3825,7 +3886,14 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3825
3886
  }
3826
3887
  }
3827
3888
  }
3828
- if (type === 'string') {
3889
+ if (value === undefined) {
3890
+ if (ignoreUndefined === false)
3891
+ index = serializeNull(buffer, key, value, index);
3892
+ }
3893
+ else if (value === null) {
3894
+ index = serializeNull(buffer, key, value, index);
3895
+ }
3896
+ else if (type === 'string') {
3829
3897
  index = serializeString(buffer, key, value, index);
3830
3898
  }
3831
3899
  else if (type === 'number') {
@@ -3837,68 +3905,64 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3837
3905
  else if (type === 'boolean') {
3838
3906
  index = serializeBoolean(buffer, key, value, index);
3839
3907
  }
3840
- else if (value instanceof Date || isDate(value)) {
3841
- index = serializeDate(buffer, key, value, index);
3842
- }
3843
- else if (value === undefined) {
3844
- if (ignoreUndefined === false)
3845
- index = serializeNull(buffer, key, value, index);
3846
- }
3847
- else if (value === null) {
3848
- index = serializeNull(buffer, key, value, index);
3849
- }
3850
- else if (isUint8Array(value)) {
3851
- index = serializeBuffer(buffer, key, value, index);
3852
- }
3853
- else if (value instanceof RegExp || isRegExp(value)) {
3854
- index = serializeRegExp(buffer, key, value, index);
3855
- }
3856
3908
  else if (type === 'object' && value._bsontype == null) {
3857
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3858
- }
3859
- else if (typeof value === 'object' &&
3860
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3861
- throw new BSONVersionError();
3862
- }
3863
- else if (value._bsontype === 'ObjectId') {
3864
- index = serializeObjectId(buffer, key, value, index);
3865
- }
3866
- else if (type === 'object' && value._bsontype === 'Decimal128') {
3867
- index = serializeDecimal128(buffer, key, value, index);
3868
- }
3869
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3870
- index = serializeLong(buffer, key, value, index);
3871
- }
3872
- else if (value._bsontype === 'Double') {
3873
- index = serializeDouble(buffer, key, value, index);
3909
+ if (value instanceof Date || isDate(value)) {
3910
+ index = serializeDate(buffer, key, value, index);
3911
+ }
3912
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3913
+ index = serializeBuffer(buffer, key, value, index);
3914
+ }
3915
+ else if (value instanceof RegExp || isRegExp(value)) {
3916
+ index = serializeRegExp(buffer, key, value, index);
3917
+ }
3918
+ else {
3919
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3920
+ }
3874
3921
  }
3875
- else if (value._bsontype === 'Code') {
3876
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3922
+ else if (type === 'object') {
3923
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3924
+ throw new BSONVersionError();
3925
+ }
3926
+ else if (value._bsontype === 'ObjectId') {
3927
+ index = serializeObjectId(buffer, key, value, index);
3928
+ }
3929
+ else if (value._bsontype === 'Decimal128') {
3930
+ index = serializeDecimal128(buffer, key, value, index);
3931
+ }
3932
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3933
+ index = serializeLong(buffer, key, value, index);
3934
+ }
3935
+ else if (value._bsontype === 'Double') {
3936
+ index = serializeDouble(buffer, key, value, index);
3937
+ }
3938
+ else if (value._bsontype === 'Code') {
3939
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3940
+ }
3941
+ else if (value._bsontype === 'Binary') {
3942
+ index = serializeBinary(buffer, key, value, index);
3943
+ }
3944
+ else if (value._bsontype === 'BSONSymbol') {
3945
+ index = serializeSymbol(buffer, key, value, index);
3946
+ }
3947
+ else if (value._bsontype === 'DBRef') {
3948
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3949
+ }
3950
+ else if (value._bsontype === 'BSONRegExp') {
3951
+ index = serializeBSONRegExp(buffer, key, value, index);
3952
+ }
3953
+ else if (value._bsontype === 'Int32') {
3954
+ index = serializeInt32(buffer, key, value, index);
3955
+ }
3956
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3957
+ index = serializeMinMax(buffer, key, value, index);
3958
+ }
3959
+ else if (typeof value._bsontype !== 'undefined') {
3960
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3961
+ }
3877
3962
  }
3878
- else if (typeof value === 'function' && serializeFunctions) {
3963
+ else if (type === 'function' && serializeFunctions) {
3879
3964
  index = serializeFunction(buffer, key, value, index);
3880
3965
  }
3881
- else if (value._bsontype === 'Binary') {
3882
- index = serializeBinary(buffer, key, value, index);
3883
- }
3884
- else if (value._bsontype === 'BSONSymbol') {
3885
- index = serializeSymbol(buffer, key, value, index);
3886
- }
3887
- else if (value._bsontype === 'DBRef') {
3888
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3889
- }
3890
- else if (value._bsontype === 'BSONRegExp') {
3891
- index = serializeBSONRegExp(buffer, key, value, index);
3892
- }
3893
- else if (value._bsontype === 'Int32') {
3894
- index = serializeInt32(buffer, key, value, index);
3895
- }
3896
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3897
- index = serializeMinMax(buffer, key, value, index);
3898
- }
3899
- else if (typeof value._bsontype !== 'undefined') {
3900
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3901
- }
3902
3966
  }
3903
3967
  }
3904
3968
  path.delete(object);
@@ -4150,7 +4214,7 @@ function serializeDocument(doc, options) {
4150
4214
  else if (doc != null &&
4151
4215
  typeof doc === 'object' &&
4152
4216
  typeof doc._bsontype === 'string' &&
4153
- doc[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
4217
+ doc[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
4154
4218
  throw new BSONVersionError();
4155
4219
  }
4156
4220
  else if (isBSONType(doc)) {