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.
@@ -2074,11 +2074,29 @@ var BSON = (function (exports) {
2074
2074
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
2075
2075
  }
2076
2076
 
2077
+ var _assign = function __assign() {
2078
+ _assign = Object.assign || function __assign(t) {
2079
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
2080
+ s = arguments[i];
2081
+
2082
+ for (var p in s) {
2083
+ if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
2084
+ }
2085
+ }
2086
+
2087
+ return t;
2088
+ };
2089
+
2090
+ return _assign.apply(this, arguments);
2091
+ };
2092
+
2077
2093
  /** @public */
2078
2094
  var BSONError = /** @class */ (function (_super) {
2079
2095
  __extends(BSONError, _super);
2080
- function BSONError() {
2081
- return _super !== null && _super.apply(this, arguments) || this;
2096
+ function BSONError(message) {
2097
+ var _this = _super.call(this, message) || this;
2098
+ Object.setPrototypeOf(_this, BSONError.prototype);
2099
+ return _this;
2082
2100
  }
2083
2101
  Object.defineProperty(BSONError.prototype, "name", {
2084
2102
  get: function () {
@@ -2092,8 +2110,10 @@ var BSON = (function (exports) {
2092
2110
  /** @public */
2093
2111
  var BSONTypeError = /** @class */ (function (_super) {
2094
2112
  __extends(BSONTypeError, _super);
2095
- function BSONTypeError() {
2096
- return _super !== null && _super.apply(this, arguments) || this;
2113
+ function BSONTypeError(message) {
2114
+ var _this = _super.call(this, message) || this;
2115
+ Object.setPrototypeOf(_this, BSONTypeError.prototype);
2116
+ return _this;
2097
2117
  }
2098
2118
  Object.defineProperty(BSONTypeError.prototype, "name", {
2099
2119
  get: function () {
@@ -2216,7 +2236,7 @@ var BSON = (function (exports) {
2216
2236
  * @param potentialBuffer - The potential buffer
2217
2237
  * @returns Buffer the input if potentialBuffer is a buffer, or a buffer that
2218
2238
  * wraps a passed in Uint8Array
2219
- * @throws TypeError If anything other than a Buffer or Uint8Array is passed in
2239
+ * @throws BSONTypeError If anything other than a Buffer or Uint8Array is passed in
2220
2240
  */
2221
2241
  function ensureBuffer(potentialBuffer) {
2222
2242
  if (ArrayBuffer.isView(potentialBuffer)) {
@@ -3783,9 +3803,15 @@ var BSON = (function (exports) {
3783
3803
  if (typeof bytes === 'string') {
3784
3804
  this.bytes = Decimal128.fromString(bytes).bytes;
3785
3805
  }
3786
- else {
3806
+ else if (isUint8Array(bytes)) {
3807
+ if (bytes.byteLength !== 16) {
3808
+ throw new BSONTypeError('Decimal128 must take a Buffer of 16 bytes');
3809
+ }
3787
3810
  this.bytes = bytes;
3788
3811
  }
3812
+ else {
3813
+ throw new BSONTypeError('Decimal128 must take a Buffer or string');
3814
+ }
3789
3815
  }
3790
3816
  /**
3791
3817
  * Create a Decimal128 instance from a string representation
@@ -4570,7 +4596,7 @@ var BSON = (function (exports) {
4570
4596
  this[kId] = buffer_1.from(workingId, 'hex');
4571
4597
  }
4572
4598
  else {
4573
- throw new BSONTypeError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters');
4599
+ throw new BSONTypeError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer');
4574
4600
  }
4575
4601
  }
4576
4602
  else {
@@ -4687,7 +4713,7 @@ var BSON = (function (exports) {
4687
4713
  return false;
4688
4714
  }
4689
4715
  if (otherId instanceof ObjectId) {
4690
- return this.toString() === otherId.toString();
4716
+ return this[kId][11] === otherId[kId][11] && this[kId].equals(otherId[kId]);
4691
4717
  }
4692
4718
  if (typeof otherId === 'string' &&
4693
4719
  ObjectId.isValid(otherId) &&
@@ -4704,7 +4730,9 @@ var BSON = (function (exports) {
4704
4730
  if (typeof otherId === 'object' &&
4705
4731
  'toHexString' in otherId &&
4706
4732
  typeof otherId.toHexString === 'function') {
4707
- return otherId.toHexString() === this.toHexString();
4733
+ var otherIdString = otherId.toHexString();
4734
+ var thisIdString = this.toHexString().toLowerCase();
4735
+ return typeof otherIdString === 'string' && otherIdString.toLowerCase() === thisIdString;
4708
4736
  }
4709
4737
  return false;
4710
4738
  };
@@ -4751,26 +4779,13 @@ var BSON = (function (exports) {
4751
4779
  ObjectId.isValid = function (id) {
4752
4780
  if (id == null)
4753
4781
  return false;
4754
- if (typeof id === 'number') {
4755
- return true;
4756
- }
4757
- if (typeof id === 'string') {
4758
- return id.length === 12 || (id.length === 24 && checkForHexRegExp.test(id));
4759
- }
4760
- if (id instanceof ObjectId) {
4761
- return true;
4762
- }
4763
- if (isUint8Array(id) && id.length === 12) {
4782
+ try {
4783
+ new ObjectId(id);
4764
4784
  return true;
4765
4785
  }
4766
- // Duck-Typing detection of ObjectId like objects
4767
- if (typeof id === 'object' && 'toHexString' in id && typeof id.toHexString === 'function') {
4768
- if (typeof id.id === 'string') {
4769
- return id.id.length === 12;
4770
- }
4771
- return id.toHexString().length === 24 && checkForHexRegExp.test(id.id.toString('hex'));
4786
+ catch (_a) {
4787
+ return false;
4772
4788
  }
4773
- return false;
4774
4789
  };
4775
4790
  /** @internal */
4776
4791
  ObjectId.prototype.toExtendedJSON = function () {
@@ -5566,7 +5581,7 @@ var BSON = (function (exports) {
5566
5581
  }
5567
5582
  else {
5568
5583
  // If we have toBSON defined, override the current object
5569
- if (object.toBSON) {
5584
+ if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
5570
5585
  object = object.toBSON();
5571
5586
  }
5572
5587
  // Calculate size
@@ -5584,7 +5599,7 @@ var BSON = (function (exports) {
5584
5599
  if (isArray === void 0) { isArray = false; }
5585
5600
  if (ignoreUndefined === void 0) { ignoreUndefined = false; }
5586
5601
  // If we have toBSON defined, override the current object
5587
- if (value && value.toBSON) {
5602
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
5588
5603
  value = value.toBSON();
5589
5604
  }
5590
5605
  switch (typeof value) {
@@ -5830,6 +5845,43 @@ var BSON = (function (exports) {
5830
5845
  var promoteBuffers = options['promoteBuffers'] == null ? false : options['promoteBuffers'];
5831
5846
  var promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
5832
5847
  var promoteValues = options['promoteValues'] == null ? true : options['promoteValues'];
5848
+ // Ensures default validation option if none given
5849
+ var validation = options.validation == null ? { utf8: true } : options.validation;
5850
+ // Shows if global utf-8 validation is enabled or disabled
5851
+ var globalUTFValidation = true;
5852
+ // Reflects utf-8 validation setting regardless of global or specific key validation
5853
+ var validationSetting;
5854
+ // Set of keys either to enable or disable validation on
5855
+ var utf8KeysSet = new Set();
5856
+ // Check for boolean uniformity and empty validation option
5857
+ var utf8ValidatedKeys = validation.utf8;
5858
+ if (typeof utf8ValidatedKeys === 'boolean') {
5859
+ validationSetting = utf8ValidatedKeys;
5860
+ }
5861
+ else {
5862
+ globalUTFValidation = false;
5863
+ var utf8ValidationValues = Object.keys(utf8ValidatedKeys).map(function (key) {
5864
+ return utf8ValidatedKeys[key];
5865
+ });
5866
+ if (utf8ValidationValues.length === 0) {
5867
+ throw new BSONError('UTF-8 validation setting cannot be empty');
5868
+ }
5869
+ if (typeof utf8ValidationValues[0] !== 'boolean') {
5870
+ throw new BSONError('Invalid UTF-8 validation option, must specify boolean values');
5871
+ }
5872
+ validationSetting = utf8ValidationValues[0];
5873
+ // Ensures boolean uniformity in utf-8 validation (all true or all false)
5874
+ if (!utf8ValidationValues.every(function (item) { return item === validationSetting; })) {
5875
+ throw new BSONError('Invalid UTF-8 validation option - keys must be all true or all false');
5876
+ }
5877
+ }
5878
+ // Add keys to set that will either be validated or not based on validationSetting
5879
+ if (!globalUTFValidation) {
5880
+ for (var _i = 0, _a = Object.keys(utf8ValidatedKeys); _i < _a.length; _i++) {
5881
+ var key = _a[_i];
5882
+ utf8KeysSet.add(key);
5883
+ }
5884
+ }
5833
5885
  // Set the start index
5834
5886
  var startIndex = index;
5835
5887
  // Validate that we have at least 4 bytes of buffer
@@ -5862,7 +5914,16 @@ var BSON = (function (exports) {
5862
5914
  // If are at the end of the buffer there is a problem with the document
5863
5915
  if (i >= buffer.byteLength)
5864
5916
  throw new BSONError('Bad BSON Document: illegal CString');
5917
+ // Represents the key
5865
5918
  var name = isArray ? arrayIndex++ : buffer.toString('utf8', index, i);
5919
+ // shouldValidateKey is true if the key should be validated, false otherwise
5920
+ var shouldValidateKey = true;
5921
+ if (globalUTFValidation || utf8KeysSet.has(name)) {
5922
+ shouldValidateKey = validationSetting;
5923
+ }
5924
+ else {
5925
+ shouldValidateKey = !validationSetting;
5926
+ }
5866
5927
  if (isPossibleDBRef !== false && name[0] === '$') {
5867
5928
  isPossibleDBRef = allowedDBRefKeys.test(name);
5868
5929
  }
@@ -5878,7 +5939,7 @@ var BSON = (function (exports) {
5878
5939
  buffer[index + stringSize - 1] !== 0) {
5879
5940
  throw new BSONError('bad string length in bson');
5880
5941
  }
5881
- value = getValidatedString(buffer, index, index + stringSize - 1);
5942
+ value = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
5882
5943
  index = index + stringSize;
5883
5944
  }
5884
5945
  else if (elementType === BSON_DATA_OID) {
@@ -5934,7 +5995,11 @@ var BSON = (function (exports) {
5934
5995
  value = buffer.slice(index, index + objectSize);
5935
5996
  }
5936
5997
  else {
5937
- value = deserializeObject(buffer, _index, options, false);
5998
+ var objectOptions = options;
5999
+ if (!globalUTFValidation) {
6000
+ objectOptions = _assign(_assign({}, options), { validation: { utf8: shouldValidateKey } });
6001
+ }
6002
+ value = deserializeObject(buffer, _index, objectOptions, false);
5938
6003
  }
5939
6004
  index = index + objectSize;
5940
6005
  }
@@ -5955,6 +6020,9 @@ var BSON = (function (exports) {
5955
6020
  }
5956
6021
  arrayOptions['raw'] = true;
5957
6022
  }
6023
+ if (!globalUTFValidation) {
6024
+ arrayOptions = _assign(_assign({}, arrayOptions), { validation: { utf8: shouldValidateKey } });
6025
+ }
5958
6026
  value = deserializeObject(buffer, _index, arrayOptions, true);
5959
6027
  index = index + objectSize;
5960
6028
  if (buffer[index - 1] !== 0)
@@ -6155,7 +6223,7 @@ var BSON = (function (exports) {
6155
6223
  buffer[index + stringSize - 1] !== 0) {
6156
6224
  throw new BSONError('bad string length in bson');
6157
6225
  }
6158
- var symbol = getValidatedString(buffer, index, index + stringSize - 1);
6226
+ var symbol = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6159
6227
  value = promoteValues ? symbol : new BSONSymbol(symbol);
6160
6228
  index = index + stringSize;
6161
6229
  }
@@ -6186,7 +6254,7 @@ var BSON = (function (exports) {
6186
6254
  buffer[index + stringSize - 1] !== 0) {
6187
6255
  throw new BSONError('bad string length in bson');
6188
6256
  }
6189
- var functionString = getValidatedString(buffer, index, index + stringSize - 1);
6257
+ var functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6190
6258
  // If we are evaluating the functions
6191
6259
  if (evalFunctions) {
6192
6260
  // If we have cache enabled let's look for the md5 of the function in the cache
@@ -6225,7 +6293,7 @@ var BSON = (function (exports) {
6225
6293
  throw new BSONError('bad string length in bson');
6226
6294
  }
6227
6295
  // Javascript function
6228
- var functionString = getValidatedString(buffer, index, index + stringSize - 1);
6296
+ var functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6229
6297
  // Update parse index position
6230
6298
  index = index + stringSize;
6231
6299
  // Parse the element
@@ -6275,8 +6343,10 @@ var BSON = (function (exports) {
6275
6343
  buffer[index + stringSize - 1] !== 0)
6276
6344
  throw new BSONError('bad string length in bson');
6277
6345
  // Namespace
6278
- if (!validateUtf8(buffer, index, index + stringSize - 1)) {
6279
- throw new BSONError('Invalid UTF-8 string in BSON document');
6346
+ if (validation != null && validation.utf8) {
6347
+ if (!validateUtf8(buffer, index, index + stringSize - 1)) {
6348
+ throw new BSONError('Invalid UTF-8 string in BSON document');
6349
+ }
6280
6350
  }
6281
6351
  var namespace = buffer.toString('utf8', index, index + stringSize - 1);
6282
6352
  // Update parse index position
@@ -6338,14 +6408,17 @@ var BSON = (function (exports) {
6338
6408
  // Set the object
6339
6409
  return functionCache[functionString].bind(object);
6340
6410
  }
6341
- function getValidatedString(buffer, start, end) {
6411
+ function getValidatedString(buffer, start, end, shouldValidateUtf8) {
6342
6412
  var value = buffer.toString('utf8', start, end);
6343
- for (var i = 0; i < value.length; i++) {
6344
- if (value.charCodeAt(i) === 0xfffd) {
6345
- if (!validateUtf8(buffer, start, end)) {
6346
- throw new BSONError('Invalid UTF-8 string in BSON document');
6413
+ // if utf8 validation is on, do the check
6414
+ if (shouldValidateUtf8) {
6415
+ for (var i = 0; i < value.length; i++) {
6416
+ if (value.charCodeAt(i) === 0xfffd) {
6417
+ if (!validateUtf8(buffer, start, end)) {
6418
+ throw new BSONError('Invalid UTF-8 string in BSON document');
6419
+ }
6420
+ break;
6347
6421
  }
6348
- break;
6349
6422
  }
6350
6423
  }
6351
6424
  return value;
@@ -6974,9 +7047,7 @@ var BSON = (function (exports) {
6974
7047
  var key = '' + i;
6975
7048
  var value = object[i];
6976
7049
  // Is there an override value
6977
- if (value && value.toBSON) {
6978
- if (typeof value.toBSON !== 'function')
6979
- throw new BSONTypeError('toBSON is not a function');
7050
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
6980
7051
  value = value.toBSON();
6981
7052
  }
6982
7053
  if (typeof value === 'string') {
@@ -7152,21 +7223,18 @@ var BSON = (function (exports) {
7152
7223
  }
7153
7224
  }
7154
7225
  else {
7155
- // Did we provide a custom serialization method
7156
- if (object.toBSON) {
7157
- if (typeof object.toBSON !== 'function')
7158
- throw new BSONTypeError('toBSON is not a function');
7226
+ if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
7227
+ // Provided a custom serialization method
7159
7228
  object = object.toBSON();
7160
- if (object != null && typeof object !== 'object')
7229
+ if (object != null && typeof object !== 'object') {
7161
7230
  throw new BSONTypeError('toBSON function did not return an object');
7231
+ }
7162
7232
  }
7163
7233
  // Iterate over all the keys
7164
7234
  for (var key in object) {
7165
7235
  var value = object[key];
7166
7236
  // Is there an override value
7167
- if (value && value.toBSON) {
7168
- if (typeof value.toBSON !== 'function')
7169
- throw new BSONTypeError('toBSON is not a function');
7237
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
7170
7238
  value = value.toBSON();
7171
7239
  }
7172
7240
  // Check the type of the value