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.
@@ -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)) {
@@ -5830,6 +5850,43 @@ var BSON = (function (exports) {
5830
5850
  var promoteBuffers = options['promoteBuffers'] == null ? false : options['promoteBuffers'];
5831
5851
  var promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
5832
5852
  var promoteValues = options['promoteValues'] == null ? true : options['promoteValues'];
5853
+ // Ensures default validation option if none given
5854
+ var validation = options.validation == null ? { utf8: true } : options.validation;
5855
+ // Shows if global utf-8 validation is enabled or disabled
5856
+ var globalUTFValidation = true;
5857
+ // Reflects utf-8 validation setting regardless of global or specific key validation
5858
+ var validationSetting;
5859
+ // Set of keys either to enable or disable validation on
5860
+ var utf8KeysSet = new Set();
5861
+ // Check for boolean uniformity and empty validation option
5862
+ var utf8ValidatedKeys = validation.utf8;
5863
+ if (typeof utf8ValidatedKeys === 'boolean') {
5864
+ validationSetting = utf8ValidatedKeys;
5865
+ }
5866
+ else {
5867
+ globalUTFValidation = false;
5868
+ var utf8ValidationValues = Object.keys(utf8ValidatedKeys).map(function (key) {
5869
+ return utf8ValidatedKeys[key];
5870
+ });
5871
+ if (utf8ValidationValues.length === 0) {
5872
+ throw new BSONError('UTF-8 validation setting cannot be empty');
5873
+ }
5874
+ if (typeof utf8ValidationValues[0] !== 'boolean') {
5875
+ throw new BSONError('Invalid UTF-8 validation option, must specify boolean values');
5876
+ }
5877
+ validationSetting = utf8ValidationValues[0];
5878
+ // Ensures boolean uniformity in utf-8 validation (all true or all false)
5879
+ if (!utf8ValidationValues.every(function (item) { return item === validationSetting; })) {
5880
+ throw new BSONError('Invalid UTF-8 validation option - keys must be all true or all false');
5881
+ }
5882
+ }
5883
+ // Add keys to set that will either be validated or not based on validationSetting
5884
+ if (!globalUTFValidation) {
5885
+ for (var _i = 0, _a = Object.keys(utf8ValidatedKeys); _i < _a.length; _i++) {
5886
+ var key = _a[_i];
5887
+ utf8KeysSet.add(key);
5888
+ }
5889
+ }
5833
5890
  // Set the start index
5834
5891
  var startIndex = index;
5835
5892
  // Validate that we have at least 4 bytes of buffer
@@ -5862,7 +5919,16 @@ var BSON = (function (exports) {
5862
5919
  // If are at the end of the buffer there is a problem with the document
5863
5920
  if (i >= buffer.byteLength)
5864
5921
  throw new BSONError('Bad BSON Document: illegal CString');
5922
+ // Represents the key
5865
5923
  var name = isArray ? arrayIndex++ : buffer.toString('utf8', index, i);
5924
+ // shouldValidateKey is true if the key should be validated, false otherwise
5925
+ var shouldValidateKey = true;
5926
+ if (globalUTFValidation || utf8KeysSet.has(name)) {
5927
+ shouldValidateKey = validationSetting;
5928
+ }
5929
+ else {
5930
+ shouldValidateKey = !validationSetting;
5931
+ }
5866
5932
  if (isPossibleDBRef !== false && name[0] === '$') {
5867
5933
  isPossibleDBRef = allowedDBRefKeys.test(name);
5868
5934
  }
@@ -5878,7 +5944,7 @@ var BSON = (function (exports) {
5878
5944
  buffer[index + stringSize - 1] !== 0) {
5879
5945
  throw new BSONError('bad string length in bson');
5880
5946
  }
5881
- value = getValidatedString(buffer, index, index + stringSize - 1);
5947
+ value = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
5882
5948
  index = index + stringSize;
5883
5949
  }
5884
5950
  else if (elementType === BSON_DATA_OID) {
@@ -5934,7 +6000,11 @@ var BSON = (function (exports) {
5934
6000
  value = buffer.slice(index, index + objectSize);
5935
6001
  }
5936
6002
  else {
5937
- value = deserializeObject(buffer, _index, options, false);
6003
+ var objectOptions = options;
6004
+ if (!globalUTFValidation) {
6005
+ objectOptions = _assign(_assign({}, options), { validation: { utf8: shouldValidateKey } });
6006
+ }
6007
+ value = deserializeObject(buffer, _index, objectOptions, false);
5938
6008
  }
5939
6009
  index = index + objectSize;
5940
6010
  }
@@ -5955,6 +6025,9 @@ var BSON = (function (exports) {
5955
6025
  }
5956
6026
  arrayOptions['raw'] = true;
5957
6027
  }
6028
+ if (!globalUTFValidation) {
6029
+ arrayOptions = _assign(_assign({}, arrayOptions), { validation: { utf8: shouldValidateKey } });
6030
+ }
5958
6031
  value = deserializeObject(buffer, _index, arrayOptions, true);
5959
6032
  index = index + objectSize;
5960
6033
  if (buffer[index - 1] !== 0)
@@ -6155,7 +6228,7 @@ var BSON = (function (exports) {
6155
6228
  buffer[index + stringSize - 1] !== 0) {
6156
6229
  throw new BSONError('bad string length in bson');
6157
6230
  }
6158
- var symbol = getValidatedString(buffer, index, index + stringSize - 1);
6231
+ var symbol = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6159
6232
  value = promoteValues ? symbol : new BSONSymbol(symbol);
6160
6233
  index = index + stringSize;
6161
6234
  }
@@ -6186,7 +6259,7 @@ var BSON = (function (exports) {
6186
6259
  buffer[index + stringSize - 1] !== 0) {
6187
6260
  throw new BSONError('bad string length in bson');
6188
6261
  }
6189
- var functionString = getValidatedString(buffer, index, index + stringSize - 1);
6262
+ var functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6190
6263
  // If we are evaluating the functions
6191
6264
  if (evalFunctions) {
6192
6265
  // If we have cache enabled let's look for the md5 of the function in the cache
@@ -6225,7 +6298,7 @@ var BSON = (function (exports) {
6225
6298
  throw new BSONError('bad string length in bson');
6226
6299
  }
6227
6300
  // Javascript function
6228
- var functionString = getValidatedString(buffer, index, index + stringSize - 1);
6301
+ var functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6229
6302
  // Update parse index position
6230
6303
  index = index + stringSize;
6231
6304
  // Parse the element
@@ -6275,8 +6348,10 @@ var BSON = (function (exports) {
6275
6348
  buffer[index + stringSize - 1] !== 0)
6276
6349
  throw new BSONError('bad string length in bson');
6277
6350
  // Namespace
6278
- if (!validateUtf8(buffer, index, index + stringSize - 1)) {
6279
- throw new BSONError('Invalid UTF-8 string in BSON document');
6351
+ if (validation != null && validation.utf8) {
6352
+ if (!validateUtf8(buffer, index, index + stringSize - 1)) {
6353
+ throw new BSONError('Invalid UTF-8 string in BSON document');
6354
+ }
6280
6355
  }
6281
6356
  var namespace = buffer.toString('utf8', index, index + stringSize - 1);
6282
6357
  // Update parse index position
@@ -6338,14 +6413,17 @@ var BSON = (function (exports) {
6338
6413
  // Set the object
6339
6414
  return functionCache[functionString].bind(object);
6340
6415
  }
6341
- function getValidatedString(buffer, start, end) {
6416
+ function getValidatedString(buffer, start, end, shouldValidateUtf8) {
6342
6417
  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');
6418
+ // if utf8 validation is on, do the check
6419
+ if (shouldValidateUtf8) {
6420
+ for (var i = 0; i < value.length; i++) {
6421
+ if (value.charCodeAt(i) === 0xfffd) {
6422
+ if (!validateUtf8(buffer, start, end)) {
6423
+ throw new BSONError('Invalid UTF-8 string in BSON document');
6424
+ }
6425
+ break;
6347
6426
  }
6348
- break;
6349
6427
  }
6350
6428
  }
6351
6429
  return value;