bson 6.7.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) {
@@ -872,19 +901,18 @@ class Long extends BSONValue {
872
901
  get __isLong__() {
873
902
  return true;
874
903
  }
875
- constructor(low = 0, high, unsigned) {
904
+ constructor(lowOrValue = 0, highOrUnsigned, unsigned) {
876
905
  super();
877
- if (typeof low === 'bigint') {
878
- Object.assign(this, Long.fromBigInt(low, !!high));
879
- }
880
- else if (typeof low === 'string') {
881
- Object.assign(this, Long.fromString(low, !!high));
882
- }
883
- else {
884
- this.low = low | 0;
885
- this.high = high | 0;
886
- this.unsigned = !!unsigned;
887
- }
906
+ const unsignedBool = typeof highOrUnsigned === 'boolean' ? highOrUnsigned : Boolean(unsigned);
907
+ const high = typeof highOrUnsigned === 'number' ? highOrUnsigned : 0;
908
+ const res = typeof lowOrValue === 'string'
909
+ ? Long.fromString(lowOrValue, unsignedBool)
910
+ : typeof lowOrValue === 'bigint'
911
+ ? Long.fromBigInt(lowOrValue, unsignedBool)
912
+ : { low: lowOrValue | 0, high: high | 0, unsigned: unsignedBool };
913
+ this.low = res.low;
914
+ this.high = res.high;
915
+ this.unsigned = res.unsigned;
888
916
  }
889
917
  static fromBits(lowBits, highBits, unsigned) {
890
918
  return new Long(lowBits, highBits, unsigned);
@@ -936,7 +964,9 @@ class Long extends BSONValue {
936
964
  return Long.fromBits(value % TWO_PWR_32_DBL | 0, (value / TWO_PWR_32_DBL) | 0, unsigned);
937
965
  }
938
966
  static fromBigInt(value, unsigned) {
939
- return Long.fromString(value.toString(), unsigned);
967
+ const FROM_BIGINT_BIT_MASK = BigInt(0xffffffff);
968
+ const FROM_BIGINT_BIT_SHIFT = BigInt(32);
969
+ return new Long(Number(value & FROM_BIGINT_BIT_MASK), Number((value >> FROM_BIGINT_BIT_SHIFT) & FROM_BIGINT_BIT_MASK), unsigned);
940
970
  }
941
971
  static _fromString(str, unsigned, radix) {
942
972
  if (str.length === 0)
@@ -1604,7 +1634,7 @@ class Decimal128 extends BSONValue {
1604
1634
  if (typeof bytes === 'string') {
1605
1635
  this.bytes = Decimal128.fromString(bytes).bytes;
1606
1636
  }
1607
- else if (isUint8Array(bytes)) {
1637
+ else if (bytes instanceof Uint8Array || isUint8Array(bytes)) {
1608
1638
  if (bytes.byteLength !== 16) {
1609
1639
  throw new BSONError('Decimal128 must take a Buffer of 16 bytes');
1610
1640
  }
@@ -2295,7 +2325,7 @@ const NumberUtils = {
2295
2325
  return 4;
2296
2326
  },
2297
2327
  setBigInt64LE(destination, offset, value) {
2298
- const mask32bits = BigInt(4294967295);
2328
+ const mask32bits = BigInt(0xffff_ffff);
2299
2329
  let lo = Number(value & mask32bits);
2300
2330
  destination[offset] = lo;
2301
2331
  lo >>= 8;
@@ -2341,7 +2371,6 @@ const NumberUtils = {
2341
2371
  }
2342
2372
  };
2343
2373
 
2344
- const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
2345
2374
  let PROCESS_UNIQUE = null;
2346
2375
  class ObjectId extends BSONValue {
2347
2376
  get _bsontype() {
@@ -2371,7 +2400,7 @@ class ObjectId extends BSONValue {
2371
2400
  this.buffer = ByteUtils.toLocalBufferType(workingId);
2372
2401
  }
2373
2402
  else if (typeof workingId === 'string') {
2374
- if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
2403
+ if (ObjectId.validateHexString(workingId)) {
2375
2404
  this.buffer = ByteUtils.fromHex(workingId);
2376
2405
  }
2377
2406
  else {
@@ -2394,6 +2423,20 @@ class ObjectId extends BSONValue {
2394
2423
  this.__id = ByteUtils.toHex(value);
2395
2424
  }
2396
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
+ }
2397
2440
  toHexString() {
2398
2441
  if (ObjectId.cacheHexString && this.__id) {
2399
2442
  return this.__id;
@@ -2506,6 +2549,8 @@ class ObjectId extends BSONValue {
2506
2549
  static isValid(id) {
2507
2550
  if (id == null)
2508
2551
  return false;
2552
+ if (typeof id === 'string')
2553
+ return ObjectId.validateHexString(id);
2509
2554
  try {
2510
2555
  new ObjectId(id);
2511
2556
  return true;
@@ -2576,7 +2621,7 @@ function calculateElement(name, value, serializeFunctions = false, isArray = fal
2576
2621
  case 'object':
2577
2622
  if (value != null &&
2578
2623
  typeof value._bsontype === 'string' &&
2579
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
2624
+ value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
2580
2625
  throw new BSONVersionError();
2581
2626
  }
2582
2627
  else if (value == null || value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
@@ -2780,6 +2825,12 @@ class Timestamp extends LongWithoutOverridesClass {
2780
2825
  get _bsontype() {
2781
2826
  return 'Timestamp';
2782
2827
  }
2828
+ get i() {
2829
+ return this.low >>> 0;
2830
+ }
2831
+ get t() {
2832
+ return this.high >>> 0;
2833
+ }
2783
2834
  constructor(low) {
2784
2835
  if (low == null) {
2785
2836
  super(0, 0, true);
@@ -2805,10 +2856,10 @@ class Timestamp extends LongWithoutOverridesClass {
2805
2856
  if (i < 0 || Number.isNaN(i)) {
2806
2857
  throw new BSONError('Timestamp constructed from { t, i } must provide a positive i');
2807
2858
  }
2808
- if (t > 4294967295) {
2859
+ if (t > 0xffff_ffff) {
2809
2860
  throw new BSONError('Timestamp constructed from { t, i } must provide t equal or less than uint32 max');
2810
2861
  }
2811
- if (i > 4294967295) {
2862
+ if (i > 0xffff_ffff) {
2812
2863
  throw new BSONError('Timestamp constructed from { t, i } must provide i equal or less than uint32 max');
2813
2864
  }
2814
2865
  super(i, t, true);
@@ -2835,7 +2886,7 @@ class Timestamp extends LongWithoutOverridesClass {
2835
2886
  return new Timestamp(Long.fromString(str, true, optRadix));
2836
2887
  }
2837
2888
  toExtendedJSON() {
2838
- return { $timestamp: { t: this.high >>> 0, i: this.low >>> 0 } };
2889
+ return { $timestamp: { t: this.t, i: this.i } };
2839
2890
  }
2840
2891
  static fromExtendedJSON(doc) {
2841
2892
  const i = Long.isLong(doc.$timestamp.i)
@@ -2848,8 +2899,8 @@ class Timestamp extends LongWithoutOverridesClass {
2848
2899
  }
2849
2900
  inspect(depth, options, inspect) {
2850
2901
  inspect ??= defaultInspect;
2851
- const t = inspect(this.high >>> 0, options);
2852
- const i = inspect(this.low >>> 0, options);
2902
+ const t = inspect(this.t, options);
2903
+ const i = inspect(this.i, options);
2853
2904
  return `new Timestamp({ t: ${t}, i: ${i} })`;
2854
2905
  }
2855
2906
  }
@@ -3618,79 +3669,83 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3618
3669
  if (typeof value?.toBSON === 'function') {
3619
3670
  value = value.toBSON();
3620
3671
  }
3621
- if (typeof value === 'string') {
3622
- index = serializeString(buffer, key, value, index);
3623
- }
3624
- else if (typeof value === 'number') {
3625
- index = serializeNumber(buffer, key, value, index);
3626
- }
3627
- else if (typeof value === 'bigint') {
3628
- index = serializeBigInt(buffer, key, value, index);
3629
- }
3630
- else if (typeof value === 'boolean') {
3631
- index = serializeBoolean(buffer, key, value, index);
3632
- }
3633
- else if (value instanceof Date || isDate(value)) {
3634
- index = serializeDate(buffer, key, value, index);
3635
- }
3636
- else if (value === undefined) {
3672
+ const type = typeof value;
3673
+ if (value === undefined) {
3637
3674
  index = serializeNull(buffer, key, value, index);
3638
3675
  }
3639
3676
  else if (value === null) {
3640
3677
  index = serializeNull(buffer, key, value, index);
3641
3678
  }
3642
- else if (isUint8Array(value)) {
3643
- index = serializeBuffer(buffer, key, value, index);
3644
- }
3645
- else if (value instanceof RegExp || isRegExp(value)) {
3646
- index = serializeRegExp(buffer, key, value, index);
3647
- }
3648
- else if (typeof value === 'object' && value._bsontype == null) {
3649
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3679
+ else if (type === 'string') {
3680
+ index = serializeString(buffer, key, value, index);
3650
3681
  }
3651
- else if (typeof value === 'object' &&
3652
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3653
- throw new BSONVersionError();
3682
+ else if (type === 'number') {
3683
+ index = serializeNumber(buffer, key, value, index);
3654
3684
  }
3655
- else if (value._bsontype === 'ObjectId') {
3656
- index = serializeObjectId(buffer, key, value, index);
3685
+ else if (type === 'bigint') {
3686
+ index = serializeBigInt(buffer, key, value, index);
3657
3687
  }
3658
- else if (value._bsontype === 'Decimal128') {
3659
- index = serializeDecimal128(buffer, key, value, index);
3688
+ else if (type === 'boolean') {
3689
+ index = serializeBoolean(buffer, key, value, index);
3660
3690
  }
3661
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3662
- 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
+ }
3663
3704
  }
3664
- else if (value._bsontype === 'Double') {
3665
- 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
+ }
3666
3745
  }
3667
- else if (typeof value === 'function' && serializeFunctions) {
3746
+ else if (type === 'function' && serializeFunctions) {
3668
3747
  index = serializeFunction(buffer, key, value, index);
3669
3748
  }
3670
- else if (value._bsontype === 'Code') {
3671
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3672
- }
3673
- else if (value._bsontype === 'Binary') {
3674
- index = serializeBinary(buffer, key, value, index);
3675
- }
3676
- else if (value._bsontype === 'BSONSymbol') {
3677
- index = serializeSymbol(buffer, key, value, index);
3678
- }
3679
- else if (value._bsontype === 'DBRef') {
3680
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3681
- }
3682
- else if (value._bsontype === 'BSONRegExp') {
3683
- index = serializeBSONRegExp(buffer, key, value, index);
3684
- }
3685
- else if (value._bsontype === 'Int32') {
3686
- index = serializeInt32(buffer, key, value, index);
3687
- }
3688
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3689
- index = serializeMinMax(buffer, key, value, index);
3690
- }
3691
- else if (typeof value._bsontype !== 'undefined') {
3692
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3693
- }
3694
3749
  }
3695
3750
  }
3696
3751
  else if (object instanceof Map || isMap(object)) {
@@ -3720,7 +3775,14 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3720
3775
  }
3721
3776
  }
3722
3777
  }
3723
- 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') {
3724
3786
  index = serializeString(buffer, key, value, index);
3725
3787
  }
3726
3788
  else if (type === 'number') {
@@ -3732,64 +3794,64 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3732
3794
  else if (type === 'boolean') {
3733
3795
  index = serializeBoolean(buffer, key, value, index);
3734
3796
  }
3735
- else if (value instanceof Date || isDate(value)) {
3736
- index = serializeDate(buffer, key, value, index);
3737
- }
3738
- else if (value === null || (value === undefined && ignoreUndefined === false)) {
3739
- index = serializeNull(buffer, key, value, index);
3740
- }
3741
- else if (isUint8Array(value)) {
3742
- index = serializeBuffer(buffer, key, value, index);
3743
- }
3744
- else if (value instanceof RegExp || isRegExp(value)) {
3745
- index = serializeRegExp(buffer, key, value, index);
3746
- }
3747
3797
  else if (type === 'object' && value._bsontype == null) {
3748
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3749
- }
3750
- else if (typeof value === 'object' &&
3751
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3752
- throw new BSONVersionError();
3753
- }
3754
- else if (value._bsontype === 'ObjectId') {
3755
- index = serializeObjectId(buffer, key, value, index);
3756
- }
3757
- else if (type === 'object' && value._bsontype === 'Decimal128') {
3758
- index = serializeDecimal128(buffer, key, value, index);
3759
- }
3760
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3761
- index = serializeLong(buffer, key, value, index);
3762
- }
3763
- else if (value._bsontype === 'Double') {
3764
- 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
+ }
3765
3810
  }
3766
- else if (value._bsontype === 'Code') {
3767
- 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
+ }
3768
3851
  }
3769
- else if (typeof value === 'function' && serializeFunctions) {
3852
+ else if (type === 'function' && serializeFunctions) {
3770
3853
  index = serializeFunction(buffer, key, value, index);
3771
3854
  }
3772
- else if (value._bsontype === 'Binary') {
3773
- index = serializeBinary(buffer, key, value, index);
3774
- }
3775
- else if (value._bsontype === 'BSONSymbol') {
3776
- index = serializeSymbol(buffer, key, value, index);
3777
- }
3778
- else if (value._bsontype === 'DBRef') {
3779
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3780
- }
3781
- else if (value._bsontype === 'BSONRegExp') {
3782
- index = serializeBSONRegExp(buffer, key, value, index);
3783
- }
3784
- else if (value._bsontype === 'Int32') {
3785
- index = serializeInt32(buffer, key, value, index);
3786
- }
3787
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3788
- index = serializeMinMax(buffer, key, value, index);
3789
- }
3790
- else if (typeof value._bsontype !== 'undefined') {
3791
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3792
- }
3793
3855
  }
3794
3856
  }
3795
3857
  else {
@@ -3818,7 +3880,14 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3818
3880
  }
3819
3881
  }
3820
3882
  }
3821
- 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') {
3822
3891
  index = serializeString(buffer, key, value, index);
3823
3892
  }
3824
3893
  else if (type === 'number') {
@@ -3830,68 +3899,64 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3830
3899
  else if (type === 'boolean') {
3831
3900
  index = serializeBoolean(buffer, key, value, index);
3832
3901
  }
3833
- else if (value instanceof Date || isDate(value)) {
3834
- index = serializeDate(buffer, key, value, index);
3835
- }
3836
- else if (value === undefined) {
3837
- if (ignoreUndefined === false)
3838
- index = serializeNull(buffer, key, value, index);
3839
- }
3840
- else if (value === null) {
3841
- index = serializeNull(buffer, key, value, index);
3842
- }
3843
- else if (isUint8Array(value)) {
3844
- index = serializeBuffer(buffer, key, value, index);
3845
- }
3846
- else if (value instanceof RegExp || isRegExp(value)) {
3847
- index = serializeRegExp(buffer, key, value, index);
3848
- }
3849
3902
  else if (type === 'object' && value._bsontype == null) {
3850
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3851
- }
3852
- else if (typeof value === 'object' &&
3853
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3854
- throw new BSONVersionError();
3855
- }
3856
- else if (value._bsontype === 'ObjectId') {
3857
- index = serializeObjectId(buffer, key, value, index);
3858
- }
3859
- else if (type === 'object' && value._bsontype === 'Decimal128') {
3860
- index = serializeDecimal128(buffer, key, value, index);
3861
- }
3862
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3863
- index = serializeLong(buffer, key, value, index);
3864
- }
3865
- else if (value._bsontype === 'Double') {
3866
- 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
+ }
3867
3915
  }
3868
- else if (value._bsontype === 'Code') {
3869
- 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
+ }
3870
3956
  }
3871
- else if (typeof value === 'function' && serializeFunctions) {
3957
+ else if (type === 'function' && serializeFunctions) {
3872
3958
  index = serializeFunction(buffer, key, value, index);
3873
3959
  }
3874
- else if (value._bsontype === 'Binary') {
3875
- index = serializeBinary(buffer, key, value, index);
3876
- }
3877
- else if (value._bsontype === 'BSONSymbol') {
3878
- index = serializeSymbol(buffer, key, value, index);
3879
- }
3880
- else if (value._bsontype === 'DBRef') {
3881
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3882
- }
3883
- else if (value._bsontype === 'BSONRegExp') {
3884
- index = serializeBSONRegExp(buffer, key, value, index);
3885
- }
3886
- else if (value._bsontype === 'Int32') {
3887
- index = serializeInt32(buffer, key, value, index);
3888
- }
3889
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3890
- index = serializeMinMax(buffer, key, value, index);
3891
- }
3892
- else if (typeof value._bsontype !== 'undefined') {
3893
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3894
- }
3895
3960
  }
3896
3961
  }
3897
3962
  path.delete(object);
@@ -4143,7 +4208,7 @@ function serializeDocument(doc, options) {
4143
4208
  else if (doc != null &&
4144
4209
  typeof doc === 'object' &&
4145
4210
  typeof doc._bsontype === 'string' &&
4146
- doc[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
4211
+ doc[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
4147
4212
  throw new BSONVersionError();
4148
4213
  }
4149
4214
  else if (isBSONType(doc)) {