bson 6.0.0 → 6.2.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
@@ -21,6 +21,26 @@ function isMap(d) {
21
21
  function isDate(d) {
22
22
  return Object.prototype.toString.call(d) === '[object Date]';
23
23
  }
24
+ function defaultInspect(x, _options) {
25
+ return JSON.stringify(x, (k, v) => {
26
+ if (typeof v === 'bigint') {
27
+ return { $numberLong: `${v}` };
28
+ }
29
+ else if (isMap(v)) {
30
+ return Object.fromEntries(v);
31
+ }
32
+ return v;
33
+ });
34
+ }
35
+ function getStylizeFunction(options) {
36
+ const stylizeExists = options != null &&
37
+ typeof options === 'object' &&
38
+ 'stylize' in options &&
39
+ typeof options.stylize === 'function';
40
+ if (stylizeExists) {
41
+ return options.stylize;
42
+ }
43
+ }
24
44
 
25
45
  const BSON_MAJOR_VERSION = 6;
26
46
  const BSON_INT32_MAX = 0x7fffffff;
@@ -108,7 +128,7 @@ class BSONVersionError extends BSONError {
108
128
  return 'BSONVersionError';
109
129
  }
110
130
  constructor() {
111
- super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.0 or later`);
131
+ super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.x.x`);
112
132
  }
113
133
  }
