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.mjs CHANGED
@@ -13,6 +13,26 @@ function isMap(d) {
13
13
  function isDate(d) {
14
14
  return Object.prototype.toString.call(d) === '[object Date]';
15
15
  }
16
+ function defaultInspect(x, _options) {
17
+ return JSON.stringify(x, (k, v) => {
18
+ if (typeof v === 'bigint') {
19
+ return { $numberLong: `${v}` };
20
+ }
21
+ else if (isMap(v)) {
22
+ return Object.fromEntries(v);
23
+ }
24
+ return v;
25
+ });
26
+ }
27
+ function getStylizeFunction(options) {
28
+ const stylizeExists = options != null &&
29
+ typeof options === 'object' &&
30
+ 'stylize' in options &&
31
+ typeof options.stylize === 'function';
32
+ if (stylizeExists) {
33
+ return options.stylize;
34
+ }
35
+ }
16
36
 
17
37
  const BSON_MAJOR_VERSION = 6;
18
38
  const BSON_INT32_MAX = 0x7fffffff;
@@ -93,7 +113,7 @@ class BSONVersionError extends BSONError {
93
113
  return 'BSONVersionError';
94
114
  }
95
115
  constructor() {
96
- super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.0 or later`);
116
+ super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.x.x`);
97
117
  }
98
118
  }
