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.
package/bower.json CHANGED
@@ -22,5 +22,5 @@
22
22
  "test",
23
23
  "tools"
24
24
  ],
25
- "version": "4.5.4"
25
+ "version": "4.6.2"
26
26
  }
package/bson.d.ts CHANGED
@@ -156,6 +156,7 @@ export default BSON;
156
156
  /* Excluded from this release type: BSON_INT64_MIN */
157
157
  /** @public */
158
158
  export declare class BSONError extends Error {
159
+ constructor(message: string);
159
160
  readonly name: string;
160
161
  }
161
162
  /**
@@ -208,6 +209,7 @@ export declare interface BSONSymbolExtended {
208
209
  }
209
210
  /** @public */
210
211
  export declare class BSONTypeError extends TypeError {
212
+ constructor(message: string);
211
213
  readonly name: string;
212
214
  }
213
215
  /**
@@ -339,6 +341,24 @@ export declare interface DeserializeOptions {
339
341
  /** Offset into buffer to begin reading document from */
340
342
  index?: number;
341
343
  raw?: boolean;
344
+ /** Allows for opt-out utf-8 validation for all keys or
345
+ * specified keys. Must be all true or all false.
346
+ *
347
+ * @example
348
+ * ```js
349
+ * // disables validation on all keys
350
+ * validation: { utf8: false }
351
+ *
352
+ * // enables validation only on specified keys a, b, and c
353
+ * validation: { utf8: { a: true, b: true, c: true } }
354
+ *
355
+ * // disables validation only on specified keys a, b
356
+ * validation: { utf8: { a: false, b: false } }
357
+ * ```
358
+ */
359
+ validation?: {
360
+ utf8: boolean | Record<string, true> | Record<string, false>;
361
+ };
342
362
  }
343
363
  /**
344
364
  * Deserialize stream data as BSON documents.
@@ -855,7 +875,7 @@ export declare interface MinKeyExtended {
855
875
  * @public
856
876
  */
