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.
package/dist/bson.esm.js CHANGED
@@ -1769,9 +1769,15 @@ var Decimal128 = /** @class */ (function () {
1769
1769
  if (typeof bytes === 'string') {
1770
1770
  this.bytes = Decimal128.fromString(bytes).bytes;
1771
1771
  }
1772
- else {
1772
+ else if (isUint8Array(bytes)) {
1773
+ if (bytes.byteLength !== 16) {
1774
+ throw new BSONTypeError('Decimal128 must take a Buffer of 16 bytes');
1775
+ }
1773
1776
  this.bytes = bytes;
1774
1777
  }
1778
+ else {
1779
+ throw new BSONTypeError('Decimal128 must take a Buffer or string');
1780
+ }
1775
1781
  }
1776
1782
  /**
1777
1783
  * Create a Decimal128 instance from a string representation
@@ -2737,26 +2743,13 @@ var ObjectId = /** @class */ (function () {
2737
2743
  ObjectId.isValid = function (id) {
2738
2744
  if (id == null)
2739
2745
  return false;
2740
- if (typeof id === 'number') {
2741
- return true;
2742
- }
2743
- if (typeof id === 'string') {
2744
- return id.length === 12 || (id.length === 24 && checkForHexRegExp.test(id));
2745
- }
2746
- if (id instanceof ObjectId) {
2747
- return true;
2748
- }
2749
- if (isUint8Array(id) && id.length === 12) {
2746
+ try {
2747
+ new ObjectId(id);
2750
2748
  return true;
2751
2749
  }
2752
- // Duck-Typing detection of ObjectId like objects
2753
- if (typeof id === 'object' && 'toHexString' in id && typeof id.toHexString === 'function') {
2754
- if (typeof id.id === 'string') {
2755
- return id.id.length === 12;
2756
- }
2757
- return id.toHexString().length === 24 && checkForHexRegExp.test(id.id.toString('hex'));
2750
+ catch (_a) {
2751
+ return false;
2758
2752
  }
2759
- return false;
2760
2753
  };
2761
2754
  /** @internal */
2762
2755
  ObjectId.prototype.toExtendedJSON = function () {
@@ -3552,7 +3545,7 @@ function calculateObjectSize$1(object, serializeFunctions, ignoreUndefined) {
3552
3545
  }
3553
3546
  else {
3554
3547
  // If we have toBSON defined, override the current object
3555
- if (object.toBSON) {
3548
+ if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
3556
3549
  object = object.toBSON();
3557
3550
  }
3558
3551
  // Calculate size
@@ -3570,7 +3563,7 @@ value, serializeFunctions, isArray, ignoreUndefined) {
3570
3563
  if (isArray === void 0) { isArray = false; }
3571
3564
  if (ignoreUndefined === void 0) { ignoreUndefined = false; }
3572
3565
  // If we have toBSON defined, override the current object
3573
- if (value && value.toBSON) {
3566
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
3574
3567
  value = value.toBSON();
3575
3568
  }
3576
3569
  switch (typeof value) {
@@ -5018,9 +5011,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
5018
5011
  var key = '' + i;
5019
5012
  var value = object[i];
5020
5013
  // Is there an override value
5021
- if (value && value.toBSON) {
5022
- if (typeof value.toBSON !== 'function')
5023
- throw new BSONTypeError('toBSON is not a function');
5014
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
5024
5015
  value = value.toBSON();
5025
5016
  }
5026
5017
  if (typeof value === 'string') {
@@ -5196,21 +5187,18 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
5196
5187
  }
5197
5188
  }
5198
5189
  else {
5199
- // Did we provide a custom serialization method
5200
- if (object.toBSON) {
5201
- if (typeof object.toBSON !== 'function')
5202
- throw new BSONTypeError('toBSON is not a function');
5190
+ if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
5191
+ // Provided a custom serialization method
5203
5192
  object = object.toBSON();
5204
- if (object != null && typeof object !== 'object')
5193
+ if (object != null && typeof object !== 'object') {
5205
5194
  throw new BSONTypeError('toBSON function did not return an object');
5195
+ }
5206
5196
  }
5207
5197
  // Iterate over all the keys
5208
5198
  for (var key in object) {
5209
5199
  var value = object[key];
5210
5200
  // Is there an override value
5211
- if (value && value.toBSON) {
5212
- if (typeof value.toBSON !== 'function')
5213
- throw new BSONTypeError('toBSON is not a function');
5201
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
5214
5202
  value = value.toBSON();
5215
5203
  }
5216
5204
  // Check the type of the value