bson 5.4.0 → 6.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 +47 -32
- package/bson.d.ts +10 -23
- package/lib/bson.bundle.js +53 -112
- package/lib/bson.bundle.js.map +1 -1
- package/lib/bson.cjs +53 -112
- package/lib/bson.cjs.map +1 -1
- package/lib/bson.mjs +53 -112
- package/lib/bson.mjs.map +1 -1
- package/lib/bson.rn.cjs +53 -112
- package/lib/bson.rn.cjs.map +1 -1
- package/package.json +20 -19
- package/src/binary.ts +16 -51
- package/src/code.ts +1 -1
- package/src/constants.ts +1 -1
- package/src/decimal128.ts +35 -67
- package/src/objectid.ts +20 -37
- package/src/symbol.ts +1 -1
- package/src/utils/byte_utils.ts +1 -1
package/lib/bson.cjs
CHANGED
|
@@ -16,7 +16,7 @@ function isDate(d) {
|
|
|
16
16
|
return Object.prototype.toString.call(d) === '[object Date]';
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
const BSON_MAJOR_VERSION =
|
|
19
|
+
const BSON_MAJOR_VERSION = 6;
|
|
20
20
|
const BSON_INT32_MAX = 0x7fffffff;
|
|
21
21
|
const BSON_INT32_MIN = -0x80000000;
|
|
22
22
|
const BSON_INT64_MAX = Math.pow(2, 63) - 1;
|
|
@@ -311,11 +311,11 @@ class Binary extends BSONValue {
|
|
|
311
311
|
constructor(buffer, subType) {
|
|
312
312
|
super();
|
|
313
313
|
if (!(buffer == null) &&
|
|
314
|
-
|
|
314
|
+
typeof buffer === 'string' &&
|
|
315
315
|
!ArrayBuffer.isView(buffer) &&
|
|
316
|
-
!(buffer
|
|
316
|
+
!isAnyArrayBuffer(buffer) &&
|
|
317
317
|
!Array.isArray(buffer)) {
|
|
318
|
-
throw new BSONError('Binary can only be constructed from
|
|
318
|
+
throw new BSONError('Binary can only be constructed from Uint8Array or number[]');
|
|
319
319
|
}
|
|
320
320
|
this.sub_type = subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT;
|
|
321
321
|
if (buffer == null) {
|
|
@@ -323,15 +323,9 @@ class Binary extends BSONValue {
|
|
|
323
323
|
this.position = 0;
|
|
324
324
|
}
|
|
325
325
|
else {
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
else if (Array.isArray(buffer)) {
|
|
330
|
-
this.buffer = ByteUtils.fromNumberArray(buffer);
|
|
331
|
-
}
|
|
332
|
-
else {
|
|
333
|
-
this.buffer = ByteUtils.toLocalBufferType(buffer);
|
|
334
|
-
}
|
|
326
|
+
this.buffer = Array.isArray(buffer)
|
|
327
|
+
? ByteUtils.fromNumberArray(buffer)
|
|
328
|
+
: ByteUtils.toLocalBufferType(buffer);
|
|
335
329
|
this.position = this.buffer.byteLength;
|
|
336
330
|
}
|
|
337
331
|
}
|
|
@@ -377,25 +371,17 @@ class Binary extends BSONValue {
|
|
|
377
371
|
offset + sequence.byteLength > this.position ? offset + sequence.length : this.position;
|
|
378
372
|
}
|
|
379
373
|
else if (typeof sequence === 'string') {
|
|
380
|
-
|
|
381
|
-
this.buffer.set(bytes, offset);
|
|
382
|
-
this.position =
|
|
383
|
-
offset + sequence.length > this.position ? offset + sequence.length : this.position;
|
|
374
|
+
throw new BSONError('input cannot be string');
|
|
384
375
|
}
|
|
385
376
|
}
|
|
386
377
|
read(position, length) {
|
|
387
378
|
length = length && length > 0 ? length : this.position;
|
|
388
379
|
return this.buffer.slice(position, position + length);
|
|
389
380
|
}
|
|
390
|
-
value(
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
}
|
|
395
|
-
if (asRaw) {
|
|
396
|
-
return this.buffer.slice(0, this.position);
|
|
397
|
-
}
|
|
398
|
-
return ByteUtils.toISO88591(this.buffer.subarray(0, this.position));
|
|
381
|
+
value() {
|
|
382
|
+
return this.buffer.length === this.position
|
|
383
|
+
? this.buffer
|
|
384
|
+
: this.buffer.subarray(0, this.position);
|
|
399
385
|
}
|
|
400
386
|
length() {
|
|
401
387
|
return this.position;
|
|
@@ -596,7 +582,6 @@ class UUID extends Binary {
|
|
|
596
582
|
return `new UUID("${this.toHexString()}")`;
|
|
597
583
|
}
|
|
598
584
|
}
|
|
599
|
-
UUID.cacheHexString = false;
|
|
600
585
|
|
|
601
586
|
class Code extends BSONValue {
|
|
602
587
|
get _bsontype() {
|
|
@@ -627,7 +612,7 @@ class Code extends BSONValue {
|
|
|
627
612
|
}
|
|
628
613
|
inspect() {
|
|
629
614
|
const codeJson = this.toJSON();
|
|
630
|
-
return `new Code(
|
|
615
|
+
return `new Code(${JSON.stringify(String(codeJson.code))}${codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
|
|
631
616
|
}
|
|
632
617
|
}
|
|
633
618
|
|
|
@@ -1437,6 +1422,7 @@ class Decimal128 extends BSONValue {
|
|
|
1437
1422
|
}
|
|
1438
1423
|
static fromString(representation) {
|
|
1439
1424
|
let isNegative = false;
|
|
1425
|
+
let sawSign = false;
|
|
1440
1426
|
let sawRadix = false;
|
|
1441
1427
|
let foundNonZero = false;
|
|
1442
1428
|
let significantDigits = 0;
|
|
@@ -1447,10 +1433,8 @@ class Decimal128 extends BSONValue {
|
|
|
1447
1433
|
const digits = [0];
|
|
1448
1434
|
let nDigitsStored = 0;
|
|
1449
1435
|
let digitsInsert = 0;
|
|
1450
|
-
let firstDigit = 0;
|
|
1451
1436
|
let lastDigit = 0;
|
|
1452
1437
|
let exponent = 0;
|
|
1453
|
-
let i = 0;
|
|
1454
1438
|
let significandHigh = new Long(0, 0);
|
|
1455
1439
|
let significandLow = new Long(0, 0);
|
|
1456
1440
|
let biasedExponent = 0;
|
|
@@ -1478,6 +1462,7 @@ class Decimal128 extends BSONValue {
|
|
|
1478
1462
|
}
|
|
1479
1463
|
}
|
|
1480
1464
|
if (representation[index] === '+' || representation[index] === '-') {
|
|
1465
|
+
sawSign = true;
|
|
1481
1466
|
isNegative = representation[index++] === '-';
|
|
1482
1467
|
}
|
|
1483
1468
|
if (!isDigit(representation[index]) && representation[index] !== '.') {
|
|
@@ -1496,7 +1481,7 @@ class Decimal128 extends BSONValue {
|
|
|
1496
1481
|
index = index + 1;
|
|
1497
1482
|
continue;
|
|
1498
1483
|
}
|
|
1499
|
-
if (nDigitsStored <
|
|
1484
|
+
if (nDigitsStored < MAX_DIGITS) {
|
|
1500
1485
|
if (representation[index] !== '0' || foundNonZero) {
|
|
1501
1486
|
if (!foundNonZero) {
|
|
1502
1487
|
firstNonZero = nDigitsRead;
|
|
@@ -1524,10 +1509,7 @@ class Decimal128 extends BSONValue {
|
|
|
1524
1509
|
}
|
|
1525
1510
|
if (representation[index])
|
|
1526
1511
|
return new Decimal128(NAN_BUFFER);
|
|
1527
|
-
firstDigit = 0;
|
|
1528
1512
|
if (!nDigitsStored) {
|
|
1529
|
-
firstDigit = 0;
|
|
1530
|
-
lastDigit = 0;
|
|
1531
1513
|
digits[0] = 0;
|
|
1532
1514
|
nDigits = 1;
|
|
1533
1515
|
nDigitsStored = 1;
|
|
@@ -1537,12 +1519,12 @@ class Decimal128 extends BSONValue {
|
|
|
1537
1519
|
lastDigit = nDigitsStored - 1;
|
|
1538
1520
|
significantDigits = nDigits;
|
|
1539
1521
|
if (significantDigits !== 1) {
|
|
1540
|
-
while (
|
|
1522
|
+
while (representation[firstNonZero + significantDigits - 1 + Number(sawSign) + Number(sawRadix)] === '0') {
|
|
1541
1523
|
significantDigits = significantDigits - 1;
|
|
1542
1524
|
}
|
|
1543
1525
|
}
|
|
1544
1526
|
}
|
|
1545
|
-
if (exponent <= radixPosition && radixPosition
|
|
1527
|
+
if (exponent <= radixPosition && radixPosition > exponent + (1 << 14)) {
|
|
1546
1528
|
exponent = EXPONENT_MIN;
|
|
1547
1529
|
}
|
|
1548
1530
|
else {
|
|
@@ -1550,9 +1532,8 @@ class Decimal128 extends BSONValue {
|
|
|
1550
1532
|
}
|
|
1551
1533
|
while (exponent > EXPONENT_MAX) {
|
|
1552
1534
|
lastDigit = lastDigit + 1;
|
|
1553
|
-
if (lastDigit
|
|
1554
|
-
|
|
1555
|
-
if (digitsString.match(/^0+$/)) {
|
|
1535
|
+
if (lastDigit >= MAX_DIGITS) {
|
|
1536
|
+
if (significantDigits === 0) {
|
|
1556
1537
|
exponent = EXPONENT_MAX;
|
|
1557
1538
|
break;
|
|
1558
1539
|
}
|
|
@@ -1561,69 +1542,43 @@ class Decimal128 extends BSONValue {
|
|
|
1561
1542
|
exponent = exponent - 1;
|
|
1562
1543
|
}
|
|
1563
1544
|
while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
|
|
1564
|
-
if (lastDigit === 0
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1545
|
+
if (lastDigit === 0) {
|
|
1546
|
+
if (significantDigits === 0) {
|
|
1547
|
+
exponent = EXPONENT_MIN;
|
|
1548
|
+
break;
|
|
1549
|
+
}
|
|
1550
|
+
invalidErr(representation, 'exponent underflow');
|
|
1568
1551
|
}
|
|
1569
1552
|
if (nDigitsStored < nDigits) {
|
|
1553
|
+
if (representation[nDigits - 1 + Number(sawSign) + Number(sawRadix)] !== '0' &&
|
|
1554
|
+
significantDigits !== 0) {
|
|
1555
|
+
invalidErr(representation, 'inexact rounding');
|
|
1556
|
+
}
|
|
1570
1557
|
nDigits = nDigits - 1;
|
|
1571
1558
|
}
|
|
1572
1559
|
else {
|
|
1560
|
+
if (digits[lastDigit] !== 0) {
|
|
1561
|
+
invalidErr(representation, 'inexact rounding');
|
|
1562
|
+
}
|
|
1573
1563
|
lastDigit = lastDigit - 1;
|
|
1574
1564
|
}
|
|
1575
1565
|
if (exponent < EXPONENT_MAX) {
|
|
1576
1566
|
exponent = exponent + 1;
|
|
1577
1567
|
}
|
|
1578
1568
|
else {
|
|
1579
|
-
const digitsString = digits.join('');
|
|
1580
|
-
if (digitsString.match(/^0+$/)) {
|
|
1581
|
-
exponent = EXPONENT_MAX;
|
|
1582
|
-
break;
|
|
1583
|
-
}
|
|
1584
1569
|
invalidErr(representation, 'overflow');
|
|
1585
1570
|
}
|
|
1586
1571
|
}
|
|
1587
|
-
if (lastDigit
|
|
1588
|
-
let endOfString = nDigitsRead;
|
|
1572
|
+
if (lastDigit + 1 < significantDigits) {
|
|
1589
1573
|
if (sawRadix) {
|
|
1590
1574
|
firstNonZero = firstNonZero + 1;
|
|
1591
|
-
endOfString = endOfString + 1;
|
|
1592
1575
|
}
|
|
1593
|
-
if (
|
|
1576
|
+
if (sawSign) {
|
|
1594
1577
|
firstNonZero = firstNonZero + 1;
|
|
1595
|
-
endOfString = endOfString + 1;
|
|
1596
1578
|
}
|
|
1597
1579
|
const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
roundBit = 1;
|
|
1601
|
-
if (roundDigit === 5) {
|
|
1602
|
-
roundBit = digits[lastDigit] % 2 === 1 ? 1 : 0;
|
|
1603
|
-
for (i = firstNonZero + lastDigit + 2; i < endOfString; i++) {
|
|
1604
|
-
if (parseInt(representation[i], 10)) {
|
|
1605
|
-
roundBit = 1;
|
|
1606
|
-
break;
|
|
1607
|
-
}
|
|
1608
|
-
}
|
|
1609
|
-
}
|
|
1610
|
-
}
|
|
1611
|
-
if (roundBit) {
|
|
1612
|
-
let dIdx = lastDigit;
|
|
1613
|
-
for (; dIdx >= 0; dIdx--) {
|
|
1614
|
-
if (++digits[dIdx] > 9) {
|
|
1615
|
-
digits[dIdx] = 0;
|
|
1616
|
-
if (dIdx === 0) {
|
|
1617
|
-
if (exponent < EXPONENT_MAX) {
|
|
1618
|
-
exponent = exponent + 1;
|
|
1619
|
-
digits[dIdx] = 1;
|
|
1620
|
-
}
|
|
1621
|
-
else {
|
|
1622
|
-
return new Decimal128(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER);
|
|
1623
|
-
}
|
|
1624
|
-
}
|
|
1625
|
-
}
|
|
1626
|
-
}
|
|
1580
|
+
if (roundDigit !== 0) {
|
|
1581
|
+
invalidErr(representation, 'inexact rounding');
|
|
1627
1582
|
}
|
|
1628
1583
|
}
|
|
1629
1584
|
significandHigh = Long.fromNumber(0);
|
|
@@ -1632,8 +1587,8 @@ class Decimal128 extends BSONValue {
|
|
|
1632
1587
|
significandHigh = Long.fromNumber(0);
|
|
1633
1588
|
significandLow = Long.fromNumber(0);
|
|
1634
1589
|
}
|
|
1635
|
-
else if (lastDigit
|
|
1636
|
-
let dIdx =
|
|
1590
|
+
else if (lastDigit < 17) {
|
|
1591
|
+
let dIdx = 0;
|
|
1637
1592
|
significandLow = Long.fromNumber(digits[dIdx++]);
|
|
1638
1593
|
significandHigh = new Long(0, 0);
|
|
1639
1594
|
for (; dIdx <= lastDigit; dIdx++) {
|
|
@@ -1642,7 +1597,7 @@ class Decimal128 extends BSONValue {
|
|
|
1642
1597
|
}
|
|
1643
1598
|
}
|
|
1644
1599
|
else {
|
|
1645
|
-
let dIdx =
|
|
1600
|
+
let dIdx = 0;
|
|
1646
1601
|
significandHigh = Long.fromNumber(digits[dIdx++]);
|
|
1647
1602
|
for (; dIdx <= lastDigit - 17; dIdx++) {
|
|
1648
1603
|
significandHigh = significandHigh.multiply(Long.fromNumber(10));
|
|
@@ -1990,20 +1945,11 @@ class ObjectId extends BSONValue {
|
|
|
1990
1945
|
this[kId] = ByteUtils.toLocalBufferType(workingId);
|
|
1991
1946
|
}
|
|
1992
1947
|
else if (typeof workingId === 'string') {
|
|
1993
|
-
if (workingId.length ===
|
|
1994
|
-
const bytes = ByteUtils.fromUTF8(workingId);
|
|
1995
|
-
if (bytes.byteLength === 12) {
|
|
1996
|
-
this[kId] = bytes;
|
|
1997
|
-
}
|
|
1998
|
-
else {
|
|
1999
|
-
throw new BSONError('Argument passed in must be a string of 12 bytes');
|
|
2000
|
-
}
|
|
2001
|
-
}
|
|
2002
|
-
else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
|
|
1948
|
+
if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
|
|
2003
1949
|
this[kId] = ByteUtils.fromHex(workingId);
|
|
2004
1950
|
}
|
|
2005
1951
|
else {
|
|
2006
|
-
throw new BSONError('
|
|
1952
|
+
throw new BSONError('input must be a 24 character hex string, 12 byte Uint8Array, or an integer');
|
|
2007
1953
|
}
|
|
2008
1954
|
}
|
|
2009
1955
|
else {
|
|
@@ -2065,30 +2011,25 @@ class ObjectId extends BSONValue {
|
|
|
2065
2011
|
toJSON() {
|
|
2066
2012
|
return this.toHexString();
|
|
2067
2013
|
}
|
|
2014
|
+
static is(variable) {
|
|
2015
|
+
return (variable != null &&
|
|
2016
|
+
typeof variable === 'object' &&
|
|
2017
|
+
'_bsontype' in variable &&
|
|
2018
|
+
variable._bsontype === 'ObjectId');
|
|
2019
|
+
}
|
|
2068
2020
|
equals(otherId) {
|
|
2069
2021
|
if (otherId === undefined || otherId === null) {
|
|
2070
2022
|
return false;
|
|
2071
2023
|
}
|
|
2072
|
-
if (otherId
|
|
2024
|
+
if (ObjectId.is(otherId)) {
|
|
2073
2025
|
return this[kId][11] === otherId[kId][11] && ByteUtils.equals(this[kId], otherId[kId]);
|
|
2074
2026
|
}
|
|
2075
|
-
if (typeof otherId === 'string'
|
|
2076
|
-
ObjectId.isValid(otherId) &&
|
|
2077
|
-
otherId.length === 12 &&
|
|
2078
|
-
isUint8Array(this.id)) {
|
|
2079
|
-
return ByteUtils.equals(this.id, ByteUtils.fromISO88591(otherId));
|
|
2080
|
-
}
|
|
2081
|
-
if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 24) {
|
|
2027
|
+
if (typeof otherId === 'string') {
|
|
2082
2028
|
return otherId.toLowerCase() === this.toHexString();
|
|
2083
2029
|
}
|
|
2084
|
-
if (typeof otherId === '
|
|
2085
|
-
return ByteUtils.equals(ByteUtils.fromUTF8(otherId), this.id);
|
|
2086
|
-
}
|
|
2087
|
-
if (typeof otherId === 'object' &&
|
|
2088
|
-
'toHexString' in otherId &&
|
|
2089
|
-
typeof otherId.toHexString === 'function') {
|
|
2030
|
+
if (typeof otherId === 'object' && typeof otherId.toHexString === 'function') {
|
|
2090
2031
|
const otherIdString = otherId.toHexString();
|
|
2091
|
-
const thisIdString = this.toHexString()
|
|
2032
|
+
const thisIdString = this.toHexString();
|
|
2092
2033
|
return typeof otherIdString === 'string' && otherIdString.toLowerCase() === thisIdString;
|
|
2093
2034
|
}
|
|
2094
2035
|
return false;
|
|
@@ -2378,7 +2319,7 @@ class BSONSymbol extends BSONValue {
|
|
|
2378
2319
|
return this.value;
|
|
2379
2320
|
}
|
|
2380
2321
|
inspect() {
|
|
2381
|
-
return `new BSONSymbol(
|
|
2322
|
+
return `new BSONSymbol(${JSON.stringify(this.value)})`;
|
|
2382
2323
|
}
|
|
2383
2324
|
toJSON() {
|
|
2384
2325
|
return this.value;
|