bson 4.6.0 → 4.6.1

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.
@@ -3803,9 +3803,15 @@ var BSON = (function (exports) {
3803
3803
  if (typeof bytes === 'string') {
3804
3804
  this.bytes = Decimal128.fromString(bytes).bytes;
3805
3805
  }
3806
- else {
3806
+ else if (isUint8Array(bytes)) {
3807
+ if (bytes.byteLength !== 16) {
3808
+ throw new BSONTypeError('Decimal128 must take a Buffer of 16 bytes');
3809
+ }
3807
3810
  this.bytes = bytes;
3808
3811
  }
3812
+ else {
3813
+ throw new BSONTypeError('Decimal128 must take a Buffer or string');
3814
+ }
3809
3815
  }
3810
3816
  /**
3811
3817
  * Create a Decimal128 instance from a string representation
@@ -4771,26 +4777,13 @@ var BSON = (function (exports) {
4771
4777
  ObjectId.isValid = function (id) {
4772
4778
  if (id == null)
4773
4779
  return false;
4774
- if (typeof id === 'number') {
4775
- return true;
4776
- }
4777
- if (typeof id === 'string') {
4778
- return id.length === 12 || (id.length === 24 && checkForHexRegExp.test(id));
4779
- }
4780
- if (id instanceof ObjectId) {
4781
- return true;
4782
- }
4783
- if (isUint8Array(id) && id.length === 12) {
4780
+ try {
4781
+ new ObjectId(id);
4784
4782
  return true;
4785
4783
  }
4786
- // Duck-Typing detection of ObjectId like objects
4787
- if (typeof id === 'object' && 'toHexString' in id && typeof id.toHexString === 'function') {
4788
- if (typeof id.id === 'string') {
4789
- return id.id.length === 12;
4790
- }
4791
- return id.toHexString().length === 24 && checkForHexRegExp.test(id.id.toString('hex'));
4784
+ catch (_a) {
4785
+ return false;
4792
4786
  }
4793
- return false;
4794
4787
  };
4795
4788
  /** @internal */
4796
4789
  ObjectId.prototype.toExtendedJSON = function () {
@@ -5586,7 +5579,7 @@ var BSON = (function (exports) {
5586
5579
  }
5587
5580
  else {
5588
5581
  // If we have toBSON defined, override the current object
5589
- if (object.toBSON) {
5582
+ if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
5590
5583
  object = object.toBSON();
5591
5584
  }
5592
5585
  // Calculate size
@@ -5604,7 +5597,7 @@ var BSON = (function (exports) {
5604
5597
  if (isArray === void 0) { isArray = false; }
5605
5598
  if (ignoreUndefined === void 0) { ignoreUndefined = false; }
5606
5599
  // If we have toBSON defined, override the current object
5607
- if (value && value.toBSON) {
5600
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
5608
5601
  value = value.toBSON();
5609
5602
  }
5610
5603
  switch (typeof value) {
@@ -7052,9 +7045,7 @@ var BSON = (function (exports) {
7052
7045
  var key = '' + i;
7053
7046
  var value = object[i];
7054
7047
  // Is there an override value
7055
- if (value && value.toBSON) {
7056
- if (typeof value.toBSON !== 'function')
7057
- throw new BSONTypeError('toBSON is not a function');
7048
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
7058
7049
  value = value.toBSON();
7059
7050
  }
7060
7051
  if (typeof value === 'string') {
@@ -7230,21 +7221,18 @@ var BSON = (function (exports) {
7230
7221
  }
7231
7222
  }
7232
7223
  else {
7233
- // Did we provide a custom serialization method
7234
- if (object.toBSON) {
7235
- if (typeof object.toBSON !== 'function')
7236
- throw new BSONTypeError('toBSON is not a function');
7224
+ if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
7225
+ // Provided a custom serialization method
7237
7226
  object = object.toBSON();
7238
- if (object != null && typeof object !== 'object')
7227
+ if (object != null && typeof object !== 'object') {
7239
7228
  throw new BSONTypeError('toBSON function did not return an object');
7229
+ }
7240
7230
  }
7241
7231
  // Iterate over all the keys
7242
7232
  for (var key in object) {
7243
7233
  var value = object[key];
7244
7234
  // Is there an override value
7245
- if (value && value.toBSON) {
7246
- if (typeof value.toBSON !== 'function')
7247
- throw new BSONTypeError('toBSON is not a function');
7235
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
7248
7236
  value = value.toBSON();
7249
7237
  }
7250
7238
  // Check the type of the value