bson 4.5.4 → 4.6.2

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/dist/bson.esm.js CHANGED
@@ -40,11 +40,29 @@ function __extends(d, b) {
40
40
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
41
41
  }
42
42
 
43
+ var _assign = function __assign() {
44
+ _assign = Object.assign || function __assign(t) {
45
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
46
+ s = arguments[i];
47
+
48
+ for (var p in s) {
49
+ if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
50
+ }
51
+ }
52
+
53
+ return t;
54
+ };
55
+
56
+ return _assign.apply(this, arguments);
57
+ };
58
+
43
59
  /** @public */
44
60
  var BSONError = /** @class */ (function (_super) {
45
61
  __extends(BSONError, _super);
46
- function BSONError() {
47
- return _super !== null && _super.apply(this, arguments) || this;
62
+ function BSONError(message) {
63
+ var _this = _super.call(this, message) || this;
64
+ Object.setPrototypeOf(_this, BSONError.prototype);
65
+ return _this;
48
66
  }
49
67
  Object.defineProperty(BSONError.prototype, "name", {
50
68
  get: function () {
@@ -58,8 +76,10 @@ var BSONError = /** @class */ (function (_super) {
58
76
  /** @public */
59
77
  var BSONTypeError = /** @class */ (function (_super) {
60
78
  __extends(BSONTypeError, _super);
61
- function BSONTypeError() {
62
- return _super !== null && _super.apply(this, arguments) || this;
79
+ function BSONTypeError(message) {
80
+ var _this = _super.call(this, message) || this;
81
+ Object.setPrototypeOf(_this, BSONTypeError.prototype);
82
+ return _this;
63
83
  }
64
84
  Object.defineProperty(BSONTypeError.prototype, "name", {
65
85
  get: function () {
@@ -182,7 +202,7 @@ function deprecate(fn, message) {
182
202
  * @param potentialBuffer - The potential buffer
183
203
  * @returns Buffer the input if potentialBuffer is a buffer, or a buffer that
184
204
  * wraps a passed in Uint8Array
185
- * @throws TypeError If anything other than a Buffer or Uint8Array is passed in
205
+ * @throws BSONTypeError If anything other than a Buffer or Uint8Array is passed in
186
206
  */
187
207
  function ensureBuffer(potentialBuffer) {
188
208
  if (ArrayBuffer.isView(potentialBuffer)) {
@@ -1749,9 +1769,15 @@ var Decimal128 = /** @class */ (function () {
1749
1769
  if (typeof bytes === 'string') {
1750
1770
  this.bytes = Decimal128.fromString(bytes).bytes;
1751
1771
  }
1752
- else {
1772
+ else if (isUint8Array(bytes)) {
1773
+ if (bytes.byteLength !== 16) {
1774
+ throw new BSONTypeError('Decimal128 must take a Buffer of 16 bytes');
1775
+ }
1753
1776
  this.bytes = bytes;
1754
1777
  }
1778
+ else {
1779
+ throw new BSONTypeError('Decimal128 must take a Buffer or string');
1780
+ }
1755
1781
  }
1756
1782
  /**
1757
1783
  * Create a Decimal128 instance from a string representation
@@ -2536,7 +2562,7 @@ var ObjectId = /** @class */ (function () {
2536
2562
  this[kId] = Buffer.from(workingId, 'hex');
2537
2563
  }
2538
2564
  else {
2539
- throw new BSONTypeError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters');
2565
+ throw new BSONTypeError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer');
2540
2566
  }
2541
2567
  }
2542
2568
  else {
@@ -2653,7 +2679,7 @@ var ObjectId = /** @class */ (function () {
2653
2679
  return false;
2654
2680
  }
2655
2681
  if (otherId instanceof ObjectId) {
2656
- return this.toString() === otherId.toString();
2682
+ return this[kId][11] === otherId[kId][11] && this[kId].equals(otherId[kId]);
2657
2683
  }
2658
2684
  if (typeof otherId === 'string' &&
2659
2685
  ObjectId.isValid(otherId) &&
@@ -2670,7 +2696,9 @@ var ObjectId = /** @class */ (function () {
2670
2696
  if (typeof otherId === 'object' &&
2671
2697
  'toHexString' in otherId &&
2672
2698
  typeof otherId.toHexString === 'function') {
2673
- return otherId.toHexString() === this.toHexString();
2699
+ var otherIdString = otherId.toHexString();
2700
+ var thisIdString = this.toHexString().toLowerCase();
2701
+ return typeof otherIdString === 'string' && otherIdString.toLowerCase() === thisIdString;
2674
2702
  }
2675
2703
  return false;
2676
2704
  };
@@ -2717,26 +2745,13 @@ var ObjectId = /** @class */ (function () {
2717
2745
  ObjectId.isValid = function (id) {
2718
2746
  if (id == null)
2719
2747
  return false;
2720
- if (typeof id === 'number') {
2721
- return true;
2722
- }
2723
- if (typeof id === 'string') {
2724
- return id.length === 12 || (id.length === 24 && checkForHexRegExp.test(id));
2725
- }
2726
- if (id instanceof ObjectId) {
2727
- return true;
2728
- }
2729
- if (isUint8Array(id) && id.length === 12) {
2748
+ try {
2749
+ new ObjectId(id);
2730
2750
  return true;
2731
2751
  }
2732
- // Duck-Typing detection of ObjectId like objects
2733
- if (typeof id === 'object' && 'toHexString' in id && typeof id.toHexString === 'function') {
2734
- if (typeof id.id === 'string') {
2735
- return id.id.length === 12;
2736
- }
2737
- return id.toHexString().length === 24 && checkForHexRegExp.test(id.id.toString('hex'));
2752
+ catch (_a) {
2753
+ return false;
2738
2754
  }
2739
- return false;
2740
2755
  };
2741
2756
  /** @internal */
2742
2757
  ObjectId.prototype.toExtendedJSON = function () {
@@ -3532,7 +3547,7 @@ function calculateObjectSize$1(object, serializeFunctions, ignoreUndefined) {
3532
3547
  }
3533
3548
  else {
3534
3549
  // If we have toBSON defined, override the current object
3535
- if (object.toBSON) {
3550
+ if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
3536
3551
  object = object.toBSON();
3537
3552
  }
3538
3553
  // Calculate size
@@ -3550,7 +3565,7 @@ value, serializeFunctions, isArray, ignoreUndefined) {
3550
3565
  if (isArray === void 0) { isArray = false; }
3551
3566
  if (ignoreUndefined === void 0) { ignoreUndefined = false; }
3552
3567
  // If we have toBSON defined, override the current object
3553
- if (value && value.toBSON) {
3568
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
3554
3569
  value = value.toBSON();
3555
3570
  }
3556
3571
  switch (typeof value) {
@@ -3796,6 +3811,43 @@ function deserializeObject(buffer, index, options, isArray) {
3796
3811
  var promoteBuffers = options['promoteBuffers'] == null ? false : options['promoteBuffers'];
3797
3812
  var promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
3798
3813
  var promoteValues = options['promoteValues'] == null ? true : options['promoteValues'];
3814
+ // Ensures default validation option if none given
3815
+ var validation = options.validation == null ? { utf8: true } : options.validation;
3816
+ // Shows if global utf-8 validation is enabled or disabled
3817
+ var globalUTFValidation = true;
3818
+ // Reflects utf-8 validation setting regardless of global or specific key validation
3819
+ var validationSetting;
3820
+ // Set of keys either to enable or disable validation on
3821
+ var utf8KeysSet = new Set();
3822
+ // Check for boolean uniformity and empty validation option
3823
+ var utf8ValidatedKeys = validation.utf8;
3824
+ if (typeof utf8ValidatedKeys === 'boolean') {
3825
+ validationSetting = utf8ValidatedKeys;
3826
+ }
3827
+ else {
3828
+ globalUTFValidation = false;
3829
+ var utf8ValidationValues = Object.keys(utf8ValidatedKeys).map(function (key) {
3830
+ return utf8ValidatedKeys[key];
3831
+ });
3832
+ if (utf8ValidationValues.length === 0) {
3833
+ throw new BSONError('UTF-8 validation setting cannot be empty');
3834
+ }
3835
+ if (typeof utf8ValidationValues[0] !== 'boolean') {
3836
+ throw new BSONError('Invalid UTF-8 validation option, must specify boolean values');
3837
+ }
3838
+ validationSetting = utf8ValidationValues[0];
3839
+ // Ensures boolean uniformity in utf-8 validation (all true or all false)
3840
+ if (!utf8ValidationValues.every(function (item) { return item === validationSetting; })) {
3841
+ throw new BSONError('Invalid UTF-8 validation option - keys must be all true or all false');
3842
+ }
3843
+ }
3844
+ // Add keys to set that will either be validated or not based on validationSetting
3845
+ if (!globalUTFValidation) {
3846
+ for (var _i = 0, _a = Object.keys(utf8ValidatedKeys); _i < _a.length; _i++) {
3847
+ var key = _a[_i];
3848
+ utf8KeysSet.add(key);
3849
+ }
3850
+ }
3799
3851
  // Set the start index
3800
3852
  var startIndex = index;
3801
3853
  // Validate that we have at least 4 bytes of buffer
@@ -3828,7 +3880,16 @@ function deserializeObject(buffer, index, options, isArray) {
3828
3880
  // If are at the end of the buffer there is a problem with the document
3829
3881
  if (i >= buffer.byteLength)
3830
3882
  throw new BSONError('Bad BSON Document: illegal CString');
3883
+ // Represents the key
3831
3884
  var name = isArray ? arrayIndex++ : buffer.toString('utf8', index, i);
3885
+ // shouldValidateKey is true if the key should be validated, false otherwise
3886
+ var shouldValidateKey = true;
3887
+ if (globalUTFValidation || utf8KeysSet.has(name)) {
3888
+ shouldValidateKey = validationSetting;
3889
+ }
3890
+ else {
3891
+ shouldValidateKey = !validationSetting;
3892
+ }
3832
3893
  if (isPossibleDBRef !== false && name[0] === '$') {
3833
3894
  isPossibleDBRef = allowedDBRefKeys.test(name);
3834
3895
  }
@@ -3844,7 +3905,7 @@ function deserializeObject(buffer, index, options, isArray) {
3844
3905
  buffer[index + stringSize - 1] !== 0) {
3845
3906
  throw new BSONError('bad string length in bson');
3846
3907
  }
3847
- value = getValidatedString(buffer, index, index + stringSize - 1);
3908
+ value = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
3848
3909
  index = index + stringSize;
3849
3910
  }
3850
3911
  else if (elementType === BSON_DATA_OID) {
@@ -3900,7 +3961,11 @@ function deserializeObject(buffer, index, options, isArray) {
3900
3961
  value = buffer.slice(index, index + objectSize);
3901
3962
  }
3902
3963
  else {
3903
- value = deserializeObject(buffer, _index, options, false);
3964
+ var objectOptions = options;
3965
+ if (!globalUTFValidation) {
3966
+ objectOptions = _assign(_assign({}, options), { validation: { utf8: shouldValidateKey } });
3967
+ }
3968
+ value = deserializeObject(buffer, _index, objectOptions, false);
3904
3969
  }
3905
3970
  index = index + objectSize;
3906
3971
  }
@@ -3921,6 +3986,9 @@ function deserializeObject(buffer, index, options, isArray) {
3921
3986
  }
3922
3987
  arrayOptions['raw'] = true;
3923
3988
  }
3989
+ if (!globalUTFValidation) {
3990
+ arrayOptions = _assign(_assign({}, arrayOptions), { validation: { utf8: shouldValidateKey } });
3991
+ }
3924
3992
  value = deserializeObject(buffer, _index, arrayOptions, true);
3925
3993
  index = index + objectSize;
3926
3994
  if (buffer[index - 1] !== 0)
@@ -4121,7 +4189,7 @@ function deserializeObject(buffer, index, options, isArray) {
4121
4189
  buffer[index + stringSize - 1] !== 0) {
4122
4190
  throw new BSONError('bad string length in bson');
4123
4191
  }
4124
- var symbol = getValidatedString(buffer, index, index + stringSize - 1);
4192
+ var symbol = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
4125
4193
  value = promoteValues ? symbol : new BSONSymbol(symbol);
4126
4194
  index = index + stringSize;
4127
4195
  }
@@ -4152,7 +4220,7 @@ function deserializeObject(buffer, index, options, isArray) {
4152
4220
  buffer[index + stringSize - 1] !== 0) {
4153
4221
  throw new BSONError('bad string length in bson');
4154
4222
  }
4155
- var functionString = getValidatedString(buffer, index, index + stringSize - 1);
4223
+ var functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
4156
4224
  // If we are evaluating the functions
4157
4225
  if (evalFunctions) {
4158
4226
  // If we have cache enabled let's look for the md5 of the function in the cache
@@ -4191,7 +4259,7 @@ function deserializeObject(buffer, index, options, isArray) {
4191
4259
  throw new BSONError('bad string length in bson');
4192
4260
  }
4193
4261
  // Javascript function
4194
- var functionString = getValidatedString(buffer, index, index + stringSize - 1);
4262
+ var functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
4195
4263
  // Update parse index position
4196
4264
  index = index + stringSize;
4197
4265
  // Parse the element
@@ -4241,8 +4309,10 @@ function deserializeObject(buffer, index, options, isArray) {
4241
4309
  buffer[index + stringSize - 1] !== 0)
4242
4310
  throw new BSONError('bad string length in bson');
4243
4311
  // Namespace
4244
- if (!validateUtf8(buffer, index, index + stringSize - 1)) {
4245
- throw new BSONError('Invalid UTF-8 string in BSON document');
4312
+ if (validation != null && validation.utf8) {
4313
+ if (!validateUtf8(buffer, index, index + stringSize - 1)) {
4314
+ throw new BSONError('Invalid UTF-8 string in BSON document');
4315
+ }
4246
4316
  }
4247
4317
  var namespace = buffer.toString('utf8', index, index + stringSize - 1);
4248
4318
  // Update parse index position
@@ -4304,14 +4374,17 @@ function isolateEval(functionString, functionCache, object) {
4304
4374
  // Set the object
4305
4375
  return functionCache[functionString].bind(object);
4306
4376
  }
4307
- function getValidatedString(buffer, start, end) {
4377
+ function getValidatedString(buffer, start, end, shouldValidateUtf8) {
4308
4378
  var value = buffer.toString('utf8', start, end);
4309
- for (var i = 0; i < value.length; i++) {
4310
- if (value.charCodeAt(i) === 0xfffd) {
4311
- if (!validateUtf8(buffer, start, end)) {
4312
- throw new BSONError('Invalid UTF-8 string in BSON document');
4379
+ // if utf8 validation is on, do the check
4380
+ if (shouldValidateUtf8) {
4381
+ for (var i = 0; i < value.length; i++) {
4382
+ if (value.charCodeAt(i) === 0xfffd) {
4383
+ if (!validateUtf8(buffer, start, end)) {
4384
+ throw new BSONError('Invalid UTF-8 string in BSON document');
4385
+ }
4386
+ break;
4313
4387
  }
4314
- break;
4315
4388
  }
4316
4389
  }
4317
4390
  return value;
@@ -4940,9 +5013,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
4940
5013
  var key = '' + i;
4941
5014
  var value = object[i];
4942
5015
  // Is there an override value
4943
- if (value && value.toBSON) {
4944
- if (typeof value.toBSON !== 'function')
4945
- throw new BSONTypeError('toBSON is not a function');
5016
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
4946
5017
  value = value.toBSON();
4947
5018
  }
4948
5019
  if (typeof value === 'string') {
@@ -5118,21 +5189,18 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
5118
5189
  }
5119
5190
  }
5120
5191
  else {
5121
- // Did we provide a custom serialization method
5122
- if (object.toBSON) {
5123
- if (typeof object.toBSON !== 'function')
5124
- throw new BSONTypeError('toBSON is not a function');
5192
+ if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
5193
+ // Provided a custom serialization method
5125
5194
  object = object.toBSON();
5126
- if (object != null && typeof object !== 'object')
5195
+ if (object != null && typeof object !== 'object') {
5127
5196
  throw new BSONTypeError('toBSON function did not return an object');
5197
+ }
5128
5198
  }
5129
5199
  // Iterate over all the keys
5130
5200
  for (var key in object) {
5131
5201
  var value = object[key];
5132
5202
  // Is there an override value
5133
- if (value && value.toBSON) {
5134
- if (typeof value.toBSON !== 'function')
5135
- throw new BSONTypeError('toBSON is not a function');
5203
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
5136
5204
  value = value.toBSON();
5137
5205
  }
5138
5206
  // Check the type of the value