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/lib/bson.cjs CHANGED
@@ -457,6 +457,12 @@ class Binary extends BSONValue {
457
457
  }
458
458
  throw new BSONError(`Binary sub_type "${this.sub_type}" is not supported for converting to UUID. Only "${Binary.SUBTYPE_UUID}" is currently supported.`);
459
459
  }
460
+ static createFromHexString(hex, subType) {
461
+ return new Binary(ByteUtils.fromHex(hex), subType);
462
+ }
463
+ static createFromBase64(base64, subType) {
464
+ return new Binary(ByteUtils.fromBase64(base64), subType);
465
+ }
460
466
  static fromExtendedJSON(doc, options) {
461
467
  options = options || {};
462
468
  let data;
@@ -486,7 +492,8 @@ class Binary extends BSONValue {
486
492
  return this.inspect();
487
493
  }
488
494
  inspect() {
489
- return `new Binary(Buffer.from("${ByteUtils.toHex(this.buffer)}", "hex"), ${this.sub_type})`;
495
+ const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
496
+ return `Binary.createFromBase64("${base64}", ${this.sub_type})`;
490
497
  }
491
498
  }
492
499
  Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
@@ -598,6 +605,9 @@ class UUID extends Binary {
598
605
  const buffer = uuidHexStringToBuffer(hexString);
599
606
  return new UUID(buffer);
600
607
  }
608
+ static createFromBase64(base64) {
609
+ return new UUID(ByteUtils.fromBase64(base64));
610
+ }
601
611
  [Symbol.for('nodejs.util.inspect.custom')]() {
602
612
  return this.inspect();
603
613
  }
@@ -2116,11 +2126,17 @@ class ObjectId extends BSONValue {
2116
2126
  return new ObjectId(buffer);
2117
2127
  }
2118
2128
  static createFromHexString(hexString) {
2119
- if (typeof hexString === 'undefined' || (hexString != null && hexString.length !== 24)) {
2120
- throw new BSONError('Argument passed in must be a single String of 12 bytes or a string of 24 hex characters');
2129
+ if (hexString?.length !== 24) {
2130
+ throw new BSONError('hex string must be 24 characters');
2121
2131
  }
2122
2132
  return new ObjectId(ByteUtils.fromHex(hexString));
2123
2133
  }
2134
+ static createFromBase64(base64) {
2135
+ if (base64?.length !== 16) {
2136
+ throw new BSONError('base64 string must be 16 characters');
2137
+ }
2138
+ return new ObjectId(ByteUtils.fromBase64(base64));
2139
+ }
2124
2140
  static isValid(id) {
2125
2141
  if (id == null)
2126
2142
  return false;
@@ -3787,6 +3803,16 @@ function getISOString(date) {
3787
3803
  return date.getUTCMilliseconds() !== 0 ? isoStr : isoStr.slice(0, -5) + 'Z';
3788
3804
  }
3789
3805
  function serializeValue(value, options) {
3806
+ if (value instanceof Map || isMap(value)) {
3807
+ const obj = Object.create(null);
3808
+ for (const [k, v] of value) {
3809
+ if (typeof k !== 'string') {
3810
+ throw new BSONError('Can only serialize maps with string keys');
3811
+ }
3812
+ obj[k] = v;
3813
+ }
3814
+ return serializeValue(obj, options);
3815
+ }
3790
3816
  if ((typeof value === 'object' || typeof value === 'function') && value !== null) {
3791
3817
  const index = options.seenObjects.findIndex(entry => entry.obj === value);
3792
3818
  if (index !== -1) {