bson 4.5.2 → 4.6.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.
Files changed (56) hide show
  1. package/bower.json +1 -1
  2. package/bson.d.ts +39 -3
  3. package/dist/bson.browser.esm.js +320 -187
  4. package/dist/bson.browser.esm.js.map +1 -1
  5. package/dist/bson.browser.umd.js +323 -186
  6. package/dist/bson.browser.umd.js.map +1 -1
  7. package/dist/bson.bundle.js +323 -186
  8. package/dist/bson.bundle.js.map +1 -1
  9. package/dist/bson.esm.js +320 -187
  10. package/dist/bson.esm.js.map +1 -1
  11. package/lib/binary.js +11 -6
  12. package/lib/binary.js.map +1 -1
  13. package/lib/bson.js +11 -3
  14. package/lib/bson.js.map +1 -1
  15. package/lib/constants.js +5 -1
  16. package/lib/constants.js.map +1 -1
  17. package/lib/decimal128.js +13 -5
  18. package/lib/decimal128.js.map +1 -1
  19. package/lib/ensure_buffer.js +3 -2
  20. package/lib/ensure_buffer.js.map +1 -1
  21. package/lib/error.js +55 -0
  22. package/lib/error.js.map +1 -0
  23. package/lib/extended_json.js +11 -5
  24. package/lib/extended_json.js.map +1 -1
  25. package/lib/int_32.js +1 -1
  26. package/lib/int_32.js.map +1 -1
  27. package/lib/objectid.js +42 -47
  28. package/lib/objectid.js.map +1 -1
  29. package/lib/parser/calculate_size.js +2 -2
  30. package/lib/parser/calculate_size.js.map +1 -1
  31. package/lib/parser/deserializer.js +131 -53
  32. package/lib/parser/deserializer.js.map +1 -1
  33. package/lib/parser/serializer.js +16 -20
  34. package/lib/parser/serializer.js.map +1 -1
  35. package/lib/regexp.js +9 -2
  36. package/lib/regexp.js.map +1 -1
  37. package/lib/uuid.js +2 -1
  38. package/lib/uuid.js.map +1 -1
  39. package/lib/uuid_utils.js +2 -1
  40. package/lib/uuid_utils.js.map +1 -1
  41. package/package.json +4 -2
  42. package/src/binary.ts +11 -6
  43. package/src/bson.ts +7 -1
  44. package/src/constants.ts +6 -0
  45. package/src/decimal128.ts +12 -5
  46. package/src/ensure_buffer.ts +3 -2
  47. package/src/error.ts +23 -0
  48. package/src/extended_json.ts +13 -5
  49. package/src/int_32.ts +1 -1
  50. package/src/objectid.ts +44 -62
  51. package/src/parser/calculate_size.ts +2 -2
  52. package/src/parser/deserializer.ts +159 -57
  53. package/src/parser/serializer.ts +16 -17
  54. package/src/regexp.ts +14 -2
  55. package/src/uuid.ts +2 -1
  56. package/src/uuid_utils.ts +2 -1
@@ -2037,6 +2037,97 @@
2037
2037
  buffer$1.INSPECT_MAX_BYTES;
2038
2038
  buffer$1.kMaxLength;
2039
2039
 
2040
+ /*! *****************************************************************************
2041
+ Copyright (c) Microsoft Corporation.
2042
+
2043
+ Permission to use, copy, modify, and/or distribute this software for any
2044
+ purpose with or without fee is hereby granted.
2045
+
2046
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
2047
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
2048
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
2049
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
2050
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
2051
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
2052
+ PERFORMANCE OF THIS SOFTWARE.
2053
+ ***************************************************************************** */
2054
+
2055
+ /* global Reflect, Promise */
2056
+ var _extendStatics = function extendStatics(d, b) {
2057
+ _extendStatics = Object.setPrototypeOf || {
2058
+ __proto__: []
2059
+ } instanceof Array && function (d, b) {
2060
+ d.__proto__ = b;
2061
+ } || function (d, b) {
2062
+ for (var p in b) {
2063
+ if (b.hasOwnProperty(p)) d[p] = b[p];
2064
+ }
2065
+ };
2066
+
2067
+ return _extendStatics(d, b);
2068
+ };
2069
+
2070
+ function __extends(d, b) {
2071
+ _extendStatics(d, b);
2072
+
2073
+ function __() {
2074
+ this.constructor = d;
2075
+ }
2076
+
2077
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
2078
+ }
2079
+
2080
+ var _assign = function __assign() {
2081
+ _assign = Object.assign || function __assign(t) {
2082
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
2083
+ s = arguments[i];
2084
+
2085
+ for (var p in s) {
2086
+ if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
2087
+ }
2088
+ }
2089
+
2090
+ return t;
2091
+ };
2092
+
2093
+ return _assign.apply(this, arguments);
2094
+ };
2095
+
2096
+ /** @public */
2097
+ var BSONError = /** @class */ (function (_super) {
2098
+ __extends(BSONError, _super);
2099
+ function BSONError(message) {
2100
+ var _this = _super.call(this, message) || this;
2101
+ Object.setPrototypeOf(_this, BSONError.prototype);
2102
+ return _this;
2103
+ }
2104
+ Object.defineProperty(BSONError.prototype, "name", {
2105
+ get: function () {
2106
+ return 'BSONError';
2107
+ },
2108
+ enumerable: false,
2109
+ configurable: true
2110
+ });
2111
+ return BSONError;
2112
+ }(Error));
2113
+ /** @public */
2114
+ var BSONTypeError = /** @class */ (function (_super) {
2115
+ __extends(BSONTypeError, _super);
2116
+ function BSONTypeError(message) {
2117
+ var _this = _super.call(this, message) || this;
2118
+ Object.setPrototypeOf(_this, BSONTypeError.prototype);
2119
+ return _this;
2120
+ }
2121
+ Object.defineProperty(BSONTypeError.prototype, "name", {
2122
+ get: function () {
2123
+ return 'BSONTypeError';
2124
+ },
2125
+ enumerable: false,
2126
+ configurable: true
2127
+ });
2128
+ return BSONTypeError;
2129
+ }(TypeError));
2130
+
2040
2131
  function checkForMath(potentialGlobal) {
2041
2132
  // eslint-disable-next-line eqeqeq
2042
2133
  return potentialGlobal && potentialGlobal.Math == Math && potentialGlobal;
@@ -2148,7 +2239,7 @@
2148
2239
  * @param potentialBuffer - The potential buffer
2149
2240
  * @returns Buffer the input if potentialBuffer is a buffer, or a buffer that
2150
2241
  * wraps a passed in Uint8Array
2151
- * @throws TypeError If anything other than a Buffer or Uint8Array is passed in
2242
+ * @throws BSONTypeError If anything other than a Buffer or Uint8Array is passed in
2152
2243
  */
2153
2244
  function ensureBuffer(potentialBuffer) {
2154
2245
  if (ArrayBuffer.isView(potentialBuffer)) {
@@ -2157,7 +2248,7 @@
2157
2248
  if (isAnyArrayBuffer(potentialBuffer)) {
2158
2249
  return buffer_1.from(potentialBuffer);
2159
2250
  }
2160
- throw new TypeError('Must use either Buffer or TypedArray');
2251
+ throw new BSONTypeError('Must use either Buffer or TypedArray');
2161
2252
  }
2162
2253
 
2163
2254
  // Validation regex for v4 uuid (validates with or without dashes)
@@ -2167,7 +2258,7 @@
2167
2258
  };
2168
2259
  var uuidHexStringToBuffer = function (hexString) {
2169
2260
  if (!uuidValidateString(hexString)) {
2170
- throw new TypeError('UUID string representations must be a 32 or 36 character hex string (dashes excluded/included). Format: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" or "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".');
2261
+ throw new BSONTypeError('UUID string representations must be a 32 or 36 character hex string (dashes excluded/included). Format: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" or "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".');
2171
2262
  }
2172
2263
  var sanitizedHexString = hexString.replace(/-/g, '');
2173
2264
  return buffer_1.from(sanitizedHexString, 'hex');
@@ -2215,7 +2306,7 @@
2215
2306
  this.id = uuidHexStringToBuffer(input);
2216
2307
  }
2217
2308
  else {
2218
- throw new TypeError('Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).');
2309
+ throw new BSONTypeError('Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).');
2219
2310
  }
2220
2311
  }
