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