99
119
  class BSONRuntimeError extends BSONError {
@@ -300,6 +320,9 @@ class BSONValue {
300
320
  get [Symbol.for('@@mdb.bson.version')]() {
301
321
  return BSON_MAJOR_VERSION;
302
322
  }
323
+ [Symbol.for('nodejs.util.inspect.custom')](depth, options, inspect) {
324
+ return this.inspect(depth, options, inspect);
325
+ }
303
326
  }
304
327
 
305
328
  class Binary extends BSONValue {
@@ -450,12 +473,12 @@ class Binary extends BSONValue {
450
473
  }
451
474
  return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
452
475
  }
453
- [Symbol.for('nodejs.util.inspect.custom')]() {
454
- return this.inspect();
455
- }
456
- inspect() {
476
+ inspect(depth, options, inspect) {
477
+ inspect ??= defaultInspect;
457
478
  const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
458
- return `Binary.createFromBase64("${base64}", ${this.sub_type})`;
479
+ const base64Arg = inspect(base64, options);
480
+ const subTypeArg = inspect(this.sub_type, options);
481
+ return `Binary.createFromBase64(${base64Arg}, ${subTypeArg})`;
459
482
  }
460
483
  }
461
484
  Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
@@ -573,11 +596,9 @@ class UUID extends Binary {
573
596
  static isValidUUIDString(representation) {
574
597
  return UUID_WITHOUT_DASHES.test(representation) || UUID_WITH_DASHES.test(representation);
575
598
  }
576
- [Symbol.for('nodejs.util.inspect.custom')]() {
577
- return this.inspect();
578
- }
579
- inspect() {
580
- return `new UUID("${this.toHexString()}")`;
599
+ inspect(depth, options, inspect) {
600
+ inspect ??= defaultInspect;
601
+ return `new UUID(${inspect(this.toHexString(), options)})`;
581
602
  }
582
603
  }
583
604
 
@@ -605,12 +626,15 @@ class Code extends BSONValue {
605
626
  static fromExtendedJSON(doc) {
606
627
  return new Code(doc.$code, doc.$scope);
607
628
  }
608
- [Symbol.for('nodejs.util.inspect.custom')]() {
609
- return this.inspect();
610
- }
611
- inspect() {
612
- const codeJson = this.toJSON();
613
- return `new Code(${JSON.stringify(String(codeJson.code))}${codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
629
+ inspect(depth, options, inspect) {
630
+ inspect ??= defaultInspect;
631
+ let parametersString = inspect(this.code, options);
632
+ const multiLineFn = parametersString.includes('\n');
633
+ if (this.scope != null) {
634
+ parametersString += `,${multiLineFn ? '\n' : ' '}${inspect(this.scope, options)}`;
635
+ }
636
+ const endingNewline = multiLineFn && this.scope === null;
637
+ return `new Code(${multiLineFn ? '\n' : ''}${parametersString}${endingNewline ? '\n' : ''})`;
614
638
  }
615
639
  }
616
640
 
@@ -675,12 +699,16 @@ class DBRef extends BSONValue {
675
699
  delete copy.$db;
676
700
  return new DBRef(doc.$ref, doc.$id, doc.$db, copy);
677
701
  }
678
- [Symbol.for('nodejs.util.inspect.custom')]() {
679
- return this.inspect();
680
- }
681
- inspect() {
682
- const oid = this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
683
- return `new DBRef("${this.namespace}", new ObjectId("${String(oid)}")${this.db ? `, "${this.db}"` : ''})`;
702
+ inspect(depth, options, inspect) {
703
+ inspect ??= defaultInspect;
704
+ const args = [
705
+ inspect(this.namespace, options),
706
+ inspect(this.oid, options),
707
+ ...(this.db ? [inspect(this.db, options)] : []),
708
+ ...(Object.keys(this.fields).length > 0 ? [inspect(this.fields, options)] : [])
709
+ ];
710
+ args[1] = inspect === defaultInspect ? `new ObjectId(${args[1]})` : args[1];
711
+ return `new DBRef(${args.join(', ')})`;
684
712
  }
685
713
  }
686
714
 
@@ -1307,11 +1335,11 @@ class Long extends BSONValue {
1307
1335
  }
1308
1336
  return longResult;
1309
1337
  }
1310
- [Symbol.for('nodejs.util.inspect.custom')]() {
1311
- return this.inspect();
1312
- }
1313
- inspect() {
1314
- return `new Long("${this.toString()}"${this.unsigned ? ', true' : ''})`;
1338
+ inspect(depth, options, inspect) {
1339
+ inspect ??= defaultInspect;
1340
+ const longVal = inspect(this.toString(), options);
1341
+ const unsignedVal = this.unsigned ? `, ${inspect(this.unsigned, options)}` : '';
1342
+ return `new Long(${longVal}${unsignedVal})`;
1315
1343
  }
1316
1344
  }
1317
1345
  Long.TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
@@ -1419,6 +1447,12 @@ class Decimal128 extends BSONValue {
1419
1447
  }
1420
1448
  }
1421
1449
  static fromString(representation) {
1450
+ return Decimal128._fromString(representation, { allowRounding: false });
1451
+ }
1452
+ static fromStringWithRounding(representation) {
1453
+ return Decimal128._fromString(representation, { allowRounding: true });
1454
+ }
1455
+ static _fromString(representation, options) {
1422
1456
  let isNegative = false;
1423
1457
  let sawSign = false;
1424
1458
  let sawRadix = false;
@@ -1539,44 +1573,117 @@ class Decimal128 extends BSONValue {
1539
1573
  }
1540
1574
  exponent = exponent - 1;
1541
1575
  }
1542
- while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
1543
- if (lastDigit === 0) {
1544
- if (significantDigits === 0) {
1576
+ if (options.allowRounding) {
1577
+ while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
1578
+ if (lastDigit === 0 && significantDigits < nDigitsStored) {
1545
1579
  exponent = EXPONENT_MIN;
1580
+ significantDigits = 0;
1546
1581
  break;
1547
1582
  }
1548
- invalidErr(representation, 'exponent underflow');
1549
- }
1550
- if (nDigitsStored < nDigits) {
1551
- if (representation[nDigits - 1 + Number(sawSign) + Number(sawRadix)] !== '0' &&
1552
- significantDigits !== 0) {
1553
- invalidErr(representation, 'inexact rounding');
1583
+ if (nDigitsStored < nDigits) {
1584
+ nDigits = nDigits - 1;
1554
1585
  }
1555
- nDigits = nDigits - 1;
1556
- }
1557
- else {
1558
- if (digits[lastDigit] !== 0) {
1559
- invalidErr(representation, 'inexact rounding');
1586
+ else {
1587
+ lastDigit = lastDigit - 1;
1588
+ }
1589
+ if (exponent < EXPONENT_MAX) {
1590
+ exponent = exponent + 1;
1591
+ }
1592
+ else {
1593
+ const digitsString = digits.join('');
1594
+ if (digitsString.match(/^0+$/)) {
1595
+ exponent = EXPONENT_MAX;
1596
+ break;
1597
+ }
1598
+ invalidErr(representation, 'overflow');
1560
1599
  }
1561
- lastDigit = lastDigit - 1;
1562
- }
1563
- if (exponent < EXPONENT_MAX) {
1564
- exponent = exponent + 1;
1565
1600
  }
1566
- else {
1567
- invalidErr(representation, 'overflow');
1601
+ if (lastDigit + 1 < significantDigits) {
1602
+ let endOfString = nDigitsRead;
1603
+ if (sawRadix) {
1604
+ firstNonZero = firstNonZero + 1;
1605
+ endOfString = endOfString + 1;
1606
+ }
1607
+ if (sawSign) {
1608
+ firstNonZero = firstNonZero + 1;
1609
+ endOfString = endOfString + 1;
1610
+ }
1611
+ const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
1612
+ let roundBit = 0;
1613
+ if (roundDigit >= 5) {
1614
+ roundBit = 1;
1615
+ if (roundDigit === 5) {
1616
+ roundBit = digits[lastDigit] % 2 === 1 ? 1 : 0;
1617
+ for (let i = firstNonZero + lastDigit + 2; i < endOfString; i++) {
1618
+ if (parseInt(representation[i], 10)) {
1619
+ roundBit = 1;
1620
+ break;
1621
+ }
1622
+ }
1623
+ }
1624
+ }
1625
+ if (roundBit) {
1626
+ let dIdx = lastDigit;
1627
+ for (; dIdx >= 0; dIdx--) {
1628
+ if (++digits[dIdx] > 9) {
1629
+ digits[dIdx] = 0;
1630
+ if (dIdx === 0) {
1631
+ if (exponent < EXPONENT_MAX) {
1632
+ exponent = exponent + 1;
1633
+ digits[dIdx] = 1;
1634
+ }
1635
+ else {
1636
+ return new Decimal128(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER);
1637
+ }
1638
+ }
1639
+ }
1640
+ else {
1641
+ break;
1642
+ }
1643
+ }
1644
+ }
1568
1645
  }
1569
1646
  }
1570
- if (lastDigit + 1 < significantDigits) {
1571
- if (sawRadix) {
1572
- firstNonZero = firstNonZero + 1;
1573
- }
1574
- if (sawSign) {
1575
- firstNonZero = firstNonZero + 1;
1647
+ else {
1648
+ while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
1649
+ if (lastDigit === 0) {
1650
+ if (significantDigits === 0) {
1651
+ exponent = EXPONENT_MIN;
1652
+ break;
1653
+ }
1654
+ invalidErr(representation, 'exponent underflow');
1655
+ }
1656
+ if (nDigitsStored < nDigits) {
1657
+ if (representation[nDigits - 1 + Number(sawSign) + Number(sawRadix)] !== '0' &&
1658
+ significantDigits !== 0) {
1659
+ invalidErr(representation, 'inexact rounding');
1660
+ }
1661
+ nDigits = nDigits - 1;
1662
+ }
1663
+ else {
1664
+ if (digits[lastDigit] !== 0) {
1665
+ invalidErr(representation, 'inexact rounding');
1666
+ }
1667
+ lastDigit = lastDigit - 1;
1668
+ }
1669
+ if (exponent < EXPONENT_MAX) {
1670
+ exponent = exponent + 1;
1671
+ }
1672
+ else {
1673
+ invalidErr(representation, 'overflow');
1674
+ }
1576
1675
  }
1577
- const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
1578
- if (roundDigit !== 0) {
1579
- invalidErr(representation, 'inexact rounding');
1676
+ if (lastDigit + 1 < significantDigits) {
1677
+ if (sawRadix) {
1678
+ firstNonZero = firstNonZero + 1;
1679
+ }
1680
+ if (sawSign) {
1681
+ firstNonZero = firstNonZero + 1;
1682
+ }
1683
+ const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
1684
+ if (roundDigit !== 0) {
1685
+ invalidErr(representation, 'inexact rounding');
1686
+ }
1580
1687
  }
1581
1688
  }
1582
1689
  significandHigh = Long.fromNumber(0);
@@ -1788,11 +1895,10 @@ class Decimal128 extends BSONValue {
1788
1895
  static fromExtendedJSON(doc) {
1789
1896
  return Decimal128.fromString(doc.$numberDecimal);
1790
1897
  }
1791
- [Symbol.for('nodejs.util.inspect.custom')]() {
1792
- return this.inspect();
1793
- }
1794
- inspect() {
1795
- return `new Decimal128("${this.toString()}")`;
1898
+ inspect(depth, options, inspect) {
1899
+ inspect ??= defaultInspect;
1900
+ const d128string = inspect(this.toString(), options);
1901
+ return `new Decimal128(${d128string})`;
1796
1902
  }
1797
1903
  }
1798
1904
 
@@ -1831,12 +1937,9 @@ class Double extends BSONValue {
1831
1937
  const doubleValue = parseFloat(doc.$numberDouble);
1832
1938
  return options && options.relaxed ? doubleValue : new Double(doubleValue);
1833
1939
  }
1834
- [Symbol.for('nodejs.util.inspect.custom')]() {
1835
- return this.inspect();
1836
- }
1837
- inspect() {
1838
- const eJSON = this.toExtendedJSON();
1839
- return `new Double(${eJSON.$numberDouble})`;
1940
+ inspect(depth, options, inspect) {
1941
+ inspect ??= defaultInspect;
1942
+ return `new Double(${inspect(this.value, options)})`;
1840
1943
  }
1841
1944
  }
1842
1945
 
@@ -1868,11 +1971,9 @@ class Int32 extends BSONValue {
1868
1971
  static fromExtendedJSON(doc, options) {
1869
1972
  return options && options.relaxed ? parseInt(doc.$numberInt, 10) : new Int32(doc.$numberInt);
1870
1973
  }
1871
- [Symbol.for('nodejs.util.inspect.custom')]() {
1872
- return this.inspect();
1873
- }
1874
- inspect() {
1875
- return `new Int32(${this.valueOf()})`;
1974
+ inspect(depth, options, inspect) {
1975
+ inspect ??= defaultInspect;
1976
+ return `new Int32(${inspect(this.value, options)})`;
1876
1977
  }
1877
1978
  }
1878
1979
 
@@ -1886,9 +1987,6 @@ class MaxKey extends BSONValue {
1886
1987
  static fromExtendedJSON() {
1887
1988
  return new MaxKey();
1888
1989
  }
1889
- [Symbol.for('nodejs.util.inspect.custom')]() {
1890
- return this.inspect();
1891
- }
1892
1990
  inspect() {
1893
1991
  return 'new MaxKey()';
1894
1992
  }
@@ -1904,9 +2002,6 @@ class MinKey extends BSONValue {
1904
2002
  static fromExtendedJSON() {
1905
2003
  return new MinKey();
1906
2004
  }
1907
- [Symbol.for('nodejs.util.inspect.custom')]() {
1908
- return this.inspect();
1909
- }
1910
2005
  inspect() {
1911
2006
  return 'new MinKey()';
1912
2007
  }
@@ -2077,11 +2172,9 @@ class ObjectId extends BSONValue {
2077
2172
  static fromExtendedJSON(doc) {
2078
2173
  return new ObjectId(doc.$oid);
2079
2174
  }
2080
- [Symbol.for('nodejs.util.inspect.custom')]() {
2081
- return this.inspect();
2082
- }
2083
- inspect() {
2084
- return `new ObjectId("${this.toHexString()}")`;
2175
+ inspect(depth, options, inspect) {
2176
+ inspect ??= defaultInspect;
2177
+ return `new ObjectId(${inspect(this.toHexString(), options)})`;
2085
2178
  }
2086
2179
  }
2087
2180
  ObjectId.index = Math.floor(Math.random() * 0xffffff);
@@ -2294,11 +2387,12 @@ class BSONRegExp extends BSONValue {
2294
2387
  }
2295
2388
  throw new BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
2296
2389
  }
2297
- [Symbol.for('nodejs.util.inspect.custom')]() {
2298
- return this.inspect();
2299
- }
2300
- inspect() {
2301
- return `new BSONRegExp(${JSON.stringify(this.pattern)}, ${JSON.stringify(this.options)})`;
2390
+ inspect(depth, options, inspect) {
2391
+ const stylize = getStylizeFunction(options) ?? (v => v);
2392
+ inspect ??= defaultInspect;
2393
+ const pattern = stylize(inspect(this.pattern), 'regexp');
2394
+ const flags = stylize(inspect(this.options), 'regexp');
2395
+ return `new BSONRegExp(${pattern}, ${flags})`;
2302
2396
  }
2303
2397
  }
2304
2398
 
@@ -2316,9 +2410,6 @@ class BSONSymbol extends BSONValue {
2316
2410
  toString() {
2317
2411
  return this.value;
2318
2412
  }
2319
- inspect() {
2320
- return `new BSONSymbol(${JSON.stringify(this.value)})`;
2321
- }
2322
2413
  toJSON() {
2323
2414
  return this.value;
2324
2415
  }
@@ -2328,8 +2419,9 @@ class BSONSymbol extends BSONValue {
2328
2419
  static fromExtendedJSON(doc) {
2329
2420
  return new BSONSymbol(doc.$symbol);
2330
2421
  }
2331
- [Symbol.for('nodejs.util.inspect.custom')]() {
2332
- return this.inspect();
2422
+ inspect(depth, options, inspect) {
2423
+ inspect ??= defaultInspect;
2424
+ return `new BSONSymbol(${inspect(this.value, options)})`;
2333
2425
  }
2334
2426
  }
2335
2427
 
@@ -2404,11 +2496,11 @@ class Timestamp extends LongWithoutOverridesClass {
2404
2496
  : doc.$timestamp.t;
2405
2497
  return new Timestamp({ t, i });
2406
2498
  }
2407
- [Symbol.for('nodejs.util.inspect.custom')]() {
2408
- return this.inspect();
2409
- }
2410
- inspect() {
2411
- return `new Timestamp({ t: ${this.getHighBits()}, i: ${this.getLowBits()} })`;
2499
+ inspect(depth, options, inspect) {
2500
+ inspect ??= defaultInspect;
2501
+ const t = inspect(this.high >>> 0, options);
2502
+ const i = inspect(this.low >>> 0, options);
2503
+ return `new Timestamp({ t: ${t}, i: ${i} })`;
2412
2504
  }
2413
2505
  }
2414
2506
  Timestamp.MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
@@ -3087,13 +3179,16 @@ function serializeObjectId(buffer, key, value, index) {
3087
3179
  const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
3088
3180
  index = index + numberOfWrittenBytes;
3089
3181
  buffer[index++] = 0;
3090
- if (isUint8Array(value.id)) {
3091
- buffer.set(value.id.subarray(0, 12), index);
3182
+ const idValue = value.id;
3183
+ if (isUint8Array(idValue)) {
3184
+ for (let i = 0; i < 12; i++) {
3185
+ buffer[index++] = idValue[i];
3186
+ }
3092
3187
  }
3093
3188
  else {
3094
3189
  throw new BSONError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
3095
3190
  }
3096
- return index + 12;
3191
+ return index;
3097
3192
  }
3098
3193
  function serializeBuffer(buffer, key, value, index) {
3099
3194
  buffer[index++] = BSON_DATA_BINARY;