bson 4.6.0 → 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/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
@@ -2556,7 +2562,7 @@ var ObjectId = /** @class */ (function () {
2556
2562
  this[kId] = Buffer.from(workingId, 'hex');
2557
2563
  }
2558
2564
  else {
2559
- throw new BSONTypeError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters');
2565
+ throw new BSONTypeError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer');
2560
2566
  }
2561
2567
  }
2562
2568
  else {
@@ -2673,7 +2679,7 @@ var ObjectId = /** @class */ (function () {
2673
2679
  return false;
2674
2680
  }
2675
2681
  if (otherId instanceof ObjectId) {
2676
- return this.toString() === otherId.toString();
2682
+ return this[kId][11] === otherId[kId][11] && this[kId].equals(otherId[kId]);
2677
2683
  }
2678
2684
  if (typeof otherId === 'string' &&
2679
2685
  ObjectId.isValid(otherId) &&
@@ -2690,7 +2696,9 @@ var ObjectId = /** @class */ (function () {
2690
2696
  if (typeof otherId === 'object' &&
2691
2697
  'toHexString' in otherId &&
2692
2698
  typeof otherId.toHexString === 'function') {
2693
- return otherId.toHexString() === this.toHexString();
2699
+ var otherIdString = otherId.toHexString();
2700
+ var thisIdString = this.toHexString().toLowerCase();
2701
+ return typeof otherIdString === 'string' && otherIdString.toLowerCase() === thisIdString;
2694
2702
  }
2695
2703
  return false;
2696
2704
  };
@@ -2737,26 +2745,13 @@ var ObjectId = /** @class */ (function () {
2737
2745
  ObjectId.isValid = function (id) {
2738
2746
  if (id == null)
2739
2747
  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) {
2748
+ try {
2749
+ new ObjectId(id);
2750
2750
  return true;
2751
2751
  }
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'));
2752
+ catch (_a) {
2753
+ return false;
2758
2754
  }
2759
- return false;
2760
2755
  };
2761
2756
  /** @internal */
2762
2757
  ObjectId.prototype.toExtendedJSON = function () {
@@ -3552,7 +3547,7 @@ function calculateObjectSize$1(object, serializeFunctions, ignoreUndefined) {
3552
3547
  }
3553
3548
  else {
3554
3549
  // If we have toBSON defined, override the current object
3555
- if (object.toBSON) {
3550
+ if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
3556
3551
  object = object.toBSON();
3557
3552
  }
3558
3553
  // Calculate size
@@ -3570,7 +3565,7 @@ value, serializeFunctions, isArray, ignoreUndefined) {
3570
3565
  if (isArray === void 0) { isArray = false; }
3571
3566
  if (ignoreUndefined === void 0) { ignoreUndefined = false; }
3572
3567
  // If we have toBSON defined, override the current object
3573
- if (value && value.toBSON) {
3568
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
3574
3569
  value = value.toBSON();
3575
3570
  }
3576
3571
  switch (typeof value) {
@@ -5018,9 +5013,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
5018
5013
  var key = '' + i;
5019
5014
  var value = object[i];
5020
5015
  // 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');
5016
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
5024
5017
  value = value.toBSON();
5025
5018
  }
5026
5019
  if (typeof value === 'string') {
@@ -5196,21 +5189,18 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
5196
5189
  }
5197
5190
  }
5198
5191
  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');
5192
+ if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
5193
+ // Provided a custom serialization method
5203
5194
  object = object.toBSON();
5204
- if (object != null && typeof object !== 'object')
5195
+ if (object != null && typeof object !== 'object') {
5205
5196
  throw new BSONTypeError('toBSON function did not return an object');
5197
+ }
5206
5198
  }
5207
5199
  // Iterate over all the keys
5208
5200
  for (var key in object) {
5209
5201
  var value = object[key];
5210
5202
  // 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');
5203
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
5214
5204
  value = value.toBSON();
5215
5205
  }
5216
5206
  // Check the type of the value