2221
2312
  Object.defineProperty(UUID.prototype, "id", {
@@ -2373,7 +2464,7 @@
2373
2464
  !ArrayBuffer.isView(buffer) &&
2374
2465
  !(buffer instanceof ArrayBuffer) &&
2375
2466
  !Array.isArray(buffer)) {
2376
- throw new TypeError('Binary can only be constructed from string, Buffer, TypedArray, or Array<number>');
2467
+ throw new BSONTypeError('Binary can only be constructed from string, Buffer, TypedArray, or Array<number>');
2377
2468
  }
2378
2469
  this.sub_type = subType !== null && subType !== void 0 ? subType : Binary.BSON_BINARY_SUBTYPE_DEFAULT;
2379
2470
  if (buffer == null) {
@@ -2405,10 +2496,10 @@
2405
2496
  Binary.prototype.put = function (byteValue) {
2406
2497
  // If it's a string and a has more than one character throw an error
2407
2498
  if (typeof byteValue === 'string' && byteValue.length !== 1) {
2408
- throw new TypeError('only accepts single character String');
2499
+ throw new BSONTypeError('only accepts single character String');
2409
2500
  }
2410
2501
  else if (typeof byteValue !== 'number' && byteValue.length !== 1)
2411
- throw new TypeError('only accepts single character Uint8Array or Array');
2502
+ throw new BSONTypeError('only accepts single character Uint8Array or Array');
2412
2503
  // Decode the byte value once
2413
2504
  var decodedByte;
2414
2505
  if (typeof byteValue === 'string') {
@@ -2421,7 +2512,7 @@
2421
2512
  decodedByte = byteValue[0];
2422
2513
  }
2423
2514
  if (decodedByte < 0 || decodedByte > 255) {
2424
- throw new TypeError('only accepts number in a valid unsigned byte range 0-255');
2515
+ throw new BSONTypeError('only accepts number in a valid unsigned byte range 0-255');
2425
2516
  }
2426
2517
  if (this.buffer.length > this.position) {
2427
2518
  this.buffer[this.position++] = decodedByte;
@@ -2521,7 +2612,7 @@
2521
2612
  if (this.sub_type === Binary.SUBTYPE_UUID) {
2522
2613
  return new UUID(this.buffer.slice(0, this.position));
2523
2614
  }
2524
- throw new Error("Binary sub_type \"" + this.sub_type + "\" is not supported for converting to UUID. Only \"" + Binary.SUBTYPE_UUID + "\" is currently supported.");
2615
+ throw new BSONError("Binary sub_type \"" + this.sub_type + "\" is not supported for converting to UUID. Only \"" + Binary.SUBTYPE_UUID + "\" is currently supported.");
2525
2616
  };
2526
2617
  /** @internal */
2527
2618
  Binary.fromExtendedJSON = function (doc, options) {
@@ -2545,7 +2636,7 @@
2545
2636
  data = uuidHexStringToBuffer(doc.$uuid);
2546
2637
  }
2547
2638
  if (!data) {
2548
- throw new TypeError("Unexpected Binary Extended JSON format " + JSON.stringify(doc));
2639
+ throw new BSONTypeError("Unexpected Binary Extended JSON format " + JSON.stringify(doc));
2549
2640
  }
2550
2641
  return new Binary(data, type);
2551
2642
  };
@@ -2576,6 +2667,10 @@
2576
2667
  Binary.SUBTYPE_UUID = 4;
2577
2668
  /** MD5 BSON type */
2578
2669
  Binary.SUBTYPE_MD5 = 5;
2670
+ /** Encrypted BSON type */
2671
+ Binary.SUBTYPE_ENCRYPTED = 6;
2672
+ /** Column BSON type */
2673
+ Binary.SUBTYPE_COLUMN = 7;
2579
2674
  /** User BSON type */
2580
2675
  Binary.SUBTYPE_USER_DEFINED = 128;
2581
2676
  return Binary;
@@ -3694,7 +3789,7 @@
3694
3789
  return false;
3695
3790
  }
3696
3791
  function invalidErr(string, message) {
3697
- throw new TypeError("\"" + string + "\" is not a valid Decimal128 string - " + message);
3792
+ throw new BSONTypeError("\"" + string + "\" is not a valid Decimal128 string - " + message);
3698
3793
  }
3699
3794
  /**
3700
3795
  * A class representation of the BSON Decimal128 type.
@@ -3711,9 +3806,15 @@
3711
3806
  if (typeof bytes === 'string') {
3712
3807
  this.bytes = Decimal128.fromString(bytes).bytes;
3713
3808
  }
3714
- else {
3809
+ else if (isUint8Array(bytes)) {
3810
+ if (bytes.byteLength !== 16) {
3811
+ throw new BSONTypeError('Decimal128 must take a Buffer of 16 bytes');
3812
+ }
3715
3813
  this.bytes = bytes;
3716
3814
  }
3815
+ else {
3816
+ throw new BSONTypeError('Decimal128 must take a Buffer or string');
3817
+ }
3717
3818
  }
3718
3819
  /**
3719
3820
  * Create a Decimal128 instance from a string representation
@@ -3761,7 +3862,7 @@
3761
3862
  // TODO: implementing a custom parsing for this, or refactoring the regex would yield
3762
3863
  // further gains.
3763
3864
  if (representation.length >= 7000) {
3764
- throw new TypeError('' + representation + ' not a valid Decimal128 string');
3865
+ throw new BSONTypeError('' + representation + ' not a valid Decimal128 string');
3765
3866
  }
3766
3867
  // Results
3767
3868
  var stringMatch = representation.match(PARSE_STRING_REGEXP);
@@ -3769,7 +3870,7 @@
3769
3870
  var nanMatch = representation.match(PARSE_NAN_REGEXP);
3770
3871
  // Validate the string
3771
3872
  if ((!stringMatch && !infMatch && !nanMatch) || representation.length === 0) {
3772
- throw new TypeError('' + representation + ' not a valid Decimal128 string');
3873
+ throw new BSONTypeError('' + representation + ' not a valid Decimal128 string');
3773
3874
  }
3774
3875
  if (stringMatch) {
3775
3876
  // full_match = stringMatch[0]
@@ -3831,7 +3932,7 @@
3831
3932
  index = index + 1;
3832
3933
  }
3833
3934
  if (sawRadix && !nDigitsRead)
3834
- throw new TypeError('' + representation + ' not a valid Decimal128 string');
3935
+ throw new BSONTypeError('' + representation + ' not a valid Decimal128 string');
3835
3936
  // Read exponent if exists
3836
3937
  if (representation[index] === 'e' || representation[index] === 'E') {
3837
3938
  // Read exponent digits
@@ -4348,7 +4449,7 @@
4348
4449
  if (value instanceof Number) {
4349
4450
  value = value.valueOf();
4350
4451
  }
4351
- this.value = +value;
4452
+ this.value = +value | 0;
4352
4453
  }
4353
4454
  /**
4354
4455
  * Access the number value.
@@ -4454,50 +4555,57 @@
4454
4555
  /**
4455
4556
  * Create an ObjectId type
4456
4557
  *
4457
- * @param id - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
4558
+ * @param inputId - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
4458
4559
  */
4459
- function ObjectId(id) {
4560
+ function ObjectId(inputId) {
4460
4561
  if (!(this instanceof ObjectId))
4461
- return new ObjectId(id);
4462
- // Duck-typing to support ObjectId from different npm packages
4463
- if (id instanceof ObjectId) {
4464
- this[kId] = id.id;
4465
- this.__id = id.__id;
4466
- }
4467
- if (typeof id === 'object' && id && 'id' in id) {
4468
- if ('toHexString' in id && typeof id.toHexString === 'function') {
4469
- this[kId] = buffer_1.from(id.toHexString(), 'hex');
4562
+ return new ObjectId(inputId);
4563
+ // workingId is set based on type of input and whether valid id exists for the input
4564
+ var workingId;
4565
+ if (typeof inputId === 'object' && inputId && 'id' in inputId) {
4566
+ if (typeof inputId.id !== 'string' && !ArrayBuffer.isView(inputId.id)) {
4567
+ throw new BSONTypeError('Argument passed in must have an id that is of type string or Buffer');
4568
+ }
4569
+ if ('toHexString' in inputId && typeof inputId.toHexString === 'function') {
4570
+ workingId = buffer_1.from(inputId.toHexString(), 'hex');
4470
4571
  }
4471
4572
  else {
4472
- this[kId] = typeof id.id === 'string' ? buffer_1.from(id.id) : id.id;
4573
+ workingId = inputId.id;
4473
4574
  }
4474
4575
  }
4475
- // The most common use case (blank id, new objectId instance)
4476
- if (id == null || typeof id === 'number') {
4576
+ else {
4577
+ workingId = inputId;
4578
+ }
4579
+ // the following cases use workingId to construct an ObjectId
4580
+ if (workingId == null || typeof workingId === 'number') {
4581
+ // The most common use case (blank id, new objectId instance)
4477
4582
  // Generate a new id
4478
- this[kId] = ObjectId.generate(typeof id === 'number' ? id : undefined);
4479
- // If we are caching the hex string
4480
- if (ObjectId.cacheHexString) {
4481
- this.__id = this.id.toString('hex');
4482
- }
4583
+ this[kId] = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
4483
4584
  }
4484
- if (ArrayBuffer.isView(id) && id.byteLength === 12) {
4485
- this[kId] = ensureBuffer(id);
4585
+ else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
4586
+ this[kId] = ensureBuffer(workingId);
4486
4587
  }
4487
- if (typeof id === 'string') {
4488
- if (id.length === 12) {
4489
- var bytes = buffer_1.from(id);
4588
+ else if (typeof workingId === 'string') {
4589
+ if (workingId.length === 12) {
4590
+ var bytes = buffer_1.from(workingId);
4490
4591
  if (bytes.byteLength === 12) {
4491
4592
  this[kId] = bytes;
4492
4593
  }
4594
+ else {
4595
+ throw new BSONTypeError('Argument passed in must be a string of 12 bytes');
4596
+ }
4493
4597
  }
4494
- else if (id.length === 24 && checkForHexRegExp.test(id)) {
4495
- this[kId] = buffer_1.from(id, 'hex');
4598
+ else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
4599
+ this[kId] = buffer_1.from(workingId, 'hex');
4496
4600
  }
4497
4601
  else {
4498
- throw new TypeError('Argument passed in must be a Buffer or string of 12 bytes or a string of 24 hex characters');
4602
+ throw new BSONTypeError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters');
4499
4603
  }
4500
4604
  }
4605
+ else {
4606
+ throw new BSONTypeError('Argument passed in does not match the accepted types');
4607
+ }
4608
+ // If we are caching the hex string
4501
4609
  if (ObjectId.cacheHexString) {
4502
4610
  this.__id = this.id.toString('hex');
4503
4611
  }
@@ -4561,7 +4669,7 @@
4561
4669
  */
4562
4670
  ObjectId.generate = function (time) {
4563
4671
  if ('number' !== typeof time) {
4564
- time = ~~(Date.now() / 1000);
4672
+ time = Math.floor(Date.now() / 1000);
4565
4673
  }
4566
4674
  var inc = ObjectId.getInc();
4567
4675
  var buffer = buffer_1.alloc(12);
@@ -4660,7 +4768,7 @@
4660
4768
  ObjectId.createFromHexString = function (hexString) {
4661
4769
  // Throw an error if it's not a valid setup
4662
4770
  if (typeof hexString === 'undefined' || (hexString != null && hexString.length !== 24)) {
4663
- throw new TypeError('Argument passed in must be a single String of 12 bytes or a string of 24 hex characters');
4771
+ throw new BSONTypeError('Argument passed in must be a single String of 12 bytes or a string of 24 hex characters');
4664
4772
  }
4665
4773
  return new ObjectId(buffer_1.from(hexString, 'hex'));
4666
4774
  };
@@ -4672,26 +4780,13 @@
4672
4780
  ObjectId.isValid = function (id) {
4673
4781
  if (id == null)
4674
4782
  return false;
4675
- if (typeof id === 'number') {
4676
- return true;
4677
- }
4678
- if (typeof id === 'string') {
4679
- return id.length === 12 || (id.length === 24 && checkForHexRegExp.test(id));
4680
- }
4681
- if (id instanceof ObjectId) {
4682
- return true;
4683
- }
4684
- if (isUint8Array(id) && id.length === 12) {
4783
+ try {
4784
+ new ObjectId(id);
4685
4785
  return true;
4686
4786
  }
4687
- // Duck-Typing detection of ObjectId like objects
4688
- if (typeof id === 'object' && 'toHexString' in id && typeof id.toHexString === 'function') {
4689
- if (typeof id.id === 'string') {
4690
- return id.id.length === 12;
4691
- }
4692
- return id.toHexString().length === 24 && checkForHexRegExp.test(id.id.toString('hex'));
4787
+ catch (_a) {
4788
+ return false;
4693
4789
  }
4694
- return false;
4695
4790
  };
4696
4791
  /** @internal */
4697
4792
  ObjectId.prototype.toExtendedJSON = function () {
@@ -4716,7 +4811,7 @@
4716
4811
  return "new ObjectId(\"" + this.toHexString() + "\")";
4717
4812
  };
4718
4813
  /** @internal */
4719
- ObjectId.index = ~~(Math.random() * 0xffffff);
4814
+ ObjectId.index = Math.floor(Math.random() * 0xffffff);
4720
4815
  return ObjectId;
4721
4816
  }());
4722
4817
  // Deprecated methods
@@ -4751,6 +4846,12 @@
4751
4846
  return new BSONRegExp(pattern, options);
4752
4847
  this.pattern = pattern;
4753
4848
  this.options = alphabetize(options !== null && options !== void 0 ? options : '');
4849
+ if (this.pattern.indexOf('\x00') !== -1) {
4850
+ throw new BSONError("BSON Regex patterns cannot contain null bytes, found: " + JSON.stringify(this.pattern));
4851
+ }
4852
+ if (this.options.indexOf('\x00') !== -1) {
4853
+ throw new BSONError("BSON Regex options cannot contain null bytes, found: " + JSON.stringify(this.options));
4854
+ }
4754
4855
  // Validate options
4755
4856
  for (var i = 0; i < this.options.length; i++) {
4756
4857
  if (!(this.options[i] === 'i' ||
@@ -4759,7 +4860,7 @@
4759
4860
  this.options[i] === 'l' ||
4760
4861
  this.options[i] === 's' ||
4761
4862
  this.options[i] === 'u')) {
4762
- throw new Error("The regular expression option [" + this.options[i] + "] is not supported");
4863
+ throw new BSONError("The regular expression option [" + this.options[i] + "] is not supported");
4763
4864
  }
4764
4865
  }
4765
4866
  }
@@ -4790,7 +4891,7 @@
4790
4891
  if ('$regularExpression' in doc) {
4791
4892
  return new BSONRegExp(doc.$regularExpression.pattern, BSONRegExp.parseOptions(doc.$regularExpression.options));
4792
4893
  }
4793
- throw new TypeError("Unexpected BSONRegExp EJSON object form: " + JSON.stringify(doc));
4894
+ throw new BSONTypeError("Unexpected BSONRegExp EJSON object form: " + JSON.stringify(doc));
4794
4895
  };
4795
4896
  return BSONRegExp;
4796
4897
  }());
@@ -4839,46 +4940,6 @@
4839
4940
  }());
4840
4941
  Object.defineProperty(BSONSymbol.prototype, '_bsontype', { value: 'Symbol' });
4841
4942
 
4842
- /*! *****************************************************************************
4843
- Copyright (c) Microsoft Corporation.
4844
-
4845
- Permission to use, copy, modify, and/or distribute this software for any
4846
- purpose with or without fee is hereby granted.
4847
-
4848
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
4849
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
4850
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
4851
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
4852
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
4853
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
4854
- PERFORMANCE OF THIS SOFTWARE.
4855
- ***************************************************************************** */
4856
-
4857
- /* global Reflect, Promise */
4858
- var _extendStatics = function extendStatics(d, b) {
4859
- _extendStatics = Object.setPrototypeOf || {
4860
- __proto__: []
4861
- } instanceof Array && function (d, b) {
4862
- d.__proto__ = b;
4863
- } || function (d, b) {
4864
- for (var p in b) {
4865
- if (b.hasOwnProperty(p)) d[p] = b[p];
4866
- }
4867
- };
4868
-
4869
- return _extendStatics(d, b);
4870
- };
4871
-
4872
- function __extends(d, b) {
4873
- _extendStatics(d, b);
4874
-
4875
- function __() {
4876
- this.constructor = d;
4877
- }
4878
-
4879
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
4880
- }
4881
-
4882
4943
  /** @public */
4883
4944
  var LongWithoutOverridesClass = Long;
4884
4945
  /** @public */
@@ -5093,7 +5154,7 @@
5093
5154
  var current = props[props.length - 1];
5094
5155
  var leadingSpace = ' '.repeat(leadingPart.length + alreadySeen.length / 2);
5095
5156
  var dashes = '-'.repeat(circularPart.length + (alreadySeen.length + current.length) / 2 - 1);
5096
- throw new TypeError('Converting circular structure to EJSON:\n' +
5157
+ throw new BSONTypeError('Converting circular structure to EJSON:\n' +
5097
5158
  (" " + leadingPart + alreadySeen + circularPart + current + "\n") +
5098
5159
  (" " + leadingSpace + "\\" + dashes + "/"));
5099
5160
  }
@@ -5166,7 +5227,7 @@
5166
5227
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
5167
5228
  function serializeDocument(doc, options) {
5168
5229
  if (doc == null || typeof doc !== 'object')
5169
- throw new Error('not an object instance');
5230
+ throw new BSONError('not an object instance');
5170
5231
  var bsontype = doc._bsontype;
5171
5232
  if (typeof bsontype === 'undefined') {
5172
5233
  // It's a regular object. Recursively serialize its property values.
@@ -5193,7 +5254,7 @@
5193
5254
  // Copy the object into this library's version of that type.
5194
5255
  var mapper = BSON_TYPE_MAPPINGS[doc._bsontype];
5195
5256
  if (!mapper) {
5196
- throw new TypeError('Unrecognized or invalid _bsontype: ' + doc._bsontype);
5257
+ throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + doc._bsontype);
5197
5258
  }
5198
5259
  outDoc = mapper(outDoc);
5199
5260
  }
@@ -5207,7 +5268,7 @@
5207
5268
  return outDoc.toExtendedJSON(options);
5208
5269
  }
5209
5270
  else {
5210
- throw new Error('_bsontype must be a string, but was: ' + typeof bsontype);
5271
+ throw new BSONError('_bsontype must be a string, but was: ' + typeof bsontype);
5211
5272
  }
5212
5273
  }
5213
5274
  /**
@@ -5242,7 +5303,12 @@
5242
5303
  finalOptions.strict = !finalOptions.relaxed;
5243
5304
  if (typeof finalOptions.strict === 'boolean')
5244
5305
  finalOptions.relaxed = !finalOptions.strict;
5245
- return JSON.parse(text, function (_key, value) { return deserializeValue(value, finalOptions); });
5306
+ return JSON.parse(text, function (key, value) {
5307
+ if (key.indexOf('\x00') !== -1) {
5308
+ throw new BSONError("BSON Document field names cannot contain null bytes, found: " + JSON.stringify(key));
5309
+ }
5310
+ return deserializeValue(value, finalOptions);
5311
+ });
5246
5312
  }
5247
5313
  EJSON.parse = parse;
5248
5314
  /**
@@ -5500,6 +5566,10 @@
5500
5566
  var BSON_BINARY_SUBTYPE_UUID_NEW = 4;
5501
5567
  /** Binary MD5 Type @internal */
5502
5568
  var BSON_BINARY_SUBTYPE_MD5 = 5;
5569
+ /** Encrypted BSON type @internal */
5570
+ var BSON_BINARY_SUBTYPE_ENCRYPTED = 6;
5571
+ /** Column BSON type @internal */
5572
+ var BSON_BINARY_SUBTYPE_COLUMN = 7;
5503
5573
  /** Binary User Defined Type @internal */
5504
5574
  var BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
5505
5575
 
@@ -5512,7 +5582,7 @@
5512
5582
  }