114
134
  class BSONRuntimeError extends BSONError {
@@ -317,6 +337,9 @@ class BSONValue {
317
337
  get [Symbol.for('@@mdb.bson.version')]() {
318
338
  return BSON_MAJOR_VERSION;
319
339
  }
340
+ [Symbol.for('nodejs.util.inspect.custom')](depth, options, inspect) {
341
+ return this.inspect(depth, options, inspect);
342
+ }
320
343
  }
321
344
 
322
345
  class Binary extends BSONValue {
@@ -467,12 +490,12 @@ class Binary extends BSONValue {
467
490
  }
468
491
  return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
469
492
  }
470
- [Symbol.for('nodejs.util.inspect.custom')]() {
471
- return this.inspect();
472
- }
473
- inspect() {
493
+ inspect(depth, options, inspect) {
494
+ inspect ??= defaultInspect;
474
495
  const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
475
- return `Binary.createFromBase64("${base64}", ${this.sub_type})`;
496
+ const base64Arg = inspect(base64, options);
497
+ const subTypeArg = inspect(this.sub_type, options);
498
+ return `Binary.createFromBase64(${base64Arg}, ${subTypeArg})`;
476
499
  }
477
500
  }
478
501
  Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
@@ -590,11 +613,9 @@ class UUID extends Binary {
590
613
  static isValidUUIDString(representation) {
591
614
  return UUID_WITHOUT_DASHES.test(representation) || UUID_WITH_DASHES.test(representation);
592
615
  }
593
- [Symbol.for('nodejs.util.inspect.custom')]() {
594
- return this.inspect();
595
- }
596
- inspect() {
597
- return `new UUID("${this.toHexString()}")`;
616
+ inspect(depth, options, inspect) {
617
+ inspect ??= defaultInspect;
618
+ return `new UUID(${inspect(this.toHexString(), options)})`;
598
619
  }
599
620
  }
600
621
 
@@ -622,12 +643,15 @@ class Code extends BSONValue {
622
643
  static fromExtendedJSON(doc) {
623
644
  return new Code(doc.$code, doc.$scope);
624
645
  }
625
- [Symbol.for('nodejs.util.inspect.custom')]() {
626
- return this.inspect();
627
- }
628
- inspect() {
629
- const codeJson = this.toJSON();
630
- return `new Code(${JSON.stringify(String(codeJson.code))}${codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
646
+ inspect(depth, options, inspect) {
647
+ inspect ??= defaultInspect;
648
+ let parametersString = inspect(this.code, options);
649
+ const multiLineFn = parametersString.includes('\n');
650
+ if (this.scope != null) {
651
+ parametersString += `,${multiLineFn ? '\n' : ' '}${inspect(this.scope, options)}`;
652
+ }
653
+ const endingNewline = multiLineFn && this.scope === null;
654
+ return `new Code(${multiLineFn ? '\n' : ''}${parametersString}${endingNewline ? '\n' : ''})`;
631
655
  }
632
656
  }
633
657
 
@@ -692,12 +716,16 @@ class DBRef extends BSONValue {
692
716
  delete copy.$db;
693
717
  return new DBRef(doc.$ref, doc.$id, doc.$db, copy);
694
718
  }
695
- [Symbol.for('nodejs.util.inspect.custom')]() {
696
- return this.inspect();
697
- }
698
- inspect() {
699
- const oid = this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
700
- return `new DBRef("${this.namespace}", new ObjectId("${String(oid)}")${this.db ? `, "${this.db}"` : ''})`;
719
+ inspect(depth, options, inspect) {
720
+ inspect ??= defaultInspect;
721
+ const args = [
722
+ inspect(this.namespace, options),
723
+ inspect(this.oid, options),
724
+ ...(this.db ? [inspect(this.db, options)] : []),
725
+ ...(Object.keys(this.fields).length > 0 ? [inspect(this.fields, options)] : [])
726
+ ];
727
+ args[1] = inspect === defaultInspect ? `new ObjectId(${args[1]})` : args[1];
728
+ return `new DBRef(${args.join(', ')})`;
701
729
  }
702
730
  }
703
731
 
@@ -1324,11 +1352,11 @@ class Long extends BSONValue {
1324
1352
  }
1325
1353
  return longResult;
1326
1354
  }
1327
- [Symbol.for('nodejs.util.inspect.custom')]() {
1328
- return this.inspect();
1329
- }
1330
- inspect() {
1331
- return `new Long("${this.toString()}"${this.unsigned ? ', true' : ''})`;
1355
+ inspect(depth, options, inspect) {
1356
+ inspect ??= defaultInspect;
1357
+ const longVal = inspect(this.toString(), options);
1358
+ const unsignedVal = this.unsigned ? `, ${inspect(this.unsigned, options)}` : '';
1359
+ return `new Long(${longVal}${unsignedVal})`;
1332
1360
  }
1333
1361
  }
1334
1362
  Long.TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
@@ -1436,6 +1464,12 @@ class Decimal128 extends BSONValue {
1436
1464
  }
1437
1465
  }
1438
1466
  static fromString(representation) {
1467
+ return Decimal128._fromString(representation, { allowRounding: false });
1468
+ }
1469
+ static fromStringWithRounding(representation) {
1470
+ return Decimal128._fromString(representation, { allowRounding: true });
1471
+ }
1472
+ static _fromString(representation, options) {
1439
1473
  let isNegative = false;
1440
1474
  let sawSign = false;
1441
1475
  let sawRadix = false;
@@ -1556,44 +1590,117 @@ class Decimal128 extends BSONValue {
1556
1590
  }
1557
1591
  exponent = exponent - 1;
1558
1592
  }
1559
- while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
1560
- if (lastDigit === 0) {
1561
- if (significantDigits === 0) {
1593
+ if (options.allowRounding) {
1594
+ while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
1595
+ if (lastDigit === 0 && significantDigits < nDigitsStored) {
1562
1596
  exponent = EXPONENT_MIN;
1597
+ significantDigits = 0;
1563
1598
  break;
1564
1599
  }
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');
1600
+ if (nDigitsStored < nDigits) {
1601
+ nDigits = nDigits - 1;
1571
1602
  }
1572
- nDigits = nDigits - 1;
1573
- }
1574
- else {
1575
- if (digits[lastDigit] !== 0) {
1576
- invalidErr(representation, 'inexact rounding');
1603
+ else {
1604
+ lastDigit = lastDigit - 1;
1605
+ }
1606
+ if (exponent < EXPONENT_MAX) {
1607
+ exponent = exponent + 1;
1608
+ }
1609
+ else {
1610
+ const digitsString = digits.join('');
1611
+ if (digitsString.match(/^0+$/)) {
1612
+ exponent = EXPONENT_MAX;
1613
+ break;
1614
+ }
1615
+ invalidErr(representation, 'overflow');
1577
1616
  }
1578
- lastDigit = lastDigit - 1;
1579
- }
1580
- if (exponent < EXPONENT_MAX) {
1581
- exponent = exponent + 1;
1582
1617
  }
1583
- else {
1584
- invalidErr(representation, 'overflow');
1618
+ if (lastDigit + 1 < significantDigits) {
1619
+ let endOfString = nDigitsRead;
1620
+ if (sawRadix) {
1621
+ firstNonZero = firstNonZero + 1;
1622
+ endOfString = endOfString + 1;
1623
+ }
1624
+ if (sawSign) {
1625
+ firstNonZero = firstNonZero + 1;
1626
+ endOfString = endOfString + 1;
1627
+ }
1628
+ const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
1629
+ let roundBit = 0;
1630
+ if (roundDigit >= 5) {
1631
+ roundBit = 1;
1632
+ if (roundDigit === 5) {
1633
+ roundBit = digits[lastDigit] % 2 === 1 ? 1 : 0;
1634
+ for (let i = firstNonZero + lastDigit + 2; i < endOfString; i++) {
1635
+ if (parseInt(representation[i], 10)) {
1636
+ roundBit = 1;
1637
+ break;
1638
+ }
1639
+ }
1640
+ }
1641
+ }
1642
+ if (roundBit) {
1643
+ let dIdx = lastDigit;
1644
+ for (; dIdx >= 0; dIdx--) {
1645
+ if (++digits[dIdx] > 9) {
1646
+ digits[dIdx] = 0;
1647
+ if (dIdx === 0) {
1648
+ if (exponent < EXPONENT_MAX) {
1649
+ exponent = exponent + 1;
1650
+ digits[dIdx] = 1;
1651
+ }
1652
+ else {
1653
+ return new Decimal128(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER);
1654
+ }
1655
+ }
1656
+ }
1657
+ else {
1658
+ break;
1659
+ }
1660
+ }
1661
+ }
1585
1662
  }
1586
1663
  }
1587
- if (lastDigit + 1 < significantDigits) {
1588
- if (sawRadix) {
1589
- firstNonZero = firstNonZero + 1;
1590
- }
1591
- if (sawSign) {
1592
- firstNonZero = firstNonZero + 1;
1664
+ else {
1665
+ while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
1666
+ if (lastDigit === 0) {
1667
+ if (significantDigits === 0) {
1668
+ exponent = EXPONENT_MIN;
1669
+ break;
1670
+ }
1671
+ invalidErr(representation, 'exponent underflow');
1672
+ }
1673
+ if (nDigitsStored < nDigits) {
1674
+ if (representation[nDigits - 1 + Number(sawSign) + Number(sawRadix)] !== '0' &&
1675
+ significantDigits !== 0) {
1676
+ invalidErr(representation, 'inexact rounding');
1677
+ }
1678
+ nDigits = nDigits - 1;
1679
+ }
1680
+ else {
1681
+ if (digits[lastDigit] !== 0) {
1682
+ invalidErr(representation, 'inexact rounding');
1683
+ }
1684
+ lastDigit = lastDigit - 1;
1685
+ }
1686
+ if (exponent < EXPONENT_MAX) {
1687
+ exponent = exponent + 1;
1688
+ }
1689
+ else {
1690
+ invalidErr(representation, 'overflow');
1691
+ }
1593
1692
  }
1594
- const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
1595
- if (roundDigit !== 0) {
1596
- invalidErr(representation, 'inexact rounding');
1693
+ if (lastDigit + 1 < significantDigits) {
1694
+ if (sawRadix) {
1695
+ firstNonZero = firstNonZero + 1;
1696
+ }
1697
+ if (sawSign) {
1698
+ firstNonZero = firstNonZero + 1;
1699
+ }
1700
+ const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
1701
+ if (roundDigit !== 0) {
1702
+ invalidErr(representation, 'inexact rounding');
1703
+ }
1597
1704
  }
1598
1705
  }
1599
1706
  significandHigh = Long.fromNumber(0);
@@ -1805,11 +1912,10 @@ class Decimal128 extends BSONValue {
1805
1912
  static fromExtendedJSON(doc) {
1806
1913
  return Decimal128.fromString(doc.$numberDecimal);
1807
1914
  }
1808
- [Symbol.for('nodejs.util.inspect.custom')]() {
1809
- return this.inspect();
1810
- }
1811
- inspect() {
1812
- return `new Decimal128("${this.toString()}")`;
1915
+ inspect(depth, options, inspect) {
1916
+ inspect ??= defaultInspect;
1917
+ const d128string = inspect(this.toString(), options);
1918
+ return `new Decimal128(${d128string})`;
1813
1919
  }
1814
1920
  }
1815
1921
 
@@ -1848,12 +1954,9 @@ class Double extends BSONValue {
1848
1954
  const doubleValue = parseFloat(doc.$numberDouble);
1849
1955
  return options && options.relaxed ? doubleValue : new Double(doubleValue);
1850
1956
  }
1851
- [Symbol.for('nodejs.util.inspect.custom')]() {
1852
- return this.inspect();
1853
- }
1854
- inspect() {
1855
- const eJSON = this.toExtendedJSON();
1856
- return `new Double(${eJSON.$numberDouble})`;
1957
+ inspect(depth, options, inspect) {
1958
+ inspect ??= defaultInspect;
1959
+ return `new Double(${inspect(this.value, options)})`;
1857
1960
  }
1858
1961
  }
1859
1962
 
@@ -1885,11 +1988,9 @@ class Int32 extends BSONValue {
1885
1988
  static fromExtendedJSON(doc, options) {
1886
1989
  return options && options.relaxed ? parseInt(doc.$numberInt, 10) : new Int32(doc.$numberInt);
1887
1990
  }
1888
- [Symbol.for('nodejs.util.inspect.custom')]() {
1889
- return this.inspect();
1890
- }
1891
- inspect() {
1892
- return `new Int32(${this.valueOf()})`;
1991
+ inspect(depth, options, inspect) {
1992
+ inspect ??= defaultInspect;
1993
+ return `new Int32(${inspect(this.value, options)})`;
1893
1994
  }
1894
1995
  }
1895
1996
 
@@ -1903,9 +2004,6 @@ class MaxKey extends BSONValue {
1903
2004
  static fromExtendedJSON() {
1904
2005
  return new MaxKey();
1905
2006
  }
1906
- [Symbol.for('nodejs.util.inspect.custom')]() {
1907
- return this.inspect();
1908
- }
1909
2007
  inspect() {
1910
2008
  return 'new MaxKey()';
1911
2009
  }
@@ -1921,9 +2019,6 @@ class MinKey extends BSONValue {
1921
2019
  static fromExtendedJSON() {
1922
2020
  return new MinKey();
1923
2021
  }
1924
- [Symbol.for('nodejs.util.inspect.custom')]() {
1925
- return this.inspect();
1926
- }
1927
2022
  inspect() {
1928
2023
  return 'new MinKey()';
1929
2024
  }
@@ -2094,11 +2189,9 @@ class ObjectId extends BSONValue {
2094
2189
  static fromExtendedJSON(doc) {
2095
2190
  return new ObjectId(doc.$oid);
2096
2191
  }
2097
- [Symbol.for('nodejs.util.inspect.custom')]() {
2098
- return this.inspect();
2099
- }
2100
- inspect() {
2101
- return `new ObjectId("${this.toHexString()}")`;
2192
+ inspect(depth, options, inspect) {
2193
+ inspect ??= defaultInspect;
2194
+ return `new ObjectId(${inspect(this.toHexString(), options)})`;
2102
2195
  }
2103
2196
  }
2104
2197
  ObjectId.index = Math.floor(Math.random() * 0xffffff);
@@ -2311,11 +2404,12 @@ class BSONRegExp extends BSONValue {
2311
2404
  }
2312
2405
  throw new BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
2313
2406
  }
2314
- [Symbol.for('nodejs.util.inspect.custom')]() {
2315
- return this.inspect();
2316
- }
2317
- inspect() {
2318
- return `new BSONRegExp(${JSON.stringify(this.pattern)}, ${JSON.stringify(this.options)})`;
2407
+ inspect(depth, options, inspect) {
2408
+ const stylize = getStylizeFunction(options) ?? (v => v);
2409
+ inspect ??= defaultInspect;
2410
+ const pattern = stylize(inspect(this.pattern), 'regexp');
2411
+ const flags = stylize(inspect(this.options), 'regexp');
2412
+ return `new BSONRegExp(${pattern}, ${flags})`;
2319
2413
  }
2320
2414
  }
2321
2415
 
@@ -2333,9 +2427,6 @@ class BSONSymbol extends BSONValue {
2333
2427
  toString() {
2334
2428
  return this.value;
2335
2429
  }
2336
- inspect() {
2337
- return `new BSONSymbol(${JSON.stringify(this.value)})`;
2338
- }
2339
2430
  toJSON() {
2340
2431
  return this.value;
2341
2432
  }
@@ -2345,8 +2436,9 @@ class BSONSymbol extends BSONValue {
2345
2436
  static fromExtendedJSON(doc) {
2346
2437
  return new BSONSymbol(doc.$symbol);
2347
2438
  }
2348
- [Symbol.for('nodejs.util.inspect.custom')]() {
2349
- return this.inspect();
2439
+ inspect(depth, options, inspect) {
2440
+ inspect ??= defaultInspect;
2441
+ return `new BSONSymbol(${inspect(this.value, options)})`;
2350
2442
  }
2351
2443
  }
2352
2444
 
@@ -2421,11 +2513,11 @@ class Timestamp extends LongWithoutOverridesClass {
2421
2513
  : doc.$timestamp.t;
2422
2514
  return new Timestamp({ t, i });
2423
2515
  }
2424
- [Symbol.for('nodejs.util.inspect.custom')]() {
2425
- return this.inspect();
2426
- }
2427
- inspect() {
2428
- return `new Timestamp({ t: ${this.getHighBits()}, i: ${this.getLowBits()} })`;
2516
+ inspect(depth, options, inspect) {
2517
+ inspect ??= defaultInspect;
2518
+ const t = inspect(this.high >>> 0, options);
2519
+ const i = inspect(this.low >>> 0, options);
2520
+ return `new Timestamp({ t: ${t}, i: ${i} })`;
2429
2521
  }
2430
2522
  }
2431
2523
  Timestamp.MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
@@ -3104,13 +3196,16 @@ function serializeObjectId(buffer, key, value, index) {
3104
3196
  const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
3105
3197
  index = index + numberOfWrittenBytes;
3106
3198
  buffer[index++] = 0;
3107
- if (isUint8Array(value.id)) {
3108
- buffer.set(value.id.subarray(0, 12), index);
3199
+ const idValue = value.id;
3200
+ if (isUint8Array(idValue)) {
3201
+ for (let i = 0; i < 12; i++) {
3202
+ buffer[index++] = idValue[i];
3203
+ }
3109
3204
  }
3110
3205
  else {
3111
3206
  throw new BSONError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
3112
3207
  }
3113
- return index + 12;
3208
+ return index;
3114
3209
  }
3115
3210
  function serializeBuffer(buffer, key, value, index) {
3116
3211
  buffer[index++] = BSON_DATA_BINARY;