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.
@@ -2071,11 +2071,29 @@ function __extends(d, b) {
2071
2071
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
2072
2072
  }
2073
2073
 
2074
+ var _assign = function __assign() {
2075
+ _assign = Object.assign || function __assign(t) {
2076
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
2077
+ s = arguments[i];
2078
+
2079
+ for (var p in s) {
2080
+ if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
2081
+ }
2082
+ }
2083
+
2084
+ return t;
2085
+ };
2086
+
2087
+ return _assign.apply(this, arguments);
2088
+ };
2089
+
2074
2090
  /** @public */
2075
2091
  var BSONError = /** @class */ (function (_super) {
2076
2092
  __extends(BSONError, _super);
2077
- function BSONError() {
2078
- return _super !== null && _super.apply(this, arguments) || this;
2093
+ function BSONError(message) {
2094
+ var _this = _super.call(this, message) || this;
2095
+ Object.setPrototypeOf(_this, BSONError.prototype);
2096
+ return _this;
2079
2097
  }
2080
2098
  Object.defineProperty(BSONError.prototype, "name", {
2081
2099
  get: function () {
@@ -2089,8 +2107,10 @@ var BSONError = /** @class */ (function (_super) {
2089
2107
  /** @public */
2090
2108
  var BSONTypeError = /** @class */ (function (_super) {
2091
2109
  __extends(BSONTypeError, _super);
2092
- function BSONTypeError() {
2093
- return _super !== null && _super.apply(this, arguments) || this;
2110
+ function BSONTypeError(message) {
2111
+ var _this = _super.call(this, message) || this;
2112
+ Object.setPrototypeOf(_this, BSONTypeError.prototype);
2113
+ return _this;
2094
2114
  }
2095
2115
  Object.defineProperty(BSONTypeError.prototype, "name", {
2096
2116
  get: function () {
@@ -2213,7 +2233,7 @@ function deprecate(fn, message) {
2213
2233
  * @param potentialBuffer - The potential buffer
2214
2234
  * @returns Buffer the input if potentialBuffer is a buffer, or a buffer that
2215
2235
  * wraps a passed in Uint8Array
2216
- * @throws TypeError If anything other than a Buffer or Uint8Array is passed in
2236
+ * @throws BSONTypeError If anything other than a Buffer or Uint8Array is passed in
2217
2237
  */
2218
2238
  function ensureBuffer(potentialBuffer) {
2219
2239
  if (ArrayBuffer.isView(potentialBuffer)) {
@@ -5827,6 +5847,43 @@ function deserializeObject(buffer, index, options, isArray) {
5827
5847
  var promoteBuffers = options['promoteBuffers'] == null ? false : options['promoteBuffers'];
5828
5848
  var promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
5829
5849
  var promoteValues = options['promoteValues'] == null ? true : options['promoteValues'];
5850
+ // Ensures default validation option if none given
5851
+ var validation = options.validation == null ? { utf8: true } : options.validation;
5852
+ // Shows if global utf-8 validation is enabled or disabled
5853
+ var globalUTFValidation = true;
5854
+ // Reflects utf-8 validation setting regardless of global or specific key validation
5855
+ var validationSetting;
5856
+ // Set of keys either to enable or disable validation on
5857
+ var utf8KeysSet = new Set();
5858
+ // Check for boolean uniformity and empty validation option
5859
+ var utf8ValidatedKeys = validation.utf8;
5860
+ if (typeof utf8ValidatedKeys === 'boolean') {
5861
+ validationSetting = utf8ValidatedKeys;
5862
+ }
5863
+ else {
5864
+ globalUTFValidation = false;
5865
+ var utf8ValidationValues = Object.keys(utf8ValidatedKeys).map(function (key) {
5866
+ return utf8ValidatedKeys[key];
5867
+ });
5868
+ if (utf8ValidationValues.length === 0) {
5869
+ throw new BSONError('UTF-8 validation setting cannot be empty');
5870
+ }
5871
+ if (typeof utf8ValidationValues[0] !== 'boolean') {
5872
+ throw new BSONError('Invalid UTF-8 validation option, must specify boolean values');
5873
+ }
5874
+ validationSetting = utf8ValidationValues[0];
5875
+ // Ensures boolean uniformity in utf-8 validation (all true or all false)
5876
+ if (!utf8ValidationValues.every(function (item) { return item === validationSetting; })) {
5877
+ throw new BSONError('Invalid UTF-8 validation option - keys must be all true or all false');
5878
+ }
5879
+ }
5880
+ // Add keys to set that will either be validated or not based on validationSetting
5881
+ if (!globalUTFValidation) {
5882
+ for (var _i = 0, _a = Object.keys(utf8ValidatedKeys); _i < _a.length; _i++) {
5883
+ var key = _a[_i];
5884
+ utf8KeysSet.add(key);
5885
+ }
5886
+ }
5830
5887
  // Set the start index
5831
5888
  var startIndex = index;
5832
5889
  // Validate that we have at least 4 bytes of buffer
@@ -5859,7 +5916,16 @@ function deserializeObject(buffer, index, options, isArray) {
5859
5916
  // If are at the end of the buffer there is a problem with the document
5860
5917
  if (i >= buffer.byteLength)
5861
5918
  throw new BSONError('Bad BSON Document: illegal CString');
5919
+ // Represents the key
5862
5920
  var name = isArray ? arrayIndex++ : buffer.toString('utf8', index, i);
5921
+ // shouldValidateKey is true if the key should be validated, false otherwise
5922
+ var shouldValidateKey = true;
5923
+ if (globalUTFValidation || utf8KeysSet.has(name)) {
5924
+ shouldValidateKey = validationSetting;
5925
+ }
5926
+ else {
5927
+ shouldValidateKey = !validationSetting;
5928
+ }
5863
5929
  if (isPossibleDBRef !== false && name[0] === '$') {
5864
5930
  isPossibleDBRef = allowedDBRefKeys.test(name);
5865
5931
  }
@@ -5875,7 +5941,7 @@ function deserializeObject(buffer, index, options, isArray) {
5875
5941
  buffer[index + stringSize - 1] !== 0) {
5876
5942
  throw new BSONError('bad string length in bson');
5877
5943
  }
5878
- value = getValidatedString(buffer, index, index + stringSize - 1);
5944
+ value = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
5879
5945
  index = index + stringSize;
5880
5946
  }
5881
5947
  else if (elementType === BSON_DATA_OID) {
@@ -5931,7 +5997,11 @@ function deserializeObject(buffer, index, options, isArray) {
5931
5997
  value = buffer.slice(index, index + objectSize);
5932
5998
  }
5933
5999
  else {
5934
- value = deserializeObject(buffer, _index, options, false);
6000
+ var objectOptions = options;
6001
+ if (!globalUTFValidation) {
6002
+ objectOptions = _assign(_assign({}, options), { validation: { utf8: shouldValidateKey } });
6003
+ }
6004
+ value = deserializeObject(buffer, _index, objectOptions, false);
5935
6005
  }
5936
6006
  index = index + objectSize;
5937
6007
  }
@@ -5952,6 +6022,9 @@ function deserializeObject(buffer, index, options, isArray) {
5952
6022
  }
5953
6023
  arrayOptions['raw'] = true;
5954
6024
  }
6025
+ if (!globalUTFValidation) {
6026
+ arrayOptions = _assign(_assign({}, arrayOptions), { validation: { utf8: shouldValidateKey } });
6027
+ }
5955
6028
  value = deserializeObject(buffer, _index, arrayOptions, true);
5956
6029
  index = index + objectSize;
5957
6030
  if (buffer[index - 1] !== 0)
@@ -6152,7 +6225,7 @@ function deserializeObject(buffer, index, options, isArray) {
6152
6225
  buffer[index + stringSize - 1] !== 0) {
6153
6226
  throw new BSONError('bad string length in bson');
6154
6227
  }
6155
- var symbol = getValidatedString(buffer, index, index + stringSize - 1);
6228
+ var symbol = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6156
6229
  value = promoteValues ? symbol : new BSONSymbol(symbol);
6157
6230
  index = index + stringSize;
6158
6231
  }
@@ -6183,7 +6256,7 @@ function deserializeObject(buffer, index, options, isArray) {
6183
6256
  buffer[index + stringSize - 1] !== 0) {
6184
6257
  throw new BSONError('bad string length in bson');
6185
6258
  }
6186
- var functionString = getValidatedString(buffer, index, index + stringSize - 1);
6259
+ var functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6187
6260
  // If we are evaluating the functions
6188
6261
  if (evalFunctions) {
6189
6262
  // If we have cache enabled let's look for the md5 of the function in the cache
@@ -6222,7 +6295,7 @@ function deserializeObject(buffer, index, options, isArray) {
6222
6295
  throw new BSONError('bad string length in bson');
6223
6296
  }
6224
6297
  // Javascript function
6225
- var functionString = getValidatedString(buffer, index, index + stringSize - 1);
6298
+ var functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6226
6299
  // Update parse index position
6227
6300
  index = index + stringSize;
6228
6301
  // Parse the element
@@ -6272,8 +6345,10 @@ function deserializeObject(buffer, index, options, isArray) {
6272
6345
  buffer[index + stringSize - 1] !== 0)
6273
6346
  throw new BSONError('bad string length in bson');
6274
6347
  // Namespace
6275
- if (!validateUtf8(buffer, index, index + stringSize - 1)) {
6276
- throw new BSONError('Invalid UTF-8 string in BSON document');
6348
+ if (validation != null && validation.utf8) {
6349
+ if (!validateUtf8(buffer, index, index + stringSize - 1)) {
6350
+ throw new BSONError('Invalid UTF-8 string in BSON document');
6351
+ }
6277
6352
  }
6278
6353
  var namespace = buffer.toString('utf8', index, index + stringSize - 1);
6279
6354
  // Update parse index position
@@ -6335,14 +6410,17 @@ function isolateEval(functionString, functionCache, object) {
6335
6410
  // Set the object
6336
6411
  return functionCache[functionString].bind(object);
6337
6412
  }
6338
- function getValidatedString(buffer, start, end) {
6413
+ function getValidatedString(buffer, start, end, shouldValidateUtf8) {
6339
6414
  var value = buffer.toString('utf8', start, end);
6340
- for (var i = 0; i < value.length; i++) {
6341
- if (value.charCodeAt(i) === 0xfffd) {
6342
- if (!validateUtf8(buffer, start, end)) {
6343
- throw new BSONError('Invalid UTF-8 string in BSON document');
6415
+ // if utf8 validation is on, do the check
6416
+ if (shouldValidateUtf8) {
6417
+ for (var i = 0; i < value.length; i++) {
6418
+ if (value.charCodeAt(i) === 0xfffd) {
6419
+ if (!validateUtf8(buffer, start, end)) {
6420
+ throw new BSONError('Invalid UTF-8 string in BSON document');
6421
+ }
6422
+ break;
6344
6423
  }
6345
- break;
6346
6424
  }
6347
6425
  }
6348
6426
  return value;