5513
5583
  else {
5514
5584
  // If we have toBSON defined, override the current object
5515
- if (object.toBSON) {
5585
+ if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
5516
5586
  object = object.toBSON();
5517
5587
  }
5518
5588
  // Calculate size
@@ -5530,7 +5600,7 @@
5530
5600
  if (isArray === void 0) { isArray = false; }
5531
5601
  if (ignoreUndefined === void 0) { ignoreUndefined = false; }
5532
5602
  // If we have toBSON defined, override the current object
5533
- if (value && value.toBSON) {
5603
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
5534
5604
  value = value.toBSON();
5535
5605
  }
5536
5606
  switch (typeof value) {
@@ -5744,20 +5814,20 @@
5744
5814
  (buffer[index + 2] << 16) |
5745
5815
  (buffer[index + 3] << 24);
5746
5816
  if (size < 5) {
5747
- throw new Error("bson size must be >= 5, is " + size);
5817
+ throw new BSONError("bson size must be >= 5, is " + size);
5748
5818
  }
5749
5819
  if (options.allowObjectSmallerThanBufferSize && buffer.length < size) {
5750
- throw new Error("buffer length " + buffer.length + " must be >= bson size " + size);
5820
+ throw new BSONError("buffer length " + buffer.length + " must be >= bson size " + size);
5751
5821
  }
5752
5822
  if (!options.allowObjectSmallerThanBufferSize && buffer.length !== size) {
5753
- throw new Error("buffer length " + buffer.length + " must === bson size " + size);
5823
+ throw new BSONError("buffer length " + buffer.length + " must === bson size " + size);
5754
5824
  }
5755
5825
  if (size + index > buffer.byteLength) {
5756
- throw new Error("(bson size " + size + " + options.index " + index + " must be <= buffer length " + buffer.byteLength + ")");
5826
+ throw new BSONError("(bson size " + size + " + options.index " + index + " must be <= buffer length " + buffer.byteLength + ")");
5757
5827
  }
5758
5828
  // Illegal end value
5759
5829
  if (buffer[index + size - 1] !== 0) {
5760
- throw new Error("One object, sized correctly, with a spot for an EOO, but the EOO isn't 0x00");
5830
+ throw new BSONError("One object, sized correctly, with a spot for an EOO, but the EOO isn't 0x00");
5761
5831
  }
5762
5832
  // Start deserializtion
5763
5833
  return deserializeObject(buffer, index, options, isArray);
@@ -5776,16 +5846,53 @@
5776
5846
  var promoteBuffers = options['promoteBuffers'] == null ? false : options['promoteBuffers'];
5777
5847
  var promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
5778
5848
  var promoteValues = options['promoteValues'] == null ? true : options['promoteValues'];
5849
+ // Ensures default validation option if none given
5850
+ var validation = options.validation == null ? { utf8: true } : options.validation;
5851
+ // Shows if global utf-8 validation is enabled or disabled
5852
+ var globalUTFValidation = true;
5853
+ // Reflects utf-8 validation setting regardless of global or specific key validation
5854
+ var validationSetting;
5855
+ // Set of keys either to enable or disable validation on
5856
+ var utf8KeysSet = new Set();
5857
+ // Check for boolean uniformity and empty validation option
5858
+ var utf8ValidatedKeys = validation.utf8;
5859
+ if (typeof utf8ValidatedKeys === 'boolean') {
5860
+ validationSetting = utf8ValidatedKeys;
5861
+ }
5862
+ else {
5863
+ globalUTFValidation = false;
5864
+ var utf8ValidationValues = Object.keys(utf8ValidatedKeys).map(function (key) {
5865
+ return utf8ValidatedKeys[key];
5866
+ });
5867
+ if (utf8ValidationValues.length === 0) {
5868
+ throw new BSONError('UTF-8 validation setting cannot be empty');
5869
+ }
5870
+ if (typeof utf8ValidationValues[0] !== 'boolean') {
5871
+ throw new BSONError('Invalid UTF-8 validation option, must specify boolean values');
5872
+ }
5873
+ validationSetting = utf8ValidationValues[0];
5874
+ // Ensures boolean uniformity in utf-8 validation (all true or all false)
5875
+ if (!utf8ValidationValues.every(function (item) { return item === validationSetting; })) {
5876
+ throw new BSONError('Invalid UTF-8 validation option - keys must be all true or all false');
5877
+ }
5878
+ }
5879
+ // Add keys to set that will either be validated or not based on validationSetting
5880
+ if (!globalUTFValidation) {
5881
+ for (var _i = 0, _a = Object.keys(utf8ValidatedKeys); _i < _a.length; _i++) {
5882
+ var key = _a[_i];
5883
+ utf8KeysSet.add(key);
5884
+ }
5885
+ }
5779
5886
  // Set the start index
5780
5887
  var startIndex = index;
5781
5888
  // Validate that we have at least 4 bytes of buffer
5782
5889
  if (buffer.length < 5)
5783
- throw new Error('corrupt bson message < 5 bytes long');
5890
+ throw new BSONError('corrupt bson message < 5 bytes long');
5784
5891
  // Read the document size
5785
5892
  var size = buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
5786
5893
  // Ensure buffer is valid size
5787
5894
  if (size < 5 || size > buffer.length)
5788
- throw new Error('corrupt bson message');
5895
+ throw new BSONError('corrupt bson message');
5789
5896
  // Create holding object
5790
5897
  var object = isArray ? [] : {};
5791
5898
  // Used for arrays to skip having to perform utf8 decoding
@@ -5807,8 +5914,17 @@
5807
5914
  }
5808
5915
  // If are at the end of the buffer there is a problem with the document
5809
5916
  if (i >= buffer.byteLength)
5810
- throw new Error('Bad BSON Document: illegal CString');
5917
+ throw new BSONError('Bad BSON Document: illegal CString');
5918
+ // Represents the key
5811
5919
  var name = isArray ? arrayIndex++ : buffer.toString('utf8', index, i);
5920
+ // shouldValidateKey is true if the key should be validated, false otherwise
5921
+ var shouldValidateKey = true;
5922
+ if (globalUTFValidation || utf8KeysSet.has(name)) {
5923
+ shouldValidateKey = validationSetting;
5924
+ }
5925
+ else {
5926
+ shouldValidateKey = !validationSetting;
5927
+ }
5812
5928
  if (isPossibleDBRef !== false && name[0] === '$') {
5813
5929
  isPossibleDBRef = allowedDBRefKeys.test(name);
5814
5930
  }
@@ -5821,17 +5937,10 @@
5821
5937
  (buffer[index++] << 24);
5822
5938
  if (stringSize <= 0 ||
5823
5939
  stringSize > buffer.length - index ||
5824
- buffer[index + stringSize - 1] !== 0)
5825
- throw new Error('bad string length in bson');
5826
- value = buffer.toString('utf8', index, index + stringSize - 1);
5827
- for (var i_1 = 0; i_1 < value.length; i_1++) {
5828
- if (value.charCodeAt(i_1) === 0xfffd) {
5829
- if (!validateUtf8(buffer, index, index + stringSize - 1)) {
5830
- throw new Error('Invalid UTF-8 string in BSON document');
5831
- }
5832
- break;
5833
- }
5940
+ buffer[index + stringSize - 1] !== 0) {
5941
+ throw new BSONError('bad string length in bson');
5834
5942
  }
5943
+ value = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
5835
5944
  index = index + stringSize;
5836
5945
  }
