bson 6.0.0 → 6.1.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.rn.cjs CHANGED
@@ -1436,6 +1436,12 @@ class Decimal128 extends BSONValue {
1436
1436
  }
1437
1437
  }
1438
1438
  static fromString(representation) {
1439
+ return Decimal128._fromString(representation, { allowRounding: false });
1440
+ }
1441
+ static fromStringWithRounding(representation) {
1442
+ return Decimal128._fromString(representation, { allowRounding: true });
1443
+ }
1444
+ static _fromString(representation, options) {
1439
1445
  let isNegative = false;
1440
1446
  let sawSign = false;
1441
1447
  let sawRadix = false;
@@ -1556,44 +1562,117 @@ class Decimal128 extends BSONValue {
1556
1562
  }
1557
1563
  exponent = exponent - 1;
1558
1564
  }
1559
- while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
1560
- if (lastDigit === 0) {
1561
- if (significantDigits === 0) {
1565
+ if (options.allowRounding) {
1566
+ while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
1567
+ if (lastDigit === 0 && significantDigits < nDigitsStored) {
1562
1568
  exponent = EXPONENT_MIN;
1569
+ significantDigits = 0;
1563
1570
  break;
1564
1571
  }
1565
- invalidErr(representation, 'exponent underflow');
1566
- }
1567
- if (nDigitsStored < nDigits) {
1568
- if (representation[nDigits - 1 + Number(sawSign) + Number(sawRadix)] !== '0' &&
1569
- significantDigits !== 0) {
1570
- invalidErr(representation, 'inexact rounding');
1572
+ if (nDigitsStored < nDigits) {
1573
+ nDigits = nDigits - 1;
1571
1574
  }
1572
- nDigits = nDigits - 1;
1573
- }
1574
- else {
1575
- if (digits[lastDigit] !== 0) {
1576
- invalidErr(representation, 'inexact rounding');
1575
+ else {
1576
+ lastDigit = lastDigit - 1;
1577
+ }
1578
+ if (exponent < EXPONENT_MAX) {
1579
+ exponent = exponent + 1;
1580
+ }
1581
+ else {
1582
+ const digitsString = digits.join('');
1583
+ if (digitsString.match(/^0+$/)) {
1584
+ exponent = EXPONENT_MAX;
1585
+ break;
1586
+ }
1587
+ invalidErr(representation, 'overflow');
1577
1588
  }
1578
- lastDigit = lastDigit - 1;
1579
- }
1580
- if (exponent < EXPONENT_MAX) {
1581
- exponent = exponent + 1;
1582
1589
  }
1583
- else {
1584
- invalidErr(representation, 'overflow');
1590
+ if (lastDigit + 1 < significantDigits) {
1591
+ let endOfString = nDigitsRead;
1592
+ if (sawRadix) {
1593
+ firstNonZero = firstNonZero + 1;
1594
+ endOfString = endOfString + 1;
1595
+ }
1596
+ if (sawSign) {
1597
+ firstNonZero = firstNonZero + 1;
1598
+ endOfString = endOfString + 1;
1599
+ }
1600
+ const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
1601
+ let roundBit = 0;
1602
+ if (roundDigit >= 5) {
1603
+ roundBit = 1;
1604
+ if (roundDigit === 5) {
1605
+ roundBit = digits[lastDigit] % 2 === 1 ? 1 : 0;
1606
+ for (let i = firstNonZero + lastDigit + 2; i < endOfString; i++) {
1607
+ if (parseInt(representation[i], 10)) {
1608
+ roundBit = 1;
1609
+ break;
1610
+ }
1611
+ }
1612
+ }
1613
+ }
1614
+ if (roundBit) {
1615
+ let dIdx = lastDigit;
1616
+ for (; dIdx >= 0; dIdx--) {
1617
+ if (++digits[dIdx] > 9) {
1618
+ digits[dIdx] = 0;
1619
+ if (dIdx === 0) {
1620
+ if (exponent < EXPONENT_MAX) {
1621
+ exponent = exponent + 1;
1622
+ digits[dIdx] = 1;
1623
+ }
1624
+ else {
1625
+ return new Decimal128(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER);
1626
+ }
1627
+ }
1628
+ }
1629
+ else {
1630
+ break;
1631
+ }
1632
+ }
1633
+ }
1585
1634
  }
1586
1635
  }
1587
- if (lastDigit + 1 < significantDigits) {
1588
- if (sawRadix) {
1589
- firstNonZero = firstNonZero + 1;
1590
- }
1591
- if (sawSign) {
1592
- firstNonZero = firstNonZero + 1;
1636
+ else {
1637
+ while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
1638
+ if (lastDigit === 0) {
1639
+ if (significantDigits === 0) {
1640
+ exponent = EXPONENT_MIN;
1641
+ break;
1642
+ }
1643
+ invalidErr(representation, 'exponent underflow');
1644
+ }
1645
+ if (nDigitsStored < nDigits) {
1646
+ if (representation[nDigits - 1 + Number(sawSign) + Number(sawRadix)] !== '0' &&
1647
+ significantDigits !== 0) {
1648
+ invalidErr(representation, 'inexact rounding');
1649
+ }
1650
+ nDigits = nDigits - 1;
1651
+ }
1652
+ else {
1653
+ if (digits[lastDigit] !== 0) {
1654
+ invalidErr(representation, 'inexact rounding');
1655
+ }
1656
+ lastDigit = lastDigit - 1;
1657
+ }
1658
+ if (exponent < EXPONENT_MAX) {
1659
+ exponent = exponent + 1;
1660
+ }
1661
+ else {
1662
+ invalidErr(representation, 'overflow');
1663
+ }
1593
1664
  }
1594
- const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
1595
- if (roundDigit !== 0) {
1596
- invalidErr(representation, 'inexact rounding');
1665
+ if (lastDigit + 1 < significantDigits) {
1666
+ if (sawRadix) {
1667
+ firstNonZero = firstNonZero + 1;
1668
+ }
1669
+ if (sawSign) {
1670
+ firstNonZero = firstNonZero + 1;
1671
+ }
1672
+ const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
1673
+ if (roundDigit !== 0) {
1674
+ invalidErr(representation, 'inexact rounding');
1675
+ }
1597
1676
  }
1598
1677
  }
1599
1678
  significandHigh = Long.fromNumber(0);
@@ -3104,13 +3183,16 @@ function serializeObjectId(buffer, key, value, index) {
3104
3183
  const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
3105
3184
  index = index + numberOfWrittenBytes;
3106
3185
  buffer[index++] = 0;
3107
- if (isUint8Array(value.id)) {
3108
- buffer.set(value.id.subarray(0, 12), index);
3186
+ const idValue = value.id;
3187
+ if (isUint8Array(idValue)) {
3188
+ for (let i = 0; i < 12; i++) {
3189
+ buffer[index++] = idValue[i];
3190
+ }
3109
3191
  }
3110
3192
  else {
3111
3193
  throw new BSONError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
3112
3194
  }
3113
- return index + 12;
3195
+ return index;
3114
3196
  }
3115
3197
  function serializeBuffer(buffer, key, value, index) {
3116
3198
  buffer[index++] = BSON_DATA_BINARY;