bson 5.0.0-alpha.0 → 5.0.0-alpha.2
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 +85 -93
- package/bson.d.ts +20 -11
- package/lib/bson.bundle.js +119 -96
- package/lib/bson.bundle.js.map +1 -1
- package/lib/bson.cjs +119 -96
- package/lib/bson.cjs.map +1 -1
- package/lib/bson.mjs +120 -96
- package/lib/bson.mjs.map +1 -1
- package/package.json +4 -4
- package/src/binary.ts +12 -12
- package/src/bson.ts +1 -1
- package/src/code.ts +3 -2
- package/src/constants.ts +5 -0
- package/src/db_ref.ts +3 -2
- package/src/decimal128.ts +10 -9
- package/src/double.ts +9 -16
- package/src/error.ts +33 -9
- package/src/extended_json.ts +11 -5
- package/src/int_32.ts +3 -2
- package/src/long.ts +9 -7
- package/src/max_key.ts +4 -2
- package/src/min_key.ts +4 -2
- package/src/objectid.ts +9 -10
- package/src/parser/calculate_size.ts +12 -1
- package/src/parser/deserializer.ts +24 -9
- package/src/parser/serializer.ts +53 -34
- package/src/regexp.ts +5 -4
- package/src/symbol.ts +4 -2
- package/src/timestamp.ts +3 -2
- package/src/uuid_utils.ts +2 -2
package/lib/bson.mjs
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
class BSONError extends Error {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
get bsonError() {
|
|
3
|
+
return true;
|
|
4
4
|
}
|
|
5
5
|
get name() {
|
|
6
6
|
return 'BSONError';
|
|
7
7
|
}
|
|
8
|
-
}
|
|
9
|
-
class BSONTypeError extends TypeError {
|
|
10
8
|
constructor(message) {
|
|
11
9
|
super(message);
|
|
12
10
|
}
|
|
13
|
-
|
|
14
|
-
return
|
|
11
|
+
static isBSONError(value) {
|
|
12
|
+
return (value != null &&
|
|
13
|
+
typeof value === 'object' &&
|
|
14
|
+
'bsonError' in value &&
|
|
15
|
+
value.bsonError === true &&
|
|
16
|
+
'name' in value &&
|
|
17
|
+
'message' in value &&
|
|
18
|
+
'stack' in value);
|
|
15
19
|
}
|
|
16
20
|
}
|
|
17
21
|
|
|
@@ -210,7 +214,7 @@ const VALIDATION_REGEX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f
|
|
|
210
214
|
const uuidValidateString = (str) => typeof str === 'string' && VALIDATION_REGEX.test(str);
|
|
211
215
|
const uuidHexStringToBuffer = (hexString) => {
|
|
212
216
|
if (!uuidValidateString(hexString)) {
|
|
213
|
-
throw new
|
|
217
|
+
throw new BSONError('UUID string representations must be a 32 or 36 character hex string (dashes excluded/included). Format: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" or "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".');
|
|
214
218
|
}
|
|
215
219
|
const sanitizedHexString = hexString.replace(/-/g, '');
|
|
216
220
|
return ByteUtils.fromHex(sanitizedHexString);
|
|
@@ -234,12 +238,6 @@ function isAnyArrayBuffer(value) {
|
|
|
234
238
|
function isUint8Array(value) {
|
|
235
239
|
return Object.prototype.toString.call(value) === '[object Uint8Array]';
|
|
236
240
|
}
|
|
237
|
-
function isBigInt64Array(value) {
|
|
238
|
-
return Object.prototype.toString.call(value) === '[object BigInt64Array]';
|
|
239
|
-
}
|
|
240
|
-
function isBigUInt64Array(value) {
|
|
241
|
-
return Object.prototype.toString.call(value) === '[object BigUint64Array]';
|
|
242
|
-
}
|
|
243
241
|
function isRegExp(d) {
|
|
244
242
|
return Object.prototype.toString.call(d) === '[object RegExp]';
|
|
245
243
|
}
|
|
@@ -250,6 +248,7 @@ function isDate(d) {
|
|
|
250
248
|
return Object.prototype.toString.call(d) === '[object Date]';
|
|
251
249
|
}
|
|
252
250
|
|
|
251
|
+
const BSON_MAJOR_VERSION = 5;
|
|
253
252
|
const BSON_INT32_MAX = 0x7fffffff;
|
|
254
253
|
const BSON_INT32_MIN = -0x80000000;
|
|
255
254
|
const BSON_INT64_MAX = Math.pow(2, 63) - 1;
|
|
@@ -308,7 +307,7 @@ class Binary {
|
|
|
308
307
|
return 'Binary';
|
|
309
308
|
}
|
|
310
309
|
get [Symbol.for('@@mdb.bson.version')]() {
|
|
311
|
-
return
|
|
310
|
+
return BSON_MAJOR_VERSION;
|
|
312
311
|
}
|
|
313
312
|
constructor(buffer, subType) {
|
|
314
313
|
if (!(buffer == null) &&
|
|
@@ -316,7 +315,7 @@ class Binary {
|
|
|
316
315
|
!ArrayBuffer.isView(buffer) &&
|
|
317
316
|
!(buffer instanceof ArrayBuffer) &&
|
|
318
317
|
!Array.isArray(buffer)) {
|
|
319
|
-
throw new
|
|
318
|
+
throw new BSONError('Binary can only be constructed from string, Buffer, TypedArray, or Array<number>');
|
|
320
319
|
}
|
|
321
320
|
this.sub_type = subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT;
|
|
322
321
|
if (buffer == null) {
|
|
@@ -338,10 +337,10 @@ class Binary {
|
|
|
338
337
|
}
|
|
339
338
|
put(byteValue) {
|
|
340
339
|
if (typeof byteValue === 'string' && byteValue.length !== 1) {
|
|
341
|
-
throw new
|
|
340
|
+
throw new BSONError('only accepts single character String');
|
|
342
341
|
}
|
|
343
342
|
else if (typeof byteValue !== 'number' && byteValue.length !== 1)
|
|
344
|
-
throw new
|
|
343
|
+
throw new BSONError('only accepts single character Uint8Array or Array');
|
|
345
344
|
let decodedByte;
|
|
346
345
|
if (typeof byteValue === 'string') {
|
|
347
346
|
decodedByte = byteValue.charCodeAt(0);
|
|
@@ -353,7 +352,7 @@ class Binary {
|
|
|
353
352
|
decodedByte = byteValue[0];
|
|
354
353
|
}
|
|
355
354
|
if (decodedByte < 0 || decodedByte > 255) {
|
|
356
|
-
throw new
|
|
355
|
+
throw new BSONError('only accepts number in a valid unsigned byte range 0-255');
|
|
357
356
|
}
|
|
358
357
|
if (this.buffer.byteLength > this.position) {
|
|
359
358
|
this.buffer[this.position++] = decodedByte;
|
|
@@ -457,7 +456,7 @@ class Binary {
|
|
|
457
456
|
data = uuidHexStringToBuffer(doc.$uuid);
|
|
458
457
|
}
|
|
459
458
|
if (!data) {
|
|
460
|
-
throw new
|
|
459
|
+
throw new BSONError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
|
|
461
460
|
}
|
|
462
461
|
return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
|
|
463
462
|
}
|
|
@@ -482,7 +481,7 @@ Binary.SUBTYPE_USER_DEFINED = 128;
|
|
|
482
481
|
const UUID_BYTE_LENGTH = 16;
|
|
483
482
|
class UUID extends Binary {
|
|
484
483
|
get [Symbol.for('@@mdb.bson.version')]() {
|
|
485
|
-
return
|
|
484
|
+
return BSON_MAJOR_VERSION;
|
|
486
485
|
}
|
|
487
486
|
constructor(input) {
|
|
488
487
|
let bytes;
|
|
@@ -501,7 +500,7 @@ class UUID extends Binary {
|
|
|
501
500
|
bytes = uuidHexStringToBuffer(input);
|
|
502
501
|
}
|
|
503
502
|
else {
|
|
504
|
-
throw new
|
|
503
|
+
throw new BSONError('Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).');
|
|
505
504
|
}
|
|
506
505
|
super(bytes, BSON_BINARY_SUBTYPE_UUID_NEW);
|
|
507
506
|
this.__id = hexStr;
|
|
@@ -593,7 +592,7 @@ class Code {
|
|
|
593
592
|
return 'Code';
|
|
594
593
|
}
|
|
595
594
|
get [Symbol.for('@@mdb.bson.version')]() {
|
|
596
|
-
return
|
|
595
|
+
return BSON_MAJOR_VERSION;
|
|
597
596
|
}
|
|
598
597
|
constructor(code, scope) {
|
|
599
598
|
this.code = code.toString();
|
|
@@ -637,7 +636,7 @@ class DBRef {
|
|
|
637
636
|
return 'DBRef';
|
|
638
637
|
}
|
|
639
638
|
get [Symbol.for('@@mdb.bson.version')]() {
|
|
640
|
-
return
|
|
639
|
+
return BSON_MAJOR_VERSION;
|
|
641
640
|
}
|
|
642
641
|
constructor(collection, oid, db, fields) {
|
|
643
642
|
const parts = collection.split('.');
|
|
@@ -713,7 +712,7 @@ class Long {
|
|
|
713
712
|
return 'Long';
|
|
714
713
|
}
|
|
715
714
|
get [Symbol.for('@@mdb.bson.version')]() {
|
|
716
|
-
return
|
|
715
|
+
return BSON_MAJOR_VERSION;
|
|
717
716
|
}
|
|
718
717
|
get __isLong__() {
|
|
719
718
|
return true;
|
|
@@ -785,7 +784,7 @@ class Long {
|
|
|
785
784
|
}
|
|
786
785
|
static fromString(str, unsigned, radix) {
|
|
787
786
|
if (str.length === 0)
|
|
788
|
-
throw
|
|
787
|
+
throw new BSONError('empty string');
|
|
789
788
|
if (str === 'NaN' || str === 'Infinity' || str === '+Infinity' || str === '-Infinity')
|
|
790
789
|
return Long.ZERO;
|
|
791
790
|
if (typeof unsigned === 'number') {
|
|
@@ -796,10 +795,10 @@ class Long {
|
|
|
796
795
|
}
|
|
797
796
|
radix = radix || 10;
|
|
798
797
|
if (radix < 2 || 36 < radix)
|
|
799
|
-
throw
|
|
798
|
+
throw new BSONError('radix');
|
|
800
799
|
let p;
|
|
801
800
|
if ((p = str.indexOf('-')) > 0)
|
|
802
|
-
throw
|
|
801
|
+
throw new BSONError('interior hyphen');
|
|
803
802
|
else if (p === 0) {
|
|
804
803
|
return Long.fromString(str.substring(1), unsigned, radix).neg();
|
|
805
804
|
}
|
|
@@ -895,7 +894,7 @@ class Long {
|
|
|
895
894
|
if (!Long.isLong(divisor))
|
|
896
895
|
divisor = Long.fromValue(divisor);
|
|
897
896
|
if (divisor.isZero())
|
|
898
|
-
throw
|
|
897
|
+
throw new BSONError('division by zero');
|
|
899
898
|
if (wasm) {
|
|
900
899
|
if (!this.unsigned &&
|
|
901
900
|
this.high === -0x80000000 &&
|
|
@@ -1250,7 +1249,7 @@ class Long {
|
|
|
1250
1249
|
toString(radix) {
|
|
1251
1250
|
radix = radix || 10;
|
|
1252
1251
|
if (radix < 2 || 36 < radix)
|
|
1253
|
-
throw
|
|
1252
|
+
throw new BSONError('radix');
|
|
1254
1253
|
if (this.isZero())
|
|
1255
1254
|
return '0';
|
|
1256
1255
|
if (this.isNegative()) {
|
|
@@ -1394,14 +1393,14 @@ function lessThan(left, right) {
|
|
|
1394
1393
|
return false;
|
|
1395
1394
|
}
|
|
1396
1395
|
function invalidErr(string, message) {
|
|
1397
|
-
throw new
|
|
1396
|
+
throw new BSONError(`"${string}" is not a valid Decimal128 string - ${message}`);
|
|
1398
1397
|
}
|
|
1399
1398
|
class Decimal128 {
|
|
1400
1399
|
get _bsontype() {
|
|
1401
1400
|
return 'Decimal128';
|
|
1402
1401
|
}
|
|
1403
1402
|
get [Symbol.for('@@mdb.bson.version')]() {
|
|
1404
|
-
return
|
|
1403
|
+
return BSON_MAJOR_VERSION;
|
|
1405
1404
|
}
|
|
1406
1405
|
constructor(bytes) {
|
|
1407
1406
|
if (typeof bytes === 'string') {
|
|
@@ -1409,12 +1408,12 @@ class Decimal128 {
|
|
|
1409
1408
|
}
|
|
1410
1409
|
else if (isUint8Array(bytes)) {
|
|
1411
1410
|
if (bytes.byteLength !== 16) {
|
|
1412
|
-
throw new
|
|
1411
|
+
throw new BSONError('Decimal128 must take a Buffer of 16 bytes');
|
|
1413
1412
|
}
|
|
1414
1413
|
this.bytes = bytes;
|
|
1415
1414
|
}
|
|
1416
1415
|
else {
|
|
1417
|
-
throw new
|
|
1416
|
+
throw new BSONError('Decimal128 must take a Buffer or string');
|
|
1418
1417
|
}
|
|
1419
1418
|
}
|
|
1420
1419
|
static fromString(representation) {
|
|
@@ -1438,13 +1437,13 @@ class Decimal128 {
|
|
|
1438
1437
|
let biasedExponent = 0;
|
|
1439
1438
|
let index = 0;
|
|
1440
1439
|
if (representation.length >= 7000) {
|
|
1441
|
-
throw new
|
|
1440
|
+
throw new BSONError('' + representation + ' not a valid Decimal128 string');
|
|
1442
1441
|
}
|
|
1443
1442
|
const stringMatch = representation.match(PARSE_STRING_REGEXP);
|
|
1444
1443
|
const infMatch = representation.match(PARSE_INF_REGEXP);
|
|
1445
1444
|
const nanMatch = representation.match(PARSE_NAN_REGEXP);
|
|
1446
1445
|
if ((!stringMatch && !infMatch && !nanMatch) || representation.length === 0) {
|
|
1447
|
-
throw new
|
|
1446
|
+
throw new BSONError('' + representation + ' not a valid Decimal128 string');
|
|
1448
1447
|
}
|
|
1449
1448
|
if (stringMatch) {
|
|
1450
1449
|
const unsignedNumber = stringMatch[2];
|
|
@@ -1496,7 +1495,7 @@ class Decimal128 {
|
|
|
1496
1495
|
index = index + 1;
|
|
1497
1496
|
}
|
|
1498
1497
|
if (sawRadix && !nDigitsRead)
|
|
1499
|
-
throw new
|
|
1498
|
+
throw new BSONError('' + representation + ' not a valid Decimal128 string');
|
|
1500
1499
|
if (representation[index] === 'e' || representation[index] === 'E') {
|
|
1501
1500
|
const match = representation.substr(++index).match(EXPONENT_REGEX);
|
|
1502
1501
|
if (!match || !match[2])
|
|
@@ -1830,7 +1829,7 @@ class Double {
|
|
|
1830
1829
|
return 'Double';
|
|
1831
1830
|
}
|
|
1832
1831
|
get [Symbol.for('@@mdb.bson.version')]() {
|
|
1833
|
-
return
|
|
1832
|
+
return BSON_MAJOR_VERSION;
|
|
1834
1833
|
}
|
|
1835
1834
|
constructor(value) {
|
|
1836
1835
|
if (value instanceof Number) {
|
|
@@ -1852,19 +1851,11 @@ class Double {
|
|
|
1852
1851
|
return this.value;
|
|
1853
1852
|
}
|
|
1854
1853
|
if (Object.is(Math.sign(this.value), -0)) {
|
|
1855
|
-
return { $numberDouble:
|
|
1856
|
-
}
|
|
1857
|
-
let $numberDouble;
|
|
1858
|
-
if (Number.isInteger(this.value)) {
|
|
1859
|
-
$numberDouble = this.value.toFixed(1);
|
|
1860
|
-
if ($numberDouble.length >= 13) {
|
|
1861
|
-
$numberDouble = this.value.toExponential(13).toUpperCase();
|
|
1862
|
-
}
|
|
1854
|
+
return { $numberDouble: '-0.0' };
|
|
1863
1855
|
}
|
|
1864
|
-
|
|
1865
|
-
$numberDouble
|
|
1866
|
-
}
|
|
1867
|
-
return { $numberDouble };
|
|
1856
|
+
return {
|
|
1857
|
+
$numberDouble: Number.isInteger(this.value) ? this.value.toFixed(1) : this.value.toString()
|
|
1858
|
+
};
|
|
1868
1859
|
}
|
|
1869
1860
|
static fromExtendedJSON(doc, options) {
|
|
1870
1861
|
const doubleValue = parseFloat(doc.$numberDouble);
|
|
@@ -1884,7 +1875,7 @@ class Int32 {
|
|
|
1884
1875
|
return 'Int32';
|
|
1885
1876
|
}
|
|
1886
1877
|
get [Symbol.for('@@mdb.bson.version')]() {
|
|
1887
|
-
return
|
|
1878
|
+
return BSON_MAJOR_VERSION;
|
|
1888
1879
|
}
|
|
1889
1880
|
constructor(value) {
|
|
1890
1881
|
if (value instanceof Number) {
|
|
@@ -1922,7 +1913,7 @@ class MaxKey {
|
|
|
1922
1913
|
return 'MaxKey';
|
|
1923
1914
|
}
|
|
1924
1915
|
get [Symbol.for('@@mdb.bson.version')]() {
|
|
1925
|
-
return
|
|
1916
|
+
return BSON_MAJOR_VERSION;
|
|
1926
1917
|
}
|
|
1927
1918
|
toExtendedJSON() {
|
|
1928
1919
|
return { $maxKey: 1 };
|
|
@@ -1943,7 +1934,7 @@ class MinKey {
|
|
|
1943
1934
|
return 'MinKey';
|
|
1944
1935
|
}
|
|
1945
1936
|
get [Symbol.for('@@mdb.bson.version')]() {
|
|
1946
|
-
return
|
|
1937
|
+
return BSON_MAJOR_VERSION;
|
|
1947
1938
|
}
|
|
1948
1939
|
toExtendedJSON() {
|
|
1949
1940
|
return { $minKey: 1 };
|
|
@@ -1967,13 +1958,13 @@ class ObjectId {
|
|
|
1967
1958
|
return 'ObjectId';
|
|
1968
1959
|
}
|
|
1969
1960
|
get [Symbol.for('@@mdb.bson.version')]() {
|
|
1970
|
-
return
|
|
1961
|
+
return BSON_MAJOR_VERSION;
|
|
1971
1962
|
}
|
|
1972
1963
|
constructor(inputId) {
|
|
1973
1964
|
let workingId;
|
|
1974
1965
|
if (typeof inputId === 'object' && inputId && 'id' in inputId) {
|
|
1975
1966
|
if (typeof inputId.id !== 'string' && !ArrayBuffer.isView(inputId.id)) {
|
|
1976
|
-
throw new
|
|
1967
|
+
throw new BSONError('Argument passed in must have an id that is of type string or Buffer');
|
|
1977
1968
|
}
|
|
1978
1969
|
if ('toHexString' in inputId && typeof inputId.toHexString === 'function') {
|
|
1979
1970
|
workingId = ByteUtils.fromHex(inputId.toHexString());
|
|
@@ -1998,18 +1989,18 @@ class ObjectId {
|
|
|
1998
1989
|
this[kId] = bytes;
|
|
1999
1990
|
}
|
|
2000
1991
|
else {
|
|
2001
|
-
throw new
|
|
1992
|
+
throw new BSONError('Argument passed in must be a string of 12 bytes');
|
|
2002
1993
|
}
|
|
2003
1994
|
}
|
|
2004
1995
|
else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
|
|
2005
1996
|
this[kId] = ByteUtils.fromHex(workingId);
|
|
2006
1997
|
}
|
|
2007
1998
|
else {
|
|
2008
|
-
throw new
|
|
1999
|
+
throw new BSONError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer');
|
|
2009
2000
|
}
|
|
2010
2001
|
}
|
|
2011
2002
|
else {
|
|
2012
|
-
throw new
|
|
2003
|
+
throw new BSONError('Argument passed in does not match the accepted types');
|
|
2013
2004
|
}
|
|
2014
2005
|
if (ObjectId.cacheHexString) {
|
|
2015
2006
|
this.__id = ByteUtils.toHex(this.id);
|
|
@@ -2111,7 +2102,7 @@ class ObjectId {
|
|
|
2111
2102
|
}
|
|
2112
2103
|
static createFromHexString(hexString) {
|
|
2113
2104
|
if (typeof hexString === 'undefined' || (hexString != null && hexString.length !== 24)) {
|
|
2114
|
-
throw new
|
|
2105
|
+
throw new BSONError('Argument passed in must be a single String of 12 bytes or a string of 24 hex characters');
|
|
2115
2106
|
}
|
|
2116
2107
|
return new ObjectId(ByteUtils.fromHex(hexString));
|
|
2117
2108
|
}
|
|
@@ -2188,7 +2179,14 @@ function calculateElement(name, value, serializeFunctions = false, isArray = fal
|
|
|
2188
2179
|
case 'boolean':
|
|
2189
2180
|
return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (1 + 1);
|
|
2190
2181
|
case 'object':
|
|
2191
|
-
if (value
|
|
2182
|
+
if (value != null &&
|
|
2183
|
+
typeof value._bsontype === 'string' &&
|
|
2184
|
+
value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
|
|
2185
|
+
throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
|
|
2186
|
+
}
|
|
2187
|
+
else if (value == null ||
|
|
2188
|
+
value['_bsontype'] === 'MinKey' ||
|
|
2189
|
+
value['_bsontype'] === 'MaxKey') {
|
|
2192
2190
|
return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + 1;
|
|
2193
2191
|
}
|
|
2194
2192
|
else if (value['_bsontype'] === 'ObjectId') {
|
|
@@ -2300,7 +2298,7 @@ class BSONRegExp {
|
|
|
2300
2298
|
return 'BSONRegExp';
|
|
2301
2299
|
}
|
|
2302
2300
|
get [Symbol.for('@@mdb.bson.version')]() {
|
|
2303
|
-
return
|
|
2301
|
+
return BSON_MAJOR_VERSION;
|
|
2304
2302
|
}
|
|
2305
2303
|
constructor(pattern, options) {
|
|
2306
2304
|
this.pattern = pattern;
|
|
@@ -2346,7 +2344,7 @@ class BSONRegExp {
|
|
|
2346
2344
|
if ('$regularExpression' in doc) {
|
|
2347
2345
|
return new BSONRegExp(doc.$regularExpression.pattern, BSONRegExp.parseOptions(doc.$regularExpression.options));
|
|
2348
2346
|
}
|
|
2349
|
-
throw new
|
|
2347
|
+
throw new BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
|
|
2350
2348
|
}
|
|
2351
2349
|
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
2352
2350
|
return this.inspect();
|
|
@@ -2361,7 +2359,7 @@ class BSONSymbol {
|
|
|
2361
2359
|
return 'BSONSymbol';
|
|
2362
2360
|
}
|
|
2363
2361
|
get [Symbol.for('@@mdb.bson.version')]() {
|
|
2364
|
-
return
|
|
2362
|
+
return BSON_MAJOR_VERSION;
|
|
2365
2363
|
}
|
|
2366
2364
|
constructor(value) {
|
|
2367
2365
|
this.value = value;
|
|
@@ -2395,7 +2393,7 @@ class Timestamp extends LongWithoutOverridesClass {
|
|
|
2395
2393
|
return 'Timestamp';
|
|
2396
2394
|
}
|
|
2397
2395
|
get [Symbol.for('@@mdb.bson.version')]() {
|
|
2398
|
-
return
|
|
2396
|
+
return BSON_MAJOR_VERSION;
|
|
2399
2397
|
}
|
|
2400
2398
|
constructor(low) {
|
|
2401
2399
|
if (low == null) {
|
|
@@ -2538,9 +2536,16 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
2538
2536
|
const fieldsAsRaw = options['fieldsAsRaw'] == null ? null : options['fieldsAsRaw'];
|
|
2539
2537
|
const raw = options['raw'] == null ? false : options['raw'];
|
|
2540
2538
|
const bsonRegExp = typeof options['bsonRegExp'] === 'boolean' ? options['bsonRegExp'] : false;
|
|
2541
|
-
const promoteBuffers = options
|
|
2542
|
-
const promoteLongs = options
|
|
2543
|
-
const promoteValues = options
|
|
2539
|
+
const promoteBuffers = options.promoteBuffers ?? false;
|
|
2540
|
+
const promoteLongs = options.promoteLongs ?? true;
|
|
2541
|
+
const promoteValues = options.promoteValues ?? true;
|
|
2542
|
+
const useBigInt64 = options.useBigInt64 ?? false;
|
|
2543
|
+
if (useBigInt64 && !promoteValues) {
|
|
2544
|
+
throw new BSONError('Must either request bigint or Long for int64 deserialization');
|
|
2545
|
+
}
|
|
2546
|
+
if (useBigInt64 && !promoteLongs) {
|
|
2547
|
+
throw new BSONError('Must either request bigint or Long for int64 deserialization');
|
|
2548
|
+
}
|
|
2544
2549
|
const validation = options.validation == null ? { utf8: true } : options.validation;
|
|
2545
2550
|
let globalUTFValidation = true;
|
|
2546
2551
|
let validationSetting;
|
|
@@ -2705,6 +2710,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
2705
2710
|
value = null;
|
|
2706
2711
|
}
|
|
2707
2712
|
else if (elementType === BSON_DATA_LONG) {
|
|
2713
|
+
const dataview = BSONDataView.fromUint8Array(buffer.subarray(index, index + 8));
|
|
2708
2714
|
const lowBits = buffer[index++] |
|
|
2709
2715
|
(buffer[index++] << 8) |
|
|
2710
2716
|
(buffer[index++] << 16) |
|
|
@@ -2714,7 +2720,10 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
2714
2720
|
(buffer[index++] << 16) |
|
|
2715
2721
|
(buffer[index++] << 24);
|
|
2716
2722
|
const long = new Long(lowBits, highBits);
|
|
2717
|
-
if (
|
|
2723
|
+
if (useBigInt64) {
|
|
2724
|
+
value = dataview.getBigInt64(0, true);
|
|
2725
|
+
}
|
|
2726
|
+
else if (promoteLongs && promoteValues === true) {
|
|
2718
2727
|
value =
|
|
2719
2728
|
long.lessThanOrEqual(JS_INT_MAX_LONG) && long.greaterThanOrEqual(JS_INT_MIN_LONG)
|
|
2720
2729
|
? long.toNumber()
|
|
@@ -3036,6 +3045,16 @@ function serializeNumber(buffer, key, value, index) {
|
|
|
3036
3045
|
index += bytes.byteLength;
|
|
3037
3046
|
return index;
|
|
3038
3047
|
}
|
|
3048
|
+
function serializeBigInt(buffer, key, value, index) {
|
|
3049
|
+
buffer[index++] = BSON_DATA_LONG;
|
|
3050
|
+
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
|
|
3051
|
+
index += numberOfWrittenBytes;
|
|
3052
|
+
buffer[index++] = 0;
|
|
3053
|
+
NUMBER_SPACE.setBigInt64(0, value, true);
|
|
3054
|
+
buffer.set(EIGHT_BYTE_VIEW_ON_NUMBER, index);
|
|
3055
|
+
index += EIGHT_BYTE_VIEW_ON_NUMBER.byteLength;
|
|
3056
|
+
return index;
|
|
3057
|
+
}
|
|
3039
3058
|
function serializeNull(buffer, key, _, index) {
|
|
3040
3059
|
buffer[index++] = BSON_DATA_NULL;
|
|
3041
3060
|
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
|
|
@@ -3075,7 +3094,7 @@ function serializeRegExp(buffer, key, value, index) {
|
|
|
3075
3094
|
index = index + numberOfWrittenBytes;
|
|
3076
3095
|
buffer[index++] = 0;
|
|
3077
3096
|
if (value.source && value.source.match(regexp) != null) {
|
|
3078
|
-
throw
|
|
3097
|
+
throw new BSONError('value ' + value.source + ' must not contain null bytes');
|
|
3079
3098
|
}
|
|
3080
3099
|
index = index + ByteUtils.encodeUTF8Into(buffer, value.source, index);
|
|
3081
3100
|
buffer[index++] = 0x00;
|
|
@@ -3094,7 +3113,7 @@ function serializeBSONRegExp(buffer, key, value, index) {
|
|
|
3094
3113
|
index = index + numberOfWrittenBytes;
|
|
3095
3114
|
buffer[index++] = 0;
|
|
3096
3115
|
if (value.pattern.match(regexp) != null) {
|
|
3097
|
-
throw
|
|
3116
|
+
throw new BSONError('pattern ' + value.pattern + ' must not contain null bytes');
|
|
3098
3117
|
}
|
|
3099
3118
|
index = index + ByteUtils.encodeUTF8Into(buffer, value.pattern, index);
|
|
3100
3119
|
buffer[index++] = 0x00;
|
|
@@ -3127,7 +3146,7 @@ function serializeObjectId(buffer, key, value, index) {
|
|
|
3127
3146
|
buffer.set(value.id.subarray(0, 12), index);
|
|
3128
3147
|
}
|
|
3129
3148
|
else {
|
|
3130
|
-
throw new
|
|
3149
|
+
throw new BSONError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
|
|
3131
3150
|
}
|
|
3132
3151
|
return index + 12;
|
|
3133
3152
|
}
|
|
@@ -3367,7 +3386,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
|
|
|
3367
3386
|
index = serializeNumber(buffer, key, value, index);
|
|
3368
3387
|
}
|
|
3369
3388
|
else if (typeof value === 'bigint') {
|
|
3370
|
-
|
|
3389
|
+
index = serializeBigInt(buffer, key, value, index);
|
|
3371
3390
|
}
|
|
3372
3391
|
else if (typeof value === 'boolean') {
|
|
3373
3392
|
index = serializeBoolean(buffer, key, value, index);
|
|
@@ -3390,7 +3409,8 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
|
|
|
3390
3409
|
else if (typeof value === 'object' && value._bsontype == null) {
|
|
3391
3410
|
index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
|
|
3392
3411
|
}
|
|
3393
|
-
else if (typeof value === 'object' &&
|
|
3412
|
+
else if (typeof value === 'object' &&
|
|
3413
|
+
value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
|
|
3394
3414
|
throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
|
|
3395
3415
|
}
|
|
3396
3416
|
else if (value._bsontype === 'ObjectId') {
|
|
@@ -3414,7 +3434,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
|
|
|
3414
3434
|
else if (value._bsontype === 'Binary') {
|
|
3415
3435
|
index = serializeBinary(buffer, key, value, index);
|
|
3416
3436
|
}
|
|
3417
|
-
else if (value._bsontype === '
|
|
3437
|
+
else if (value._bsontype === 'BSONSymbol') {
|
|
3418
3438
|
index = serializeSymbol(buffer, key, value, index);
|
|
3419
3439
|
}
|
|
3420
3440
|
else if (value._bsontype === 'DBRef') {
|
|
@@ -3430,7 +3450,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
|
|
|
3430
3450
|
index = serializeMinMax(buffer, key, value, index);
|
|
3431
3451
|
}
|
|
3432
3452
|
else if (typeof value._bsontype !== 'undefined') {
|
|
3433
|
-
throw new
|
|
3453
|
+
throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
|
|
3434
3454
|
}
|
|
3435
3455
|
}
|
|
3436
3456
|
}
|
|
@@ -3443,18 +3463,21 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
|
|
|
3443
3463
|
if (done)
|
|
3444
3464
|
continue;
|
|
3445
3465
|
const key = entry.value[0];
|
|
3446
|
-
|
|
3466
|
+
let value = entry.value[1];
|
|
3467
|
+
if (typeof value?.toBSON === 'function') {
|
|
3468
|
+
value = value.toBSON();
|
|
3469
|
+
}
|
|
3447
3470
|
const type = typeof value;
|
|
3448
3471
|
if (typeof key === 'string' && !ignoreKeys.has(key)) {
|
|
3449
3472
|
if (key.match(regexp) != null) {
|
|
3450
|
-
throw
|
|
3473
|
+
throw new BSONError('key ' + key + ' must not contain null bytes');
|
|
3451
3474
|
}
|
|
3452
3475
|
if (checkKeys) {
|
|
3453
3476
|
if ('$' === key[0]) {
|
|
3454
|
-
throw
|
|
3477
|
+
throw new BSONError('key ' + key + " must not start with '$'");
|
|
3455
3478
|
}
|
|
3456
3479
|
else if (~key.indexOf('.')) {
|
|
3457
|
-
throw
|
|
3480
|
+
throw new BSONError('key ' + key + " must not contain '.'");
|
|
3458
3481
|
}
|
|
3459
3482
|
}
|
|
3460
3483
|
}
|
|
@@ -3464,8 +3487,8 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
|
|
|
3464
3487
|
else if (type === 'number') {
|
|
3465
3488
|
index = serializeNumber(buffer, key, value, index);
|
|
3466
3489
|
}
|
|
3467
|
-
else if (type === 'bigint'
|
|
3468
|
-
|
|
3490
|
+
else if (type === 'bigint') {
|
|
3491
|
+
index = serializeBigInt(buffer, key, value, index);
|
|
3469
3492
|
}
|
|
3470
3493
|
else if (type === 'boolean') {
|
|
3471
3494
|
index = serializeBoolean(buffer, key, value, index);
|
|
@@ -3485,7 +3508,8 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
|
|
|
3485
3508
|
else if (type === 'object' && value._bsontype == null) {
|
|
3486
3509
|
index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
|
|
3487
3510
|
}
|
|
3488
|
-
else if (typeof value === 'object' &&
|
|
3511
|
+
else if (typeof value === 'object' &&
|
|
3512
|
+
value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
|
|
3489
3513
|
throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
|
|
3490
3514
|
}
|
|
3491
3515
|
else if (value._bsontype === 'ObjectId') {
|
|
@@ -3509,7 +3533,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
|
|
|
3509
3533
|
else if (value._bsontype === 'Binary') {
|
|
3510
3534
|
index = serializeBinary(buffer, key, value, index);
|
|
3511
3535
|
}
|
|
3512
|
-
else if (value._bsontype === '
|
|
3536
|
+
else if (value._bsontype === 'BSONSymbol') {
|
|
3513
3537
|
index = serializeSymbol(buffer, key, value, index);
|
|
3514
3538
|
}
|
|
3515
3539
|
else if (value._bsontype === 'DBRef') {
|
|
@@ -3525,7 +3549,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
|
|
|
3525
3549
|
index = serializeMinMax(buffer, key, value, index);
|
|
3526
3550
|
}
|
|
3527
3551
|
else if (typeof value._bsontype !== 'undefined') {
|
|
3528
|
-
throw new
|
|
3552
|
+
throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
|
|
3529
3553
|
}
|
|
3530
3554
|
}
|
|
3531
3555
|
}
|
|
@@ -3533,7 +3557,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
|
|
|
3533
3557
|
if (typeof object?.toBSON === 'function') {
|
|
3534
3558
|
object = object.toBSON();
|
|
3535
3559
|
if (object != null && typeof object !== 'object') {
|
|
3536
|
-
throw new
|
|
3560
|
+
throw new BSONError('toBSON function did not return an object');
|
|
3537
3561
|
}
|
|
3538
3562
|
}
|
|
3539
3563
|
for (const key of Object.keys(object)) {
|
|
@@ -3544,14 +3568,14 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
|
|
|
3544
3568
|
const type = typeof value;
|
|
3545
3569
|
if (typeof key === 'string' && !ignoreKeys.has(key)) {
|
|
3546
3570
|
if (key.match(regexp) != null) {
|
|
3547
|
-
throw
|
|
3571
|
+
throw new BSONError('key ' + key + ' must not contain null bytes');
|
|
3548
3572
|
}
|
|
3549
3573
|
if (checkKeys) {
|
|
3550
3574
|
if ('$' === key[0]) {
|
|
3551
|
-
throw
|
|
3575
|
+
throw new BSONError('key ' + key + " must not start with '$'");
|
|
3552
3576
|
}
|
|
3553
3577
|
else if (~key.indexOf('.')) {
|
|
3554
|
-
throw
|
|
3578
|
+
throw new BSONError('key ' + key + " must not contain '.'");
|
|
3555
3579
|
}
|
|
3556
3580
|
}
|
|
3557
3581
|
}
|
|
@@ -3562,7 +3586,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
|
|
|
3562
3586
|
index = serializeNumber(buffer, key, value, index);
|
|
3563
3587
|
}
|
|
3564
3588
|
else if (type === 'bigint') {
|
|
3565
|
-
|
|
3589
|
+
index = serializeBigInt(buffer, key, value, index);
|
|
3566
3590
|
}
|
|
3567
3591
|
else if (type === 'boolean') {
|
|
3568
3592
|
index = serializeBoolean(buffer, key, value, index);
|
|
@@ -3586,7 +3610,8 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
|
|
|
3586
3610
|
else if (type === 'object' && value._bsontype == null) {
|
|
3587
3611
|
index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, path);
|
|
3588
3612
|
}
|
|
3589
|
-
else if (typeof value === 'object' &&
|
|
3613
|
+
else if (typeof value === 'object' &&
|
|
3614
|
+
value[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
|
|
3590
3615
|
throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
|
|
3591
3616
|
}
|
|
3592
3617
|
else if (value._bsontype === 'ObjectId') {
|
|
@@ -3610,7 +3635,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
|
|
|
3610
3635
|
else if (value._bsontype === 'Binary') {
|
|
3611
3636
|
index = serializeBinary(buffer, key, value, index);
|
|
3612
3637
|
}
|
|
3613
|
-
else if (value._bsontype === '
|
|
3638
|
+
else if (value._bsontype === 'BSONSymbol') {
|
|
3614
3639
|
index = serializeSymbol(buffer, key, value, index);
|
|
3615
3640
|
}
|
|
3616
3641
|
else if (value._bsontype === 'DBRef') {
|
|
@@ -3626,7 +3651,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
|
|
|
3626
3651
|
index = serializeMinMax(buffer, key, value, index);
|
|
3627
3652
|
}
|
|
3628
3653
|
else if (typeof value._bsontype !== 'undefined') {
|
|
3629
|
-
throw new
|
|
3654
|
+
throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
|
|
3630
3655
|
}
|
|
3631
3656
|
}
|
|
3632
3657
|
}
|
|
@@ -3760,7 +3785,7 @@ function serializeValue(value, options) {
|
|
|
3760
3785
|
const current = props[props.length - 1];
|
|
3761
3786
|
const leadingSpace = ' '.repeat(leadingPart.length + alreadySeen.length / 2);
|
|
3762
3787
|
const dashes = '-'.repeat(circularPart.length + (alreadySeen.length + current.length) / 2 - 1);
|
|
3763
|
-
throw new
|
|
3788
|
+
throw new BSONError('Converting circular structure to EJSON:\n' +
|
|
3764
3789
|
` ${leadingPart}${alreadySeen}${circularPart}${current}\n` +
|
|
3765
3790
|
` ${leadingSpace}\\${dashes}/`);
|
|
3766
3791
|
}
|
|
@@ -3853,7 +3878,7 @@ function serializeDocument(doc, options) {
|
|
|
3853
3878
|
else if (doc != null &&
|
|
3854
3879
|
typeof doc === 'object' &&
|
|
3855
3880
|
typeof doc._bsontype === 'string' &&
|
|
3856
|
-
doc[Symbol.for('@@mdb.bson.version')]
|
|
3881
|
+
doc[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION) {
|
|
3857
3882
|
throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
|
|
3858
3883
|
}
|
|
3859
3884
|
else if (isBSONType(doc)) {
|
|
@@ -3861,7 +3886,7 @@ function serializeDocument(doc, options) {
|
|
|
3861
3886
|
if (typeof outDoc.toExtendedJSON !== 'function') {
|
|
3862
3887
|
const mapper = BSON_TYPE_MAPPINGS[doc._bsontype];
|
|
3863
3888
|
if (!mapper) {
|
|
3864
|
-
throw new
|
|
3889
|
+
throw new BSONError('Unrecognized or invalid _bsontype: ' + doc._bsontype);
|
|
3865
3890
|
}
|
|
3866
3891
|
outDoc = mapper(outDoc);
|
|
3867
3892
|
}
|
|
@@ -3993,10 +4018,9 @@ var bson = /*#__PURE__*/Object.freeze({
|
|
|
3993
4018
|
calculateObjectSize: calculateObjectSize,
|
|
3994
4019
|
deserializeStream: deserializeStream,
|
|
3995
4020
|
BSONError: BSONError,
|
|
3996
|
-
BSONTypeError: BSONTypeError,
|
|
3997
4021
|
BSONType: BSONType,
|
|
3998
4022
|
EJSON: EJSON
|
|
3999
4023
|
});
|
|
4000
4024
|
|
|
4001
|
-
export { bson as BSON, BSONError, BSONRegExp, BSONSymbol, BSONType,
|
|
4025
|
+
export { bson as BSON, BSONError, BSONRegExp, BSONSymbol, BSONType, Binary, Code, DBRef, Decimal128, Double, EJSON, Int32, Long, MaxKey, MinKey, ObjectId, Timestamp, UUID, calculateObjectSize, deserialize, deserializeStream, serialize, serializeWithBufferAndIndex, setInternalBufferSize };
|
|
4002
4026
|
//# sourceMappingURL=bson.mjs.map
|