5837
5946
  else if (elementType === BSON_DATA_OID) {
@@ -5871,7 +5980,7 @@
5871
5980
  }
5872
5981
  else if (elementType === BSON_DATA_BOOLEAN) {
5873
5982
  if (buffer[index] !== 0 && buffer[index] !== 1)
5874
- throw new Error('illegal boolean type value');
5983
+ throw new BSONError('illegal boolean type value');
5875
5984
  value = buffer[index++] === 1;
5876
5985
  }
5877
5986
  else if (elementType === BSON_DATA_OBJECT) {
@@ -5881,13 +5990,17 @@
5881
5990
  (buffer[index + 2] << 16) |
5882
5991
  (buffer[index + 3] << 24);
5883
5992
  if (objectSize <= 0 || objectSize > buffer.length - index)
5884
- throw new Error('bad embedded document length in bson');
5993
+ throw new BSONError('bad embedded document length in bson');
5885
5994
  // We have a raw value
5886
5995
  if (raw) {
5887
5996
  value = buffer.slice(index, index + objectSize);
5888
5997
  }
5889
5998
  else {
5890
- value = deserializeObject(buffer, _index, options, false);
5999
+ var objectOptions = options;
6000
+ if (!globalUTFValidation) {
6001
+ objectOptions = _assign(_assign({}, options), { validation: { utf8: shouldValidateKey } });
6002
+ }
6003
+ value = deserializeObject(buffer, _index, objectOptions, false);
5891
6004
  }
5892
6005
  index = index + objectSize;
5893
6006
  }
