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/bson.d.ts CHANGED
@@ -151,6 +151,10 @@ declare namespace BSON {
151
151
  }
152
152
  export { BSON }
153
153
 
154
+ /* Excluded from this release type: BSON_MAJOR_VERSION */
155
+
156
+ /* Excluded from this release type: BSON_VERSION_SYMBOL */
157
+
154
158
  /**
155
159
  * @public
156
160
  * @experimental
@@ -311,6 +315,7 @@ export declare type BSONType = (typeof BSONType)[keyof typeof BSONType];
311
315
  export declare abstract class BSONValue {
312
316
  /** @public */
313
317
  abstract get _bsontype(): string;
318
+ /* Excluded from this release type: [BSON_VERSION_SYMBOL] */
314
319
  /**
315
320
  * @public
316
321
  * Prints a human-readable string of BSON value information
@@ -1285,6 +1290,7 @@ export declare class ObjectId extends BSONValue {
1285
1290
  */
1286
1291
  get id(): Uint8Array;
1287
1292
  set id(value: Uint8Array);
1293
+ /* Excluded from this release type: validateHexString */
1288
1294
  /** Returns the ObjectId id as a 24 lowercase character hex string representation */
1289
1295
  toHexString(): string;
1290
1296
  /* Excluded from this release type: getInc */
