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