@@ -5908,12 +6021,15 @@
5908
6021
  }
5909
6022
  arrayOptions['raw'] = true;
5910
6023
  }
6024
+ if (!globalUTFValidation) {
6025
+ arrayOptions = _assign(_assign({}, arrayOptions), { validation: { utf8: shouldValidateKey } });
6026
+ }
5911
6027
  value = deserializeObject(buffer, _index, arrayOptions, true);
5912
6028
  index = index + objectSize;
5913
6029
  if (buffer[index - 1] !== 0)
5914
- throw new Error('invalid array terminator byte');
6030
+ throw new BSONError('invalid array terminator byte');
5915
6031
  if (index !== stopIndex)
5916
- throw new Error('corrupted array bson');
6032
+ throw new BSONError('corrupted array bson');
5917
6033
  }
5918
6034
  else if (elementType === BSON_DATA_UNDEFINED) {
5919
6035
  value = undefined;
@@ -5969,10 +6085,10 @@
5969
6085
  var subType = buffer[index++];
5970
6086
  // Did we have a negative binary size, throw
5971
6087
  if (binarySize < 0)
5972
- throw new Error('Negative binary type element size found');
6088
+ throw new BSONError('Negative binary type element size found');
5973
6089
  // Is the length longer than the document
5974
6090
  if (binarySize > buffer.byteLength)
5975
- throw new Error('Binary type size larger than document size');
6091
+ throw new BSONError('Binary type size larger than document size');
5976
6092
  // Decode as raw Buffer object if options specifies it
5977
6093
  if (buffer['slice'] != null) {
5978
6094
  // If we have subtype 2 skip the 4 bytes for the size
@@ -5983,11 +6099,11 @@
5983
6099
  (buffer[index++] << 16) |
5984
6100
  (buffer[index++] << 24);
5985
6101
  if (binarySize < 0)
5986
- throw new Error('Negative binary type element size found for subtype 0x02');
6102
+ throw new BSONError('Negative binary type element size found for subtype 0x02');
5987
6103
  if (binarySize > totalBinarySize - 4)
5988
- throw new Error('Binary type with subtype 0x02 contains too long binary size');
6104
+ throw new BSONError('Binary type with subtype 0x02 contains too long binary size');
5989
6105
  if (binarySize < totalBinarySize - 4)
5990
- throw new Error('Binary type with subtype 0x02 contains too short binary size');
6106
+ throw new BSONError('Binary type with subtype 0x02 contains too short binary size');
5991
6107
  }
5992
6108
  if (promoteBuffers && promoteValues) {
5993
6109
  value = buffer.slice(index, index + binarySize);
@@ -6006,11 +6122,11 @@
6006
6122
  (buffer[index++] << 16) |
6007
6123
  (buffer[index++] << 24);
6008
6124
  if (binarySize < 0)
6009
- throw new Error('Negative binary type element size found for subtype 0x02');
6125
+ throw new BSONError('Negative binary type element size found for subtype 0x02');
6010
6126
  if (binarySize > totalBinarySize - 4)
6011
- throw new Error('Binary type with subtype 0x02 contains too long binary size');
6127
+ throw new BSONError('Binary type with subtype 0x02 contains too long binary size');
6012
6128
  if (binarySize < totalBinarySize - 4)
6013
- throw new Error('Binary type with subtype 0x02 contains too short binary size');
6129
+ throw new BSONError('Binary type with subtype 0x02 contains too short binary size');
6014
6130
  }
6015
6131
  // Copy the data
6016
6132
  for (i = 0; i < binarySize; i++) {
@@ -6035,7 +6151,7 @@
6035
6151
  }
6036
6152
  // If are at the end of the buffer there is a problem with the document
6037
6153
  if (i >= buffer.length)
6038
- throw new Error('Bad BSON Document: illegal CString');
6154
+ throw new BSONError('Bad BSON Document: illegal CString');
6039
6155
  // Return the C string
6040
6156
  var source = buffer.toString('utf8', index, i);
6041
6157
  // Create the regexp
@@ -6048,7 +6164,7 @@
6048
6164
  }
