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