@@ -1472,10 +1478,20 @@ declare function stringify(value: any, replacer?: (number | string)[] | ((this:
1472
1478
  /**
1473
1479
  * @public
1474
1480
  * @category BSONType
1481
+ *
1482
+ * A special type for _internal_ MongoDB use and is **not** associated with the regular Date type.
1475
1483
  */
1476
1484
  export declare class Timestamp extends LongWithoutOverridesClass {
1477
1485
  get _bsontype(): 'Timestamp';
1478
1486
  static readonly MAX_VALUE: Long;
1487
+ /**
1488
+ * An incrementing ordinal for operations within a given second.
1489
+ */
1490
+ get i(): number;
1491
+ /**
1492
+ * A `time_t` value measuring seconds since the Unix epoch
1493
+ */
1494
+ get t(): number;
1479
1495
  /**
1480
1496
  * @param int - A 64-bit bigint representing the Timestamp.
1481
1497
  */
@@ -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) {
@@ -1589,7 +1616,7 @@ class Decimal128 extends BSONValue {
1589
1616
  if (typeof bytes === 'string') {
1590
1617
  this.bytes = Decimal128.fromString(bytes).bytes;
1591
1618
  }
1592
- else if (isUint8Array(bytes)) {
1619
+ else if (bytes instanceof Uint8Array || isUint8Array(bytes)) {
1593
1620
  if (bytes.byteLength !== 16) {
1594
1621
  throw new BSONError('Decimal128 must take a Buffer of 16 bytes');
1595
1622
  }
@@ -2286,7 +2313,7 @@ const NumberUtils = {
2286
2313
  return 4;
2287
2314
  },
2288
2315
  setBigInt64LE(destination, offset, value) {
2289
- const mask32bits = BigInt(4294967295);
2316
+ const mask32bits = BigInt(0xffff_ffff);
2290
2317
  let lo = Number(value & mask32bits);
2291
2318
  destination[offset] = lo;
2292
2319
  lo >>= 8;
@@ -2332,7 +2359,6 @@ const NumberUtils = {
2332
2359
  }
2333
2360
  };
2334
2361
 
2335
- const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
2336
2362
  let PROCESS_UNIQUE = null;
2337
2363
  class ObjectId extends BSONValue {
2338
2364
  get _bsontype() {
@@ -2362,7 +2388,7 @@ class ObjectId extends BSONValue {
2362
2388
  this.buffer = ByteUtils.toLocalBufferType(workingId);
2363
2389
  }
2364
2390
  else if (typeof workingId === 'string') {
2365
- if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
2391
+ if (ObjectId.validateHexString(workingId)) {
2366
2392
  this.buffer = ByteUtils.fromHex(workingId);
2367
2393
  }
2368
2394
  else {
@@ -2385,6 +2411,20 @@ class ObjectId extends BSONValue {
2385
2411
  this.__id = ByteUtils.toHex(value);
2386
2412
  }
2387
2413
  }
2414
+ static validateHexString(string) {
2415
+ if (string?.length !== 24)
2416
+ return false;
2417
+ for (let i = 0; i < 24; i++) {
2418
+ const char = string.charCodeAt(i);
2419
+ if ((char >= 48 && char <= 57) ||
2420
+ (char >= 97 && char <= 102) ||
2421
+ (char >= 65 && char <= 70)) {
2422
+ continue;
2423
+ }
2424
+ return false;
2425
+ }
2426
+ return true;
2427
+ }
2388
2428
  toHexString() {
2389
2429
  if (ObjectId.cacheHexString && this.__id) {
2390
2430
  return this.__id;
@@ -2497,6 +2537,8 @@ class ObjectId extends BSONValue {
2497
2537
  static isValid(id) {
2498
2538
  if (id == null)
2499
2539
  return false;
2540
+ if (typeof id === 'string')
2541
+ return ObjectId.validateHexString(id);
2500
2542
  try {
2501
2543
  new ObjectId(id);
2502
2544
  return true;
@@ -2567,7 +2609,7 @@ function calculateElement(name, value, serializeFunctions = false, isArray = fal
2567
2609
  case 'object':
2568
2610
  if (value != null &&
2569
2611
  typeof value._bsontype === 'string' &&
2570
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
2612
+ value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
2571
2613
  throw new BSONVersionError();
2572
2614
  }
2573
2615
  else if (value == null || value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
@@ -2771,6 +2813,12 @@ class Timestamp extends LongWithoutOverridesClass {
2771
2813
  get _bsontype() {
2772
2814
  return 'Timestamp';
2773
2815
  }
2816
+ get i() {
2817
+ return this.low >>> 0;
2818
+ }
2819
+ get t() {
2820
+ return this.high >>> 0;
2821
+ }
2774
2822
  constructor(low) {
2775
2823
  if (low == null) {
2776
2824
  super(0, 0, true);
@@ -2796,10 +2844,10 @@ class Timestamp extends LongWithoutOverridesClass {
2796
2844
  if (i < 0 || Number.isNaN(i)) {
2797
2845
  throw new BSONError('Timestamp constructed from { t, i } must provide a positive i');
2798
2846
  }
2799
- if (t > 4294967295) {
2847
+ if (t > 0xffff_ffff) {
2800
2848
  throw new BSONError('Timestamp constructed from { t, i } must provide t equal or less than uint32 max');
2801
2849
  }
2802
- if (i > 4294967295) {
2850
+ if (i > 0xffff_ffff) {
2803
2851
  throw new BSONError('Timestamp constructed from { t, i } must provide i equal or less than uint32 max');
2804
2852
  }
2805
2853
  super(i, t, true);
@@ -2826,7 +2874,7 @@ class Timestamp extends LongWithoutOverridesClass {
2826
2874
  return new Timestamp(Long.fromString(str, true, optRadix));
2827
2875
  }
2828
2876
  toExtendedJSON() {
2829
- return { $timestamp: { t: this.high >>> 0, i: this.low >>> 0 } };
2877
+ return { $timestamp: { t: this.t, i: this.i } };
2830
2878
  }
2831
2879
  static fromExtendedJSON(doc) {
2832
2880
  const i = Long.isLong(doc.$timestamp.i)
@@ -2839,8 +2887,8 @@ class Timestamp extends LongWithoutOverridesClass {
2839
2887
  }
2840
2888
  inspect(depth, options, inspect) {
2841
2889
  inspect ??= defaultInspect;
2842
- const t = inspect(this.high >>> 0, options);
2843
- const i = inspect(this.low >>> 0, options);
2890
+ const t = inspect(this.t, options);
2891
+ const i = inspect(this.i, options);
2844
2892
  return `new Timestamp({ t: ${t}, i: ${i} })`;
2845
2893
  }
2846
2894
  }
@@ -3609,79 +3657,83 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3609
3657
  if (typeof value?.toBSON === 'function') {
3610
3658
  value = value.toBSON();
3611
3659
  }
3612
- if (typeof value === 'string') {
3613
- index = serializeString(buffer, key, value, index);
3614
- }
3615
- else if (typeof value === 'number') {
3616
- index = serializeNumber(buffer, key, value, index);
3617
- }
3618
- else if (typeof value === 'bigint') {
3619
- index = serializeBigInt(buffer, key, value, index);
3620
- }
3621
- else if (typeof value === 'boolean') {
3622
- index = serializeBoolean(buffer, key, value, index);
3623
- }
3624
- else if (value instanceof Date || isDate(value)) {
3625
- index = serializeDate(buffer, key, value, index);
3626
- }
3627
- else if (value === undefined) {
3660
+ const type = typeof value;
3661
+ if (value === undefined) {
3628
3662
  index = serializeNull(buffer, key, value, index);
3629
3663
  }
3630
3664
  else if (value === null) {
3631
3665
  index = serializeNull(buffer, key, value, index);
3632
3666
  }
3633
- else if (isUint8Array(value)) {
3634
- index = serializeBuffer(buffer, key, value, index);
3635
- }
3636
- else if (value instanceof RegExp || isRegExp(value)) {
3637
- index = serializeRegExp(buffer, key, value, index);
3638
- }
3639
- else if (typeof value === 'object' && value._bsontype == null) {
3640
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3667
+ else if (type === 'string') {
3668
+ index = serializeString(buffer, key, value, index);
3641
3669
  }
3642
- else if (typeof value === 'object' &&
3643
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3644
- throw new BSONVersionError();
3670
+ else if (type === 'number') {
3671
+ index = serializeNumber(buffer, key, value, index);
3645
3672
  }
3646
- else if (value._bsontype === 'ObjectId') {
3647
- index = serializeObjectId(buffer, key, value, index);
3673
+ else if (type === 'bigint') {
3674
+ index = serializeBigInt(buffer, key, value, index);
3648
3675
  }
3649
- else if (value._bsontype === 'Decimal128') {
3650
- index = serializeDecimal128(buffer, key, value, index);
3676
+ else if (type === 'boolean') {
3677
+ index = serializeBoolean(buffer, key, value, index);
3651
3678
  }
3652
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3653
- index = serializeLong(buffer, key, value, index);
3679
+ else if (type === 'object' && value._bsontype == null) {
3680
+ if (value instanceof Date || isDate(value)) {
3681
+ index = serializeDate(buffer, key, value, index);
3682
+ }
3683
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3684
+ index = serializeBuffer(buffer, key, value, index);
3685
+ }
3686
+ else if (value instanceof RegExp || isRegExp(value)) {
3687
+ index = serializeRegExp(buffer, key, value, index);
3688
+ }
3689
+ else {
3690
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3691
+ }
3654
3692
  }
3655
- else if (value._bsontype === 'Double') {
3656
- index = serializeDouble(buffer, key, value, index);
3693
+ else if (type === 'object') {
3694
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3695
+ throw new BSONVersionError();
3696
+ }
3697
+ else if (value._bsontype === 'ObjectId') {
3698
+ index = serializeObjectId(buffer, key, value, index);
3699
+ }
3700
+ else if (value._bsontype === 'Decimal128') {
3701
+ index = serializeDecimal128(buffer, key, value, index);
3702
+ }
3703
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3704
+ index = serializeLong(buffer, key, value, index);
3705
+ }
3706
+ else if (value._bsontype === 'Double') {
3707
+ index = serializeDouble(buffer, key, value, index);
3708
+ }
3709
+ else if (value._bsontype === 'Code') {
3710
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3711
+ }
3712
+ else if (value._bsontype === 'Binary') {
3713
+ index = serializeBinary(buffer, key, value, index);
3714
+ }
3715
+ else if (value._bsontype === 'BSONSymbol') {
3716
+ index = serializeSymbol(buffer, key, value, index);
3717
+ }
3718
+ else if (value._bsontype === 'DBRef') {
3719
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3720
+ }
3721
+ else if (value._bsontype === 'BSONRegExp') {
3722
+ index = serializeBSONRegExp(buffer, key, value, index);
3723
+ }
3724
+ else if (value._bsontype === 'Int32') {
3725
+ index = serializeInt32(buffer, key, value, index);
3726
+ }
3727
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3728
+ index = serializeMinMax(buffer, key, value, index);
3729
+ }
3730
+ else if (typeof value._bsontype !== 'undefined') {
3731
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3732
+ }
3657
3733
  }
3658
- else if (typeof value === 'function' && serializeFunctions) {
3734
+ else if (type === 'function' && serializeFunctions) {
3659
3735
  index = serializeFunction(buffer, key, value, index);
3660
3736
  }
3661
- else if (value._bsontype === 'Code') {
3662
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3663
- }
3664
- else if (value._bsontype === 'Binary') {
3665
- index = serializeBinary(buffer, key, value, index);
3666
- }
3667
- else if (value._bsontype === 'BSONSymbol') {
3668
- index = serializeSymbol(buffer, key, value, index);
3669
- }
3670
- else if (value._bsontype === 'DBRef') {
3671
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3672
- }
3673
- else if (value._bsontype === 'BSONRegExp') {
3674
- index = serializeBSONRegExp(buffer, key, value, index);
3675
- }
3676
- else if (value._bsontype === 'Int32') {
3677
- index = serializeInt32(buffer, key, value, index);
3678
- }
3679
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3680
- index = serializeMinMax(buffer, key, value, index);
3681
- }
3682
- else if (typeof value._bsontype !== 'undefined') {
3683
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3684
- }
3685
3737
  }
3686
3738
  }
3687
3739
  else if (object instanceof Map || isMap(object)) {
@@ -3711,7 +3763,14 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3711
3763
  }
3712
3764
  }
3713
3765
  }
3714
- if (type === 'string') {
3766
+ if (value === undefined) {
3767
+ if (ignoreUndefined === false)
3768
+ index = serializeNull(buffer, key, value, index);
3769
+ }
3770
+ else if (value === null) {
3771
+ index = serializeNull(buffer, key, value, index);
3772
+ }
3773
+ else if (type === 'string') {
3715
3774
  index = serializeString(buffer, key, value, index);
3716
3775
  }
3717
3776
  else if (type === 'number') {
@@ -3723,64 +3782,64 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3723
3782
  else if (type === 'boolean') {
3724
3783
  index = serializeBoolean(buffer, key, value, index);
3725
3784
  }
3726
- else if (value instanceof Date || isDate(value)) {
3727
- index = serializeDate(buffer, key, value, index);
3728
- }
3729
- else if (value === null || (value === undefined && ignoreUndefined === false)) {
3730
- index = serializeNull(buffer, key, value, index);
3731
- }
3732
- else if (isUint8Array(value)) {
3733
- index = serializeBuffer(buffer, key, value, index);
3734
- }
3735
- else if (value instanceof RegExp || isRegExp(value)) {
3736
- index = serializeRegExp(buffer, key, value, index);
3737
- }
3738
3785
  else if (type === 'object' && value._bsontype == null) {
3739
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3740
- }
3741
- else if (typeof value === 'object' &&
3742
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3743
- throw new BSONVersionError();
3744
- }
3745
- else if (value._bsontype === 'ObjectId') {
3746
- index = serializeObjectId(buffer, key, value, index);
3747
- }
3748
- else if (type === 'object' && value._bsontype === 'Decimal128') {
3749
- index = serializeDecimal128(buffer, key, value, index);
3750
- }
3751
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3752
- index = serializeLong(buffer, key, value, index);
3753
- }
3754
- else if (value._bsontype === 'Double') {
3755
- index = serializeDouble(buffer, key, value, index);
3786
+ if (value instanceof Date || isDate(value)) {
3787
+ index = serializeDate(buffer, key, value, index);
3788
+ }
3789
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3790
+ index = serializeBuffer(buffer, key, value, index);
3791
+ }
3792
+ else if (value instanceof RegExp || isRegExp(value)) {
3793
+ index = serializeRegExp(buffer, key, value, index);
3794
+ }
3795
+ else {
3796
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3797
+ }
3756
3798
  }
3757
- else if (value._bsontype === 'Code') {
3758
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3799
+ else if (type === 'object') {
3800
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3801
+ throw new BSONVersionError();
3802
+ }
3803
+ else if (value._bsontype === 'ObjectId') {
3804
+ index = serializeObjectId(buffer, key, value, index);
3805
+ }
3806
+ else if (value._bsontype === 'Decimal128') {
3807
+ index = serializeDecimal128(buffer, key, value, index);
3808
+ }
3809
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3810
+ index = serializeLong(buffer, key, value, index);
3811
+ }
3812
+ else if (value._bsontype === 'Double') {
3813
+ index = serializeDouble(buffer, key, value, index);
3814
+ }
3815
+ else if (value._bsontype === 'Code') {
3816
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3817
+ }
3818
+ else if (value._bsontype === 'Binary') {
3819
+ index = serializeBinary(buffer, key, value, index);
3820
+ }
3821
+ else if (value._bsontype === 'BSONSymbol') {
3822
+ index = serializeSymbol(buffer, key, value, index);
3823
+ }
3824
+ else if (value._bsontype === 'DBRef') {
3825
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3826
+ }
3827
+ else if (value._bsontype === 'BSONRegExp') {
3828
+ index = serializeBSONRegExp(buffer, key, value, index);
3829
+ }
3830
+ else if (value._bsontype === 'Int32') {
3831
+ index = serializeInt32(buffer, key, value, index);
3832
+ }
3833
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3834
+ index = serializeMinMax(buffer, key, value, index);
3835
+ }
3836
+ else if (typeof value._bsontype !== 'undefined') {
3837
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3838
+ }
3759
3839
  }
3760
- else if (typeof value === 'function' && serializeFunctions) {
3840
+ else if (type === 'function' && serializeFunctions) {
3761
3841
  index = serializeFunction(buffer, key, value, index);
3762
3842
  }
3763
- else if (value._bsontype === 'Binary') {
3764
- index = serializeBinary(buffer, key, value, index);
3765
- }
3766
- else if (value._bsontype === 'BSONSymbol') {
3767
- index = serializeSymbol(buffer, key, value, index);
3768
- }
3769
- else if (value._bsontype === 'DBRef') {
3770
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3771
- }
3772
- else if (value._bsontype === 'BSONRegExp') {
3773
- index = serializeBSONRegExp(buffer, key, value, index);
3774
- }
3775
- else if (value._bsontype === 'Int32') {
3776
- index = serializeInt32(buffer, key, value, index);
3777
- }
3778
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3779
- index = serializeMinMax(buffer, key, value, index);
3780
- }
3781
- else if (typeof value._bsontype !== 'undefined') {
3782
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3783
- }
3784
3843
  }
3785
3844
  }
3786
3845
  else {
@@ -3809,7 +3868,14 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3809
3868
  }
3810
3869
  }
3811
3870
  }
3812
- if (type === 'string') {
3871
+ if (value === undefined) {
3872
+ if (ignoreUndefined === false)
3873
+ index = serializeNull(buffer, key, value, index);
3874
+ }
3875
+ else if (value === null) {
3876
+ index = serializeNull(buffer, key, value, index);
3877
+ }
3878
+ else if (type === 'string') {
3813
3879
  index = serializeString(buffer, key, value, index);
3814
3880
  }
3815
3881
  else if (type === 'number') {
@@ -3821,68 +3887,64 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3821
3887
  else if (type === 'boolean') {
3822
3888
  index = serializeBoolean(buffer, key, value, index);
3823
3889
  }
3824
- else if (value instanceof Date || isDate(value)) {
3825
- index = serializeDate(buffer, key, value, index);
3826
- }
3827
- else if (value === undefined) {
3828
- if (ignoreUndefined === false)
3829
- index = serializeNull(buffer, key, value, index);
3830
- }
3831
- else if (value === null) {
3832
- index = serializeNull(buffer, key, value, index);
3833
- }
3834
- else if (isUint8Array(value)) {
3835
- index = serializeBuffer(buffer, key, value, index);
3836
- }
3837
- else if (value instanceof RegExp || isRegExp(value)) {
3838
- index = serializeRegExp(buffer, key, value, index);
3839
- }
3840
3890
  else if (type === 'object' && value._bsontype == null) {
3841
- index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3842
- }
3843
- else if (typeof value === 'object' &&
3844
- value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
3845
- throw new BSONVersionError();
3846
- }
3847
- else if (value._bsontype === 'ObjectId') {
3848
- index = serializeObjectId(buffer, key, value, index);
3849
- }
3850
- else if (type === 'object' && value._bsontype === 'Decimal128') {
3851
- index = serializeDecimal128(buffer, key, value, index);
3852
- }
3853
- else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3854
- index = serializeLong(buffer, key, value, index);
3855
- }
3856
- else if (value._bsontype === 'Double') {
3857
- index = serializeDouble(buffer, key, value, index);
3891
+ if (value instanceof Date || isDate(value)) {
3892
+ index = serializeDate(buffer, key, value, index);
3893
+ }
3894
+ else if (value instanceof Uint8Array || isUint8Array(value)) {
3895
+ index = serializeBuffer(buffer, key, value, index);
3896
+ }
3897
+ else if (value instanceof RegExp || isRegExp(value)) {
3898
+ index = serializeRegExp(buffer, key, value, index);
3899
+ }
3900
+ else {
3901
+ index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3902
+ }
3858
3903
  }
3859
- else if (value._bsontype === 'Code') {
3860
- index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3904
+ else if (type === 'object') {
3905
+ if (value[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
3906
+ throw new BSONVersionError();
3907
+ }
3908
+ else if (value._bsontype === 'ObjectId') {
3909
+ index = serializeObjectId(buffer, key, value, index);
3910
+ }
3911
+ else if (value._bsontype === 'Decimal128') {
3912
+ index = serializeDecimal128(buffer, key, value, index);
3913
+ }
3914
+ else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
3915
+ index = serializeLong(buffer, key, value, index);
3916
+ }
3917
+ else if (value._bsontype === 'Double') {
3918
+ index = serializeDouble(buffer, key, value, index);
3919
+ }
3920
+ else if (value._bsontype === 'Code') {
3921
+ index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
3922
+ }
3923
+ else if (value._bsontype === 'Binary') {
3924
+ index = serializeBinary(buffer, key, value, index);
3925
+ }
3926
+ else if (value._bsontype === 'BSONSymbol') {
3927
+ index = serializeSymbol(buffer, key, value, index);
3928
+ }
3929
+ else if (value._bsontype === 'DBRef') {
3930
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3931
+ }
3932
+ else if (value._bsontype === 'BSONRegExp') {
3933
+ index = serializeBSONRegExp(buffer, key, value, index);
3934
+ }
3935
+ else if (value._bsontype === 'Int32') {
3936
+ index = serializeInt32(buffer, key, value, index);
3937
+ }
3938
+ else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3939
+ index = serializeMinMax(buffer, key, value, index);
3940
+ }
3941
+ else if (typeof value._bsontype !== 'undefined') {
3942
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3943
+ }
3861
3944
  }
3862
- else if (typeof value === 'function' && serializeFunctions) {
3945
+ else if (type === 'function' && serializeFunctions) {
3863
3946
  index = serializeFunction(buffer, key, value, index);
3864
3947
  }
3865
- else if (value._bsontype === 'Binary') {
3866
- index = serializeBinary(buffer, key, value, index);
3867
- }
3868
- else if (value._bsontype === 'BSONSymbol') {
3869
- index = serializeSymbol(buffer, key, value, index);
3870
- }
3871
- else if (value._bsontype === 'DBRef') {
3872
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
3873
- }
3874
- else if (value._bsontype === 'BSONRegExp') {
3875
- index = serializeBSONRegExp(buffer, key, value, index);
3876
- }
3877
- else if (value._bsontype === 'Int32') {
3878
- index = serializeInt32(buffer, key, value, index);
3879
- }
3880
- else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
3881
- index = serializeMinMax(buffer, key, value, index);
3882
- }
3883
- else if (typeof value._bsontype !== 'undefined') {
3884
- throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
3885
- }
3886
3948
  }
3887
3949
  }
3888
3950
  path.delete(object);
@@ -4134,7 +4196,7 @@ function serializeDocument(doc, options) {
4134
4196
  else if (doc != null &&
4135
4197
  typeof doc === 'object' &&
4136
4198
  typeof doc._bsontype === 'string' &&
4137
- doc[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
4199
+ doc[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION) {
4138
4200
  throw new BSONVersionError();
4139
4201
  }
4140
4202
  else if (isBSONType(doc)) {