857
877
  declare class ObjectId {
858
- _bsontype: 'ObjectId';
878
+ _bsontype: 'ObjectID';
859
879
  /* Excluded from this release type: index */
860
880
  static cacheHexString: boolean;
861
881
  /* Excluded from this release type: [kId] */
@@ -865,7 +885,7 @@ declare class ObjectId {
865
885
  *
866
886
  * @param inputId - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
867
887
  */
868
- constructor(inputId?: string | Buffer | number | ObjectIdLike | ObjectId);
888
+ constructor(inputId?: string | number | ObjectId | ObjectIdLike | Buffer | Uint8Array);
869
889
  /*
870
890
  * The ObjectId bytes
871
891
  * @readonly
@@ -919,7 +939,7 @@ declare class ObjectId {
919
939
  *
920
940
  * @param id - ObjectId instance to validate.
921
941
  */
922
- static isValid(id: number | string | ObjectId | Uint8Array | ObjectIdLike): boolean;
942
+ static isValid(id: string | number | ObjectId | ObjectIdLike | Buffer | Uint8Array): boolean;
923
943
  /* Excluded from this release type: toExtendedJSON */
924
944
  /* Excluded from this release type: fromExtendedJSON */
925
945
  inspect(): string;
@@ -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)) {
@@ -3780,9 +3800,15 @@ var Decimal128 = /** @class */ (function () {
3780
3800
  if (typeof bytes === 'string') {
3781
3801
  this.bytes = Decimal128.fromString(bytes).bytes;
3782
3802
  }
3783
- else {
3803
+ else if (isUint8Array(bytes)) {
3804
+ if (bytes.byteLength !== 16) {
3805
+ throw new BSONTypeError('Decimal128 must take a Buffer of 16 bytes');
3806
+ }
3784
3807
  this.bytes = bytes;
3785
3808
  }
3809
+ else {
3810
+ throw new BSONTypeError('Decimal128 must take a Buffer or string');
3811
+ }
3786
3812
  }
3787
3813
  /**
3788
3814
  * Create a Decimal128 instance from a string representation
@@ -4567,7 +4593,7 @@ var ObjectId = /** @class */ (function () {
4567
4593
  this[kId] = buffer_1.from(workingId, 'hex');
4568
4594
  }
4569
4595
  else {
4570
- throw new BSONTypeError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters');
4596
+ throw new BSONTypeError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer');
4571
4597
  }
4572
4598
  }
4573
4599
  else {
@@ -4684,7 +4710,7 @@ var ObjectId = /** @class */ (function () {
4684
4710
  return false;
4685
4711
  }
4686
4712
  if (otherId instanceof ObjectId) {
4687
- return this.toString() === otherId.toString();
4713
+ return this[kId][11] === otherId[kId][11] && this[kId].equals(otherId[kId]);
4688
4714
  }
4689
4715
  if (typeof otherId === 'string' &&
4690
4716
  ObjectId.isValid(otherId) &&
@@ -4701,7 +4727,9 @@ var ObjectId = /** @class */ (function () {
4701
4727
  if (typeof otherId === 'object' &&
4702
4728
  'toHexString' in otherId &&
4703
4729
  typeof otherId.toHexString === 'function') {
4704
- return otherId.toHexString() === this.toHexString();
4730
+ var otherIdString = otherId.toHexString();
4731
+ var thisIdString = this.toHexString().toLowerCase();
4732
+ return typeof otherIdString === 'string' && otherIdString.toLowerCase() === thisIdString;
4705
4733
  }
4706
4734
  return false;
4707
4735
  };
@@ -4748,26 +4776,13 @@ var ObjectId = /** @class */ (function () {
4748
4776
  ObjectId.isValid = function (id) {
4749
4777
  if (id == null)
4750
4778
  return false;
4751
- if (typeof id === 'number') {
4752
- return true;
4753
- }
4754
- if (typeof id === 'string') {
4755
- return id.length === 12 || (id.length === 24 && checkForHexRegExp.test(id));
4756
- }
4757
- if (id instanceof ObjectId) {
4758
- return true;
4759
- }
4760
- if (isUint8Array(id) && id.length === 12) {
4779
+ try {
4780
+ new ObjectId(id);
4761
4781
  return true;
4762
4782
  }
4763
- // Duck-Typing detection of ObjectId like objects
4764
- if (typeof id === 'object' && 'toHexString' in id && typeof id.toHexString === 'function') {
4765
- if (typeof id.id === 'string') {
4766
- return id.id.length === 12;
4767
- }
4768
- return id.toHexString().length === 24 && checkForHexRegExp.test(id.id.toString('hex'));
4783
+ catch (_a) {
4784
+ return false;
4769
4785
  }
4770
- return false;
4771
4786
  };
4772
4787
  /** @internal */
4773
4788
  ObjectId.prototype.toExtendedJSON = function () {
@@ -5563,7 +5578,7 @@ function calculateObjectSize$1(object, serializeFunctions, ignoreUndefined) {
5563
5578
  }
5564
5579
  else {
5565
5580
  // If we have toBSON defined, override the current object
5566
- if (object.toBSON) {
5581
+ if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
5567
5582
  object = object.toBSON();
5568
5583
  }
5569
5584
  // Calculate size
@@ -5581,7 +5596,7 @@ value, serializeFunctions, isArray, ignoreUndefined) {
5581
5596
  if (isArray === void 0) { isArray = false; }
5582
5597
  if (ignoreUndefined === void 0) { ignoreUndefined = false; }
5583
5598
  // If we have toBSON defined, override the current object
5584
- if (value && value.toBSON) {
5599
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
5585
5600
  value = value.toBSON();
5586
5601
  }
5587
5602
  switch (typeof value) {
@@ -5827,6 +5842,43 @@ function deserializeObject(buffer, index, options, isArray) {
5827
5842
  var promoteBuffers = options['promoteBuffers'] == null ? false : options['promoteBuffers'];
5828
5843
  var promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
5829
5844
  var promoteValues = options['promoteValues'] == null ? true : options['promoteValues'];
5845
+ // Ensures default validation option if none given
5846
+ var validation = options.validation == null ? { utf8: true } : options.validation;
5847
+ // Shows if global utf-8 validation is enabled or disabled
5848
+ var globalUTFValidation = true;
5849
+ // Reflects utf-8 validation setting regardless of global or specific key validation
5850
+ var validationSetting;
5851
+ // Set of keys either to enable or disable validation on
5852
+ var utf8KeysSet = new Set();
5853
+ // Check for boolean uniformity and empty validation option
5854
+ var utf8ValidatedKeys = validation.utf8;
5855
+ if (typeof utf8ValidatedKeys === 'boolean') {
5856
+ validationSetting = utf8ValidatedKeys;
5857
+ }
5858
+ else {
5859
+ globalUTFValidation = false;
5860
+ var utf8ValidationValues = Object.keys(utf8ValidatedKeys).map(function (key) {
5861
+ return utf8ValidatedKeys[key];
5862
+ });
5863
+ if (utf8ValidationValues.length === 0) {
5864
+ throw new BSONError('UTF-8 validation setting cannot be empty');
5865
+ }
5866
+ if (typeof utf8ValidationValues[0] !== 'boolean') {
5867
+ throw new BSONError('Invalid UTF-8 validation option, must specify boolean values');
5868
+ }
5869
+ validationSetting = utf8ValidationValues[0];
5870
+ // Ensures boolean uniformity in utf-8 validation (all true or all false)
5871
+ if (!utf8ValidationValues.every(function (item) { return item === validationSetting; })) {
5872
+ throw new BSONError('Invalid UTF-8 validation option - keys must be all true or all false');
5873
+ }
5874
+ }
5875
+ // Add keys to set that will either be validated or not based on validationSetting
5876
+ if (!globalUTFValidation) {
5877
+ for (var _i = 0, _a = Object.keys(utf8ValidatedKeys); _i < _a.length; _i++) {
5878
+ var key = _a[_i];
5879
+ utf8KeysSet.add(key);
5880
+ }
5881
+ }
5830
5882
  // Set the start index
5831
5883
  var startIndex = index;
5832
5884
  // Validate that we have at least 4 bytes of buffer
@@ -5859,7 +5911,16 @@ function deserializeObject(buffer, index, options, isArray) {
5859
5911
  // If are at the end of the buffer there is a problem with the document
5860
5912
  if (i >= buffer.byteLength)
5861
5913
  throw new BSONError('Bad BSON Document: illegal CString');
5914
+ // Represents the key
5862
5915
  var name = isArray ? arrayIndex++ : buffer.toString('utf8', index, i);
5916
+ // shouldValidateKey is true if the key should be validated, false otherwise
5917
+ var shouldValidateKey = true;
5918
+ if (globalUTFValidation || utf8KeysSet.has(name)) {
5919
+ shouldValidateKey = validationSetting;
5920
+ }
5921
+ else {
5922
+ shouldValidateKey = !validationSetting;
5923
+ }
5863
5924
  if (isPossibleDBRef !== false && name[0] === '$') {
5864
5925
  isPossibleDBRef = allowedDBRefKeys.test(name);
5865
5926
  }
@@ -5875,7 +5936,7 @@ function deserializeObject(buffer, index, options, isArray) {
5875
5936
  buffer[index + stringSize - 1] !== 0) {
5876
5937
  throw new BSONError('bad string length in bson');
5877
5938
  }
5878
- value = getValidatedString(buffer, index, index + stringSize - 1);
5939
+ value = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
5879
5940
  index = index + stringSize;
5880
5941
  }
5881
5942
  else if (elementType === BSON_DATA_OID) {
@@ -5931,7 +5992,11 @@ function deserializeObject(buffer, index, options, isArray) {
5931
5992
  value = buffer.slice(index, index + objectSize);
5932
5993
  }
5933
5994
  else {
5934
- value = deserializeObject(buffer, _index, options, false);
5995
+ var objectOptions = options;
5996
+ if (!globalUTFValidation) {
5997
+ objectOptions = _assign(_assign({}, options), { validation: { utf8: shouldValidateKey } });
5998
+ }
5999
+ value = deserializeObject(buffer, _index, objectOptions, false);
5935
6000
  }
5936
6001
  index = index + objectSize;
5937
6002
  }
@@ -5952,6 +6017,9 @@ function deserializeObject(buffer, index, options, isArray) {
5952
6017
  }
5953
6018
  arrayOptions['raw'] = true;
5954
6019
  }
6020
+ if (!globalUTFValidation) {
6021
+ arrayOptions = _assign(_assign({}, arrayOptions), { validation: { utf8: shouldValidateKey } });
6022
+ }
5955
6023
  value = deserializeObject(buffer, _index, arrayOptions, true);
5956
6024
  index = index + objectSize;
5957
6025
  if (buffer[index - 1] !== 0)
@@ -6152,7 +6220,7 @@ function deserializeObject(buffer, index, options, isArray) {
6152
6220
  buffer[index + stringSize - 1] !== 0) {
6153
6221
  throw new BSONError('bad string length in bson');
6154
6222
  }
6155
- var symbol = getValidatedString(buffer, index, index + stringSize - 1);
6223
+ var symbol = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6156
6224
  value = promoteValues ? symbol : new BSONSymbol(symbol);
6157
6225
  index = index + stringSize;
6158
6226
  }
@@ -6183,7 +6251,7 @@ function deserializeObject(buffer, index, options, isArray) {
6183
6251
  buffer[index + stringSize - 1] !== 0) {
6184
6252
  throw new BSONError('bad string length in bson');
6185
6253
  }
6186
- var functionString = getValidatedString(buffer, index, index + stringSize - 1);
6254
+ var functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6187
6255
  // If we are evaluating the functions
6188
6256
  if (evalFunctions) {
6189
6257
  // If we have cache enabled let's look for the md5 of the function in the cache
@@ -6222,7 +6290,7 @@ function deserializeObject(buffer, index, options, isArray) {
6222
6290
  throw new BSONError('bad string length in bson');
6223
6291
  }
6224
6292
  // Javascript function
6225
- var functionString = getValidatedString(buffer, index, index + stringSize - 1);
6293
+ var functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
6226
6294
  // Update parse index position
6227
6295
  index = index + stringSize;
6228
6296
  // Parse the element
@@ -6272,8 +6340,10 @@ function deserializeObject(buffer, index, options, isArray) {
6272
6340
  buffer[index + stringSize - 1] !== 0)
6273
6341
  throw new BSONError('bad string length in bson');
6274
6342
  // Namespace
6275
- if (!validateUtf8(buffer, index, index + stringSize - 1)) {
6276
- throw new BSONError('Invalid UTF-8 string in BSON document');
6343
+ if (validation != null && validation.utf8) {
6344
+ if (!validateUtf8(buffer, index, index + stringSize - 1)) {
6345
+ throw new BSONError('Invalid UTF-8 string in BSON document');
6346
+ }
6277
6347
  }
6278
6348
  var namespace = buffer.toString('utf8', index, index + stringSize - 1);
6279
6349
  // Update parse index position
@@ -6335,14 +6405,17 @@ function isolateEval(functionString, functionCache, object) {
6335
6405
  // Set the object
6336
6406
  return functionCache[functionString].bind(object);
6337
6407
  }
6338
- function getValidatedString(buffer, start, end) {
6408
+ function getValidatedString(buffer, start, end, shouldValidateUtf8) {
6339
6409
  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');
6410
+ // if utf8 validation is on, do the check
6411
+ if (shouldValidateUtf8) {
6412
+ for (var i = 0; i < value.length; i++) {
6413
+ if (value.charCodeAt(i) === 0xfffd) {
6414
+ if (!validateUtf8(buffer, start, end)) {
6415
+ throw new BSONError('Invalid UTF-8 string in BSON document');
6416
+ }
6417
+ break;
6344
6418
  }
6345
- break;
6346
6419
  }
6347
6420
  }
6348
6421
  return value;
@@ -6971,9 +7044,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
6971
7044
  var key = '' + i;
6972
7045
  var value = object[i];
6973
7046
  // Is there an override value
6974
- if (value && value.toBSON) {
6975
- if (typeof value.toBSON !== 'function')
6976
- throw new BSONTypeError('toBSON is not a function');
7047
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
6977
7048
  value = value.toBSON();
6978
7049
  }
6979
7050
  if (typeof value === 'string') {
@@ -7149,21 +7220,18 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
7149
7220
  }
7150
7221
  }
7151
7222
  else {
7152
- // Did we provide a custom serialization method
7153
- if (object.toBSON) {
7154
- if (typeof object.toBSON !== 'function')
7155
- throw new BSONTypeError('toBSON is not a function');
7223
+ if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
7224
+ // Provided a custom serialization method
7156
7225
  object = object.toBSON();
7157
- if (object != null && typeof object !== 'object')
7226
+ if (object != null && typeof object !== 'object') {
7158
7227
  throw new BSONTypeError('toBSON function did not return an object');
7228
+ }
7159
7229
  }
7160
7230
  // Iterate over all the keys
7161
7231
  for (var key in object) {
7162
7232
  var value = object[key];
7163
7233
  // Is there an override value
7164
- if (value && value.toBSON) {
7165
- if (typeof value.toBSON !== 'function')
7166
- throw new BSONTypeError('toBSON is not a function');
7234
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
7167
7235
  value = value.toBSON();
7168
7236
  }
7169
7237
  // Check the type of the value