bson 4.5.4 → 4.6.0

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)) {
@@ -5833,6 +5853,43 @@
5833
5853
  var promoteBuffers = options['promoteBuffers'] == null ? false : options['promoteBuffers'];
5834
5854
  var promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
5835
5855
  var promoteValues = options['promoteValues'] == null ? true : options['promoteValues'];
5856
+ // Ensures default validation option if none given
5857
+ var validation = options.validation == null ? { utf8: true } : options.validation;
5858
+ // Shows if global utf-8 validation is enabled or disabled
5859
+ var globalUTFValidation = true;
5860
+ // Reflects utf-8 validation setting regardless of global or specific key validation
5861
+ var validationSetting;
5862
+ // Set of keys either to enable or disable validation on
5863
+ var utf8KeysSet = new Set();
5864
+ // Check for boolean uniformity and empty validation option
5865
+ var utf8ValidatedKeys = validation.utf8;
5866
+ if (typeof utf8ValidatedKeys === 'boolean') {
5867
+ validationSetting = utf8ValidatedKeys;
5868
+ }
5869
+ else {
5870
+ globalUTFValidation = false;
5871
+ var utf8ValidationValues = Object.keys(utf8ValidatedKeys).map(function (key) {
5872
+ return utf8ValidatedKeys[key];
5873
+ });
5874
+ if (utf8ValidationValues.length === 0) {
5875
+ throw new BSONError('UTF-8 validation setting cannot be empty');
5876
+ }
5877
+ if (typeof utf8ValidationValues[0] !== 'boolean') {
5878
+ throw new BSONError('Invalid UTF-8 validation option, must specify boolean values');
5879
+ }
5880
+ validationSetting = utf8ValidationValues[0];
5881
+ // Ensures boolean uniformity in utf-8 validation (all true or all false)
5882
+ if (!utf8ValidationValues.every(function (item) { return item === validationSetting; })) {
5883
+ throw new BSONError('Invalid UTF-8 validation option - keys must be all true or all false');
5884
+ }
5885
+ }
5886
+ // Add keys to set that will either be validated or not based on validationSetting
5887
+ if (!globalUTFValidation) {
5888
+ for (var _i = 0, _a = Object.keys(utf8ValidatedKeys); _i < _a.length; _i++) {
5889
+ var key = _a[_i];
5890
+ utf8KeysSet.add(key);
5891
+ }
5892
+ }
5836
5893
  // Set the start index
5837
5894
  var startIndex = index;
5838
5895
  // Validate that we have at least 4 bytes of buffer
@@ -5865,7 +5922,16 @@
5865
5922
  // If are at the end of the buffer there is a problem with the document
5866
5923
  if (i >= buffer.byteLength)
5867
5924
  throw new BSONError('Bad BSON Document: illegal CString');
5925
+ // Represents the key
5868
5926
  var name = isArray ? arrayIndex++ : buffer.toString('utf8', index, i);
5927
+ // shouldValidateKey is true if the key should be validated, false otherwise
5928
+ var shouldValidateKey = true;
5929
+ if (globalUTFValidation || utf8KeysSet.has(name)) {
5930
+ shouldValidateKey = validationSetting;
5931
+ }
5932
+ else {
5933
+ shouldValidateKey = !validationSetting;
5934
+ }
5869
5935
  if (isPossibleDBRef !== false && name[0] === '$') {
5870
5936
  isPossibleDBRef = allowedDBRefKeys.test(name);
5871
5937
  }
@@ -5881,7 +5947,7 @@
5881
5947
  buffer[index + stringSize - 1] !== 0) {
5882
5948
  throw new BSONError('bad string length in bson');
5883
5949
  }
5884
- value = getValidatedString(buffer, index, index + stringSize - 1);
5950
+ value = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
5885
5951
  index = index + stringSize;
5886
5952
  }
5887
5953
  else if (elementType === BSON_DATA_OID) {
@@ -5937,7 +6003,11 @@
5937
6003
  value = buffer.slice(index, index + objectSize);
5938
6004
  }
