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.
package/dist/bson.esm.js CHANGED
@@ -40,11 +40,29 @@ function __extends(d, b) {
40
40
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
41
41
  }
42
42
 
43
+ var _assign = function __assign() {
44
+ _assign = Object.assign || function __assign(t) {
45
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
46
+ s = arguments[i];
47
+
48
+ for (var p in s) {
49
+ if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
50
+ }
51
+ }
52
+
53
+ return t;
54
+ };
55
+
56
+ return _assign.apply(this, arguments);
57
+ };
58
+
43
59
  /** @public */
44
60
  var BSONError = /** @class */ (function (_super) {
45
61
  __extends(BSONError, _super);
46
- function BSONError() {
47
- return _super !== null && _super.apply(this, arguments) || this;
62
+ function BSONError(message) {
63
+ var _this = _super.call(this, message) || this;
64
+ Object.setPrototypeOf(_this, BSONError.prototype);
65
+ return _this;
48
66
  }
49
67
  Object.defineProperty(BSONError.prototype, "name", {
50
68
  get: function () {
@@ -58,8 +76,10 @@ var BSONError = /** @class */ (function (_super) {
58
76
  /** @public */
59
77
  var BSONTypeError = /** @class */ (function (_super) {
60
78
  __extends(BSONTypeError, _super);
61
- function BSONTypeError() {
62
- return _super !== null && _super.apply(this, arguments) || this;
79
+ function BSONTypeError(message) {
80
+ var _this = _super.call(this, message) || this;
81
+ Object.setPrototypeOf(_this, BSONTypeError.prototype);
82
+ return _this;
63
83
  }
64
84
  Object.defineProperty(BSONTypeError.prototype, "name", {
65
85
  get: function () {
@@ -182,7 +202,7 @@ function deprecate(fn, message) {
182
202
  * @param potentialBuffer - The potential buffer
183
203
  * @returns Buffer the input if potentialBuffer is a buffer, or a buffer that
184
204
  * wraps a passed in Uint8Array
185
- * @throws TypeError If anything other than a Buffer or Uint8Array is passed in
205
+ * @throws BSONTypeError If anything other than a Buffer or Uint8Array is passed in
186
206
  */
187
207
  function ensureBuffer(potentialBuffer) {
188
208
  if (ArrayBuffer.isView(potentialBuffer)) {
@@ -3796,6 +3816,43 @@ function deserializeObject(buffer, index, options, isArray) {
3796
3816
  var promoteBuffers = options['promoteBuffers'] == null ? false : options['promoteBuffers'];
3797
3817
  var promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
3798
3818
  var promoteValues = options['promoteValues'] == null ? true : options['promoteValues'];
3819
+ // Ensures default validation option if none given
3820
+ var validation = options.validation == null ? { utf8: true } : options.validation;
3821
+ // Shows if global utf-8 validation is enabled or disabled
3822
+ var globalUTFValidation = true;
3823
+ // Reflects utf-8 validation setting regardless of global or specific key validation
3824
+ var validationSetting;
3825
+ // Set of keys either to enable or disable validation on
3826
+ var utf8KeysSet = new Set();
3827
+ // Check for boolean uniformity and empty validation option
3828
+ var utf8ValidatedKeys = validation.utf8;
3829
+ if (typeof utf8ValidatedKeys === 'boolean') {
3830
+ validationSetting = utf8ValidatedKeys;
3831
+ }
3832
+ else {
3833
+ globalUTFValidation = false;
3834
+ var utf8ValidationValues = Object.keys(utf8ValidatedKeys).map(function (key) {
3835
+ return utf8ValidatedKeys[key];
3836
+ });
3837
+ if (utf8ValidationValues.length === 0) {
3838
+ throw new BSONError('UTF-8 validation setting cannot be empty');
3839
+ }
3840
+ if (typeof utf8ValidationValues[0] !== 'boolean') {
3841
+ throw new BSONError('Invalid UTF-8 validation option, must specify boolean values');
3842
+ }
3843
+ validationSetting = utf8ValidationValues[0];
3844
+ // Ensures boolean uniformity in utf-8 validation (all true or all false)
3845
+ if (!utf8ValidationValues.every(function (item) { return item === validationSetting; })) {
3846
+ throw new BSONError('Invalid UTF-8 validation option - keys must be all true or all false');
3847
+ }
3848
+ }
3849
+ // Add keys to set that will either be validated or not based on validationSetting
3850
+ if (!globalUTFValidation) {
3851
+ for (var _i = 0, _a = Object.keys(utf8ValidatedKeys); _i < _a.length; _i++) {
3852
+ var key = _a[_i];
3853
+ utf8KeysSet.add(key);
3854
+ }
3855
+ }
3799
3856
  // Set the start index
3800
3857
  var startIndex = index;
3801
3858
  // Validate that we have at least 4 bytes of buffer
@@ -3828,7 +3885,16 @@ function deserializeObject(buffer, index, options, isArray) {
3828
3885
  // If are at the end of the buffer there is a problem with the document
3829
3886
  if (i >= buffer.byteLength)
3830
3887
  throw new BSONError('Bad BSON Document: illegal CString');
3888
+ // Represents the key
3831
3889
  var name = isArray ? arrayIndex++ : buffer.toString('utf8', index, i);
3890
+ // shouldValidateKey is true if the key should be validated, false otherwise
3891
+ var shouldValidateKey = true;
3892
+ if (globalUTFValidation || utf8KeysSet.has(name)) {
3893
+ shouldValidateKey = validationSetting;
3894
+ }
3895
+ else {
3896
+ shouldValidateKey = !validationSetting;
3897
+ }
3832
3898
  if (isPossibleDBRef !== false && name[0] === '$') {
3833
3899
  isPossibleDBRef = allowedDBRefKeys.test(name);
3834
3900
  }
@@ -3844,7 +3910,7 @@ function deserializeObject(buffer, index, options, isArray) {
3844
3910
  buffer[index + stringSize - 1] !== 0) {
3845
3911
  throw new BSONError('bad string length in bson');
3846
3912
  }
3847
- value = getValidatedString(buffer, index, index + stringSize - 1);
3913
+ value = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
3848
3914
  index = index + stringSize;
3849
3915
  }
3850
3916
  else if (elementType === BSON_DATA_OID) {
@@ -3900,7 +3966,11 @@ function deserializeObject(buffer, index, options, isArray) {
3900
3966
  value = buffer.slice(index, index + objectSize);
3901
3967
  }
3902
3968
  else {
3903
- value = deserializeObject(buffer, _index, options, false);
3969
+ var objectOptions = options;
3970
+ if (!globalUTFValidation) {
3971
+ objectOptions = _assign(_assign({}, options), { validation: { utf8: shouldValidateKey } });
3972
+ }
3973
+ value = deserializeObject(buffer, _index, objectOptions, false);
3904
3974
  }
3905
3975
  index = index + objectSize;
3906
3976
  }
@@ -3921,6 +3991,9 @@ function deserializeObject(buffer, index, options, isArray) {
3921
3991
  }
3922
3992
  arrayOptions['raw'] = true;
3923
3993
  }
3994
+ if (!globalUTFValidation) {
3995
+ arrayOptions = _assign(_assign({}, arrayOptions), { validation: { utf8: shouldValidateKey } });
3996
+ }
3924
3997
  value = deserializeObject(buffer, _index, arrayOptions, true);
3925
3998
  index = index + objectSize;
3926
3999
  if (buffer[index - 1] !== 0)
@@ -4121,7 +4194,7 @@ function deserializeObject(buffer, index, options, isArray) {
4121
4194
  buffer[index + stringSize - 1] !== 0) {
4122
4195
  throw new BSONError('bad string length in bson');
4123
4196
  }
4124
- var symbol = getValidatedString(buffer, index, index + stringSize - 1);
4197
+ var symbol = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
4125
4198
  value = promoteValues ? symbol : new BSONSymbol(symbol);
4126
4199
  index = index + stringSize;
4127
4200
  }
@@ -4152,7 +4225,7 @@ function deserializeObject(buffer, index, options, isArray) {
4152
4225
  buffer[index + stringSize - 1] !== 0) {
4153
4226
  throw new BSONError('bad string length in bson');
4154
4227
  }
4155
- var functionString = getValidatedString(buffer, index, index + stringSize - 1);
4228
+ var functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
4156
4229
  // If we are evaluating the functions
4157
4230
  if (evalFunctions) {
4158
4231
  // If we have cache enabled let's look for the md5 of the function in the cache
@@ -4191,7 +4264,7 @@ function deserializeObject(buffer, index, options, isArray) {
4191
4264
  throw new BSONError('bad string length in bson');
4192
4265
  }
4193
4266
  // Javascript function
4194
- var functionString = getValidatedString(buffer, index, index + stringSize - 1);
4267
+ var functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
4195
4268
  // Update parse index position
4196
4269
  index = index + stringSize;
4197
4270
  // Parse the element
@@ -4241,8 +4314,10 @@ function deserializeObject(buffer, index, options, isArray) {
4241
4314
  buffer[index + stringSize - 1] !== 0)
4242
4315
  throw new BSONError('bad string length in bson');
4243
4316
  // Namespace
4244
- if (!validateUtf8(buffer, index, index + stringSize - 1)) {
4245
- throw new BSONError('Invalid UTF-8 string in BSON document');
4317
+ if (validation != null && validation.utf8) {
4318
+ if (!validateUtf8(buffer, index, index + stringSize - 1)) {
4319
+ throw new BSONError('Invalid UTF-8 string in BSON document');
4320
+ }
4246
4321
  }
4247
4322
  var namespace = buffer.toString('utf8', index, index + stringSize - 1);
4248
4323
  // Update parse index position
@@ -4304,14 +4379,17 @@ function isolateEval(functionString, functionCache, object) {
4304
4379
  // Set the object
4305
4380
  return functionCache[functionString].bind(object);
4306
4381
  }
4307
- function getValidatedString(buffer, start, end) {
4382
+ function getValidatedString(buffer, start, end, shouldValidateUtf8) {
4308
4383
  var value = buffer.toString('utf8', start, end);
4309
- for (var i = 0; i < value.length; i++) {
4310
- if (value.charCodeAt(i) === 0xfffd) {
4311
- if (!validateUtf8(buffer, start, end)) {
4312
- throw new BSONError('Invalid UTF-8 string in BSON document');
4384
+ // if utf8 validation is on, do the check
4385
+ if (shouldValidateUtf8) {
4386
+ for (var i = 0; i < value.length; i++) {
4387
+ if (value.charCodeAt(i) === 0xfffd) {
4388
+ if (!validateUtf8(buffer, start, end)) {
4389
+ throw new BSONError('Invalid UTF-8 string in BSON document');
4390
+ }
4391
+ break;
4313
4392
  }
4314
- break;
4315
4393
  }
4316
4394
  }
4317
4395
  return value;