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.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
  }
@@ -2279,7 +2306,7 @@ const NumberUtils = {
2279
2306
  return 4;
2280
2307
  },
2281
2308
  setBigInt64LE(destination, offset, value) {
2282
- const mask32bits = BigInt(4294967295);
2309
+ const mask32bits = BigInt(0xffff_ffff);
2283
2310
  let lo = Number(value & mask32bits);
2284
2311
  destination[offset] = lo;
2285
2312
  lo >>= 8;
@@ -2325,7 +2352,6 @@ const NumberUtils = {
2325
2352
  }
2326
2353
  };
2327
2354
 
2328
- const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
2329
2355
  let PROCESS_UNIQUE = null;
2330
2356
  class ObjectId extends BSONValue {
2331
2357
  get _bsontype() {
@@ -2355,7 +2381,7 @@ class ObjectId extends BSONValue {
2355
2381
  this.buffer = ByteUtils.toLocalBufferType(workingId);
2356
2382
  }
2357
2383
  else if (typeof workingId === 'string') {
2358
- if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
2384
+ if (ObjectId.validateHexString(workingId)) {
2359
2385
  this.buffer = ByteUtils.fromHex(workingId);
2360
2386
  }
2361
2387
  else {
@@ -2378,6 +2404,20 @@ class ObjectId extends BSONValue {
2378
2404
  this.__id = ByteUtils.toHex(value);
2379
2405
  }
2380
2406
  }
2407
+ static validateHexString(string) {
2408
+ if (string?.length !== 24)
2409
+ return false;
2410
+ for (let i = 0; i < 24; i++) {
2411
+ const char = string.charCodeAt(i);
2412
+ if ((char >= 48 && char <= 57) ||
2413
+ (char >= 97 && char <= 102) ||
2414
+ (char >= 65 && char <= 70)) {
2415
+ continue;
2416
+ }
2417
+ return false;
2418
+ }
2419
+ return true;
2420
+ }
2381
2421
  toHexString() {
2382
2422
  if (ObjectId.cacheHexString && this.__id) {
2383
2423
  return this.__id;
@@ -2490,6 +2530,8 @@ class ObjectId extends BSONValue {
2490
2530
  static isValid(id) {
2491
2531
  if (id == null)
2492
2532
  return false;
2533
+ if (typeof id === 'string')
2534
+ return ObjectId.validateHexString(id);
2493
2535
  try {
2494
2536
  new ObjectId(id);
2495
2537
  return true;
@@ -2560,7 +2602,7 @@ function calculateElement(name, value, serializeFunctions = false, isArray = fal
2560
2602
  case 'object':
2561
2603
  if (value != null &&
2562
2604
  typeof value._bsontype === 'string' &&
2563
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
2605
+ value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
2564
2606
  throw new BSONVersionError();
2565
2607
  }
2566
2608
  else if (value == null || value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
@@ -2764,6 +2806,12 @@ class Timestamp extends LongWithoutOverridesClass {
2764
2806
  get _bsontype() {
2765
2807
  return 'Timestamp';
2766
2808
  }
2809
+ get i() {
2810
+ return this.low >>> 0;
2811
+ }
2812
+ get t() {
2813
+ return this.high >>> 0;
2814
+ }
2767
2815
  constructor(low) {
2768
2816
  if (low == null) {
2769
2817
  super(0, 0, true);
@@ -2789,10 +2837,10 @@ class Timestamp extends LongWithoutOverridesClass {
2789
2837
  if (i < 0 || Number.isNaN(i)) {
2790
2838
  throw new BSONError('Timestamp constructed from { t, i } must provide a positive i');
2791
2839
  }
2792
- if (t > 4294967295) {
2840
+ if (t > 0xffff_ffff) {
2793
2841
  throw new BSONError('Timestamp constructed from { t, i } must provide t equal or less than uint32 max');
2794
2842
  }
2795
- if (i > 4294967295) {
2843
+ if (i > 0xffff_ffff) {
2796
2844
  throw new BSONError('Timestamp constructed from { t, i } must provide i equal or less than uint32 max');
2797
2845
  }
2798
2846
  super(i, t, true);
@@ -2819,7 +2867,7 @@ class Timestamp extends LongWithoutOverridesClass {
2819
2867
  return new Timestamp(Long.fromString(str, true, optRadix));
2820
2868
  }
2821
2869
  toExtendedJSON() {
2822
- return { $timestamp: { t: this.high >>> 0, i: this.low >>> 0 } };
2870
+ return { $timestamp: { t: this.t, i: this.i } };
2823
2871
  }
2824
2872
  static fromExtendedJSON(doc) {
2825
2873
  const i = Long.isLong(doc.$timestamp.i)
@@ -2832,8 +2880,8 @@ class Timestamp extends LongWithoutOverridesClass {
2832
2880
  }
2833
2881
  inspect(depth, options, inspect) {
2834
2882
  inspect ??= defaultInspect;
2835
- const t = inspect(this.high >>> 0, options);
2836
- const i = inspect(this.low >>> 0, options);
2883
+ const t = inspect(this.t, options);
2884
+ const i = inspect(this.i, options);
2837
2885
  return `new Timestamp({ t: ${t}, i: ${i} })`;
2838
2886
  }
2839
2887
  }
@@ -3602,79 +3650,83 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3602
3650
  if (typeof value?.toBSON === 'function') {
3603
3651
  value = value.toBSON();
3604
3652
  }
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) {
3653
+ const type = typeof value;
3654
+ if (value === undefined) {
3621
3655
  index = serializeNull(buffer, key, value, index);
3622
3656
  }
3623
3657
  else if (value === null) {
3624
3658
  index = serializeNull(buffer, key, value, index);
3625
3659
  }
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);
3660
+ else if (type === 'string') {
3661
+ index = serializeString(buffer, key, value, index);
3634
3662
  }
3635
- else if (typeof value === 'object' &&
3636
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3637
- throw new BSONVersionError();
3663
+ else if (type === 'number') {
3664
+ index = serializeNumber(buffer, key, value, index);
3638
3665
  }
3639
- else if (value._bsontype === 'ObjectId') {
3640
- index = serializeObjectId(buffer, key, value, index);
3666
+ else if (type === 'bigint') {
3667
+ index = serializeBigInt(buffer, key, value, index);
3641
3668
  }
3642
- else if (value._bsontype === 'Decimal128') {
3643
- index = serializeDecimal128(buffer, key, value, index);
3669
+ else if (type === 'boolean') {
3670
+ index = serializeBoolean(buffer, key, value, index);
3644
3671
  }
3645
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3646
- index = serializeLong(buffer, key, value, index);
3672
+ else if (type === 'object' && value._bsontype == null) {
3673
+ if (value instanceof Date || isDate(value)) {
3674
+ index = serializeDate(buffer, key, value, index);
3675
+ }
3676
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3677
+ index = serializeBuffer(buffer, key, value, index);
3678
+ }
3679
+ else if (value instanceof RegExp || isRegExp(value)) {
3680
+ index = serializeRegExp(buffer, key, value, index);
3681
+ }
3682
+ else {
3683
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3684
+ }
3647
3685
  }
3648
- else if (value._bsontype === 'Double') {
3649
- index = serializeDouble(buffer, key, value, index);
3686
+ else if (type === 'object') {
3687
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3688
+ throw new BSONVersionError();
3689
+ }
3690
+ else if (value._bsontype === 'ObjectId') {
3691
+ index = serializeObjectId(buffer, key, value, index);
3692
+ }
3693
+ else if (value._bsontype === 'Decimal128') {
3694
+ index = serializeDecimal128(buffer, key, value, index);
3695
+ }
3696
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3697
+ index = serializeLong(buffer, key, value, index);
3698
+ }
3699
+ else if (value._bsontype === 'Double') {
3700
+ index = serializeDouble(buffer, key, value, index);
3701
+ }
3702
+ else if (value._bsontype === 'Code') {
3703
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3704
+ }
3705
+ else if (value._bsontype === 'Binary') {
3706
+ index = serializeBinary(buffer, key, value, index);
3707
+ }
3708
+ else if (value._bsontype === 'BSONSymbol') {
3709
+ index = serializeSymbol(buffer, key, value, index);
3710
+ }
3711
+ else if (value._bsontype === 'DBRef') {
3712
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3713
+ }
3714
+ else if (value._bsontype === 'BSONRegExp') {
3715
+ index = serializeBSONRegExp(buffer, key, value, index);
3716
+ }
3717
+ else if (value._bsontype === 'Int32') {
3718
+ index = serializeInt32(buffer, key, value, index);
3719
+ }
3720
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3721
+ index = serializeMinMax(buffer, key, value, index);
3722
+ }
3723
+ else if (typeof value._bsontype !== 'undefined') {
3724
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3725
+ }
3650
3726
  }
3651
- else if (typeof value === 'function' && serializeFunctions) {
3727
+ else if (type === 'function' && serializeFunctions) {
3652
3728
  index = serializeFunction(buffer, key, value, index);
3653
3729
  }
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
3730
  }
3679
3731
  }
3680
3732
  else if (object instanceof Map || isMap(object)) {
@@ -3704,7 +3756,14 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3704
3756
  }
3705
3757
  }
3706
3758
  }
3707
- if (type === 'string') {
3759
+ if (value === undefined) {
3760
+ if (ignoreUndefined === false)
3761
+ index = serializeNull(buffer, key, value, index);
3762
+ }
3763
+ else if (value === null) {
3764
+ index = serializeNull(buffer, key, value, index);
3765
+ }
3766
+ else if (type === 'string') {
3708
3767
  index = serializeString(buffer, key, value, index);
3709
3768
  }
3710
3769
  else if (type === 'number') {
@@ -3716,64 +3775,64 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3716
3775
  else if (type === 'boolean') {
3717
3776
  index = serializeBoolean(buffer, key, value, index);
3718
3777
  }
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
3778
  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);
3779
+ if (value instanceof Date || isDate(value)) {
3780
+ index = serializeDate(buffer, key, value, index);
3781
+ }
3782
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3783
+ index = serializeBuffer(buffer, key, value, index);
3784
+ }
3785
+ else if (value instanceof RegExp || isRegExp(value)) {
3786
+ index = serializeRegExp(buffer, key, value, index);
3787
+ }
3788
+ else {
3789
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3790
+ }
3749
3791
  }
3750
- else if (value._bsontype === 'Code') {
3751
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3792
+ else if (type === 'object') {
3793
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3794
+ throw new BSONVersionError();
3795
+ }
3796
+ else if (value._bsontype === 'ObjectId') {
3797
+ index = serializeObjectId(buffer, key, value, index);
3798
+ }
3799
+ else if (value._bsontype === 'Decimal128') {
3800
+ index = serializeDecimal128(buffer, key, value, index);
3801
+ }
3802
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3803
+ index = serializeLong(buffer, key, value, index);
3804
+ }
3805
+ else if (value._bsontype === 'Double') {
3806
+ index = serializeDouble(buffer, key, value, index);
3807
+ }
3808
+ else if (value._bsontype === 'Code') {
3809
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3810
+ }
3811
+ else if (value._bsontype === 'Binary') {
3812
+ index = serializeBinary(buffer, key, value, index);
3813
+ }
3814
+ else if (value._bsontype === 'BSONSymbol') {
3815
+ index = serializeSymbol(buffer, key, value, index);
3816
+ }
3817
+ else if (value._bsontype === 'DBRef') {
3818
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3819
+ }
3820
+ else if (value._bsontype === 'BSONRegExp') {
3821
+ index = serializeBSONRegExp(buffer, key, value, index);
3822
+ }
3823
+ else if (value._bsontype === 'Int32') {
3824
+ index = serializeInt32(buffer, key, value, index);
3825
+ }
3826
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3827
+ index = serializeMinMax(buffer, key, value, index);
3828
+ }
3829
+ else if (typeof value._bsontype !== 'undefined') {
3830
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3831
+ }
3752
3832
  }
3753
- else if (typeof value === 'function' && serializeFunctions) {
3833
+ else if (type === 'function' && serializeFunctions) {
3754
3834
  index = serializeFunction(buffer, key, value, index);
3755
3835
  }
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
3836
  }
3778
3837
  }
3779
3838
  else {
@@ -3802,7 +3861,14 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3802
3861
  }
3803
3862
  }
3804
3863
  }
3805
- if (type === 'string') {
3864
+ if (value === undefined) {
3865
+ if (ignoreUndefined === false)
3866
+ index = serializeNull(buffer, key, value, index);
3867
+ }
3868
+ else if (value === null) {
3869
+ index = serializeNull(buffer, key, value, index);
3870
+ }
3871
+ else if (type === 'string') {
3806
3872
  index = serializeString(buffer, key, value, index);
3807
3873
  }
3808
3874
  else if (type === 'number') {
@@ -3814,68 +3880,64 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3814
3880
  else if (type === 'boolean') {
3815
3881
  index = serializeBoolean(buffer, key, value, index);
3816
3882
  }
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
3883
  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);
3884
+ if (value instanceof Date || isDate(value)) {
3885
+ index = serializeDate(buffer, key, value, index);
3886
+ }
3887
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3888
+ index = serializeBuffer(buffer, key, value, index);
3889
+ }
3890
+ else if (value instanceof RegExp || isRegExp(value)) {
3891
+ index = serializeRegExp(buffer, key, value, index);
3892
+ }
3893
+ else {
3894
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3895
+ }
3851
3896
  }
3852
- else if (value._bsontype === 'Code') {
3853
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3897
+ else if (type === 'object') {
3898
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3899
+ throw new BSONVersionError();
3900
+ }
3901
+ else if (value._bsontype === 'ObjectId') {
3902
+ index = serializeObjectId(buffer, key, value, index);
3903
+ }
3904
+ else if (value._bsontype === 'Decimal128') {
3905
+ index = serializeDecimal128(buffer, key, value, index);
3906
+ }
3907
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3908
+ index = serializeLong(buffer, key, value, index);
3909
+ }
3910
+ else if (value._bsontype === 'Double') {
3911
+ index = serializeDouble(buffer, key, value, index);
3912
+ }
3913
+ else if (value._bsontype === 'Code') {
3914
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3915
+ }
3916
+ else if (value._bsontype === 'Binary') {
3917
+ index = serializeBinary(buffer, key, value, index);
3918
+ }
3919
+ else if (value._bsontype === 'BSONSymbol') {
3920
+ index = serializeSymbol(buffer, key, value, index);
3921
+ }
3922
+ else if (value._bsontype === 'DBRef') {
3923
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3924
+ }
3925
+ else if (value._bsontype === 'BSONRegExp') {
3926
+ index = serializeBSONRegExp(buffer, key, value, index);
3927
+ }
3928
+ else if (value._bsontype === 'Int32') {
3929
+ index = serializeInt32(buffer, key, value, index);
3930
+ }
3931
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3932
+ index = serializeMinMax(buffer, key, value, index);
3933
+ }
3934
+ else if (typeof value._bsontype !== 'undefined') {
3935
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3936
+ }
3854
3937
  }
3855
- else if (typeof value === 'function' && serializeFunctions) {
3938
+ else if (type === 'function' && serializeFunctions) {
3856
3939
  index = serializeFunction(buffer, key, value, index);
3857
3940
  }
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
3941
  }
3880
3942
  }
3881
3943
  path.delete(object);
@@ -4127,7 +4189,7 @@ function serializeDocument(doc, options) {
4127
4189
  else if (doc != null &&
4128
4190
  typeof doc === 'object' &&
4129
4191
  typeof doc._bsontype === 'string' &&
4130
- doc[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
4192
+ doc[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
4131
4193
  throw new BSONVersionError();
4132
4194
  }
4133
4195
  else if (isBSONType(doc)) {