6049
6165
  // If are at the end of the buffer there is a problem with the document
6050
6166
  if (i >= buffer.length)
6051
- throw new Error('Bad BSON Document: illegal CString');
6167
+ throw new BSONError('Bad BSON Document: illegal CString');
6052
6168
  // Return the C string
6053
6169
  var regExpOptions = buffer.toString('utf8', index, i);
6054
6170
  index = i + 1;
@@ -6079,7 +6195,7 @@
6079
6195
  }
6080
6196
  // If are at the end of the buffer there is a problem with the document
6081
6197
  if (i >= buffer.length)
6082
- throw new Error('Bad BSON Document: illegal CString');
6198
+ throw new BSONError('Bad BSON Document: illegal CString');
6083
6199
  // Return the C string
6084
6200
  var source = buffer.toString('utf8', index, i);
6085
6201
  index = i + 1;
@@ -6091,7 +6207,7 @@
6091
6207
  }
6092
6208
  // If are at the end of the buffer there is a problem with the document
6093
6209
  if (i >= buffer.length)
6094
- throw new Error('Bad BSON Document: illegal CString');
6210
+ throw new BSONError('Bad BSON Document: illegal CString');
6095
6211
  // Return the C string
6096
6212
  var regExpOptions = buffer.toString('utf8', index, i);
