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.
@@ -2077,11 +2077,29 @@
2077
2077
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
2078
2078
  }
2079
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
+
2080
2096
  /** @public */
2081
2097
  var BSONError = /** @class */ (function (_super) {
2082
2098
  __extends(BSONError, _super);
2083
- function BSONError() {
2084
- return _super !== null && _super.apply(this, arguments) || this;
2099
+ function BSONError(message) {
2100
+ var _this = _super.call(this, message) || this;
2101
+ Object.setPrototypeOf(_this, BSONError.prototype);
2102
+ return _this;
2085
2103
  }
2086
2104
  Object.defineProperty(BSONError.prototype, "name", {
2087
2105
  get: function () {
@@ -2095,8 +2113,10 @@
2095
2113
  /** @public */
2096
2114
  var BSONTypeError = /** @class */ (function (_super) {
2097
2115
  __extends(BSONTypeError, _super);
2098
- function BSONTypeError() {
2099
- return _super !== null && _super.apply(this, arguments) || this;
2116
+ function BSONTypeError(message) {
2117
+ var _this = _super.call(this, message) || this;
2118
+ Object.setPrototypeOf(_this, BSONTypeError.prototype);
2119
+ return _this;
2100
2120
  }
2101
2121
  Object.defineProperty(BSONTypeError.prototype, "name", {
2102
2122
  get: function () {
@@ -2219,7 +2239,7 @@
2219
2239
  * @param potentialBuffer - The potential buffer
2220
2240
  * @returns Buffer the input if potentialBuffer is a buffer, or a buffer that
2221
2241
  * wraps a passed in Uint8Array
2222
- * @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
2223
2243
  */
2224
2244
  function ensureBuffer(potentialBuffer) {
2225
2245
  if (ArrayBuffer.isView(potentialBuffer)) {
@@ -3786,9 +3806,15 @@
3786
3806
  if (typeof bytes === 'string') {
3787
3807
  this.bytes = Decimal128.fromString(bytes).bytes;
3788
3808
  }
3789
- else {
3809
+ else if (isUint8Array(bytes)) {
3810
+ if (bytes.byteLength !== 16) {
3811
+ throw new BSONTypeError('Decimal128 must take a Buffer of 16 bytes');
3812
+ }
3790
3813
  this.bytes = bytes;
3791
3814
  }
3815
+ else {
3816
+ throw new BSONTypeError('Decimal128 must take a Buffer or string');
3817
+ }
3792
3818
  }
3793
3819
  /**
3794
3820
  * Create a Decimal128 instance from a string representation
@@ -4573,7 +4599,7 @@
4573
4599
  this[kId] = buffer_1.from(workingId, 'hex');
4574
4600
  }
4575
4601
  else {
4576
- throw new BSONTypeError('Argument passed in must be a 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 or an integer');
4577
4603
  }
4578
4604
  }
4579
4605
  else {
@@ -4690,7 +4716,7 @@
4690
4716
  return false;
4691
4717
  }
4692
4718
  if (otherId instanceof ObjectId) {
4693
- return this.toString() === otherId.toString();
4719
+ return this[kId][11] === otherId[kId][11] && this[kId].equals(otherId[kId]);
4694
4720
  }
4695
4721
  if (typeof otherId === 'string' &&
4696
4722
  ObjectId.isValid(otherId) &&
@@ -4707,7 +4733,9 @@
4707
4733
  if (typeof otherId === 'object' &&
4708
4734
  'toHexString' in otherId &&
4709
4735
  typeof otherId.toHexString === 'function') {
4710
- return otherId.toHexString() === this.toHexString();
4736
+ var otherIdString = otherId.toHexString();
4737
+ var thisIdString = this.toHexString().toLowerCase();
4738
+ return typeof otherIdString === 'string' && otherIdString.toLowerCase() === thisIdString;
4711
4739
  }
4712
4740
  return false;
4713
4741
  };
@@ -4754,26 +4782,13 @@
4754
4782
  ObjectId.isValid = function (id) {
4755
4783
  if (id == null)
4756
4784
  return false;
4757
- if (typeof id === 'number') {
4758
- return true;
4759
- }
4760
- if (typeof id === 'string') {
4761
- return id.length === 12 || (id.length === 24 && checkForHexRegExp.test(id));
4762
- }
4763
- if (id instanceof ObjectId) {
4764
- return true;
4765
- }
4766
- if (isUint8Array(id) && id.length === 12) {
4785
+ try {
4786
+ new ObjectId(id);
4767
4787
  return true;
4768
4788
  }
4769
- // Duck-Typing detection of ObjectId like objects
4770
- if (typeof id === 'object' && 'toHexString' in id && typeof id.toHexString === 'function') {
4771
- if (typeof id.id === 'string') {
4772
- return id.id.length === 12;
4773
- }
4774
- return id.toHexString().length === 24 && checkForHexRegExp.test(id.id.toString('hex'));
4789
+ catch (_a) {
4790
+ return false;
4775
4791
  }
4776
- return false;
4777
4792
  };
4778
4793
  /** @internal */
4779
4794
  ObjectId.prototype.toExtendedJSON = function () {
@@ -5569,7 +5584,7 @@
5569
5584
  }
5570
5585
  else {
5571
5586
  // If we have toBSON defined, override the current object
5572
- if (object.toBSON) {
5587
+ if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
5573
5588
  object = object.toBSON();
5574
5589
  }
5575
5590
  // Calculate size
@@ -5587,7 +5602,7 @@
5587
5602
  if (isArray === void 0) { isArray = false; }
5588
5603
  if (ignoreUndefined === void 0) { ignoreUndefined = false; }
5589
5604
  // If we have toBSON defined, override the current object
5590
- if (value && value.toBSON) {
5605
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
5591
5606
  value = value.toBSON();
5592
5607
  }
5593
5608
  switch (typeof value) {
@@ -5833,6 +5848,43 @@
5833
5848
  var promoteBuffers = options['promoteBuffers'] == null ? false : options['promoteBuffers'];
5834
5849
  var promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
5835
5850
  var promoteValues = options['promoteValues'] == null ? true : options['promoteValues'];
5851
+ // Ensures default validation option if none given
5852
+ var validation = options.validation == null ? { utf8: true } : options.validation;
5853
+ // Shows if global utf-8 validation is enabled or disabled
5854
+ var globalUTFValidation = true;
5855
+ // Reflects utf-8 validation setting regardless of global or specific key validation
5856
+ var validationSetting;
5857
+ // Set of keys either to enable or disable validation on
5858
+ var utf8KeysSet = new Set();
5859
+ // Check for boolean uniformity and empty validation option
5860
+ var utf8ValidatedKeys = validation.utf8;
5861
+ if (typeof utf8ValidatedKeys === 'boolean') {
5862
+ validationSetting = utf8ValidatedKeys;
5863
+ }
5864
+ else {
5865
+ globalUTFValidation = false;
5866
+ var utf8ValidationValues = Object.keys(utf8ValidatedKeys).map(function (key) {
5867
+ return utf8ValidatedKeys[key];
5868
+ });
5869
+ if (utf8ValidationValues.length === 0) {
5870
+ throw new BSONError('UTF-8 validation setting cannot be empty');
5871
+ }
5872
+ if (typeof utf8ValidationValues[0] !== 'boolean') {
5873
+ throw new BSONError('Invalid UTF-8 validation option, must specify boolean values');
5874
+ }
5875
+ validationSetting = utf8ValidationValues[0];
5876
+ // Ensures boolean uniformity in utf-8 validation (all true or all false)
5877
+ if (!utf8ValidationValues.every(function (item) { return item === validationSetting; })) {
5878
+ throw new BSONError('Invalid UTF-8 validation option - keys must be all true or all false');
5879
+ }
5880
+ }
5881
+ // Add keys to set that will either be validated or not based on validationSetting
5882
+ if (!globalUTFValidation) {
5883
+ for (var _i = 0, _a = Object.keys(utf8ValidatedKeys); _i < _a.length; _i++) {
5884
+ var key = _a[_i];
5885
+ utf8KeysSet.add(key);
5886
+ }
5887
+ }
5836
5888
  // Set the start index
5837
5889
  var startIndex = index;
5838
5890
  // Validate that we have at least 4 bytes of buffer
@@ -5865,7 +5917,16 @@
5865
5917
  // If are at the end of the buffer there is a problem with the document
5866
5918
  if (i >= buffer.byteLength)
5867
5919
  throw new BSONError('Bad BSON Document: illegal CString');
5920
+ // Represents the key
5868
5921
  var name = isArray ? arrayIndex++ : buffer.toString('utf8', index, i);
5922
+ // shouldValidateKey is true if the key should be validated, false otherwise
5923
+ var shouldValidateKey = true;
5924
+ if (globalUTFValidation || utf8KeysSet.has(name)) {
5925
+ shouldValidateKey = validationSetting;
5926
+ }
5927
+ else {
5928
+ shouldValidateKey = !validationSetting;
5929
+ }
5869
5930
  if (isPossibleDBRef !== false && name[0] === '$') {
5870
5931
  isPossibleDBRef = allowedDBRefKeys.test(name);
5871
5932
  }
@@ -5881,7 +5942,7 @@
5881
5942
  buffer[index + stringSize - 1] !== 0) {
5882
5943
  throw new BSONError('bad string length in bson');
5883
5944
  }
5884
- value = getValidatedString(buffer, index, index + stringSize - 1);
5945
+ value = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
5885
5946
  index = index + stringSize;
5886
5947
  }
5887
5948
  else if (elementType === BSON_DATA_OID) {
@@ -5937,7 +5998,11 @@
5937
5998
  value = buffer.slice(index, index + objectSize);
5938
5999
  }
5939
6000
  else {
5940
- value = deserializeObject(buffer, _index, options, false);
6001
+ var objectOptions = options;
6002
+ if (!globalUTFValidation) {
6003
+ objectOptions = _assign(_assign({}, options), { validation: { utf8: shouldValidateKey } });
6004
+ }
6005
+ value = deserializeObject(buffer, _index, objectOptions, false);
5941
6006
  }
5942
6007
  index = index + objectSize;
5943
6008
  }
@@ -5958,6 +6023,9 @@
5958
6023
  }
5959
6024
  arrayOptions['raw'] = true;
5960
6025
  }
6026
+ if (!globalUTFValidation) {
6027
+ arrayOptions = _assign(_assign({}, arrayOptions), { validation: { utf8: shouldValidateKey } });
6028
+ }
5961
6029
  value = deserializeObject(buffer, _index, arrayOptions, true);
5962
6030
  index = index + objectSize;
5963
6031
  if (buffer[index - 1] !== 0)
@@ -6158,7 +6226,7 @@
6158
6226
  buffer[index + stringSize - 1] !== 0) {
6159
6227
  throw new BSONError('bad string length in bson');
6160
6228
  }
6161
- var symbol = getValidatedString(buffer, index, index + stringSize - 1);
6229
+ var symbol = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6162
6230
  value = promoteValues ? symbol : new BSONSymbol(symbol);
6163
6231
  index = index + stringSize;
6164
6232
  }
@@ -6189,7 +6257,7 @@
6189
6257
  buffer[index + stringSize - 1] !== 0) {
6190
6258
  throw new BSONError('bad string length in bson');
6191
6259
  }
6192
- var functionString = getValidatedString(buffer, index, index + stringSize - 1);
6260
+ var functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6193
6261
  // If we are evaluating the functions
6194
6262
  if (evalFunctions) {
6195
6263
  // If we have cache enabled let's look for the md5 of the function in the cache
@@ -6228,7 +6296,7 @@
6228
6296
  throw new BSONError('bad string length in bson');
6229
6297
  }
6230
6298
  // Javascript function
6231
- var functionString = getValidatedString(buffer, index, index + stringSize - 1);
6299
+ var functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6232
6300
  // Update parse index position
6233
6301
  index = index + stringSize;
6234
6302
  // Parse the element
@@ -6278,8 +6346,10 @@
6278
6346
  buffer[index + stringSize - 1] !== 0)
6279
6347
  throw new BSONError('bad string length in bson');
6280
6348
  // Namespace
6281
- if (!validateUtf8(buffer, index, index + stringSize - 1)) {
6282
- throw new BSONError('Invalid UTF-8 string in BSON document');
6349
+ if (validation != null && validation.utf8) {
6350
+ if (!validateUtf8(buffer, index, index + stringSize - 1)) {
6351
+ throw new BSONError('Invalid UTF-8 string in BSON document');
6352
+ }
6283
6353
  }
6284
6354
  var namespace = buffer.toString('utf8', index, index + stringSize - 1);
6285
6355
  // Update parse index position
@@ -6341,14 +6411,17 @@
6341
6411
  // Set the object
6342
6412
  return functionCache[functionString].bind(object);
6343
6413
  }
6344
- function getValidatedString(buffer, start, end) {
6414
+ function getValidatedString(buffer, start, end, shouldValidateUtf8) {
6345
6415
  var value = buffer.toString('utf8', start, end);
6346
- for (var i = 0; i < value.length; i++) {
6347
- if (value.charCodeAt(i) === 0xfffd) {
6348
- if (!validateUtf8(buffer, start, end)) {
6349
- throw new BSONError('Invalid UTF-8 string in BSON document');
6416
+ // if utf8 validation is on, do the check
6417
+ if (shouldValidateUtf8) {
6418
+ for (var i = 0; i < value.length; i++) {
6419
+ if (value.charCodeAt(i) === 0xfffd) {
6420
+ if (!validateUtf8(buffer, start, end)) {
6421
+ throw new BSONError('Invalid UTF-8 string in BSON document');
6422
+ }
6423
+ break;
6350
6424
  }
6351
- break;
6352
6425
  }
6353
6426
  }
6354
6427
  return value;
@@ -6977,9 +7050,7 @@
6977
7050
  var key = '' + i;
6978
7051
  var value = object[i];
6979
7052
  // Is there an override value
6980
- if (value && value.toBSON) {
6981
- if (typeof value.toBSON !== 'function')
6982
- throw new BSONTypeError('toBSON is not a function');
7053
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
6983
7054
  value = value.toBSON();
6984
7055
  }
6985
7056
  if (typeof value === 'string') {
@@ -7155,21 +7226,18 @@
7155
7226
  }
7156
7227
  }
7157
7228
  else {
7158
- // Did we provide a custom serialization method
7159
- if (object.toBSON) {
7160
- if (typeof object.toBSON !== 'function')
7161
- throw new BSONTypeError('toBSON is not a function');
7229
+ if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
7230
+ // Provided a custom serialization method
7162
7231
  object = object.toBSON();
7163
- if (object != null && typeof object !== 'object')
7232
+ if (object != null && typeof object !== 'object') {
7164
7233
  throw new BSONTypeError('toBSON function did not return an object');
7234
+ }
7165
7235
  }
7166
7236
  // Iterate over all the keys
7167
7237
  for (var key in object) {
7168
7238
  var value = object[key];
7169
7239
  // Is there an override value
7170
- if (value && value.toBSON) {
7171
- if (typeof value.toBSON !== 'function')
7172
- throw new BSONTypeError('toBSON is not a function');
7240
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
7173
7241
  value = value.toBSON();
7174
7242
  }
7175
7243
  // Check the type of the value