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/lib/bson.rn.cjs CHANGED
@@ -22,7 +22,7 @@ function isDate(d) {
22
22
  return Object.prototype.toString.call(d) === '[object Date]';
23
23
  }
24
24
 
25
- const BSON_MAJOR_VERSION = 5;
25
+ const BSON_MAJOR_VERSION = 6;
26
26
  const BSON_INT32_MAX = 0x7fffffff;
27
27
  const BSON_INT32_MIN = -0x80000000;
28
28
  const BSON_INT64_MAX = Math.pow(2, 63) - 1;
@@ -326,11 +326,11 @@ class Binary extends BSONValue {
326
326
  constructor(buffer, subType) {
327
327
  super();
328
328
  if (!(buffer == null) &&
329
- !(typeof buffer === 'string') &&
329
+ typeof buffer === 'string' &&
330
330
  !ArrayBuffer.isView(buffer) &&
331
- !(buffer instanceof ArrayBuffer) &&
331
+ !isAnyArrayBuffer(buffer) &&
332
332
  !Array.isArray(buffer)) {
333
- throw new BSONError('Binary can only be constructed from string, Buffer, TypedArray, or Array<number>');
333
+ throw new BSONError('Binary can only be constructed from Uint8Array or number[]');
334
334
  }
335
335
  this.sub_type = subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT;
336
336
  if (buffer == null) {
@@ -338,15 +338,9 @@ class Binary extends BSONValue {
338
338
  this.position = 0;
339
339
  }
340
340
  else {
341
- if (typeof buffer === 'string') {
342
- this.buffer = ByteUtils.fromISO88591(buffer);
343
- }
344
- else if (Array.isArray(buffer)) {
345
- this.buffer = ByteUtils.fromNumberArray(buffer);
346
- }
347
- else {
348
- this.buffer = ByteUtils.toLocalBufferType(buffer);
349
- }
341
+ this.buffer = Array.isArray(buffer)
342
+ ? ByteUtils.fromNumberArray(buffer)
343
+ : ByteUtils.toLocalBufferType(buffer);
350
344
  this.position = this.buffer.byteLength;
351
345
  }
352
346
  }
@@ -392,25 +386,17 @@ class Binary extends BSONValue {
392
386
  offset + sequence.byteLength > this.position ? offset + sequence.length : this.position;
393
387
  }
394
388
  else if (typeof sequence === 'string') {
395
- const bytes = ByteUtils.fromISO88591(sequence);
396
- this.buffer.set(bytes, offset);
397
- this.position =
398
- offset + sequence.length > this.position ? offset + sequence.length : this.position;
389
+ throw new BSONError('input cannot be string');
399
390
  }
400
391
  }
401
392
  read(position, length) {
402
393
  length = length && length > 0 ? length : this.position;
403
394
  return this.buffer.slice(position, position + length);
404
395
  }
405
- value(asRaw) {
406
- asRaw = !!asRaw;
407
- if (asRaw && this.buffer.length === this.position) {
408
- return this.buffer;
409
- }
410
- if (asRaw) {
411
- return this.buffer.slice(0, this.position);
412
- }
413
- return ByteUtils.toISO88591(this.buffer.subarray(0, this.position));
396
+ value() {
397
+ return this.buffer.length === this.position
398
+ ? this.buffer
399
+ : this.buffer.subarray(0, this.position);
414
400
  }
415
401
  length() {
416
402
  return this.position;
@@ -611,7 +597,6 @@ class UUID extends Binary {
611
597
  return `new UUID("${this.toHexString()}")`;
612
598
  }
613
599
  }
614
- UUID.cacheHexString = false;
615
600
 
616
601
  class Code extends BSONValue {
617
602
  get _bsontype() {
@@ -642,7 +627,7 @@ class Code extends BSONValue {
642
627
  }
643
628
  inspect() {
644
629
  const codeJson = this.toJSON();
645
- return `new Code("${String(codeJson.code)}"${codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
630
+ return `new Code(${JSON.stringify(String(codeJson.code))}${codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
646
631
  }
647
632
  }
648
633
 
@@ -1452,6 +1437,7 @@ class Decimal128 extends BSONValue {
1452
1437
  }
1453
1438
  static fromString(representation) {
1454
1439
  let isNegative = false;
1440
+ let sawSign = false;
1455
1441
  let sawRadix = false;
1456
1442
  let foundNonZero = false;
1457
1443
  let significantDigits = 0;
@@ -1462,10 +1448,8 @@ class Decimal128 extends BSONValue {
1462
1448
  const digits = [0];
1463
1449
  let nDigitsStored = 0;
1464
1450
  let digitsInsert = 0;
1465
- let firstDigit = 0;
1466
1451
  let lastDigit = 0;
1467
1452
  let exponent = 0;
1468
- let i = 0;
1469
1453
  let significandHigh = new Long(0, 0);
1470
1454
  let significandLow = new Long(0, 0);
1471
1455
  let biasedExponent = 0;
@@ -1493,6 +1477,7 @@ class Decimal128 extends BSONValue {
1493
1477
  }
1494
1478
  }
1495
1479
  if (representation[index] === '+' || representation[index] === '-') {
1480
+ sawSign = true;
1496
1481
  isNegative = representation[index++] === '-';
1497
1482
  }
1498
1483
  if (!isDigit(representation[index]) && representation[index] !== '.') {
@@ -1511,7 +1496,7 @@ class Decimal128 extends BSONValue {
1511
1496
  index = index + 1;
1512
1497
  continue;
1513
1498
  }
1514
- if (nDigitsStored < 34) {
1499
+ if (nDigitsStored < MAX_DIGITS) {
1515
1500
  if (representation[index] !== '0' || foundNonZero) {
1516
1501
  if (!foundNonZero) {
1517
1502
  firstNonZero = nDigitsRead;
@@ -1539,10 +1524,7 @@ class Decimal128 extends BSONValue {
1539
1524
  }
1540
1525
  if (representation[index])
1541
1526
  return new Decimal128(NAN_BUFFER);
1542
- firstDigit = 0;
1543
1527
  if (!nDigitsStored) {
1544
- firstDigit = 0;
1545
- lastDigit = 0;
1546
1528
  digits[0] = 0;
1547
1529
  nDigits = 1;
1548
1530
  nDigitsStored = 1;
@@ -1552,12 +1534,12 @@ class Decimal128 extends BSONValue {
1552
1534
  lastDigit = nDigitsStored - 1;
1553
1535
  significantDigits = nDigits;
1554
1536
  if (significantDigits !== 1) {
1555
- while (digits[firstNonZero + significantDigits - 1] === 0) {
1537
+ while (representation[firstNonZero + significantDigits - 1 + Number(sawSign) + Number(sawRadix)] === '0') {
1556
1538
  significantDigits = significantDigits - 1;
1557
1539
  }
1558
1540
  }
1559
1541
  }
1560
- if (exponent <= radixPosition && radixPosition - exponent > 1 << 14) {
1542
+ if (exponent <= radixPosition && radixPosition > exponent + (1 << 14)) {
1561
1543
  exponent = EXPONENT_MIN;
1562
1544
  }
1563
1545
  else {
@@ -1565,9 +1547,8 @@ class Decimal128 extends BSONValue {
1565
1547
  }
1566
1548
  while (exponent > EXPONENT_MAX) {
1567
1549
  lastDigit = lastDigit + 1;
1568
- if (lastDigit - firstDigit > MAX_DIGITS) {
1569
- const digitsString = digits.join('');
1570
- if (digitsString.match(/^0+$/)) {
1550
+ if (lastDigit >= MAX_DIGITS) {
1551
+ if (significantDigits === 0) {
1571
1552
  exponent = EXPONENT_MAX;
1572
1553
  break;
1573
1554
  }
@@ -1576,69 +1557,43 @@ class Decimal128 extends BSONValue {
1576
1557
  exponent = exponent - 1;
1577
1558
  }
1578
1559
  while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
1579
- if (lastDigit === 0 && significantDigits < nDigitsStored) {
1580
- exponent = EXPONENT_MIN;
1581
- significantDigits = 0;
1582
- break;
1560
+ if (lastDigit === 0) {
1561
+ if (significantDigits === 0) {
1562
+ exponent = EXPONENT_MIN;
1563
+ break;
1564
+ }
1565
+ invalidErr(representation, 'exponent underflow');
1583
1566
  }
1584
1567
  if (nDigitsStored < nDigits) {
1568
+ if (representation[nDigits - 1 + Number(sawSign) + Number(sawRadix)] !== '0' &&
1569
+ significantDigits !== 0) {
1570
+ invalidErr(representation, 'inexact rounding');
1571
+ }
1585
1572
  nDigits = nDigits - 1;
1586
1573
  }
1587
1574
  else {
1575
+ if (digits[lastDigit] !== 0) {
1576
+ invalidErr(representation, 'inexact rounding');
1577
+ }
1588
1578
  lastDigit = lastDigit - 1;
1589
1579
  }
1590
1580
  if (exponent < EXPONENT_MAX) {
1591
1581
  exponent = exponent + 1;
1592
1582
  }
1593
1583
  else {
1594
- const digitsString = digits.join('');
1595
- if (digitsString.match(/^0+$/)) {
1596
- exponent = EXPONENT_MAX;
1597
- break;
1598
- }
1599
1584
  invalidErr(representation, 'overflow');
1600
1585
  }
1601
1586
  }
1602
- if (lastDigit - firstDigit + 1 < significantDigits) {
1603
- let endOfString = nDigitsRead;
1587
+ if (lastDigit + 1 < significantDigits) {
1604
1588
  if (sawRadix) {
1605
1589
  firstNonZero = firstNonZero + 1;
1606
- endOfString = endOfString + 1;
1607
1590
  }
1608
- if (isNegative) {
1591
+ if (sawSign) {
1609
1592
  firstNonZero = firstNonZero + 1;
1610
- endOfString = endOfString + 1;
1611
1593
  }
1612
1594
  const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
1613
- let roundBit = 0;
1614
- if (roundDigit >= 5) {
1615
- roundBit = 1;
1616
- if (roundDigit === 5) {
1617
- roundBit = digits[lastDigit] % 2 === 1 ? 1 : 0;
1618
- for (i = firstNonZero + lastDigit + 2; i < endOfString; i++) {
1619
- if (parseInt(representation[i], 10)) {
1620
- roundBit = 1;
1621
- break;
1622
- }
1623
- }
1624
- }
1625
- }
1626
- if (roundBit) {
1627
- let dIdx = lastDigit;
1628
- for (; dIdx >= 0; dIdx--) {
1629
- if (++digits[dIdx] > 9) {
1630
- digits[dIdx] = 0;
1631
- if (dIdx === 0) {
1632
- if (exponent < EXPONENT_MAX) {
1633
- exponent = exponent + 1;
1634
- digits[dIdx] = 1;
1635
- }
1636
- else {
1637
- return new Decimal128(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER);
1638
- }
1639
- }
1640
- }
1641
- }
1595
+ if (roundDigit !== 0) {
1596
+ invalidErr(representation, 'inexact rounding');
1642
1597
  }
1643
1598
  }
1644
1599
  significandHigh = Long.fromNumber(0);
@@ -1647,8 +1602,8 @@ class Decimal128 extends BSONValue {
1647
1602
  significandHigh = Long.fromNumber(0);
1648
1603
  significandLow = Long.fromNumber(0);
1649
1604
  }
1650
- else if (lastDigit - firstDigit < 17) {
1651
- let dIdx = firstDigit;
1605
+ else if (lastDigit < 17) {
1606
+ let dIdx = 0;
1652
1607
  significandLow = Long.fromNumber(digits[dIdx++]);
1653
1608
  significandHigh = new Long(0, 0);
1654
1609
  for (; dIdx <= lastDigit; dIdx++) {
@@ -1657,7 +1612,7 @@ class Decimal128 extends BSONValue {
1657
1612
  }
1658
1613
  }
1659
1614
  else {
1660
- let dIdx = firstDigit;
1615
+ let dIdx = 0;
1661
1616
  significandHigh = Long.fromNumber(digits[dIdx++]);
1662
1617
  for (; dIdx <= lastDigit - 17; dIdx++) {
1663
1618
  significandHigh = significandHigh.multiply(Long.fromNumber(10));
@@ -2005,20 +1960,11 @@ class ObjectId extends BSONValue {
2005
1960
  this[kId] = ByteUtils.toLocalBufferType(workingId);
2006
1961
  }
2007
1962
  else if (typeof workingId === 'string') {
2008
- if (workingId.length === 12) {
2009
- const bytes = ByteUtils.fromUTF8(workingId);
2010
- if (bytes.byteLength === 12) {
2011
- this[kId] = bytes;
2012
- }
2013
- else {
2014
- throw new BSONError('Argument passed in must be a string of 12 bytes');
2015
- }
2016
- }
2017
- else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
1963
+ if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
2018
1964
  this[kId] = ByteUtils.fromHex(workingId);
2019
1965
  }
2020
1966
  else {
2021
- throw new BSONError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer');
1967
+ throw new BSONError('input must be a 24 character hex string, 12 byte Uint8Array, or an integer');
2022
1968
  }
2023
1969
  }
2024
1970
  else {
@@ -2080,30 +2026,25 @@ class ObjectId extends BSONValue {
2080
2026
  toJSON() {
2081
2027
  return this.toHexString();
2082
2028
  }
2029
+ static is(variable) {
2030
+ return (variable != null &&
2031
+ typeof variable === 'object' &&
2032
+ '_bsontype' in variable &&
2033
+ variable._bsontype === 'ObjectId');
2034
+ }
2083
2035
  equals(otherId) {
2084
2036
  if (otherId === undefined || otherId === null) {
2085
2037
  return false;
2086
2038
  }
2087
- if (otherId instanceof ObjectId) {
2039
+ if (ObjectId.is(otherId)) {
2088
2040
  return this[kId][11] === otherId[kId][11] && ByteUtils.equals(this[kId], otherId[kId]);
2089
2041
  }
2090
- if (typeof otherId === 'string' &&
2091
- ObjectId.isValid(otherId) &&
2092
- otherId.length === 12 &&
2093
- isUint8Array(this.id)) {
2094
- return ByteUtils.equals(this.id, ByteUtils.fromISO88591(otherId));
2095
- }
2096
- if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 24) {
2042
+ if (typeof otherId === 'string') {
2097
2043
  return otherId.toLowerCase() === this.toHexString();
2098
2044
  }
2099
- if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 12) {
2100
- return ByteUtils.equals(ByteUtils.fromUTF8(otherId), this.id);
2101
- }
2102
- if (typeof otherId === 'object' &&
2103
- 'toHexString' in otherId &&
2104
- typeof otherId.toHexString === 'function') {
2045
+ if (typeof otherId === 'object' && typeof otherId.toHexString === 'function') {
2105
2046
  const otherIdString = otherId.toHexString();
2106
- const thisIdString = this.toHexString().toLowerCase();
2047
+ const thisIdString = this.toHexString();
2107
2048
  return typeof otherIdString === 'string' && otherIdString.toLowerCase() === thisIdString;
2108
2049
  }
2109
2050
  return false;
@@ -2393,7 +2334,7 @@ class BSONSymbol extends BSONValue {
2393
2334
  return this.value;
2394
2335
  }
2395
2336
  inspect() {
2396
- return `new BSONSymbol("${this.value}")`;
2337
+ return `new BSONSymbol(${JSON.stringify(this.value)})`;
2397
2338
  }
2398
2339
  toJSON() {
2399
2340
  return this.value;