6097
6213
  index = i + 1;
@@ -6105,9 +6221,10 @@
6105
6221
  (buffer[index++] << 24);
6106
6222
  if (stringSize <= 0 ||
6107
6223
  stringSize > buffer.length - index ||
6108
- buffer[index + stringSize - 1] !== 0)
6109
- throw new Error('bad string length in bson');
6110
- var symbol = buffer.toString('utf8', index, index + stringSize - 1);
6224
+ buffer[index + stringSize - 1] !== 0) {
6225
+ throw new BSONError('bad string length in bson');
6226
+ }
6227
+ var symbol = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6111
6228
  value = promoteValues ? symbol : new BSONSymbol(symbol);
6112
6229
  index = index + stringSize;
6113
6230
  }
@@ -6135,9 +6252,10 @@
6135
6252
  (buffer[index++] << 24);
6136
6253
  if (stringSize <= 0 ||
6137
6254
  stringSize > buffer.length - index ||
6138
- buffer[index + stringSize - 1] !== 0)
6139
- throw new Error('bad string length in bson');
6140
- var functionString = buffer.toString('utf8', index, index + stringSize - 1);
6255
+ buffer[index + stringSize - 1] !== 0) {
6256
+ throw new BSONError('bad string length in bson');
6257
+ }
6258
+ var functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6141
6259
  // If we are evaluating the functions
6142
6260
  if (evalFunctions) {
6143
6261
  // If we have cache enabled let's look for the md5 of the function in the cache
@@ -6162,7 +6280,7 @@
6162
6280
  (buffer[index++] << 24);
6163
6281
  // Element cannot be shorter than totalSize + stringSize + documentSize + terminator
6164
6282
  if (totalSize < 4 + 4 + 4 + 1) {
6165
- throw new Error('code_w_scope total size shorter minimum expected length');
6283
+ throw new BSONError('code_w_scope total size shorter minimum expected length');
6166
6284
  }
6167
6285
  // Get the code string size
6168
6286
  var stringSize = buffer[index++] |
@@ -6172,10 +6290,11 @@
6172
6290
  // Check if we have a valid string
6173
6291
  if (stringSize <= 0 ||
6174
6292
  stringSize > buffer.length - index ||
6175
- buffer[index + stringSize - 1] !== 0)
6176
- throw new Error('bad string length in bson');
6293
+ buffer[index + stringSize - 1] !== 0) {
6294
+ throw new BSONError('bad string length in bson');
6295
+ }
6177
6296
  // Javascript function
6178
- var functionString = buffer.toString('utf8', index, index + stringSize - 1);
6297
+ var functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6179
6298
  // Update parse index position
6180
6299
  index = index + stringSize;
6181
6300
  // Parse the element
@@ -6191,11 +6310,11 @@
6191
6310
  index = index + objectSize;
6192
6311
  // Check if field length is too short
6193
6312
  if (totalSize < 4 + 4 + objectSize + stringSize) {
6194
- throw new Error('code_w_scope total size is too short, truncating scope');
6313
+ throw new BSONError('code_w_scope total size is too short, truncating scope');
6195
6314
  }
6196
6315
  // Check if totalSize field is too long
6197
6316
  if (totalSize > 4 + 4 + objectSize + stringSize) {
6198
- throw new Error('code_w_scope total size is too long, clips outer document');
6317
+ throw new BSONError('code_w_scope total size is too long, clips outer document');
6199
6318
  }
6200
6319
  // If we are evaluating the functions