5939
6005
  else {
5940
- value = deserializeObject(buffer, _index, options, false);
6006
+ var objectOptions = options;
6007
+ if (!globalUTFValidation) {
6008
+ objectOptions = _assign(_assign({}, options), { validation: { utf8: shouldValidateKey } });
6009
+ }
6010
+ value = deserializeObject(buffer, _index, objectOptions, false);
5941
6011
  }
5942
6012
  index = index + objectSize;
5943
6013
  }
@@ -5958,6 +6028,9 @@
5958
6028
  }
5959
6029
  arrayOptions['raw'] = true;
5960
6030
  }
6031
+ if (!globalUTFValidation) {
6032
+ arrayOptions = _assign(_assign({}, arrayOptions), { validation: { utf8: shouldValidateKey } });
6033
+ }
5961
6034
  value = deserializeObject(buffer, _index, arrayOptions, true);
5962
6035
  index = index + objectSize;
5963
6036
  if (buffer[index - 1] !== 0)
@@ -6158,7 +6231,7 @@
6158
6231
  buffer[index + stringSize - 1] !== 0) {
6159
6232
  throw new BSONError('bad string length in bson');
6160
6233
  }
6161
- var symbol = getValidatedString(buffer, index, index + stringSize - 1);
6234
+ var symbol = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6162
6235
  value = promoteValues ? symbol : new BSONSymbol(symbol);
6163
6236
  index = index + stringSize;
6164
6237
  }
@@ -6189,7 +6262,7 @@
6189
6262
  buffer[index + stringSize - 1] !== 0) {
6190
6263
  throw new BSONError('bad string length in bson');
6191
6264
  }
6192
- var functionString = getValidatedString(buffer, index, index + stringSize - 1);
6265
+ var functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6193
6266
  // If we are evaluating the functions
6194
6267
  if (evalFunctions) {
6195
6268
  // If we have cache enabled let's look for the md5 of the function in the cache
@@ -6228,7 +6301,7 @@
6228
6301
  throw new BSONError('bad string length in bson');
6229
6302
  }
6230
6303
  // Javascript function
6231
- var functionString = getValidatedString(buffer, index, index + stringSize - 1);
6304
+ var functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6232
6305
  // Update parse index position
6233
6306
  index = index + stringSize;
6234
6307
  // Parse the element
@@ -6278,8 +6351,10 @@
6278
6351
  buffer[index + stringSize - 1] !== 0)
6279
6352
  throw new BSONError('bad string length in bson');
6280
6353
  // Namespace
6281
- if (!validateUtf8(buffer, index, index + stringSize - 1)) {
6282
- throw new BSONError('Invalid UTF-8 string in BSON document');
6354
+ if (validation != null && validation.utf8) {
6355
+ if (!validateUtf8(buffer, index, index + stringSize - 1)) {
6356
+ throw new BSONError('Invalid UTF-8 string in BSON document');
6357
+ }
6283
6358
  }
6284
6359
  var namespace = buffer.toString('utf8', index, index + stringSize - 1);
6285
6360
  // Update parse index position
@@ -6341,14 +6416,17 @@
6341
6416
  // Set the object
6342
6417
  return functionCache[functionString].bind(object);
6343
6418
  }
6344
- function getValidatedString(buffer, start, end) {
6419
+ function getValidatedString(buffer, start, end, shouldValidateUtf8) {
6345
6420
  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');
6421
+ // if utf8 validation is on, do the check
6422
+ if (shouldValidateUtf8) {
6423
+ for (var i = 0; i < value.length; i++) {
6424
+ if (value.charCodeAt(i) === 0xfffd) {
6425
+ if (!validateUtf8(buffer, start, end)) {
6426
+ throw new BSONError('Invalid UTF-8 string in BSON document');
6427
+ }
6428
+ break;
6350
6429
  }
6351
- break;
6352
6430
  }
6353
6431
  }
6354
6432
  return value;