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/bower.json CHANGED
@@ -22,5 +22,5 @@
22
22
  "test",
23
23
  "tools"
24
24
  ],
25
- "version": "4.6.0"
25
+ "version": "4.6.1"
26
26
  }
package/bson.d.ts CHANGED
@@ -885,7 +885,7 @@ declare class ObjectId {
885
885
  *
886
886
  * @param inputId - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
887
887
  */
888
- constructor(inputId?: string | Buffer | number | ObjectIdLike | ObjectId);
888
+ constructor(inputId?: string | number | ObjectId | ObjectIdLike | Buffer | Uint8Array);
889
889
  /*
890
890
  * The ObjectId bytes
891
891
  * @readonly
@@ -939,7 +939,7 @@ declare class ObjectId {
939
939
  *
940
940
  * @param id - ObjectId instance to validate.
941
941
  */
942
- static isValid(id: number | string | ObjectId | Uint8Array | ObjectIdLike): boolean;
942
+ static isValid(id: string | number | ObjectId | ObjectIdLike | Buffer | Uint8Array): boolean;
943
943
  /* Excluded from this release type: toExtendedJSON */
944
944
  /* Excluded from this release type: fromExtendedJSON */
945
945
  inspect(): string;
@@ -3800,9 +3800,15 @@ var Decimal128 = /** @class */ (function () {
3800
3800
  if (typeof bytes === 'string') {
3801
3801
  this.bytes = Decimal128.fromString(bytes).bytes;
3802
3802
  }
3803
- else {
3803
+ else if (isUint8Array(bytes)) {
3804
+ if (bytes.byteLength !== 16) {
3805
+ throw new BSONTypeError('Decimal128 must take a Buffer of 16 bytes');
3806
+ }
3804
3807
  this.bytes = bytes;
3805
3808
  }
3809
+ else {
3810
+ throw new BSONTypeError('Decimal128 must take a Buffer or string');
3811
+ }
3806
3812
  }
3807
3813
  /**
3808
3814
  * Create a Decimal128 instance from a string representation
@@ -4768,26 +4774,13 @@ var ObjectId = /** @class */ (function () {
4768
4774
  ObjectId.isValid = function (id) {
4769
4775
  if (id == null)
4770
4776
  return false;
4771
- if (typeof id === 'number') {
4772
- return true;
4773
- }
4774
- if (typeof id === 'string') {
4775
- return id.length === 12 || (id.length === 24 && checkForHexRegExp.test(id));
4776
- }
4777
- if (id instanceof ObjectId) {
4778
- return true;
4779
- }
4780
- if (isUint8Array(id) && id.length === 12) {
4777
+ try {
4778
+ new ObjectId(id);
4781
4779
  return true;
4782
4780
  }
4783
- // Duck-Typing detection of ObjectId like objects
4784
- if (typeof id === 'object' && 'toHexString' in id && typeof id.toHexString === 'function') {
4785
- if (typeof id.id === 'string') {
4786
- return id.id.length === 12;
4787
- }
4788
- return id.toHexString().length === 24 && checkForHexRegExp.test(id.id.toString('hex'));
4781
+ catch (_a) {
4782
+ return false;
4789
4783
  }
4790
- return false;
4791
4784
  };
4792
4785
  /** @internal */
4793
4786
  ObjectId.prototype.toExtendedJSON = function () {
@@ -5583,7 +5576,7 @@ function calculateObjectSize$1(object, serializeFunctions, ignoreUndefined) {
5583
5576
  }
5584
5577
  else {
5585
5578
  // If we have toBSON defined, override the current object
5586
- if (object.toBSON) {
5579
+ if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
5587
5580
  object = object.toBSON();
5588
5581
  }
5589
5582
  // Calculate size
@@ -5601,7 +5594,7 @@ value, serializeFunctions, isArray, ignoreUndefined) {
5601
5594
  if (isArray === void 0) { isArray = false; }
5602
5595
  if (ignoreUndefined === void 0) { ignoreUndefined = false; }
5603
5596
  // If we have toBSON defined, override the current object
5604
- if (value && value.toBSON) {
5597
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
5605
5598
  value = value.toBSON();
5606
5599
  }
5607
5600
  switch (typeof value) {
@@ -7049,9 +7042,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
7049
7042
  var key = '' + i;
7050
7043
  var value = object[i];
7051
7044
  // Is there an override value
7052
- if (value && value.toBSON) {
7053
- if (typeof value.toBSON !== 'function')
7054
- throw new BSONTypeError('toBSON is not a function');
7045
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
7055
7046
  value = value.toBSON();
7056
7047
  }
7057
7048
  if (typeof value === 'string') {
@@ -7227,21 +7218,18 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
7227
7218
  }
7228
7219
  }
7229
7220
  else {
7230
- // Did we provide a custom serialization method
7231
- if (object.toBSON) {
7232
- if (typeof object.toBSON !== 'function')
7233
- throw new BSONTypeError('toBSON is not a function');
7221
+ if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
7222
+ // Provided a custom serialization method
7234
7223
  object = object.toBSON();
7235
- if (object != null && typeof object !== 'object')
7224
+ if (object != null && typeof object !== 'object') {
7236
7225
  throw new BSONTypeError('toBSON function did not return an object');
7226
+ }
7237
7227
  }
7238
7228
  // Iterate over all the keys
7239
7229
  for (var key in object) {
7240
7230
  var value = object[key];
7241
7231
  // Is there an override value
7242
- if (value && value.toBSON) {
7243
- if (typeof value.toBSON !== 'function')
7244
- throw new BSONTypeError('toBSON is not a function');
7232
+ if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
7245
7233
  value = value.toBSON();
7246
7234
  }
7247
7235
  // Check the type of the value