6201
6320
  if (evalFunctions) {
@@ -6223,10 +6342,12 @@
6223
6342
  if (stringSize <= 0 ||
6224
6343
  stringSize > buffer.length - index ||
6225
6344
  buffer[index + stringSize - 1] !== 0)
6226
- throw new Error('bad string length in bson');
6345
+ throw new BSONError('bad string length in bson');
6227
6346
  // Namespace
6228
- if (!validateUtf8(buffer, index, index + stringSize - 1)) {
6229
- throw new Error('Invalid UTF-8 string in BSON document');
6347
+ if (validation != null && validation.utf8) {
6348
+ if (!validateUtf8(buffer, index, index + stringSize - 1)) {
6349
+ throw new BSONError('Invalid UTF-8 string in BSON document');
6350
+ }
6230
6351
  }
6231
6352
  var namespace = buffer.toString('utf8', index, index + stringSize - 1);
6232
6353
  // Update parse index position
@@ -6241,7 +6362,7 @@
6241
6362
  value = new DBRef(namespace, oid);
6242
6363
  }
6243
6364
  else {
6244
- throw new Error('Detected unknown BSON type ' + elementType.toString(16) + ' for fieldname "' + name + '"');
6365
+ throw new BSONError('Detected unknown BSON type ' + elementType.toString(16) + ' for fieldname "' + name + '"');
6245
6366
  }
6246
6367
  if (name === '__proto__') {
6247
6368
  Object.defineProperty(object, name, {
@@ -6258,8 +6379,8 @@
6258
6379
  // Check if the deserialization was against a valid array/object
6259
6380
  if (size !== index - startIndex) {
6260
6381
  if (isArray)
6261
- throw new Error('corrupt array bson');
6262
- throw new Error('corrupt object bson');
6382
+ throw new BSONError('corrupt array bson');
6383
+ throw new BSONError('corrupt object bson');
6263
6384
  }
6264
6385
  // if we did not find "$ref", "$id", "$db", or found an extraneous $key, don't make a DBRef
6265
6386
  if (!isPossibleDBRef)
@@ -6288,6 +6409,21 @@
6288
6409
  // Set the object
6289
6410
  return functionCache[functionString].bind(object);
6290
6411
  }
6412
+ function getValidatedString(buffer, start, end, shouldValidateUtf8) {
6413
+ var value = buffer.toString('utf8', start, end);
6414
+ // if utf8 validation is on, do the check
6415
+ if (shouldValidateUtf8) {
6416
+ for (var i = 0; i < value.length; i++) {
6417
+ if (value.charCodeAt(i) === 0xfffd) {
6418
+ if (!validateUtf8(buffer, start, end)) {
6419
+ throw new BSONError('Invalid UTF-8 string in BSON document');
6420
+ }
6421
+ break;
6422
+ }
6423
+ }
6424
+ }
6425
+ return value;
6426
+ }
6291
6427
 
6292
6428
  // Copyright (c) 2008, Fair Oaks Labs, Inc.
6293
6429
  function writeIEEE754(buffer, value, offset, endian, mLen, nBytes) {
@@ -6572,7 +6708,7 @@
6572
6708
  buffer.set(value.id.subarray(0, 12), index);
6573
6709
  }
6574
6710
  else {
6575
- throw new TypeError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
6711
+ throw new BSONTypeError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
6576
6712
  }
6577
6713
  // Adjust index
6578
6714
  return index + 12;
@@ -6611,7 +6747,7 @@
6611
6747
  if (path === void 0) { path = []; }
6612
6748
  for (var i = 0; i < path.length; i++) {
6613
6749
  if (path[i] === value)
6614
- throw new Error('cyclic dependency detected');
6750
+ throw new BSONError('cyclic dependency detected');
6615
6751
  }
6616
6752
  // Push value to stack
6617
6753
  path.push(value);
@@ -6912,9 +7048,7 @@
6912
7048
  var key = '' + i;
6913
7049
  var value = object[i];
6914
7050
  // Is there an override value
6915
- if (value && value.toBSON) {
6916
- if (typeof value.toBSON !== 'function')
6917
- throw new TypeError('toBSON is not a function');
7051
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
6918
7052
  value = value.toBSON();
6919
7053
  }
6920
7054
  if (typeof value === 'string') {
@@ -6924,7 +7058,7 @@
6924
7058
  index = serializeNumber(buffer, key, value, index, true);
6925
7059
  }
6926
7060
  else if (typeof value === 'bigint') {
6927
- throw new TypeError('Unsupported type BigInt, please use Decimal128');
7061
+ throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
6928
7062
  }
6929
7063
  else if (typeof value === 'boolean') {
6930
7064
  index = serializeBoolean(buffer, key, value, index, true);
@@ -6986,7 +7120,7 @@
6986
7120
  index = serializeMinMax(buffer, key, value, index, true);
6987
7121
  }
6988
7122
  else if (typeof value['_bsontype'] !== 'undefined') {
6989
- throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
7123
+ throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
6990
7124
  }
6991
7125
  }
6992
7126
  }
@@ -7028,7 +7162,7 @@
7028
7162
  index = serializeNumber(buffer, key, value, index);
7029
7163
  }
7030
7164
  else if (type === 'bigint' || isBigInt64Array(value) || isBigUInt64Array(value)) {
7031
- throw new TypeError('Unsupported type BigInt, please use Decimal128');
7165
+ throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
7032
7166
  }
7033
7167
  else if (type === 'boolean') {
7034
7168
  index = serializeBoolean(buffer, key, value, index);
@@ -7085,26 +7219,23 @@
7085
7219
  index = serializeMinMax(buffer, key, value, index);
7086
7220
  }
7087
7221
  else if (typeof value['_bsontype'] !== 'undefined') {
7088
- throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
7222
+ throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
7089
7223
  }
7090
7224
  }
7091
7225
  }
7092
7226
  else {
7093
- // Did we provide a custom serialization method
7094
- if (object.toBSON) {
7095
- if (typeof object.toBSON !== 'function')
7096
- throw new TypeError('toBSON is not a function');
7227
+ if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
7228
+ // Provided a custom serialization method
7097
7229
  object = object.toBSON();
7098
- if (object != null && typeof object !== 'object')
7099
- throw new TypeError('toBSON function did not return an object');
7230
+ if (object != null && typeof object !== 'object') {
7231
+ throw new BSONTypeError('toBSON function did not return an object');
7232
+ }
7100
7233
  }
7101
7234
  // Iterate over all the keys
7102
7235
  for (var key in object) {
7103
7236
  var value = object[key];
7104
7237
  // Is there an override value
7105
- if (value && value.toBSON) {
7106
- if (typeof value.toBSON !== 'function')
7107
- throw new TypeError('toBSON is not a function');
7238
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
7108
7239
  value = value.toBSON();
7109
7240
  }
7110
7241
  // Check the type of the value
@@ -7132,7 +7263,7 @@
7132
7263
  index = serializeNumber(buffer, key, value, index);
7133
7264
  }
7134
7265
  else if (type === 'bigint') {
7135
- throw new TypeError('Unsupported type BigInt, please use Decimal128');
7266
+ throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
7136
7267
  }
7137
7268
  else if (type === 'boolean') {
7138
7269
  index = serializeBoolean(buffer, key, value, index);
@@ -7193,7 +7324,7 @@
7193
7324
  index = serializeMinMax(buffer, key, value, index);
7194
7325
  }
7195
7326
  else if (typeof value['_bsontype'] !== 'undefined') {
7196
- throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
7327
+ throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
7197
7328
  }
7198
7329
  }
7199
7330
  }
@@ -7366,13 +7497,19 @@
7366
7497
  serializeWithBufferAndIndex: serializeWithBufferAndIndex,
7367
7498
  deserialize: deserialize,
7368
7499
  calculateObjectSize: calculateObjectSize,
7369
- deserializeStream: deserializeStream
7500
+ deserializeStream: deserializeStream,
7501
+ BSONError: BSONError,
7502
+ BSONTypeError: BSONTypeError
7370
7503
  };
7371
7504
 
7505
+ exports.BSONError = BSONError;
7372
7506
  exports.BSONRegExp = BSONRegExp;
7373
7507
  exports.BSONSymbol = BSONSymbol;
7508
+ exports.BSONTypeError = BSONTypeError;
7374
7509
  exports.BSON_BINARY_SUBTYPE_BYTE_ARRAY = BSON_BINARY_SUBTYPE_BYTE_ARRAY;
7510
+ exports.BSON_BINARY_SUBTYPE_COLUMN = BSON_BINARY_SUBTYPE_COLUMN;
7375
7511
  exports.BSON_BINARY_SUBTYPE_DEFAULT = BSON_BINARY_SUBTYPE_DEFAULT;
7512
+ exports.BSON_BINARY_SUBTYPE_ENCRYPTED = BSON_BINARY_SUBTYPE_ENCRYPTED;
7376
7513
  exports.BSON_BINARY_SUBTYPE_FUNCTION = BSON_BINARY_SUBTYPE_FUNCTION;
7377
7514
  exports.BSON_BINARY_SUBTYPE_MD5 = BSON_BINARY_SUBTYPE_MD5;
7378
7515
  exports.BSON_BINARY_SUBTYPE_USER_DEFINED = BSON_BINARY_SUBTYPE_USER_DEFINED;