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.
@@ -1,20 +1,46 @@
1
1
  var BSON = (function (exports) {
2
2
  'use strict';
3
3
 
4
+ const map = new WeakMap();
5
+ const TYPES = {
6
+ ArrayBuffer: '[object ArrayBuffer]',
7
+ SharedArrayBuffer: '[object SharedArrayBuffer]',
8
+ Uint8Array: '[object Uint8Array]',
9
+ BigInt64Array: '[object BigInt64Array]',
10
+ BigUint64Array: '[object BigUint64Array]',
11
+ RegExp: '[object RegExp]',
12
+ Map: '[object Map]',
13
+ Date: '[object Date]'
14
+ };
15
+ function getPrototypeString(value) {
16
+ let str = map.get(value);
17
+ if (!str) {
18
+ str = Object.prototype.toString.call(value);
19
+ if (value !== null && typeof value === 'object') {
20
+ map.set(value, str);
21
+ }
22
+ }
23
+ return str;
24
+ }
4
25
  function isAnyArrayBuffer(value) {
5
- return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(Object.prototype.toString.call(value));
26
+ const type = getPrototypeString(value);
27
+ return type === TYPES.ArrayBuffer || type === TYPES.SharedArrayBuffer;
6
28
  }
7
29
  function isUint8Array(value) {
8
- return Object.prototype.toString.call(value) === '[object Uint8Array]';
30
+ const type = getPrototypeString(value);
31
+ return type === TYPES.Uint8Array;
9
32
  }
10
33
  function isRegExp(d) {
11
- return Object.prototype.toString.call(d) === '[object RegExp]';
34
+ const type = getPrototypeString(d);
35
+ return type === TYPES.RegExp;
12
36
  }
13
37
  function isMap(d) {
14
- return Object.prototype.toString.call(d) === '[object Map]';
38
+ const type = getPrototypeString(d);
39
+ return type === TYPES.Map;
15
40
  }
16
41
  function isDate(d) {
17
- return Object.prototype.toString.call(d) === '[object Date]';
42
+ const type = getPrototypeString(d);
43
+ return type === TYPES.Date;
18
44
  }
19
45
  function defaultInspect(x, _options) {
20
46
  return JSON.stringify(x, (k, v) => {
@@ -38,6 +64,7 @@ function getStylizeFunction(options) {
38
64
  }
39
65
 
40
66
  const BSON_MAJOR_VERSION = 6;
67
+ const BSON_VERSION_SYMBOL = Symbol.for('@@mdb.bson.version');
41
68
  const BSON_INT32_MAX = 0x7fffffff;
42
69
  const BSON_INT32_MIN = -0x80000000;
43
70
  const BSON_INT64_MAX = Math.pow(2, 63) - 1;
@@ -412,7 +439,7 @@ const hasGlobalBuffer = typeof Buffer === 'function' && Buffer.prototype?._isBuf
412
439
  const ByteUtils = hasGlobalBuffer ? nodeJsByteUtils : webByteUtils;
413
440
 
414
441
  class BSONValue {
415
- get [Symbol.for('@@mdb.bson.version')]() {
442
+ get [BSON_VERSION_SYMBOL]() {
416
443
  return BSON_MAJOR_VERSION;
417
444
  }
418
445
  [Symbol.for('nodejs.util.inspect.custom')](depth, options, inspect) {
@@ -856,19 +883,18 @@ class Long extends BSONValue {
856
883
  get __isLong__() {
857
884
  return true;
858
885
  }
859
- constructor(low = 0, high, unsigned) {
886
+ constructor(lowOrValue = 0, highOrUnsigned, unsigned) {
860
887
  super();
861
- if (typeof low === 'bigint') {
862
- Object.assign(this, Long.fromBigInt(low, !!high));
863
- }
864
- else if (typeof low === 'string') {
865
- Object.assign(this, Long.fromString(low, !!high));
866
- }
867
- else {
868
- this.low = low | 0;
869
- this.high = high | 0;
870
- this.unsigned = !!unsigned;
871
- }
888
+ const unsignedBool = typeof highOrUnsigned === 'boolean' ? highOrUnsigned : Boolean(unsigned);
889
+ const high = typeof highOrUnsigned === 'number' ? highOrUnsigned : 0;
890
+ const res = typeof lowOrValue === 'string'
891
+ ? Long.fromString(lowOrValue, unsignedBool)
892
+ : typeof lowOrValue === 'bigint'
893
+ ? Long.fromBigInt(lowOrValue, unsignedBool)
894
+ : { low: lowOrValue | 0, high: high | 0, unsigned: unsignedBool };
895
+ this.low = res.low;
896
+ this.high = res.high;
897
+ this.unsigned = res.unsigned;
872
898
  }
873
899
  static fromBits(lowBits, highBits, unsigned) {
874
900
  return new Long(lowBits, highBits, unsigned);
@@ -920,7 +946,9 @@ class Long extends BSONValue {
920
946
  return Long.fromBits(value % TWO_PWR_32_DBL | 0, (value / TWO_PWR_32_DBL) | 0, unsigned);
921
947
  }
922
948
  static fromBigInt(value, unsigned) {
923
- return Long.fromString(value.toString(), unsigned);
949
+ const FROM_BIGINT_BIT_MASK = BigInt(0xffffffff);
950
+ const FROM_BIGINT_BIT_SHIFT = BigInt(32);
951
+ return new Long(Number(value & FROM_BIGINT_BIT_MASK), Number((value >> FROM_BIGINT_BIT_SHIFT) & FROM_BIGINT_BIT_MASK), unsigned);
924
952
  }
925
953
  static _fromString(str, unsigned, radix) {
926
954
  if (str.length === 0)
@@ -1588,7 +1616,7 @@ class Decimal128 extends BSONValue {
1588
1616
  if (typeof bytes === 'string') {
1589
1617
  this.bytes = Decimal128.fromString(bytes).bytes;
1590
1618
  }
1591
- else if (isUint8Array(bytes)) {
1619
+ else if (bytes instanceof Uint8Array || isUint8Array(bytes)) {
1592
1620
  if (bytes.byteLength !== 16) {
1593
1621
  throw new BSONError('Decimal128 must take a Buffer of 16 bytes');
1594
1622
  }
@@ -2279,7 +2307,7 @@ const NumberUtils = {
2279
2307
  return 4;
2280
2308
  },
2281
2309
  setBigInt64LE(destination, offset, value) {
2282
- const mask32bits = BigInt(4294967295);
2310
+ const mask32bits = BigInt(0xffff_ffff);
2283
2311
  let lo = Number(value & mask32bits);
2284
2312
  destination[offset] = lo;
2285
2313
  lo >>= 8;
@@ -2325,7 +2353,6 @@ const NumberUtils = {
2325
2353
  }
2326
2354
  };
2327
2355
 
2328
- const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
2329
2356
  let PROCESS_UNIQUE = null;
2330
2357
  class ObjectId extends BSONValue {
2331
2358
  get _bsontype() {
@@ -2355,7 +2382,7 @@ class ObjectId extends BSONValue {
2355
2382
  this.buffer = ByteUtils.toLocalBufferType(workingId);
2356
2383
  }
2357
2384
  else if (typeof workingId === 'string') {
2358
- if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
2385
+ if (ObjectId.validateHexString(workingId)) {
2359
2386
  this.buffer = ByteUtils.fromHex(workingId);
2360
2387
  }
2361
2388
  else {
@@ -2378,6 +2405,20 @@ class ObjectId extends BSONValue {
2378
2405
  this.__id = ByteUtils.toHex(value);
2379
2406
  }
2380
2407
  }
2408
+ static validateHexString(string) {
2409
+ if (string?.length !== 24)
2410
+ return false;
2411
+ for (let i = 0; i < 24; i++) {
2412
+ const char = string.charCodeAt(i);
2413
+ if ((char >= 48 && char <= 57) ||
2414
+ (char >= 97 && char <= 102) ||
2415
+ (char >= 65 && char <= 70)) {
2416
+ continue;
2417
+ }
2418
+ return false;
2419
+ }
2420
+ return true;
2421
+ }
2381
2422
  toHexString() {
2382
2423
  if (ObjectId.cacheHexString && this.__id) {
2383
2424
  return this.__id;
@@ -2490,6 +2531,8 @@ class ObjectId extends BSONValue {
2490
2531
  static isValid(id) {
2491
2532
  if (id == null)
2492
2533
  return false;
2534
+ if (typeof id === 'string')
2535
+ return ObjectId.validateHexString(id);
2493
2536
  try {
2494
2537
  new ObjectId(id);
2495
2538
  return true;
@@ -2560,7 +2603,7 @@ function calculateElement(name, value, serializeFunctions = false, isArray = fal
2560
2603
  case 'object':
2561
2604
  if (value != null &&
2562
2605
  typeof value._bsontype === 'string' &&
2563
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
2606
+ value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
2564
2607
  throw new BSONVersionError();
2565
2608
  }
2566
2609
  else if (value == null || value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
@@ -2764,6 +2807,12 @@ class Timestamp extends LongWithoutOverridesClass {
2764
2807
  get _bsontype() {
2765
2808
  return 'Timestamp';
2766
2809
  }
2810
+ get i() {
2811
+ return this.low >>> 0;
2812
+ }
2813
+ get t() {
2814
+ return this.high >>> 0;
2815
+ }
2767
2816
  constructor(low) {
2768
2817
  if (low == null) {
2769
2818
  super(0, 0, true);
@@ -2789,10 +2838,10 @@ class Timestamp extends LongWithoutOverridesClass {
2789
2838
  if (i < 0 || Number.isNaN(i)) {
2790
2839
  throw new BSONError('Timestamp constructed from { t, i } must provide a positive i');
2791
2840
  }
2792
- if (t > 4294967295) {
2841
+ if (t > 0xffff_ffff) {
2793
2842
  throw new BSONError('Timestamp constructed from { t, i } must provide t equal or less than uint32 max');
2794
2843
  }
2795
- if (i > 4294967295) {
2844
+ if (i > 0xffff_ffff) {
2796
2845
  throw new BSONError('Timestamp constructed from { t, i } must provide i equal or less than uint32 max');
2797
2846
  }
2798
2847
  super(i, t, true);
@@ -2819,7 +2868,7 @@ class Timestamp extends LongWithoutOverridesClass {
2819
2868
  return new Timestamp(Long.fromString(str, true, optRadix));
2820
2869
  }
2821
2870
  toExtendedJSON() {
2822
- return { $timestamp: { t: this.high >>> 0, i: this.low >>> 0 } };
2871
+ return { $timestamp: { t: this.t, i: this.i } };
2823
2872
  }
2824
2873
  static fromExtendedJSON(doc) {
2825
2874
  const i = Long.isLong(doc.$timestamp.i)
@@ -2832,8 +2881,8 @@ class Timestamp extends LongWithoutOverridesClass {
2832
2881
  }
2833
2882
  inspect(depth, options, inspect) {
2834
2883
  inspect ??= defaultInspect;
2835
- const t = inspect(this.high >>> 0, options);
2836
- const i = inspect(this.low >>> 0, options);
2884
+ const t = inspect(this.t, options);
2885
+ const i = inspect(this.i, options);
2837
2886
  return `new Timestamp({ t: ${t}, i: ${i} })`;
2838
2887
  }
2839
2888
  }
@@ -3602,79 +3651,83 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3602
3651
  if (typeof value?.toBSON === 'function') {
3603
3652
  value = value.toBSON();
3604
3653
  }
3605
- if (typeof value === 'string') {
3606
- index = serializeString(buffer, key, value, index);
3607
- }
3608
- else if (typeof value === 'number') {
3609
- index = serializeNumber(buffer, key, value, index);
3610
- }
3611
- else if (typeof value === 'bigint') {
3612
- index = serializeBigInt(buffer, key, value, index);
3613
- }
3614
- else if (typeof value === 'boolean') {
3615
- index = serializeBoolean(buffer, key, value, index);
3616
- }
3617
- else if (value instanceof Date || isDate(value)) {
3618
- index = serializeDate(buffer, key, value, index);
3619
- }
3620
- else if (value === undefined) {
3654
+ const type = typeof value;
3655
+ if (value === undefined) {
3621
3656
  index = serializeNull(buffer, key, value, index);
3622
3657
  }
3623
3658
  else if (value === null) {
3624
3659
  index = serializeNull(buffer, key, value, index);
3625
3660
  }
3626
- else if (isUint8Array(value)) {
3627
- index = serializeBuffer(buffer, key, value, index);
3628
- }
3629
- else if (value instanceof RegExp || isRegExp(value)) {
3630
- index = serializeRegExp(buffer, key, value, index);
3631
- }
3632
- else if (typeof value === 'object' && value._bsontype == null) {
3633
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3661
+ else if (type === 'string') {
3662
+ index = serializeString(buffer, key, value, index);
3634
3663
  }
3635
- else if (typeof value === 'object' &&
3636
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3637
- throw new BSONVersionError();
3664
+ else if (type === 'number') {
3665
+ index = serializeNumber(buffer, key, value, index);
3638
3666
  }
3639
- else if (value._bsontype === 'ObjectId') {
3640
- index = serializeObjectId(buffer, key, value, index);
3667
+ else if (type === 'bigint') {
3668
+ index = serializeBigInt(buffer, key, value, index);
3641
3669
  }
3642
- else if (value._bsontype === 'Decimal128') {
3643
- index = serializeDecimal128(buffer, key, value, index);
3670
+ else if (type === 'boolean') {
3671
+ index = serializeBoolean(buffer, key, value, index);
3644
3672
  }
3645
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3646
- index = serializeLong(buffer, key, value, index);
3673
+ else if (type === 'object' && value._bsontype == null) {
3674
+ if (value instanceof Date || isDate(value)) {
3675
+ index = serializeDate(buffer, key, value, index);
3676
+ }
3677
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3678
+ index = serializeBuffer(buffer, key, value, index);
3679
+ }
3680
+ else if (value instanceof RegExp || isRegExp(value)) {
3681
+ index = serializeRegExp(buffer, key, value, index);
3682
+ }
3683
+ else {
3684
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3685
+ }
3647
3686
  }
3648
- else if (value._bsontype === 'Double') {
3649
- index = serializeDouble(buffer, key, value, index);
3687
+ else if (type === 'object') {
3688
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3689
+ throw new BSONVersionError();
3690
+ }
3691
+ else if (value._bsontype === 'ObjectId') {
3692
+ index = serializeObjectId(buffer, key, value, index);
3693
+ }
3694
+ else if (value._bsontype === 'Decimal128') {
3695
+ index = serializeDecimal128(buffer, key, value, index);
3696
+ }
3697
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3698
+ index = serializeLong(buffer, key, value, index);
3699
+ }
3700
+ else if (value._bsontype === 'Double') {
3701
+ index = serializeDouble(buffer, key, value, index);
3702
+ }
3703
+ else if (value._bsontype === 'Code') {
3704
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3705
+ }
3706
+ else if (value._bsontype === 'Binary') {
3707
+ index = serializeBinary(buffer, key, value, index);
3708
+ }
3709
+ else if (value._bsontype === 'BSONSymbol') {
3710
+ index = serializeSymbol(buffer, key, value, index);
3711
+ }
3712
+ else if (value._bsontype === 'DBRef') {
3713
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3714
+ }
3715
+ else if (value._bsontype === 'BSONRegExp') {
3716
+ index = serializeBSONRegExp(buffer, key, value, index);
3717
+ }
3718
+ else if (value._bsontype === 'Int32') {
3719
+ index = serializeInt32(buffer, key, value, index);
3720
+ }
3721
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3722
+ index = serializeMinMax(buffer, key, value, index);
3723
+ }
3724
+ else if (typeof value._bsontype !== 'undefined') {
3725
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3726
+ }
3650
3727
  }
3651
- else if (typeof value === 'function' && serializeFunctions) {
3728
+ else if (type === 'function' && serializeFunctions) {
3652
3729
  index = serializeFunction(buffer, key, value, index);
3653
3730
  }
3654
- else if (value._bsontype === 'Code') {
3655
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3656
- }
3657
- else if (value._bsontype === 'Binary') {
3658
- index = serializeBinary(buffer, key, value, index);
3659
- }
3660
- else if (value._bsontype === 'BSONSymbol') {
3661
- index = serializeSymbol(buffer, key, value, index);
3662
- }
3663
- else if (value._bsontype === 'DBRef') {
3664
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3665
- }
3666
- else if (value._bsontype === 'BSONRegExp') {
3667
- index = serializeBSONRegExp(buffer, key, value, index);
3668
- }
3669
- else if (value._bsontype === 'Int32') {
3670
- index = serializeInt32(buffer, key, value, index);
3671
- }
3672
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3673
- index = serializeMinMax(buffer, key, value, index);
3674
- }
3675
- else if (typeof value._bsontype !== 'undefined') {
3676
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3677
- }
3678
3731
  }
3679
3732
  }
3680
3733
  else if (object instanceof Map || isMap(object)) {
@@ -3704,7 +3757,14 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3704
3757
  }
3705
3758
  }
3706
3759
  }
3707
- if (type === 'string') {
3760
+ if (value === undefined) {
3761
+ if (ignoreUndefined === false)
3762
+ index = serializeNull(buffer, key, value, index);
3763
+ }
3764
+ else if (value === null) {
3765
+ index = serializeNull(buffer, key, value, index);
3766
+ }
3767
+ else if (type === 'string') {
3708
3768
  index = serializeString(buffer, key, value, index);
3709
3769
  }
3710
3770
  else if (type === 'number') {
@@ -3716,64 +3776,64 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3716
3776
  else if (type === 'boolean') {
3717
3777
  index = serializeBoolean(buffer, key, value, index);
3718
3778
  }
3719
- else if (value instanceof Date || isDate(value)) {
3720
- index = serializeDate(buffer, key, value, index);
3721
- }
3722
- else if (value === null || (value === undefined && ignoreUndefined === false)) {
3723
- index = serializeNull(buffer, key, value, index);
3724
- }
3725
- else if (isUint8Array(value)) {
3726
- index = serializeBuffer(buffer, key, value, index);
3727
- }
3728
- else if (value instanceof RegExp || isRegExp(value)) {
3729
- index = serializeRegExp(buffer, key, value, index);
3730
- }
3731
3779
  else if (type === 'object' && value._bsontype == null) {
3732
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3733
- }
3734
- else if (typeof value === 'object' &&
3735
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3736
- throw new BSONVersionError();
3737
- }
3738
- else if (value._bsontype === 'ObjectId') {
3739
- index = serializeObjectId(buffer, key, value, index);
3740
- }
3741
- else if (type === 'object' && value._bsontype === 'Decimal128') {
3742
- index = serializeDecimal128(buffer, key, value, index);
3743
- }
3744
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3745
- index = serializeLong(buffer, key, value, index);
3746
- }
3747
- else if (value._bsontype === 'Double') {
3748
- index = serializeDouble(buffer, key, value, index);
3780
+ if (value instanceof Date || isDate(value)) {
3781
+ index = serializeDate(buffer, key, value, index);
3782
+ }
3783
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3784
+ index = serializeBuffer(buffer, key, value, index);
3785
+ }
3786
+ else if (value instanceof RegExp || isRegExp(value)) {
3787
+ index = serializeRegExp(buffer, key, value, index);
3788
+ }
3789
+ else {
3790
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3791
+ }
3749
3792
  }
3750
- else if (value._bsontype === 'Code') {
3751
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3793
+ else if (type === 'object') {
3794
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3795
+ throw new BSONVersionError();
3796
+ }
3797
+ else if (value._bsontype === 'ObjectId') {
3798
+ index = serializeObjectId(buffer, key, value, index);
3799
+ }
3800
+ else if (value._bsontype === 'Decimal128') {
3801
+ index = serializeDecimal128(buffer, key, value, index);
3802
+ }
3803
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3804
+ index = serializeLong(buffer, key, value, index);
3805
+ }
3806
+ else if (value._bsontype === 'Double') {
3807
+ index = serializeDouble(buffer, key, value, index);
3808
+ }
3809
+ else if (value._bsontype === 'Code') {
3810
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3811
+ }
3812
+ else if (value._bsontype === 'Binary') {
3813
+ index = serializeBinary(buffer, key, value, index);
3814
+ }
3815
+ else if (value._bsontype === 'BSONSymbol') {
3816
+ index = serializeSymbol(buffer, key, value, index);
3817
+ }
3818
+ else if (value._bsontype === 'DBRef') {
3819
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3820
+ }
3821
+ else if (value._bsontype === 'BSONRegExp') {
3822
+ index = serializeBSONRegExp(buffer, key, value, index);
3823
+ }
3824
+ else if (value._bsontype === 'Int32') {
3825
+ index = serializeInt32(buffer, key, value, index);
3826
+ }
3827
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3828
+ index = serializeMinMax(buffer, key, value, index);
3829
+ }
3830
+ else if (typeof value._bsontype !== 'undefined') {
3831
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3832
+ }
3752
3833
  }
3753
- else if (typeof value === 'function' && serializeFunctions) {
3834
+ else if (type === 'function' && serializeFunctions) {
3754
3835
  index = serializeFunction(buffer, key, value, index);
3755
3836
  }
3756
- else if (value._bsontype === 'Binary') {
3757
- index = serializeBinary(buffer, key, value, index);
3758
- }
3759
- else if (value._bsontype === 'BSONSymbol') {
3760
- index = serializeSymbol(buffer, key, value, index);
3761
- }
3762
- else if (value._bsontype === 'DBRef') {
3763
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3764
- }
3765
- else if (value._bsontype === 'BSONRegExp') {
3766
- index = serializeBSONRegExp(buffer, key, value, index);
3767
- }
3768
- else if (value._bsontype === 'Int32') {
3769
- index = serializeInt32(buffer, key, value, index);
3770
- }
3771
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3772
- index = serializeMinMax(buffer, key, value, index);
3773
- }
3774
- else if (typeof value._bsontype !== 'undefined') {
3775
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3776
- }
3777
3837
  }
3778
3838
  }
3779
3839
  else {
@@ -3802,7 +3862,14 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3802
3862
  }
3803
3863
  }
3804
3864
  }
3805
- if (type === 'string') {
3865
+ if (value === undefined) {
3866
+ if (ignoreUndefined === false)
3867
+ index = serializeNull(buffer, key, value, index);
3868
+ }
3869
+ else if (value === null) {
3870
+ index = serializeNull(buffer, key, value, index);
3871
+ }
3872
+ else if (type === 'string') {
3806
3873
  index = serializeString(buffer, key, value, index);
3807
3874
  }
3808
3875
  else if (type === 'number') {
@@ -3814,68 +3881,64 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3814
3881
  else if (type === 'boolean') {
3815
3882
  index = serializeBoolean(buffer, key, value, index);
3816
3883
  }
3817
- else if (value instanceof Date || isDate(value)) {
3818
- index = serializeDate(buffer, key, value, index);
3819
- }
3820
- else if (value === undefined) {
3821
- if (ignoreUndefined === false)
3822
- index = serializeNull(buffer, key, value, index);
3823
- }
3824
- else if (value === null) {
3825
- index = serializeNull(buffer, key, value, index);
3826
- }
3827
- else if (isUint8Array(value)) {
3828
- index = serializeBuffer(buffer, key, value, index);
3829
- }
3830
- else if (value instanceof RegExp || isRegExp(value)) {
3831
- index = serializeRegExp(buffer, key, value, index);
3832
- }
3833
3884
  else if (type === 'object' && value._bsontype == null) {
3834
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3835
- }
3836
- else if (typeof value === 'object' &&
3837
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3838
- throw new BSONVersionError();
3839
- }
3840
- else if (value._bsontype === 'ObjectId') {
3841
- index = serializeObjectId(buffer, key, value, index);
3842
- }
3843
- else if (type === 'object' && value._bsontype === 'Decimal128') {
3844
- index = serializeDecimal128(buffer, key, value, index);
3845
- }
3846
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3847
- index = serializeLong(buffer, key, value, index);
3848
- }
3849
- else if (value._bsontype === 'Double') {
3850
- index = serializeDouble(buffer, key, value, index);
3885
+ if (value instanceof Date || isDate(value)) {
3886
+ index = serializeDate(buffer, key, value, index);
3887
+ }
3888
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3889
+ index = serializeBuffer(buffer, key, value, index);
3890
+ }
3891
+ else if (value instanceof RegExp || isRegExp(value)) {
3892
+ index = serializeRegExp(buffer, key, value, index);
3893
+ }
3894
+ else {
3895
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3896
+ }
3851
3897
  }
3852
- else if (value._bsontype === 'Code') {
3853
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3898
+ else if (type === 'object') {
3899
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3900
+ throw new BSONVersionError();
3901
+ }
3902
+ else if (value._bsontype === 'ObjectId') {
3903
+ index = serializeObjectId(buffer, key, value, index);
3904
+ }
3905
+ else if (value._bsontype === 'Decimal128') {
3906
+ index = serializeDecimal128(buffer, key, value, index);
3907
+ }
3908
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3909
+ index = serializeLong(buffer, key, value, index);
3910
+ }
3911
+ else if (value._bsontype === 'Double') {
3912
+ index = serializeDouble(buffer, key, value, index);
3913
+ }
3914
+ else if (value._bsontype === 'Code') {
3915
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3916
+ }
3917
+ else if (value._bsontype === 'Binary') {
3918
+ index = serializeBinary(buffer, key, value, index);
3919
+ }
3920
+ else if (value._bsontype === 'BSONSymbol') {
3921
+ index = serializeSymbol(buffer, key, value, index);
3922
+ }
3923
+ else if (value._bsontype === 'DBRef') {
3924
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3925
+ }
3926
+ else if (value._bsontype === 'BSONRegExp') {
3927
+ index = serializeBSONRegExp(buffer, key, value, index);
3928
+ }
3929
+ else if (value._bsontype === 'Int32') {
3930
+ index = serializeInt32(buffer, key, value, index);
3931
+ }
3932
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3933
+ index = serializeMinMax(buffer, key, value, index);
3934
+ }
3935
+ else if (typeof value._bsontype !== 'undefined') {
3936
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3937
+ }
3854
3938
  }
3855
- else if (typeof value === 'function' && serializeFunctions) {
3939
+ else if (type === 'function' && serializeFunctions) {
3856
3940
  index = serializeFunction(buffer, key, value, index);
3857
3941
  }
3858
- else if (value._bsontype === 'Binary') {
3859
- index = serializeBinary(buffer, key, value, index);
3860
- }
3861
- else if (value._bsontype === 'BSONSymbol') {
3862
- index = serializeSymbol(buffer, key, value, index);
3863
- }
3864
- else if (value._bsontype === 'DBRef') {
3865
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3866
- }
3867
- else if (value._bsontype === 'BSONRegExp') {
3868
- index = serializeBSONRegExp(buffer, key, value, index);
3869
- }
3870
- else if (value._bsontype === 'Int32') {
3871
- index = serializeInt32(buffer, key, value, index);
3872
- }
3873
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3874
- index = serializeMinMax(buffer, key, value, index);
3875
- }
3876
- else if (typeof value._bsontype !== 'undefined') {
3877
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3878
- }
3879
3942
  }
3880
3943
  }
3881
3944
  path.delete(object);
@@ -4127,7 +4190,7 @@ function serializeDocument(doc, options) {
4127
4190
  else if (doc != null &&
4128
4191
  typeof doc === 'object' &&
4129
4192
  typeof doc._bsontype === 'string' &&
4130
- doc[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
4193
+ doc[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
4131
4194
  throw new BSONVersionError();
4132
4195
  }
4133
4196
  else if (isBSONType(doc)) {