bson 6.8.0 → 6.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/bson.mjs CHANGED
@@ -1,17 +1,43 @@
1
+ const map = new WeakMap();
2
+ const TYPES = {
3
+ ArrayBuffer: '[object ArrayBuffer]',
4
+ SharedArrayBuffer: '[object SharedArrayBuffer]',
5
+ Uint8Array: '[object Uint8Array]',
6
+ BigInt64Array: '[object BigInt64Array]',
7
+ BigUint64Array: '[object BigUint64Array]',
8
+ RegExp: '[object RegExp]',
9
+ Map: '[object Map]',
10
+ Date: '[object Date]'
11
+ };
12
+ function getPrototypeString(value) {
13
+ let str = map.get(value);
14
+ if (!str) {
15
+ str = Object.prototype.toString.call(value);
16
+ if (value !== null && typeof value === 'object') {
17
+ map.set(value, str);
18
+ }
19
+ }
20
+ return str;
21
+ }
1
22
  function isAnyArrayBuffer(value) {
2
- return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(Object.prototype.toString.call(value));
23
+ const type = getPrototypeString(value);
24
+ return type === TYPES.ArrayBuffer || type === TYPES.SharedArrayBuffer;
3
25
  }
4
26
  function isUint8Array(value) {
5
- return Object.prototype.toString.call(value) === '[object Uint8Array]';
27
+ const type = getPrototypeString(value);
28
+ return type === TYPES.Uint8Array;
6
29
  }
7
30
  function isRegExp(d) {
8
- return Object.prototype.toString.call(d) === '[object RegExp]';
31
+ const type = getPrototypeString(d);
32
+ return type === TYPES.RegExp;
9
33
  }
10
34
  function isMap(d) {
11
- return Object.prototype.toString.call(d) === '[object Map]';
35
+ const type = getPrototypeString(d);
36
+ return type === TYPES.Map;
12
37
  }
13
38
  function isDate(d) {
14
- return Object.prototype.toString.call(d) === '[object Date]';
39
+ const type = getPrototypeString(d);
40
+ return type === TYPES.Date;
15
41
  }
