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 +8 -0
- package/lib/bson.bundle.js +29 -3
- package/lib/bson.bundle.js.map +1 -1
- package/lib/bson.cjs +29 -3
- package/lib/bson.cjs.map +1 -1
- package/lib/bson.mjs +29 -3
- package/lib/bson.mjs.map +1 -1
- package/package.json +1 -1
- package/src/binary.ts +18 -2
- package/src/extended_json.ts +13 -1
- package/src/objectid.ts +11 -5
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
|
-
|
|
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 (
|
|
2118
|
-
throw new BSONError('
|
|
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;
|
|
@@ -3785,6 +3801,16 @@ function getISOString(date) {
|
|
|
3785
3801
|
return date.getUTCMilliseconds() !== 0 ? isoStr : isoStr.slice(0, -5) + 'Z';
|
|
3786
3802
|
}
|
|
3787
3803
|
function serializeValue(value, options) {
|
|
3804
|
+
if (value instanceof Map || isMap(value)) {
|
|
3805
|
+
const obj = Object.create(null);
|
|
3806
|
+
for (const [k, v] of value) {
|
|
3807
|
+
if (typeof k !== 'string') {
|
|
3808
|
+
throw new BSONError('Can only serialize maps with string keys');
|
|
3809
|
+
}
|
|
3810
|
+
obj[k] = v;
|
|
3811
|
+
}
|
|
3812
|
+
return serializeValue(obj, options);
|
|
3813
|
+
}
|
|
3788
3814
|
if ((typeof value === 'object' || typeof value === 'function') && value !== null) {
|
|
3789
3815
|
const index = options.seenObjects.findIndex(entry => entry.obj === value);
|
|
3790
3816
|
if (index !== -1) {
|