bson 5.1.0 → 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/lib/bson.mjs CHANGED
@@ -455,6 +455,12 @@ class Binary extends BSONValue {
455
455
  }
456
456
  throw new BSONError(`Binary sub_type "${this.sub_type}" is not supported for converting to UUID. Only "${Binary.SUBTYPE_UUID}" is currently supported.`);
457
457
  }
458
+ static createFromHexString(hex, subType) {
459
+ return new Binary(ByteUtils.fromHex(hex), subType);
460
+ }
461
+ static createFromBase64(base64, subType) {
462
+ return new Binary(ByteUtils.fromBase64(base64), subType);
463
+ }
458
464
  static fromExtendedJSON(doc, options) {
459
465
  options = options || {};
460
466
  let data;
@@ -484,7 +490,8 @@ class Binary extends BSONValue {
484
490
  return this.inspect();
485
491
  }
486
492
  inspect() {
487
- return `new Binary(Buffer.from("${ByteUtils.toHex(this.buffer)}", "hex"), ${this.sub_type})`;
493
+ const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
494
+ return `Binary.createFromBase64("${base64}", ${this.sub_type})`;
488
495
  }
489
496
  }
490
497
  Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
@@ -596,6 +603,9 @@ class UUID extends Binary {
596
603
  const buffer = uuidHexStringToBuffer(hexString);
597
604
  return new UUID(buffer);
598
605
  }
606
+ static createFromBase64(base64) {
607
+ return new UUID(ByteUtils.fromBase64(base64));
608
+ }
599
609
  [Symbol.for('nodejs.util.inspect.custom')]() {
600
610
  return this.inspect();
601
611
  }
@@ -2114,11 +2124,17 @@ class ObjectId extends BSONValue {
2114
2124
  return new ObjectId(buffer);
2115
2125
  }
2116
2126
  static createFromHexString(hexString) {
2117
- if (typeof hexString === 'undefined' || (hexString != null && hexString.length !== 24)) {
2118
- throw new BSONError('Argument passed in must be a single String of 12 bytes or a string of 24 hex characters');
2127
+ if (hexString?.length !== 24) {
2128
+ throw new BSONError('hex string must be 24 characters');
2119
2129
  }
2120
2130
  return new ObjectId(ByteUtils.fromHex(hexString));
2121
2131
  }
2132
+ static createFromBase64(base64) {
2133
+ if (base64?.length !== 16) {
2134
+ throw new BSONError('base64 string must be 16 characters');
2135
+ }
2136
+ return new ObjectId(ByteUtils.fromBase64(base64));
2137
+ }
2122
2138
  static isValid(id) {
2123
2139
  if (id == null)
2124
2140
  return false;