16
42
  function defaultInspect(x, _options) {
17
43
  return JSON.stringify(x, (k, v) => {
@@ -35,6 +61,7 @@ function getStylizeFunction(options) {
35
61
  }
36
62
 
37
63
  const BSON_MAJOR_VERSION = 6;
64
+ const BSON_VERSION_SYMBOL = Symbol.for('@@mdb.bson.version');
38
65
  const BSON_INT32_MAX = 0x7fffffff;
39
66
  const BSON_INT32_MIN = -0x80000000;
40
67
  const BSON_INT64_MAX = Math.pow(2, 63) - 1;
@@ -409,7 +436,7 @@ const hasGlobalBuffer = typeof Buffer === 'function' && Buffer.prototype?._isBuf
409
436
  const ByteUtils = hasGlobalBuffer ? nodeJsByteUtils : webByteUtils;
410
437
 
411
438
  class BSONValue {
412
- get [Symbol.for('@@mdb.bson.version')]() {
439
+ get [BSON_VERSION_SYMBOL]() {
413
440
  return BSON_MAJOR_VERSION;
414
441
  }
415
442
  [Symbol.for('nodejs.util.inspect.custom')](depth, options, inspect) {
@@ -1586,7 +1613,7 @@ class Decimal128 extends BSONValue {
1586
1613
  if (typeof bytes === 'string') {
1587
1614
  this.bytes = Decimal128.fromString(bytes).bytes;
1588
1615
  }
1589
- else if (isUint8Array(bytes)) {
1616
+ else if (bytes instanceof Uint8Array || isUint8Array(bytes)) {
1590
1617
  if (bytes.byteLength !== 16) {
1591
1618
  throw new BSONError('Decimal128 must take a Buffer of 16 bytes');
1592
1619
  }
@@ -2277,7 +2304,7 @@ const NumberUtils = {
2277
2304
  return 4;
2278
2305
  },
2279
2306
  setBigInt64LE(destination, offset, value) {
2280
- const mask32bits = BigInt(4294967295);
2307
+ const mask32bits = BigInt(0xffff_ffff);
2281
2308
  let lo = Number(value & mask32bits);
2282
2309
  destination[offset] = lo;
2283
2310
  lo >>= 8;
@@ -2323,7 +2350,6 @@ const NumberUtils = {
2323
2350
  }
2324
2351
  };
2325
2352
 
2326
- const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
2327
2353
  let PROCESS_UNIQUE = null;
2328
2354
  class ObjectId extends BSONValue {
2329
2355
  get _bsontype() {
@@ -2353,7 +2379,7 @@ class ObjectId extends BSONValue {
2353
2379
  this.buffer = ByteUtils.toLocalBufferType(workingId);
2354
2380
  }
2355
2381
  else if (typeof workingId === 'string') {
2356
- if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
2382
+ if (ObjectId.validateHexString(workingId)) {
2357
2383
  this.buffer = ByteUtils.fromHex(workingId);
2358
2384
  }
2359
2385
  else {
@@ -2376,6 +2402,20 @@ class ObjectId extends BSONValue {
2376
2402
  this.__id = ByteUtils.toHex(value);
2377
2403
  }
2378
2404
  }
2405
+ static validateHexString(string) {
2406
+ if (string?.length !== 24)
2407
+ return false;
2408
+ for (let i = 0; i < 24; i++) {
2409
+ const char = string.charCodeAt(i);
2410
+ if ((char >= 48 && char <= 57) ||
2411
+ (char >= 97 && char <= 102) ||
2412
+ (char >= 65 && char <= 70)) {
2413
+ continue;
2414
+ }
2415
+ return false;
2416
+ }
2417
+ return true;
2418
+ }
2379
2419
  toHexString() {
2380
2420
  if (ObjectId.cacheHexString && this.__id) {
2381
2421
  return this.__id;
@@ -2488,6 +2528,8 @@ class ObjectId extends BSONValue {
2488
2528
  static isValid(id) {
2489
2529
  if (id == null)
2490
2530
  return false;
2531
+ if (typeof id === 'string')
2532
+ return ObjectId.validateHexString(id);
2491
2533
  try {
2492
2534
  new ObjectId(id);
2493
2535
  return true;
@@ -2558,7 +2600,7 @@ function calculateElement(name, value, serializeFunctions = false, isArray = fal
2558
2600
  case 'object':
2559
2601
  if (value != null &&
2560
2602
  typeof value._bsontype === 'string' &&
2561
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
2603
+ value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
2562
2604
  throw new BSONVersionError();
2563
2605
  }
2564
2606
  else if (value == null || value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
@@ -2762,6 +2804,12 @@ class Timestamp extends LongWithoutOverridesClass {
2762
2804
  get _bsontype() {
2763
2805
  return 'Timestamp';
2764
2806
  }
2807
+ get i() {
2808
+ return this.low >>> 0;
2809
+ }
2810
+ get t() {
2811
+ return this.high >>> 0;
2812
+ }
2765
2813
  constructor(low) {
2766
2814
  if (low == null) {
2767
2815
  super(0, 0, true);
@@ -2787,10 +2835,10 @@ class Timestamp extends LongWithoutOverridesClass {
2787
2835
  if (i < 0 || Number.isNaN(i)) {
2788
2836
  throw new BSONError('Timestamp constructed from { t, i } must provide a positive i');
2789
2837
  }
2790
- if (t > 4294967295) {
2838
+ if (t > 0xffff_ffff) {
2791
2839
  throw new BSONError('Timestamp constructed from { t, i } must provide t equal or less than uint32 max');
2792
2840
  }
2793
- if (i > 4294967295) {
2841
+ if (i > 0xffff_ffff) {
2794
2842
  throw new BSONError('Timestamp constructed from { t, i } must provide i equal or less than uint32 max');
2795
2843
  }
2796
2844
  super(i, t, true);
@@ -2817,7 +2865,7 @@ class Timestamp extends LongWithoutOverridesClass {
2817
2865
  return new Timestamp(Long.fromString(str, true, optRadix));
2818
2866
  }
2819
2867
  toExtendedJSON() {
2820
- return { $timestamp: { t: this.high >>> 0, i: this.low >>> 0 } };
2868
+ return { $timestamp: { t: this.t, i: this.i } };
2821
2869
  }
2822
2870
  static fromExtendedJSON(doc) {
2823
2871
  const i = Long.isLong(doc.$timestamp.i)
@@ -2830,8 +2878,8 @@ class Timestamp extends LongWithoutOverridesClass {
2830
2878
  }
2831
2879
  inspect(depth, options, inspect) {
2832
2880
  inspect ??= defaultInspect;
2833
- const t = inspect(this.high >>> 0, options);
2834
- const i = inspect(this.low >>> 0, options);
2881
+ const t = inspect(this.t, options);
2882
+ const i = inspect(this.i, options);
2835
2883
  return `new Timestamp({ t: ${t}, i: ${i} })`;
2836
2884
  }
2837
2885
  }
@@ -3600,79 +3648,83 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3600
3648
  if (typeof value?.toBSON === 'function') {
3601
3649
  value = value.toBSON();
3602
3650
  }
3603
- if (typeof value === 'string') {
3604
- index = serializeString(buffer, key, value, index);
3605
- }
3606
- else if (typeof value === 'number') {
3607
- index = serializeNumber(buffer, key, value, index);
3608
- }
3609
- else if (typeof value === 'bigint') {
3610
- index = serializeBigInt(buffer, key, value, index);
3611
- }
3612
- else if (typeof value === 'boolean') {
3613
- index = serializeBoolean(buffer, key, value, index);
3614
- }
3615
- else if (value instanceof Date || isDate(value)) {
3616
- index = serializeDate(buffer, key, value, index);
3617
- }
3618
- else if (value === undefined) {
3651
+ const type = typeof value;
3652
+ if (value === undefined) {
3619
3653
  index = serializeNull(buffer, key, value, index);
3620
3654
  }
3621
3655
  else if (value === null) {
3622
3656
  index = serializeNull(buffer, key, value, index);
3623
3657
  }
3624
- else if (isUint8Array(value)) {
3625
- index = serializeBuffer(buffer, key, value, index);
3626
- }
3627
- else if (value instanceof RegExp || isRegExp(value)) {
3628
- index = serializeRegExp(buffer, key, value, index);
3629
- }
3630
- else if (typeof value === 'object' && value._bsontype == null) {
3631
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3658
+ else if (type === 'string') {
3659
+ index = serializeString(buffer, key, value, index);
3632
3660
  }
3633
- else if (typeof value === 'object' &&
3634
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3635
- throw new BSONVersionError();
3661
+ else if (type === 'number') {
3662
+ index = serializeNumber(buffer, key, value, index);
3636
3663
  }
3637
- else if (value._bsontype === 'ObjectId') {
3638
- index = serializeObjectId(buffer, key, value, index);
3664
+ else if (type === 'bigint') {
3665
+ index = serializeBigInt(buffer, key, value, index);
3639
3666
  }
3640
- else if (value._bsontype === 'Decimal128') {
3641
- index = serializeDecimal128(buffer, key, value, index);
3667
+ else if (type === 'boolean') {
3668
+ index = serializeBoolean(buffer, key, value, index);
3642
3669
  }
3643
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3644
- index = serializeLong(buffer, key, value, index);
3670
+ else if (type === 'object' && value._bsontype == null) {
3671
+ if (value instanceof Date || isDate(value)) {
3672
+ index = serializeDate(buffer, key, value, index);
3673
+ }
3674
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3675
+ index = serializeBuffer(buffer, key, value, index);
3676
+ }
3677
+ else if (value instanceof RegExp || isRegExp(value)) {
3678
+ index = serializeRegExp(buffer, key, value, index);
3679
+ }
3680
+ else {
3681
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3682
+ }
3645
3683
  }
3646
- else if (value._bsontype === 'Double') {
3647
- index = serializeDouble(buffer, key, value, index);
3684
+ else if (type === 'object') {
3685
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3686
+ throw new BSONVersionError();
3687
+ }
3688
+ else if (value._bsontype === 'ObjectId') {
3689
+ index = serializeObjectId(buffer, key, value, index);
3690
+ }
3691
+ else if (value._bsontype === 'Decimal128') {
3692
+ index = serializeDecimal128(buffer, key, value, index);
3693
+ }
3694
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3695
+ index = serializeLong(buffer, key, value, index);
3696
+ }
3697
+ else if (value._bsontype === 'Double') {
3698
+ index = serializeDouble(buffer, key, value, index);
3699
+ }
3700
+ else if (value._bsontype === 'Code') {
3701
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3702
+ }
3703
+ else if (value._bsontype === 'Binary') {
3704
+ index = serializeBinary(buffer, key, value, index);
3705
+ }
3706
+ else if (value._bsontype === 'BSONSymbol') {
3707
+ index = serializeSymbol(buffer, key, value, index);
3708
+ }
3709
+ else if (value._bsontype === 'DBRef') {
3710
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3711
+ }
3712
+ else if (value._bsontype === 'BSONRegExp') {
3713
+ index = serializeBSONRegExp(buffer, key, value, index);
3714
+ }
3715
+ else if (value._bsontype === 'Int32') {
3716
+ index = serializeInt32(buffer, key, value, index);
3717
+ }
3718
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3719
+ index = serializeMinMax(buffer, key, value, index);
3720
+ }
3721
+ else if (typeof value._bsontype !== 'undefined') {
3722
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3723
+ }
3648
3724
  }
3649
- else if (typeof value === 'function' && serializeFunctions) {
3725
+ else if (type === 'function' && serializeFunctions) {
3650
3726
  index = serializeFunction(buffer, key, value, index);
3651
3727
  }
3652
- else if (value._bsontype === 'Code') {
3653
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3654
- }
3655
- else if (value._bsontype === 'Binary') {
3656
- index = serializeBinary(buffer, key, value, index);
3657
- }
3658
- else if (value._bsontype === 'BSONSymbol') {
3659
- index = serializeSymbol(buffer, key, value, index);
3660
- }
3661
- else if (value._bsontype === 'DBRef') {
3662
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3663
- }
3664
- else if (value._bsontype === 'BSONRegExp') {
3665
- index = serializeBSONRegExp(buffer, key, value, index);
3666
- }
3667
- else if (value._bsontype === 'Int32') {
3668
- index = serializeInt32(buffer, key, value, index);
3669
- }
3670
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3671
- index = serializeMinMax(buffer, key, value, index);
3672
- }
3673
- else if (typeof value._bsontype !== 'undefined') {
3674
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3675
- }
3676
3728
  }
3677
3729
  }
3678
3730
  else if (object instanceof Map || isMap(object)) {
@@ -3702,7 +3754,14 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3702
3754
  }
3703
3755
  }
3704
3756
  }
3705
- if (type === 'string') {
3757
+ if (value === undefined) {
3758
+ if (ignoreUndefined === false)
3759
+ index = serializeNull(buffer, key, value, index);
3760
+ }
3761
+ else if (value === null) {
3762
+ index = serializeNull(buffer, key, value, index);
3763
+ }
3764
+ else if (type === 'string') {
3706
3765
  index = serializeString(buffer, key, value, index);
3707
3766
  }
3708
3767
  else if (type === 'number') {
@@ -3714,64 +3773,64 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3714
3773
  else if (type === 'boolean') {
3715
3774
  index = serializeBoolean(buffer, key, value, index);
3716
3775
  }
3717
- else if (value instanceof Date || isDate(value)) {
3718
- index = serializeDate(buffer, key, value, index);
3719
- }
3720
- else if (value === null || (value === undefined && ignoreUndefined === false)) {
3721
- index = serializeNull(buffer, key, value, index);
3722
- }
3723
- else if (isUint8Array(value)) {
3724
- index = serializeBuffer(buffer, key, value, index);
3725
- }
3726
- else if (value instanceof RegExp || isRegExp(value)) {
3727
- index = serializeRegExp(buffer, key, value, index);
3728
- }
3729
3776
  else if (type === 'object' && value._bsontype == null) {
3730
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3731
- }
3732
- else if (typeof value === 'object' &&
3733
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3734
- throw new BSONVersionError();
3735
- }
3736
- else if (value._bsontype === 'ObjectId') {
3737
- index = serializeObjectId(buffer, key, value, index);
3738
- }
3739
- else if (type === 'object' && value._bsontype === 'Decimal128') {
3740
- index = serializeDecimal128(buffer, key, value, index);
3741
- }
3742
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3743
- index = serializeLong(buffer, key, value, index);
3744
- }
3745
- else if (value._bsontype === 'Double') {
3746
- index = serializeDouble(buffer, key, value, index);
3777
+ if (value instanceof Date || isDate(value)) {
3778
+ index = serializeDate(buffer, key, value, index);
3779
+ }
3780
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3781
+ index = serializeBuffer(buffer, key, value, index);
3782
+ }
3783
+ else if (value instanceof RegExp || isRegExp(value)) {
3784
+ index = serializeRegExp(buffer, key, value, index);
3785
+ }
3786
+ else {
3787
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3788
+ }
3747
3789
  }
3748
- else if (value._bsontype === 'Code') {
3749
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3790
+ else if (type === 'object') {
3791
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3792
+ throw new BSONVersionError();
3793
+ }
3794
+ else if (value._bsontype === 'ObjectId') {
3795
+ index = serializeObjectId(buffer, key, value, index);
3796
+ }
3797
+ else if (value._bsontype === 'Decimal128') {
3798
+ index = serializeDecimal128(buffer, key, value, index);
3799
+ }
3800
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3801
+ index = serializeLong(buffer, key, value, index);
3802
+ }
3803
+ else if (value._bsontype === 'Double') {
3804
+ index = serializeDouble(buffer, key, value, index);
3805
+ }
3806
+ else if (value._bsontype === 'Code') {
3807
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3808
+ }
3809
+ else if (value._bsontype === 'Binary') {
3810
+ index = serializeBinary(buffer, key, value, index);
3811
+ }
3812
+ else if (value._bsontype === 'BSONSymbol') {
3813
+ index = serializeSymbol(buffer, key, value, index);
3814
+ }
3815
+ else if (value._bsontype === 'DBRef') {
3816
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3817
+ }
3818
+ else if (value._bsontype === 'BSONRegExp') {
3819
+ index = serializeBSONRegExp(buffer, key, value, index);
3820
+ }
3821
+ else if (value._bsontype === 'Int32') {
3822
+ index = serializeInt32(buffer, key, value, index);
3823
+ }
3824
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3825
+ index = serializeMinMax(buffer, key, value, index);
3826
+ }
3827
+ else if (typeof value._bsontype !== 'undefined') {
3828
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3829
+ }
3750
3830
  }
3751
- else if (typeof value === 'function' && serializeFunctions) {
3831
+ else if (type === 'function' && serializeFunctions) {
3752
3832
  index = serializeFunction(buffer, key, value, index);
3753
3833
  }
3754
- else if (value._bsontype === 'Binary') {
3755
- index = serializeBinary(buffer, key, value, index);
3756
- }
3757
- else if (value._bsontype === 'BSONSymbol') {
3758
- index = serializeSymbol(buffer, key, value, index);
3759
- }
3760
- else if (value._bsontype === 'DBRef') {
3761
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3762
- }
3763
- else if (value._bsontype === 'BSONRegExp') {
3764
- index = serializeBSONRegExp(buffer, key, value, index);
3765
- }
3766
- else if (value._bsontype === 'Int32') {
3767
- index = serializeInt32(buffer, key, value, index);
3768
- }
3769
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3770
- index = serializeMinMax(buffer, key, value, index);
3771
- }
3772
- else if (typeof value._bsontype !== 'undefined') {
3773
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3774
- }
3775
3834
  }
3776
3835
  }
3777
3836
  else {
@@ -3800,7 +3859,14 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3800
3859
  }
3801
3860
  }
3802
3861
  }
3803
- if (type === 'string') {
3862
+ if (value === undefined) {
3863
+ if (ignoreUndefined === false)
3864
+ index = serializeNull(buffer, key, value, index);
3865
+ }
3866
+ else if (value === null) {
3867
+ index = serializeNull(buffer, key, value, index);
3868
+ }
3869
+ else if (type === 'string') {
3804
3870
  index = serializeString(buffer, key, value, index);
3805
3871
  }
3806
3872
  else if (type === 'number') {
@@ -3812,68 +3878,64 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3812
3878
  else if (type === 'boolean') {
3813
3879
  index = serializeBoolean(buffer, key, value, index);
3814
3880
  }
3815
- else if (value instanceof Date || isDate(value)) {
3816
- index = serializeDate(buffer, key, value, index);
3817
- }
3818
- else if (value === undefined) {
3819
- if (ignoreUndefined === false)
3820
- index = serializeNull(buffer, key, value, index);
3821
- }
3822
- else if (value === null) {
3823
- index = serializeNull(buffer, key, value, index);
3824
- }
3825
- else if (isUint8Array(value)) {
3826
- index = serializeBuffer(buffer, key, value, index);
3827
- }
3828
- else if (value instanceof RegExp || isRegExp(value)) {
3829
- index = serializeRegExp(buffer, key, value, index);
3830
- }
3831
3881
  else if (type === 'object' && value._bsontype == null) {
3832
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3833
- }
3834
- else if (typeof value === 'object' &&
3835
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3836
- throw new BSONVersionError();
3837
- }
3838
- else if (value._bsontype === 'ObjectId') {
3839
- index = serializeObjectId(buffer, key, value, index);
3840
- }
3841
- else if (type === 'object' && value._bsontype === 'Decimal128') {
3842
- index = serializeDecimal128(buffer, key, value, index);
3843
- }
3844
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3845
- index = serializeLong(buffer, key, value, index);
3846
- }
3847
- else if (value._bsontype === 'Double') {
3848
- index = serializeDouble(buffer, key, value, index);
3882
+ if (value instanceof Date || isDate(value)) {
3883
+ index = serializeDate(buffer, key, value, index);
3884
+ }
3885
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3886
+ index = serializeBuffer(buffer, key, value, index);
3887
+ }
3888
+ else if (value instanceof RegExp || isRegExp(value)) {
3889
+ index = serializeRegExp(buffer, key, value, index);
3890
+ }
3891
+ else {
3892
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3893
+ }
3849
3894
  }
3850
- else if (value._bsontype === 'Code') {
3851
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3895
+ else if (type === 'object') {
3896
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3897
+ throw new BSONVersionError();
3898
+ }
3899
+ else if (value._bsontype === 'ObjectId') {
3900
+ index = serializeObjectId(buffer, key, value, index);
3901
+ }
3902
+ else if (value._bsontype === 'Decimal128') {
3903
+ index = serializeDecimal128(buffer, key, value, index);
3904
+ }
3905
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3906
+ index = serializeLong(buffer, key, value, index);
3907
+ }
3908
+ else if (value._bsontype === 'Double') {
3909
+ index = serializeDouble(buffer, key, value, index);
3910
+ }
3911
+ else if (value._bsontype === 'Code') {
3912
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3913
+ }
3914
+ else if (value._bsontype === 'Binary') {
3915
+ index = serializeBinary(buffer, key, value, index);
3916
+ }
3917
+ else if (value._bsontype === 'BSONSymbol') {
3918
+ index = serializeSymbol(buffer, key, value, index);
3919
+ }
3920
+ else if (value._bsontype === 'DBRef') {
3921
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3922
+ }
3923
+ else if (value._bsontype === 'BSONRegExp') {
3924
+ index = serializeBSONRegExp(buffer, key, value, index);
3925
+ }
3926
+ else if (value._bsontype === 'Int32') {
3927
+ index = serializeInt32(buffer, key, value, index);
3928
+ }
3929
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3930
+ index = serializeMinMax(buffer, key, value, index);
3931
+ }
3932
+ else if (typeof value._bsontype !== 'undefined') {
3933
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3934
+ }
3852
3935
  }
3853
- else if (typeof value === 'function' && serializeFunctions) {
3936
+ else if (type === 'function' && serializeFunctions) {
3854
3937
  index = serializeFunction(buffer, key, value, index);
3855
3938
  }
3856
- else if (value._bsontype === 'Binary') {
3857
- index = serializeBinary(buffer, key, value, index);
3858
- }
3859
- else if (value._bsontype === 'BSONSymbol') {
3860
- index = serializeSymbol(buffer, key, value, index);
3861
- }
3862
- else if (value._bsontype === 'DBRef') {
3863
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3864
- }
3865
- else if (value._bsontype === 'BSONRegExp') {
3866
- index = serializeBSONRegExp(buffer, key, value, index);
3867
- }
3868
- else if (value._bsontype === 'Int32') {
3869
- index = serializeInt32(buffer, key, value, index);
3870
- }
3871
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3872
- index = serializeMinMax(buffer, key, value, index);
3873
- }
3874
- else if (typeof value._bsontype !== 'undefined') {
3875
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3876
- }
3877
3939
  }
3878
3940
  }
3879
3941
  path.delete(object);
@@ -4125,7 +4187,7 @@ function serializeDocument(doc, options) {
4125
4187
  else if (doc != null &&
4126
4188
  typeof doc === 'object' &&
4127
4189
  typeof doc._bsontype === 'string' &&
4128
- doc[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
4190
+ doc[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
4129
4191
  throw new BSONVersionError();
4130
4192
  }
4131
4193
  else if (isBSONType(doc)) {