bson 5.0.1 → 5.2.0

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/bson.d.ts CHANGED
@@ -74,6 +74,10 @@ export declare class Binary extends BSONValue {
74
74
  toString(encoding?: 'hex' | 'base64' | 'utf8' | 'utf-8'): string;
75
75
  /* Excluded from this release type: toExtendedJSON */
76
76
  toUUID(): UUID;
77
+ /** Creates an Binary instance from a hex digit string */
78
+ static createFromHexString(hex: string, subType?: number): Binary;
79
+ /** Creates an Binary instance from a base64 string */
80
+ static createFromBase64(base64: string, subType?: number): Binary;
77
81
  /* Excluded from this release type: fromExtendedJSON */
78
82
  inspect(): string;
79
83
  }
@@ -991,6 +995,8 @@ export declare class ObjectId extends BSONValue {
991
995
  * @param hexString - create a ObjectId from a passed in 24 character hexstring.
992
996
  */
993
997
  static createFromHexString(hexString: string): ObjectId;
998
+ /** Creates an ObjectId instance from a base64 string */
999
+ static createFromBase64(base64: string): ObjectId;
994
1000
  /**
995
1001
  * Checks if a value is a valid bson ObjectId
996
1002
  *
@@ -1214,6 +1220,8 @@ export declare class UUID extends Binary {
1214
1220
  * @param hexString - 32 or 36 character hex string (dashes excluded/included).
1215
1221
  */
1216
1222
  static createFromHexString(hexString: string): UUID;
1223
+ /** Creates an UUID from a base64 string representation of an UUID. */
1224
+ static createFromBase64(base64: string): UUID;
1217
1225
  inspect(): string;
1218
1226
  }
1219
1227
 
@@ -458,6 +458,12 @@ class Binary extends BSONValue {
458
458
  }
459
459
  throw new BSONError(`Binary sub_type "${this.sub_type}" is not supported for converting to UUID. Only "${Binary.SUBTYPE_UUID}" is currently supported.`);
460
460
  }
461
+ static createFromHexString(hex, subType) {
462
+ return new Binary(ByteUtils.fromHex(hex), subType);
463
+ }
464
+ static createFromBase64(base64, subType) {
465
+ return new Binary(ByteUtils.fromBase64(base64), subType);
466
+ }
461
467
  static fromExtendedJSON(doc, options) {
462
468
  options = options || {};
463
469
  let data;
@@ -487,7 +493,8 @@ class Binary extends BSONValue {
487
493
  return this.inspect();
488
494
  }
489
495
  inspect() {
490
- return `new Binary(Buffer.from("${ByteUtils.toHex(this.buffer)}", "hex"), ${this.sub_type})`;
496
+ const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
497
+ return `Binary.createFromBase64("${base64}", ${this.sub_type})`;
491
498
  }
492
499
  }
493
500
  Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
@@ -599,6 +606,9 @@ class UUID extends Binary {
599
606
  const buffer = uuidHexStringToBuffer(hexString);
600
607
  return new UUID(buffer);
601
608
  }
609
+ static createFromBase64(base64) {
610
+ return new UUID(ByteUtils.fromBase64(base64));
611
+ }
602
612
  [Symbol.for('nodejs.util.inspect.custom')]() {
603
613
  return this.inspect();
604
614
  }
@@ -2117,11 +2127,17 @@ class ObjectId extends BSONValue {
2117
2127
  return new ObjectId(buffer);
2118
2128
  }
2119
2129
  static createFromHexString(hexString) {
2120
- if (typeof hexString === 'undefined' || (hexString != null && hexString.length !== 24)) {
2121
- throw new BSONError('Argument passed in must be a single String of 12 bytes or a string of 24 hex characters');
2130
+ if (hexString?.length !== 24) {
2131
+ throw new BSONError('hex string must be 24 characters');
2122
2132
  }
2123
2133
  return new ObjectId(ByteUtils.fromHex(hexString));
2124
2134
  }
2135
+ static createFromBase64(base64) {
2136
+ if (base64?.length !== 16) {
2137
+ throw new BSONError('base64 string must be 16 characters');
2138
+ }
2139
+ return new ObjectId(ByteUtils.fromBase64(base64));
2140
+ }
2125
2141
  static isValid(id) {
2126
2142
  if (id == null)
2127
2143
  return false;
@@ -3788,6 +3804,16 @@ function getISOString(date) {
3788
3804
  return date.getUTCMilliseconds() !== 0 ? isoStr : isoStr.slice(0, -5) + 'Z';
3789
3805
  }
3790
3806
  function serializeValue(value, options) {
3807
+ if (value instanceof Map || isMap(value)) {
3808
+ const obj = Object.create(null);
3809
+ for (const [k, v] of value) {
3810
+ if (typeof k !== 'string') {
3811
+ throw new BSONError('Can only serialize maps with string keys');
3812
+ }
3813
+ obj[k] = v;
3814
+ }
3815
+ return serializeValue(obj, options);
3816
+ }
3791
3817
  if ((typeof value === 'object' || typeof value === 'function') && value !== null) {
3792
3818
  const index = options.seenObjects.findIndex(entry => entry.obj === value);
3793
3819
  if (index !== -1) {