bson 6.10.3 → 7.0.0-alpha
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/README.md +11 -1
- package/bson.d.ts +21 -14
- package/lib/bson.bundle.js +147 -78
- package/lib/bson.bundle.js.map +1 -1
- package/lib/bson.cjs +147 -78
- package/lib/bson.cjs.map +1 -1
- package/lib/bson.mjs +148 -80
- package/lib/bson.mjs.map +1 -1
- package/lib/bson.node.mjs +4636 -0
- package/lib/bson.node.mjs.map +1 -0
- package/lib/bson.rn.cjs +146 -80
- package/lib/bson.rn.cjs.map +1 -1
- package/package.json +32 -32
- package/src/binary.ts +27 -4
- package/src/bson.ts +1 -1
- package/src/bson_value.ts +24 -1
- package/src/extended_json.ts +0 -3
- package/src/long.ts +4 -9
- package/src/objectid.ts +25 -25
- package/src/parser/on_demand/parse_to_elements.ts +25 -23
- package/src/timestamp.ts +10 -1
- package/src/utils/node_byte_utils.ts +15 -22
- package/src/utils/number_utils.ts +4 -13
- package/vendor/base64/LICENSE-MIT.txt +0 -20
- package/vendor/base64/README.md +0 -112
- package/vendor/base64/base64.js +0 -157
- package/vendor/base64/package.json +0 -43
- package/vendor/text-encoding/LICENSE.md +0 -237
- package/vendor/text-encoding/README.md +0 -111
- package/vendor/text-encoding/index.js +0 -9
- package/vendor/text-encoding/lib/encoding-indexes.js +0 -47
- package/vendor/text-encoding/lib/encoding.js +0 -3301
- package/vendor/text-encoding/package.json +0 -37
package/lib/bson.cjs
CHANGED
|
@@ -142,6 +142,7 @@ class BSONOffsetError extends BSONError {
|
|
|
142
142
|
get name() {
|
|
143
143
|
return 'BSONOffsetError';
|
|
144
144
|
}
|
|
145
|
+
offset;
|
|
145
146
|
constructor(message, offset, options) {
|
|
146
147
|
super(`${message}. offset: ${offset}`, options);
|
|
147
148
|
this.offset = offset;
|
|
@@ -218,11 +219,15 @@ function tryWriteBasicLatin(destination, source, offset) {
|
|
|
218
219
|
function nodejsMathRandomBytes(byteLength) {
|
|
219
220
|
return nodeJsByteUtils.fromNumberArray(Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256)));
|
|
220
221
|
}
|
|
222
|
+
function nodejsSecureRandomBytes(byteLength) {
|
|
223
|
+
return crypto.getRandomValues(nodeJsByteUtils.allocate(byteLength));
|
|
224
|
+
}
|
|
221
225
|
const nodejsRandomBytes = (() => {
|
|
222
|
-
|
|
223
|
-
|
|
226
|
+
const { crypto } = globalThis;
|
|
227
|
+
if (crypto != null && typeof crypto.getRandomValues === 'function') {
|
|
228
|
+
return nodejsSecureRandomBytes;
|
|
224
229
|
}
|
|
225
|
-
|
|
230
|
+
else {
|
|
226
231
|
return nodejsMathRandomBytes;
|
|
227
232
|
}
|
|
228
233
|
})();
|
|
@@ -441,7 +446,11 @@ const webByteUtils = {
|
|
|
441
446
|
const hasGlobalBuffer = typeof Buffer === 'function' && Buffer.prototype?._isBuffer !== true;
|
|
442
447
|
const ByteUtils = hasGlobalBuffer ? nodeJsByteUtils : webByteUtils;
|
|
443
448
|
|
|
449
|
+
const bsonType = Symbol.for('@@mdb.bson.type');
|
|
444
450
|
class BSONValue {
|
|
451
|
+
get [bsonType]() {
|
|
452
|
+
return this._bsontype;
|
|
453
|
+
}
|
|
445
454
|
get [BSON_VERSION_SYMBOL]() {
|
|
446
455
|
return BSON_MAJOR_VERSION;
|
|
447
456
|
}
|
|
@@ -492,7 +501,7 @@ const NumberUtils = {
|
|
|
492
501
|
source[offset + 1] * 256 +
|
|
493
502
|
source[offset + 2] * 65536 +
|
|
494
503
|
source[offset + 3] * 16777216);
|
|
495
|
-
return (hi <<
|
|
504
|
+
return (hi << 32n) + lo;
|
|
496
505
|
},
|
|
497
506
|
getFloat64LE: isBigEndian
|
|
498
507
|
? (source, offset) => {
|
|
@@ -538,7 +547,7 @@ const NumberUtils = {
|
|
|
538
547
|
return 4;
|
|
539
548
|
},
|
|
540
549
|
setBigInt64LE(destination, offset, value) {
|
|
541
|
-
const mask32bits =
|
|
550
|
+
const mask32bits = 0xffffffffn;
|
|
542
551
|
let lo = Number(value & mask32bits);
|
|
543
552
|
destination[offset] = lo;
|
|
544
553
|
lo >>= 8;
|
|
@@ -547,7 +556,7 @@ const NumberUtils = {
|
|
|
547
556
|
destination[offset + 2] = lo;
|
|
548
557
|
lo >>= 8;
|
|
549
558
|
destination[offset + 3] = lo;
|
|
550
|
-
let hi = Number((value >>
|
|
559
|
+
let hi = Number((value >> 32n) & mask32bits);
|
|
551
560
|
destination[offset + 4] = hi;
|
|
552
561
|
hi >>= 8;
|
|
553
562
|
destination[offset + 5] = hi;
|
|
@@ -588,6 +597,27 @@ class Binary extends BSONValue {
|
|
|
588
597
|
get _bsontype() {
|
|
589
598
|
return 'Binary';
|
|
590
599
|
}
|
|
600
|
+
static BSON_BINARY_SUBTYPE_DEFAULT = 0;
|
|
601
|
+
static BUFFER_SIZE = 256;
|
|
602
|
+
static SUBTYPE_DEFAULT = 0;
|
|
603
|
+
static SUBTYPE_FUNCTION = 1;
|
|
604
|
+
static SUBTYPE_BYTE_ARRAY = 2;
|
|
605
|
+
static SUBTYPE_UUID_OLD = 3;
|
|
606
|
+
static SUBTYPE_UUID = 4;
|
|
607
|
+
static SUBTYPE_MD5 = 5;
|
|
608
|
+
static SUBTYPE_ENCRYPTED = 6;
|
|
609
|
+
static SUBTYPE_COLUMN = 7;
|
|
610
|
+
static SUBTYPE_SENSITIVE = 8;
|
|
611
|
+
static SUBTYPE_VECTOR = 9;
|
|
612
|
+
static SUBTYPE_USER_DEFINED = 128;
|
|
613
|
+
static VECTOR_TYPE = Object.freeze({
|
|
614
|
+
Int8: 0x03,
|
|
615
|
+
Float32: 0x27,
|
|
616
|
+
PackedBit: 0x10
|
|
617
|
+
});
|
|
618
|
+
buffer;
|
|
619
|
+
sub_type;
|
|
620
|
+
position;
|
|
591
621
|
constructor(buffer, subType) {
|
|
592
622
|
super();
|
|
593
623
|
if (!(buffer == null) &&
|
|
@@ -750,6 +780,7 @@ class Binary extends BSONValue {
|
|
|
750
780
|
if (this.buffer[0] !== Binary.VECTOR_TYPE.Int8) {
|
|
751
781
|
throw new BSONError('Binary datatype field is not Int8');
|
|
752
782
|
}
|
|
783
|
+
validateBinaryVector(this);
|
|
753
784
|
return new Int8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
|
|
754
785
|
}
|
|
755
786
|
toFloat32Array() {
|
|
@@ -759,6 +790,7 @@ class Binary extends BSONValue {
|
|
|
759
790
|
if (this.buffer[0] !== Binary.VECTOR_TYPE.Float32) {
|
|
760
791
|
throw new BSONError('Binary datatype field is not Float32');
|
|
761
792
|
}
|
|
793
|
+
validateBinaryVector(this);
|
|
762
794
|
const floatBytes = new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
|
|
763
795
|
if (NumberUtils.isBigEndian)
|
|
764
796
|
ByteUtils.swap32(floatBytes);
|
|
@@ -771,6 +803,7 @@ class Binary extends BSONValue {
|
|
|
771
803
|
if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
|
|
772
804
|
throw new BSONError('Binary datatype field is not packed bit');
|
|
773
805
|
}
|
|
806
|
+
validateBinaryVector(this);
|
|
774
807
|
return new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
|
|
775
808
|
}
|
|
776
809
|
toBits() {
|
|
@@ -780,6 +813,7 @@ class Binary extends BSONValue {
|
|
|
780
813
|
if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
|
|
781
814
|
throw new BSONError('Binary datatype field is not packed bit');
|
|
782
815
|
}
|
|
816
|
+
validateBinaryVector(this);
|
|
783
817
|
const byteCount = this.length() - 2;
|
|
784
818
|
const bitCount = byteCount * 8 - this.buffer[1];
|
|
785
819
|
const bits = new Int8Array(bitCount);
|
|
@@ -798,7 +832,9 @@ class Binary extends BSONValue {
|
|
|
798
832
|
buffer[1] = 0;
|
|
799
833
|
const intBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
|
|
800
834
|
buffer.set(intBytes, 2);
|
|
801
|
-
|
|
835
|
+
const bin = new this(buffer, this.SUBTYPE_VECTOR);
|
|
836
|
+
validateBinaryVector(bin);
|
|
837
|
+
return bin;
|
|
802
838
|
}
|
|
803
839
|
static fromFloat32Array(array) {
|
|
804
840
|
const binaryBytes = ByteUtils.allocate(array.byteLength + 2);
|
|
@@ -808,14 +844,18 @@ class Binary extends BSONValue {
|
|
|
808
844
|
binaryBytes.set(floatBytes, 2);
|
|
809
845
|
if (NumberUtils.isBigEndian)
|
|
810
846
|
ByteUtils.swap32(new Uint8Array(binaryBytes.buffer, 2));
|
|
811
|
-
|
|
847
|
+
const bin = new this(binaryBytes, this.SUBTYPE_VECTOR);
|
|
848
|
+
validateBinaryVector(bin);
|
|
849
|
+
return bin;
|
|
812
850
|
}
|
|
813
851
|
static fromPackedBits(array, padding = 0) {
|
|
814
852
|
const buffer = ByteUtils.allocate(array.byteLength + 2);
|
|
815
853
|
buffer[0] = Binary.VECTOR_TYPE.PackedBit;
|
|
816
854
|
buffer[1] = padding;
|
|
817
855
|
buffer.set(array, 2);
|
|
818
|
-
|
|
856
|
+
const bin = new this(buffer, this.SUBTYPE_VECTOR);
|
|
857
|
+
validateBinaryVector(bin);
|
|
858
|
+
return bin;
|
|
819
859
|
}
|
|
820
860
|
static fromBits(bits) {
|
|
821
861
|
const byteLength = (bits.length + 7) >>> 3;
|
|
@@ -837,24 +877,6 @@ class Binary extends BSONValue {
|
|
|
837
877
|
return new this(bytes, Binary.SUBTYPE_VECTOR);
|
|
838
878
|
}
|
|
839
879
|
}
|
|
840
|
-
Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
|
|
841
|
-
Binary.BUFFER_SIZE = 256;
|
|
842
|
-
Binary.SUBTYPE_DEFAULT = 0;
|
|
843
|
-
Binary.SUBTYPE_FUNCTION = 1;
|
|
844
|
-
Binary.SUBTYPE_BYTE_ARRAY = 2;
|
|
845
|
-
Binary.SUBTYPE_UUID_OLD = 3;
|
|
846
|
-
Binary.SUBTYPE_UUID = 4;
|
|
847
|
-
Binary.SUBTYPE_MD5 = 5;
|
|
848
|
-
Binary.SUBTYPE_ENCRYPTED = 6;
|
|
849
|
-
Binary.SUBTYPE_COLUMN = 7;
|
|
850
|
-
Binary.SUBTYPE_SENSITIVE = 8;
|
|
851
|
-
Binary.SUBTYPE_VECTOR = 9;
|
|
852
|
-
Binary.SUBTYPE_USER_DEFINED = 128;
|
|
853
|
-
Binary.VECTOR_TYPE = Object.freeze({
|
|
854
|
-
Int8: 0x03,
|
|
855
|
-
Float32: 0x27,
|
|
856
|
-
PackedBit: 0x10
|
|
857
|
-
});
|
|
858
880
|
function validateBinaryVector(vector) {
|
|
859
881
|
if (vector.sub_type !== Binary.SUBTYPE_VECTOR)
|
|
860
882
|
return;
|
|
@@ -865,6 +887,11 @@ function validateBinaryVector(vector) {
|
|
|
865
887
|
padding !== 0) {
|
|
866
888
|
throw new BSONError('Invalid Vector: padding must be zero for int8 and float32 vectors');
|
|
867
889
|
}
|
|
890
|
+
if (datatype === Binary.VECTOR_TYPE.Float32) {
|
|
891
|
+
if (size !== 0 && size - 2 !== 0 && (size - 2) % 4 !== 0) {
|
|
892
|
+
throw new BSONError('Invalid Vector: Float32 vector must contain a multiple of 4 bytes');
|
|
893
|
+
}
|
|
894
|
+
}
|
|
868
895
|
if (datatype === Binary.VECTOR_TYPE.PackedBit && padding !== 0 && size === 2) {
|
|
869
896
|
throw new BSONError('Invalid Vector: padding must be zero for packed bit vectors that are empty');
|
|
870
897
|
}
|
|
@@ -986,6 +1013,8 @@ class Code extends BSONValue {
|
|
|
986
1013
|
get _bsontype() {
|
|
987
1014
|
return 'Code';
|
|
988
1015
|
}
|
|
1016
|
+
code;
|
|
1017
|
+
scope;
|
|
989
1018
|
constructor(code, scope) {
|
|
990
1019
|
super();
|
|
991
1020
|
this.code = code.toString();
|
|
@@ -1031,6 +1060,10 @@ class DBRef extends BSONValue {
|
|
|
1031
1060
|
get _bsontype() {
|
|
1032
1061
|
return 'DBRef';
|
|
1033
1062
|
}
|
|
1063
|
+
collection;
|
|
1064
|
+
oid;
|
|
1065
|
+
db;
|
|
1066
|
+
fields;
|
|
1034
1067
|
constructor(collection, oid, db, fields) {
|
|
1035
1068
|
super();
|
|
1036
1069
|
const parts = collection.split('.');
|
|
@@ -1140,6 +1173,9 @@ class Long extends BSONValue {
|
|
|
1140
1173
|
get __isLong__() {
|
|
1141
1174
|
return true;
|
|
1142
1175
|
}
|
|
1176
|
+
high;
|
|
1177
|
+
low;
|
|
1178
|
+
unsigned;
|
|
1143
1179
|
constructor(lowOrValue = 0, highOrUnsigned, unsigned) {
|
|
1144
1180
|
super();
|
|
1145
1181
|
const unsignedBool = typeof highOrUnsigned === 'boolean' ? highOrUnsigned : Boolean(unsigned);
|
|
@@ -1153,6 +1189,15 @@ class Long extends BSONValue {
|
|
|
1153
1189
|
this.high = res.high;
|
|
1154
1190
|
this.unsigned = res.unsigned;
|
|
1155
1191
|
}
|
|
1192
|
+
static TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
|
|
1193
|
+
static MAX_UNSIGNED_VALUE = Long.fromBits(0xffffffff | 0, 0xffffffff | 0, true);
|
|
1194
|
+
static ZERO = Long.fromInt(0);
|
|
1195
|
+
static UZERO = Long.fromInt(0, true);
|
|
1196
|
+
static ONE = Long.fromInt(1);
|
|
1197
|
+
static UONE = Long.fromInt(1, true);
|
|
1198
|
+
static NEG_ONE = Long.fromInt(-1);
|
|
1199
|
+
static MAX_VALUE = Long.fromBits(0xffffffff | 0, 0x7fffffff | 0, false);
|
|
1200
|
+
static MIN_VALUE = Long.fromBits(0, 0x80000000 | 0, false);
|
|
1156
1201
|
static fromBits(lowBits, highBits, unsigned) {
|
|
1157
1202
|
return new Long(lowBits, highBits, unsigned);
|
|
1158
1203
|
}
|
|
@@ -1193,7 +1238,7 @@ class Long extends BSONValue {
|
|
|
1193
1238
|
return Long.MAX_UNSIGNED_VALUE;
|
|
1194
1239
|
}
|
|
1195
1240
|
else {
|
|
1196
|
-
if (value <= -
|
|
1241
|
+
if (value <= -TWO_PWR_63_DBL)
|
|
1197
1242
|
return Long.MIN_VALUE;
|
|
1198
1243
|
if (value + 1 >= TWO_PWR_63_DBL)
|
|
1199
1244
|
return Long.MAX_VALUE;
|
|
@@ -1203,8 +1248,8 @@ class Long extends BSONValue {
|
|
|
1203
1248
|
return Long.fromBits(value % TWO_PWR_32_DBL | 0, (value / TWO_PWR_32_DBL) | 0, unsigned);
|
|
1204
1249
|
}
|
|
1205
1250
|
static fromBigInt(value, unsigned) {
|
|
1206
|
-
const FROM_BIGINT_BIT_MASK =
|
|
1207
|
-
const FROM_BIGINT_BIT_SHIFT =
|
|
1251
|
+
const FROM_BIGINT_BIT_MASK = 0xffffffffn;
|
|
1252
|
+
const FROM_BIGINT_BIT_SHIFT = 32n;
|
|
1208
1253
|
return new Long(Number(value & FROM_BIGINT_BIT_MASK), Number((value >> FROM_BIGINT_BIT_SHIFT) & FROM_BIGINT_BIT_MASK), unsigned);
|
|
1209
1254
|
}
|
|
1210
1255
|
static _fromString(str, unsigned, radix) {
|
|
@@ -1237,7 +1282,7 @@ class Long extends BSONValue {
|
|
|
1237
1282
|
static fromStringStrict(str, unsignedOrRadix, radix) {
|
|
1238
1283
|
let unsigned = false;
|
|
1239
1284
|
if (typeof unsignedOrRadix === 'number') {
|
|
1240
|
-
(radix = unsignedOrRadix), (unsignedOrRadix = false);
|
|
1285
|
+
((radix = unsignedOrRadix), (unsignedOrRadix = false));
|
|
1241
1286
|
}
|
|
1242
1287
|
else {
|
|
1243
1288
|
unsigned = !!unsignedOrRadix;
|
|
@@ -1259,7 +1304,7 @@ class Long extends BSONValue {
|
|
|
1259
1304
|
static fromString(str, unsignedOrRadix, radix) {
|
|
1260
1305
|
let unsigned = false;
|
|
1261
1306
|
if (typeof unsignedOrRadix === 'number') {
|
|
1262
|
-
(radix = unsignedOrRadix), (unsignedOrRadix = false);
|
|
1307
|
+
((radix = unsignedOrRadix), (unsignedOrRadix = false));
|
|
1263
1308
|
}
|
|
1264
1309
|
else {
|
|
1265
1310
|
unsigned = !!unsignedOrRadix;
|
|
@@ -1779,15 +1824,6 @@ class Long extends BSONValue {
|
|
|
1779
1824
|
return `new Long(${longVal}${unsignedVal})`;
|
|
1780
1825
|
}
|
|
1781
1826
|
}
|
|
1782
|
-
Long.TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
|
|
1783
|
-
Long.MAX_UNSIGNED_VALUE = Long.fromBits(0xffffffff | 0, 0xffffffff | 0, true);
|
|
1784
|
-
Long.ZERO = Long.fromInt(0);
|
|
1785
|
-
Long.UZERO = Long.fromInt(0, true);
|
|
1786
|
-
Long.ONE = Long.fromInt(1);
|
|
1787
|
-
Long.UONE = Long.fromInt(1, true);
|
|
1788
|
-
Long.NEG_ONE = Long.fromInt(-1);
|
|
1789
|
-
Long.MAX_VALUE = Long.fromBits(0xffffffff | 0, 0x7fffffff | 0, false);
|
|
1790
|
-
Long.MIN_VALUE = Long.fromBits(0, 0x80000000 | 0, false);
|
|
1791
1827
|
|
|
1792
1828
|
const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/;
|
|
1793
1829
|
const PARSE_INF_REGEXP = /^(\+|-)?(Infinity|inf)$/i;
|
|
@@ -1868,6 +1904,7 @@ class Decimal128 extends BSONValue {
|
|
|
1868
1904
|
get _bsontype() {
|
|
1869
1905
|
return 'Decimal128';
|
|
1870
1906
|
}
|
|
1907
|
+
bytes;
|
|
1871
1908
|
constructor(bytes) {
|
|
1872
1909
|
super();
|
|
1873
1910
|
if (typeof bytes === 'string') {
|
|
@@ -2343,6 +2380,7 @@ class Double extends BSONValue {
|
|
|
2343
2380
|
get _bsontype() {
|
|
2344
2381
|
return 'Double';
|
|
2345
2382
|
}
|
|
2383
|
+
value;
|
|
2346
2384
|
constructor(value) {
|
|
2347
2385
|
super();
|
|
2348
2386
|
if (value instanceof Number) {
|
|
@@ -2406,6 +2444,7 @@ class Int32 extends BSONValue {
|
|
|
2406
2444
|
get _bsontype() {
|
|
2407
2445
|
return 'Int32';
|
|
2408
2446
|
}
|
|
2447
|
+
value;
|
|
2409
2448
|
constructor(value) {
|
|
2410
2449
|
super();
|
|
2411
2450
|
if (value instanceof Number) {
|
|
@@ -2484,11 +2523,14 @@ class MinKey extends BSONValue {
|
|
|
2484
2523
|
}
|
|
2485
2524
|
|
|
2486
2525
|
let PROCESS_UNIQUE = null;
|
|
2487
|
-
const __idCache = new WeakMap();
|
|
2488
2526
|
class ObjectId extends BSONValue {
|
|
2489
2527
|
get _bsontype() {
|
|
2490
2528
|
return 'ObjectId';
|
|
2491
2529
|
}
|
|
2530
|
+
static index = Math.floor(Math.random() * 0xffffff);
|
|
2531
|
+
static cacheHexString = false;
|
|
2532
|
+
buffer;
|
|
2533
|
+
#cachedHexString = null;
|
|
2492
2534
|
constructor(inputId) {
|
|
2493
2535
|
super();
|
|
2494
2536
|
let workingId;
|
|
@@ -2506,8 +2548,8 @@ class ObjectId extends BSONValue {
|
|
|
2506
2548
|
else {
|
|
2507
2549
|
workingId = inputId;
|
|
2508
2550
|
}
|
|
2509
|
-
if (workingId == null
|
|
2510
|
-
this.buffer = ObjectId.generate(
|
|
2551
|
+
if (workingId == null) {
|
|
2552
|
+
this.buffer = ObjectId.generate();
|
|
2511
2553
|
}
|
|
2512
2554
|
else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
|
|
2513
2555
|
this.buffer = ByteUtils.toLocalBufferType(workingId);
|
|
@@ -2516,7 +2558,7 @@ class ObjectId extends BSONValue {
|
|
|
2516
2558
|
if (ObjectId.validateHexString(workingId)) {
|
|
2517
2559
|
this.buffer = ByteUtils.fromHex(workingId);
|
|
2518
2560
|
if (ObjectId.cacheHexString) {
|
|
2519
|
-
|
|
2561
|
+
this.#cachedHexString = workingId;
|
|
2520
2562
|
}
|
|
2521
2563
|
}
|
|
2522
2564
|
else {
|
|
@@ -2533,7 +2575,7 @@ class ObjectId extends BSONValue {
|
|
|
2533
2575
|
set id(value) {
|
|
2534
2576
|
this.buffer = value;
|
|
2535
2577
|
if (ObjectId.cacheHexString) {
|
|
2536
|
-
|
|
2578
|
+
this.#cachedHexString = ByteUtils.toHex(value);
|
|
2537
2579
|
}
|
|
2538
2580
|
}
|
|
2539
2581
|
static validateHexString(string) {
|
|
@@ -2551,14 +2593,11 @@ class ObjectId extends BSONValue {
|
|
|
2551
2593
|
return true;
|
|
2552
2594
|
}
|
|
2553
2595
|
toHexString() {
|
|
2554
|
-
if (
|
|
2555
|
-
|
|
2556
|
-
if (__id)
|
|
2557
|
-
return __id;
|
|
2558
|
-
}
|
|
2596
|
+
if (this.#cachedHexString)
|
|
2597
|
+
return this.#cachedHexString.toLowerCase();
|
|
2559
2598
|
const hexString = ByteUtils.toHex(this.id);
|
|
2560
2599
|
if (ObjectId.cacheHexString) {
|
|
2561
|
-
|
|
2600
|
+
this.#cachedHexString = hexString;
|
|
2562
2601
|
}
|
|
2563
2602
|
return hexString;
|
|
2564
2603
|
}
|
|
@@ -2683,14 +2722,13 @@ class ObjectId extends BSONValue {
|
|
|
2683
2722
|
return new ObjectId(doc.$oid);
|
|
2684
2723
|
}
|
|
2685
2724
|
isCached() {
|
|
2686
|
-
return ObjectId.cacheHexString &&
|
|
2725
|
+
return ObjectId.cacheHexString && this.#cachedHexString != null;
|
|
2687
2726
|
}
|
|
2688
2727
|
inspect(depth, options, inspect) {
|
|
2689
2728
|
inspect ??= defaultInspect;
|
|
2690
2729
|
return `new ObjectId(${inspect(this.toHexString(), options)})`;
|
|
2691
2730
|
}
|
|
2692
2731
|
}
|
|
2693
|
-
ObjectId.index = Math.floor(Math.random() * 0xffffff);
|
|
2694
2732
|
|
|
2695
2733
|
function internalCalculateObjectSize(object, serializeFunctions, ignoreUndefined) {
|
|
2696
2734
|
let totalLength = 4 + 1;
|
|
@@ -2859,6 +2897,8 @@ class BSONRegExp extends BSONValue {
|
|
|
2859
2897
|
get _bsontype() {
|
|
2860
2898
|
return 'BSONRegExp';
|
|
2861
2899
|
}
|
|
2900
|
+
pattern;
|
|
2901
|
+
options;
|
|
2862
2902
|
constructor(pattern, options) {
|
|
2863
2903
|
super();
|
|
2864
2904
|
this.pattern = pattern;
|
|
@@ -2919,6 +2959,7 @@ class BSONSymbol extends BSONValue {
|
|
|
2919
2959
|
get _bsontype() {
|
|
2920
2960
|
return 'BSONSymbol';
|
|
2921
2961
|
}
|
|
2962
|
+
value;
|
|
2922
2963
|
constructor(value) {
|
|
2923
2964
|
super();
|
|
2924
2965
|
this.value = value;
|
|
@@ -2949,6 +2990,10 @@ class Timestamp extends LongWithoutOverridesClass {
|
|
|
2949
2990
|
get _bsontype() {
|
|
2950
2991
|
return 'Timestamp';
|
|
2951
2992
|
}
|
|
2993
|
+
get [bsonType]() {
|
|
2994
|
+
return 'Timestamp';
|
|
2995
|
+
}
|
|
2996
|
+
static MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
|
|
2952
2997
|
get i() {
|
|
2953
2998
|
return this.low >>> 0;
|
|
2954
2999
|
}
|
|
@@ -3028,7 +3073,6 @@ class Timestamp extends LongWithoutOverridesClass {
|
|
|
3028
3073
|
return `new Timestamp({ t: ${t}, i: ${i} })`;
|
|
3029
3074
|
}
|
|
3030
3075
|
}
|
|
3031
|
-
Timestamp.MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
|
|
3032
3076
|
|
|
3033
3077
|
const JS_INT_MAX_LONG = Long.fromNumber(JS_INT_MAX);
|
|
3034
3078
|
const JS_INT_MIN_LONG = Long.fromNumber(JS_INT_MIN);
|
|
@@ -4376,6 +4420,29 @@ EJSON.serialize = EJSONserialize;
|
|
|
4376
4420
|
EJSON.deserialize = EJSONdeserialize;
|
|
4377
4421
|
Object.freeze(EJSON);
|
|
4378
4422
|
|
|
4423
|
+
const BSONElementType = {
|
|
4424
|
+
double: 1,
|
|
4425
|
+
string: 2,
|
|
4426
|
+
object: 3,
|
|
4427
|
+
array: 4,
|
|
4428
|
+
binData: 5,
|
|
4429
|
+
undefined: 6,
|
|
4430
|
+
objectId: 7,
|
|
4431
|
+
bool: 8,
|
|
4432
|
+
date: 9,
|
|
4433
|
+
null: 10,
|
|
4434
|
+
regex: 11,
|
|
4435
|
+
dbPointer: 12,
|
|
4436
|
+
javascript: 13,
|
|
4437
|
+
symbol: 14,
|
|
4438
|
+
javascriptWithScope: 15,
|
|
4439
|
+
int: 16,
|
|
4440
|
+
timestamp: 17,
|
|
4441
|
+
long: 18,
|
|
4442
|
+
decimal: 19,
|
|
4443
|
+
minKey: 255,
|
|
4444
|
+
maxKey: 127
|
|
4445
|
+
};
|
|
4379
4446
|
function getSize(source, offset) {
|
|
4380
4447
|
try {
|
|
4381
4448
|
return NumberUtils.getNonnegativeInt32LE(source, offset);
|
|
@@ -4420,48 +4487,48 @@ function parseToElements(bytes, startOffset = 0) {
|
|
|
4420
4487
|
const nameLength = findNull(bytes, offset) - nameOffset;
|
|
4421
4488
|
offset += nameLength + 1;
|
|
4422
4489
|
let length;
|
|
4423
|
-
if (type ===
|
|
4424
|
-
type ===
|
|
4425
|
-
type ===
|
|
4426
|
-
type ===
|
|
4490
|
+
if (type === BSONElementType.double ||
|
|
4491
|
+
type === BSONElementType.long ||
|
|
4492
|
+
type === BSONElementType.date ||
|
|
4493
|
+
type === BSONElementType.timestamp) {
|
|
4427
4494
|
length = 8;
|
|
4428
4495
|
}
|
|
4429
|
-
else if (type ===
|
|
4496
|
+
else if (type === BSONElementType.int) {
|
|
4430
4497
|
length = 4;
|
|
4431
4498
|
}
|
|
4432
|
-
else if (type ===
|
|
4499
|
+
else if (type === BSONElementType.objectId) {
|
|
4433
4500
|
length = 12;
|
|
4434
4501
|
}
|
|
4435
|
-
else if (type ===
|
|
4502
|
+
else if (type === BSONElementType.decimal) {
|
|
4436
4503
|
length = 16;
|
|
4437
4504
|
}
|
|
4438
|
-
else if (type ===
|
|
4505
|
+
else if (type === BSONElementType.bool) {
|
|
4439
4506
|
length = 1;
|
|
4440
4507
|
}
|
|
4441
|
-
else if (type ===
|
|
4442
|
-
type ===
|
|
4443
|
-
type ===
|
|
4444
|
-
type ===
|
|
4508
|
+
else if (type === BSONElementType.null ||
|
|
4509
|
+
type === BSONElementType.undefined ||
|
|
4510
|
+
type === BSONElementType.maxKey ||
|
|
4511
|
+
type === BSONElementType.minKey) {
|
|
4445
4512
|
length = 0;
|
|
4446
4513
|
}
|
|
4447
|
-
else if (type ===
|
|
4514
|
+
else if (type === BSONElementType.regex) {
|
|
4448
4515
|
length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
|
|
4449
4516
|
}
|
|
4450
|
-
else if (type ===
|
|
4451
|
-
type ===
|
|
4452
|
-
type ===
|
|
4517
|
+
else if (type === BSONElementType.object ||
|
|
4518
|
+
type === BSONElementType.array ||
|
|
4519
|
+
type === BSONElementType.javascriptWithScope) {
|
|
4453
4520
|
length = getSize(bytes, offset);
|
|
4454
4521
|
}
|
|
4455
|
-
else if (type ===
|
|
4456
|
-
type ===
|
|
4457
|
-
type ===
|
|
4458
|
-
type ===
|
|
4459
|
-
type ===
|
|
4522
|
+
else if (type === BSONElementType.string ||
|
|
4523
|
+
type === BSONElementType.binData ||
|
|
4524
|
+
type === BSONElementType.dbPointer ||
|
|
4525
|
+
type === BSONElementType.javascript ||
|
|
4526
|
+
type === BSONElementType.symbol) {
|
|
4460
4527
|
length = getSize(bytes, offset) + 4;
|
|
4461
|
-
if (type ===
|
|
4528
|
+
if (type === BSONElementType.binData) {
|
|
4462
4529
|
length += 1;
|
|
4463
4530
|
}
|
|
4464
|
-
if (type ===
|
|
4531
|
+
if (type === BSONElementType.dbPointer) {
|
|
4465
4532
|
length += 12;
|
|
4466
4533
|
}
|
|
4467
4534
|
}
|
|
@@ -4557,6 +4624,7 @@ var bson = /*#__PURE__*/Object.freeze({
|
|
|
4557
4624
|
ObjectId: ObjectId,
|
|
4558
4625
|
Timestamp: Timestamp,
|
|
4559
4626
|
UUID: UUID,
|
|
4627
|
+
bsonType: bsonType,
|
|
4560
4628
|
calculateObjectSize: calculateObjectSize,
|
|
4561
4629
|
deserialize: deserialize,
|
|
4562
4630
|
deserializeStream: deserializeStream,
|
|
@@ -4588,6 +4656,7 @@ exports.MinKey = MinKey;
|
|
|
4588
4656
|
exports.ObjectId = ObjectId;
|
|
4589
4657
|
exports.Timestamp = Timestamp;
|
|
4590
4658
|
exports.UUID = UUID;
|
|
4659
|
+
exports.bsonType = bsonType;
|
|
4591
4660
|
exports.calculateObjectSize = calculateObjectSize;
|
|
4592
4661
|
exports.deserialize = deserialize;
|
|
4593
4662
|
exports.deserializeStream = deserializeStream;
|