@refactorlabs/recount 0.11.1 → 0.11.3

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/dist/index.js CHANGED
@@ -1,4 +1,20 @@
1
- import { __privateAdd, __privateGet, __privateSet, runWithTrace, getActiveTrace, runWithSpanName, __privateMethod } from './chunk-XKR3GXTH.js';
1
+ import { __privateAdd, __privateGet, __privateSet, runWithTrace, getActiveTrace, runWithSpanName, __privateMethod, __privateWrapper } from './chunk-GLYQDK5L.js';
2
+
3
+ // src/lib/error.ts
4
+ var RecountError = class extends Error {
5
+ constructor(status, message) {
6
+ super(message);
7
+ this.name = "RecountError";
8
+ this.status = status;
9
+ }
10
+ };
11
+ function errorText(err) {
12
+ try {
13
+ return err instanceof Error ? err.message : String(err);
14
+ } catch {
15
+ return "unknown error";
16
+ }
17
+ }
2
18
 
3
19
  // src/lib/http.ts
4
20
  var HTTP_METHODS = /* @__PURE__ */ new Set(["GET", "POST", "PUT", "PATCH", "DELETE"]);
@@ -399,7 +415,7 @@ function isValidKey(key) {
399
415
  return KEY.test(key) && !RESERVED.has(key);
400
416
  }
401
417
  var _key, _options, _ops, _built, _CalculationBuilder_instances, push_fn, kindLine_fn;
402
- var CalculationBuilder = class {
418
+ var _CalculationBuilder = class _CalculationBuilder {
403
419
  constructor(key, options = {}) {
404
420
  __privateAdd(this, _CalculationBuilder_instances);
405
421
  __privateAdd(this, _key);
@@ -510,7 +526,20 @@ var CalculationBuilder = class {
510
526
  /** The calculation as it's sent. Attach calls build it for you. */
511
527
  build() {
512
528
  const { calculation: calculation2 } = this.compile();
513
- return calculation2 ? structuredClone(calculation2) : { version: 1, key: __privateGet(this, _key), lines: [] };
529
+ return calculation2 ? structuredClone(calculation2) : {
530
+ version: 1,
531
+ key: repairKey(__privateGet(this, _key), "calculation", /* @__PURE__ */ new Set(), [], ""),
532
+ lines: []
533
+ };
534
+ }
535
+ /**
536
+ * @internal A copy holding the steps so far, so adding to this builder
537
+ * doesn't change it. Cheap: nothing is built.
538
+ */
539
+ snapshot() {
540
+ const copy = new _CalculationBuilder(__privateGet(this, _key), copyData(__privateGet(this, _options)));
541
+ for (const op of __privateGet(this, _ops)) __privateGet(copy, _ops).push(copyData(op));
542
+ return copy;
514
543
  }
515
544
  /** @internal Builds once, never throws. */
516
545
  compile() {
@@ -521,7 +550,7 @@ var CalculationBuilder = class {
521
550
  __privateSet(this, _built, {
522
551
  calculation: void 0,
523
552
  problems: [
524
- `${String(__privateGet(this, _key))}: couldn't be built (${err instanceof Error ? err.message : String(err)})`
553
+ `${keyText(__privateGet(this, _key))}: couldn't be built (${errorText(err)})`
525
554
  ]
526
555
  });
527
556
  }
@@ -545,6 +574,28 @@ kindLine_fn = function(kind, key, label, formula, options) {
545
574
  spec: { key, label, kind, formula: formula ?? null, options }
546
575
  });
547
576
  };
577
+ var CalculationBuilder = _CalculationBuilder;
578
+ function copyData(value, seen = /* @__PURE__ */ new Set()) {
579
+ if (typeof value !== "object" || value === null || seen.has(value)) {
580
+ return value;
581
+ }
582
+ if (Array.isArray(value)) {
583
+ seen.add(value);
584
+ const out2 = value.map((v) => copyData(v, seen));
585
+ seen.delete(value);
586
+ return out2;
587
+ }
588
+ if (!isPlainObject(value)) return value;
589
+ seen.add(value);
590
+ const out = Object.fromEntries(
591
+ Object.entries(value).map(([k, v]) => [k, copyData(v, seen)])
592
+ );
593
+ seen.delete(value);
594
+ return out;
595
+ }
596
+ function isRedacted(value) {
597
+ return typeof value === "object" && value !== null && value.__redacted === true;
598
+ }
548
599
  function calculation(key, options) {
549
600
  return new CalculationBuilder(key, options);
550
601
  }
@@ -612,6 +663,14 @@ function compile(rawKey, options, ops) {
612
663
  for (const op of ops) {
613
664
  if (op.type === "input") addInput(s, op.key, op.value, op.options ?? {});
614
665
  }
666
+ if (s.inputs.length > MAX_INPUTS) {
667
+ problems.push(
668
+ `${key}: ${s.inputs.length} inputs; only the first ${MAX_INPUTS} are sent`
669
+ );
670
+ s.inputs.length = MAX_INPUTS;
671
+ s.inputKeys = new Set(s.inputs.map((input) => input.key));
672
+ s.taken = new Set(s.inputKeys);
673
+ }
615
674
  for (const op of ops) {
616
675
  switch (op.type) {
617
676
  case "line":
@@ -646,12 +705,6 @@ function compile(rawKey, options, ops) {
646
705
  }
647
706
  }
648
707
  }
649
- if (s.inputs.length > MAX_INPUTS) {
650
- problems.push(
651
- `${key}: ${s.inputs.length} inputs; only the first ${MAX_INPUTS} are sent`
652
- );
653
- s.inputs.length = MAX_INPUTS;
654
- }
655
708
  if (s.lines.length > MAX_LINES) {
656
709
  problems.push(
657
710
  `${key}: ${s.lines.length} lines; only the first ${MAX_LINES} are sent`
@@ -794,7 +847,10 @@ function addLine(s, spec) {
794
847
  s.problems
795
848
  );
796
849
  const expected = expectedOf(options.expected, at, s.problems);
797
- if (expected !== void 0) line.expected = expected;
850
+ if (expected !== void 0) {
851
+ if (isRedacted(line.value)) expected.value = REDACTED;
852
+ line.expected = expected;
853
+ }
798
854
  const tolerance = toleranceOf(
799
855
  options.tolerance,
800
856
  `${at}: tolerance`,
@@ -848,6 +904,9 @@ function addTiers(s, rawKey, options) {
848
904
  `${s.calcKey}: tiers`
849
905
  );
850
906
  const at = `${s.calcKey}.${key}`;
907
+ if (overflow !== void 0 && overflow !== "none" && overflow !== "last") {
908
+ s.problems.push(`${at}: overflow is 'none' or 'last'; using 'none'`);
909
+ }
851
910
  if (!Array.isArray(bands) || bands.length === 0) {
852
911
  s.problems.push(`${at}: tiers need at least one band`);
853
912
  s.taken.delete(key);
@@ -882,7 +941,13 @@ function addTiers(s, rawKey, options) {
882
941
  const formula = p === void 0 || p === 1 || p === "1" ? amount : `${amount} * ${operandText(p)}`;
883
942
  const range = size === null ? before === "0" ? `all${unitText}${perText}` : `over ${before}${unitText}${perText}` : i === 0 ? `first ${size}${unitText}${perText}` : `next ${size}${unitText}${perText}`;
884
943
  const bandLabel = band?.label ?? `${base} tier ${n} (${range})`;
885
- const bandKey = `${key}_${n}`;
944
+ const wanted = withSuffix(key, `_${n}`);
945
+ const bandKey = unique(wanted, s.taken);
946
+ if (bandKey !== wanted) {
947
+ s.problems.push(
948
+ `${at}: band ${n}'s key "${wanted}" is taken; sent as "${bandKey}"`
949
+ );
950
+ }
886
951
  const tooLong = formula.length > MAX_FORMULA;
887
952
  if (tooLong) {
888
953
  s.problems.push(
@@ -903,7 +968,11 @@ function addTiers(s, rawKey, options) {
903
968
  rateUnit: subtotalOptions.rateUnit
904
969
  },
905
970
  tags,
906
- taxFree
971
+ taxFree,
972
+ // A redacted subtotal would be the sum of the bands' values.
973
+ ...(subtotalOptions.redact || isRedacted(value)) && {
974
+ redact: true
975
+ }
907
976
  }
908
977
  })
909
978
  );
@@ -1097,7 +1166,9 @@ function cleanValue(value, at, problems) {
1097
1166
  }
1098
1167
  const decimal = decimalObjectText(value);
1099
1168
  if (decimal !== void 0) return decimal;
1100
- problems.push(`${at}: a ${typeof value} isn't a value; left out`);
1169
+ problems.push(
1170
+ `${at}: ${typeof value === "object" ? "an object" : `a ${typeof value}`} isn't a value; left out`
1171
+ );
1101
1172
  return void 0;
1102
1173
  }
1103
1174
  function operandOf(value, at, problems) {
@@ -1335,16 +1406,35 @@ function repairKey(given, label, taken, problems, at) {
1335
1406
  taken.add(key);
1336
1407
  return key;
1337
1408
  }
1409
+ function withSuffix(base, suffix) {
1410
+ const room = MAX_KEY - suffix.length;
1411
+ if (base.length <= room) return base + suffix;
1412
+ return base.slice(0, room).replace(/_+$/, "") + suffix;
1413
+ }
1414
+ var nextSuffix = /* @__PURE__ */ new WeakMap();
1338
1415
  function unique(base, taken) {
1339
1416
  if (!taken.has(base)) return base;
1340
- for (let n = 2; ; n++) {
1341
- const key = `${base}_${n}`;
1342
- if (!taken.has(key)) return key;
1417
+ let next = nextSuffix.get(taken);
1418
+ if (!next) {
1419
+ next = /* @__PURE__ */ new Map();
1420
+ nextSuffix.set(taken, next);
1421
+ }
1422
+ for (let n = next.get(base) ?? 2; ; n++) {
1423
+ const key = withSuffix(base, `_${n}`);
1424
+ if (!taken.has(key)) {
1425
+ next.set(base, n);
1426
+ return key;
1427
+ }
1343
1428
  }
1344
1429
  }
1345
1430
  function buildCalculations(calcs) {
1346
1431
  const problems = [];
1347
- const list = Array.isArray(calcs) ? calcs : [calcs];
1432
+ let list;
1433
+ try {
1434
+ list = itemsOf(calcs);
1435
+ } catch (err) {
1436
+ list = [failed(`the calculations couldn't be read (${errorText(err)})`)];
1437
+ }
1348
1438
  const calculations = [];
1349
1439
  const keys = /* @__PURE__ */ new Set();
1350
1440
  let rows = 0;
@@ -1356,7 +1446,7 @@ function buildCalculations(calcs) {
1356
1446
  problems.push("a calculation couldn't be built and was left out");
1357
1447
  continue;
1358
1448
  }
1359
- problems.push(...built.problems);
1449
+ for (const problem of built.problems) problems.push(problem);
1360
1450
  const calc = built.calculation;
1361
1451
  if (!calc) continue;
1362
1452
  if (calculations.length === MAX_CALCULATIONS) {
@@ -1387,6 +1477,56 @@ function buildCalculations(calcs) {
1387
1477
  }
1388
1478
  return { calculations, problems };
1389
1479
  }
1480
+ function itemsOf(calcs) {
1481
+ if (Array.isArray(calcs)) return calcs;
1482
+ if (calcs instanceof Map) return [...calcs.values()];
1483
+ if (!isBuilder(calcs) && typeof calcs === "object" && calcs !== null && typeof calcs[Symbol.iterator] === "function") {
1484
+ const items = [];
1485
+ try {
1486
+ for (const item of calcs) items.push(item);
1487
+ } catch (err) {
1488
+ items.push(
1489
+ failed(
1490
+ `the list of calculations couldn't be read to the end (${errorText(err)}); kept the ${items.length} read before it`
1491
+ )
1492
+ );
1493
+ }
1494
+ return items;
1495
+ }
1496
+ return [calcs];
1497
+ }
1498
+ function failed(problem) {
1499
+ return { compile: () => ({ calculation: void 0, problems: [problem] }) };
1500
+ }
1501
+ function keyText(key) {
1502
+ try {
1503
+ return String(key);
1504
+ } catch {
1505
+ return "calculation";
1506
+ }
1507
+ }
1508
+ function snapshotCalculations(calcs) {
1509
+ let items;
1510
+ try {
1511
+ items = itemsOf(calcs);
1512
+ } catch (err) {
1513
+ return [
1514
+ failed(
1515
+ `the calculations couldn't be read (${errorText(err)})`
1516
+ )
1517
+ ];
1518
+ }
1519
+ return items.map((item) => {
1520
+ try {
1521
+ if (!isBuilder(item)) {
1522
+ return fromPlain(item).snapshot();
1523
+ }
1524
+ return typeof item.snapshot === "function" ? item.snapshot() : item;
1525
+ } catch {
1526
+ return item;
1527
+ }
1528
+ });
1529
+ }
1390
1530
  function isBuilder(item) {
1391
1531
  return typeof item === "object" && item !== null && typeof item.compile === "function";
1392
1532
  }
@@ -1423,9 +1563,22 @@ var units = {
1423
1563
 
1424
1564
  // src/trace.ts
1425
1565
  var NOOP_RESULT = { traceId: "" };
1566
+ var MAX_SPANS = 500;
1567
+ function describe(value) {
1568
+ try {
1569
+ return typeof value === "string" ? JSON.stringify(value) : String(value);
1570
+ } catch {
1571
+ return "an unreadable value";
1572
+ }
1573
+ }
1426
1574
  function calculationSpanOptions(calcs, options, log, name) {
1427
1575
  const { side, ...rest } = options ?? {};
1428
1576
  const { calculations, problems } = buildCalculations(calcs);
1577
+ if (side !== void 0 && side !== "input" && side !== "output") {
1578
+ problems.push(
1579
+ `side is 'input' or 'output', not ${describe(side)}; using 'output'`
1580
+ );
1581
+ }
1429
1582
  for (const problem of problems) {
1430
1583
  log(`calculation span=${name} ${problem}`);
1431
1584
  }
@@ -1458,10 +1611,16 @@ var SpanGroup = class {
1458
1611
  }
1459
1612
  /** A table span holding these calculations, as a child of the group. */
1460
1613
  calculation(name, calcs, options) {
1614
+ const snapshot = snapshotCalculations(calcs);
1615
+ let opts = options;
1616
+ try {
1617
+ opts = options && { ...options };
1618
+ } catch {
1619
+ }
1461
1620
  const item = {
1462
1621
  type: "table",
1463
1622
  name,
1464
- calculations: (log) => calculationSpanOptions(calcs, options, log, name)
1623
+ calculations: (log) => calculationSpanOptions(snapshot, opts, log, name)
1465
1624
  };
1466
1625
  __privateGet(this, _buffer).push(item);
1467
1626
  __privateSet(this, _lastSpan, item);
@@ -1500,7 +1659,8 @@ var SpanGroup = class {
1500
1659
  let built = {};
1501
1660
  try {
1502
1661
  built = calculations(log);
1503
- } catch {
1662
+ } catch (err) {
1663
+ log(`calculation span=${name} couldn't be built (${errorText(err)})`);
1504
1664
  }
1505
1665
  push(type, name, {
1506
1666
  ...built,
@@ -1514,7 +1674,7 @@ var SpanGroup = class {
1514
1674
  };
1515
1675
  _buffer = new WeakMap();
1516
1676
  _lastSpan = new WeakMap();
1517
- var _client, _name, _options2, _spans, _startTime, _disabled, _log, _metadata, _environment, _lastSpan2, _Trace_instances, pushSpan_fn, applyRedaction_fn;
1677
+ var _client, _name, _options2, _spans, _startTime, _disabled, _log, _metadata, _environment, _lastSpan2, _nonGroupSpans, _Trace_instances, pushSpan_fn, makeSpan_fn, applyRedaction_fn;
1518
1678
  var Trace = class {
1519
1679
  constructor(client, name, options = {}, disabled = false, log = () => {
1520
1680
  }) {
@@ -1529,6 +1689,7 @@ var Trace = class {
1529
1689
  __privateAdd(this, _metadata);
1530
1690
  __privateAdd(this, _environment);
1531
1691
  __privateAdd(this, _lastSpan2);
1692
+ __privateAdd(this, _nonGroupSpans, 0);
1532
1693
  __privateSet(this, _client, client);
1533
1694
  __privateSet(this, _name, name);
1534
1695
  __privateSet(this, _options2, options);
@@ -1546,29 +1707,31 @@ var Trace = class {
1546
1707
  }
1547
1708
  group(name, builder, options) {
1548
1709
  if (!__privateGet(this, _disabled)) {
1549
- const groupOrder = __privateGet(this, _spans).length;
1550
1710
  __privateGet(this, _log).call(this, `span type=group name=${name}`);
1551
1711
  const g = new SpanGroup();
1552
1712
  try {
1553
1713
  builder(g);
1554
- } catch {
1714
+ } catch (err) {
1715
+ __privateGet(this, _log).call(this, `span type=group name=${name} builder threw (${errorText(err)}); sending what it added`);
1555
1716
  }
1556
- __privateGet(this, _spans).push({
1717
+ const groupOrder = __privateGet(this, _spans).length;
1718
+ const group = {
1557
1719
  type: "group",
1558
1720
  name,
1559
1721
  order: groupOrder,
1560
1722
  ...options?.description !== void 0 && {
1561
1723
  description: options.description
1562
1724
  }
1563
- });
1564
- const spanCountBeforeFlush = __privateGet(this, _spans).length;
1725
+ };
1726
+ __privateGet(this, _spans).push(group);
1565
1727
  g._flush(
1566
1728
  (type, spanName, opts) => __privateMethod(this, _Trace_instances, pushSpan_fn).call(this, type, spanName, opts, groupOrder),
1567
1729
  options,
1568
1730
  __privateGet(this, _log)
1569
1731
  );
1570
- if (__privateGet(this, _spans).length === spanCountBeforeFlush) {
1571
- __privateGet(this, _spans).splice(groupOrder, 1);
1732
+ if (__privateGet(this, _spans)[__privateGet(this, _spans).length - 1] === group) {
1733
+ __privateGet(this, _spans).pop();
1734
+ __privateSet(this, _lastSpan2, void 0);
1572
1735
  __privateGet(this, _log).call(this, `span type=group name=${name} dropped (no children)`);
1573
1736
  }
1574
1737
  }
@@ -1597,7 +1760,8 @@ var Trace = class {
1597
1760
  let opts;
1598
1761
  try {
1599
1762
  opts = calculationSpanOptions(calcs, options, __privateGet(this, _log), name);
1600
- } catch {
1763
+ } catch (err) {
1764
+ __privateGet(this, _log).call(this, `calculation span=${name} couldn't be built (${errorText(err)})`);
1601
1765
  return this;
1602
1766
  }
1603
1767
  return this.span("table", name, opts);
@@ -1652,14 +1816,35 @@ _log = new WeakMap();
1652
1816
  _metadata = new WeakMap();
1653
1817
  _environment = new WeakMap();
1654
1818
  _lastSpan2 = new WeakMap();
1819
+ _nonGroupSpans = new WeakMap();
1655
1820
  _Trace_instances = new WeakSet();
1656
1821
  pushSpan_fn = function(type, name, options, parentOrder) {
1657
1822
  if (type !== "group" && type !== "message") {
1658
1823
  if (options?.input === void 0 && options?.output === void 0) {
1659
1824
  __privateGet(this, _log).call(this, `span type=${type} name=${name} dropped (no input or output)`);
1825
+ __privateSet(this, _lastSpan2, void 0);
1660
1826
  return;
1661
1827
  }
1662
1828
  }
1829
+ if (type !== "group" && __privateGet(this, _nonGroupSpans) >= MAX_SPANS) {
1830
+ __privateGet(this, _log).call(this, `span type=${type} name=${name} dropped (a trace holds ${MAX_SPANS} spans)`);
1831
+ __privateSet(this, _lastSpan2, void 0);
1832
+ return;
1833
+ }
1834
+ let span;
1835
+ try {
1836
+ span = __privateMethod(this, _Trace_instances, makeSpan_fn).call(this, type, name, options, parentOrder);
1837
+ } catch (err) {
1838
+ __privateGet(this, _log).call(this, `span type=${type} name=${name} dropped (couldn't be recorded: ${errorText(err)})`);
1839
+ __privateSet(this, _lastSpan2, void 0);
1840
+ return;
1841
+ }
1842
+ __privateGet(this, _spans).push(span);
1843
+ if (type !== "group") __privateWrapper(this, _nonGroupSpans)._++;
1844
+ __privateSet(this, _lastSpan2, span);
1845
+ __privateGet(this, _log).call(this, `span type=${type} name=${name}`);
1846
+ };
1847
+ makeSpan_fn = function(type, name, options, parentOrder) {
1663
1848
  let span;
1664
1849
  if (type === "message") {
1665
1850
  span = {
@@ -1687,9 +1872,7 @@ pushSpan_fn = function(type, name, options, parentOrder) {
1687
1872
  ...__privateMethod(this, _Trace_instances, applyRedaction_fn).call(this, type, options)
1688
1873
  };
1689
1874
  }
1690
- __privateGet(this, _spans).push(span);
1691
- __privateSet(this, _lastSpan2, span);
1692
- __privateGet(this, _log).call(this, `span type=${type} name=${name}`);
1875
+ return span;
1693
1876
  };
1694
1877
  applyRedaction_fn = function(type, options) {
1695
1878
  if (!options) return {};
@@ -1733,15 +1916,6 @@ applyRedaction_fn = function(type, options) {
1733
1916
  return { ...rest, input, output };
1734
1917
  };
1735
1918
 
1736
- // src/lib/error.ts
1737
- var RecountError = class extends Error {
1738
- constructor(status, message) {
1739
- super(message);
1740
- this.name = "RecountError";
1741
- this.status = status;
1742
- }
1743
- };
1744
-
1745
1919
  // src/client.ts
1746
1920
  var DEFAULT_API_URL = "https://api.recount.refactor.gg";
1747
1921
  var NOOP_RESULT2 = { traceId: "" };
@@ -1767,6 +1941,59 @@ function retryDelayMs(res, attempt) {
1767
1941
  }
1768
1942
  return BASE_RETRY_MS * 2 ** (attempt - 1);
1769
1943
  }
1944
+ function bigintAsString(_key2, value) {
1945
+ return typeof value === "bigint" ? value.toString() : value;
1946
+ }
1947
+ function serialize(payload, log) {
1948
+ try {
1949
+ return JSON.stringify(payload);
1950
+ } catch {
1951
+ }
1952
+ try {
1953
+ return JSON.stringify(payload, bigintAsString);
1954
+ } catch {
1955
+ }
1956
+ const sendable = payload.spans.filter((span) => {
1957
+ try {
1958
+ JSON.stringify(span, bigintAsString);
1959
+ return true;
1960
+ } catch (err) {
1961
+ log(
1962
+ `span type=${span.type} name=${span.name} dropped (can't be sent as JSON: ${errorText(err)})`
1963
+ );
1964
+ return false;
1965
+ }
1966
+ });
1967
+ const spans = sendable.filter(
1968
+ (span) => span.type !== "group" || sendable.some((child) => child.parentOrder === span.order)
1969
+ );
1970
+ if (spans.length === 0) {
1971
+ log("ingest serialize error: no span can be sent as JSON");
1972
+ return void 0;
1973
+ }
1974
+ let error;
1975
+ try {
1976
+ return JSON.stringify({ ...payload, spans }, bigintAsString);
1977
+ } catch (err) {
1978
+ error = err;
1979
+ }
1980
+ if (payload.metadata !== void 0) {
1981
+ try {
1982
+ const body = JSON.stringify(
1983
+ { ...payload, spans, metadata: void 0 },
1984
+ bigintAsString
1985
+ );
1986
+ log(
1987
+ `ingest metadata dropped (can't be sent as JSON: ${errorText(error)})`
1988
+ );
1989
+ return body;
1990
+ } catch (err) {
1991
+ error = err;
1992
+ }
1993
+ }
1994
+ log(`ingest serialize error: ${errorText(error)}`);
1995
+ return void 0;
1996
+ }
1770
1997
  var _apiKey, _apiUrl, _environment2, _disabled2, _timeoutMs, _log2;
1771
1998
  var Recount = class {
1772
1999
  constructor(options = {}) {
@@ -1810,11 +2037,8 @@ var Recount = class {
1810
2037
  return NOOP_RESULT2;
1811
2038
  }
1812
2039
  __privateGet(this, _log2).call(this, `ingest name=${payload.name} spans=${payload.spans.length}`);
1813
- let body;
1814
- try {
1815
- body = JSON.stringify(payload);
1816
- } catch (err) {
1817
- __privateGet(this, _log2).call(this, `ingest serialize error: ${err instanceof Error ? err.message : String(err)}`);
2040
+ const body = serialize(payload, __privateGet(this, _log2));
2041
+ if (body === void 0) {
1818
2042
  return NOOP_RESULT2;
1819
2043
  }
1820
2044
  const headers = {
@@ -1847,7 +2071,8 @@ var Recount = class {
1847
2071
  }
1848
2072
  if (!res.ok) {
1849
2073
  const text2 = await res.text().catch(() => res.statusText);
1850
- __privateGet(this, _log2).call(this, `ingest error status=${res.status} message=${text2}`);
2074
+ const size = res.status === 413 ? ` bytes=${new TextEncoder().encode(body).length}` : "";
2075
+ __privateGet(this, _log2).call(this, `ingest error status=${res.status}${size} message=${text2}`);
1851
2076
  return NOOP_RESULT2;
1852
2077
  }
1853
2078
  let json;
@@ -1910,8 +2135,45 @@ function cleanCellValue(value) {
1910
2135
  }
1911
2136
  return value;
1912
2137
  }
1913
- function signOf(value, highlight) {
1914
- const n = typeof value === "number" ? value : typeof value === "string" && value.trim() !== "" ? Number(value) : NaN;
2138
+ function contentOf(value) {
2139
+ return typeof value === "object" && value !== null && (value.__cell === true || value.__cellSpan === true) ? value.value : value;
2140
+ }
2141
+ function numberOf(value) {
2142
+ if (typeof value === "number") return value;
2143
+ if (typeof value === "string" && value.trim() !== "") return Number(value);
2144
+ return NaN;
2145
+ }
2146
+ function shownAs(sent) {
2147
+ if (typeof sent !== "object" || sent === null || sent.__cell !== true) {
2148
+ return void 0;
2149
+ }
2150
+ const { format, currency, unit, places } = sent;
2151
+ const kind = format ?? (currency ? "money" : unit !== void 0 || places !== void 0 ? "number" : void 0);
2152
+ if (kind === void 0 || kind === "text") return void 0;
2153
+ return {
2154
+ places: kind === "integer" ? 0 : typeof places === "number" && Number.isInteger(places) && places >= 0 ? places : 2,
2155
+ percent: kind === "percent" || unit === "%" && !(kind === "money" && currency)
2156
+ };
2157
+ }
2158
+ function showsAsZero(n, places, percent) {
2159
+ const text2 = decimalText(n);
2160
+ if (text2 === void 0) return false;
2161
+ let [whole, fraction = ""] = text2.replace("-", "").split(".");
2162
+ if (percent) {
2163
+ whole += fraction.slice(0, 2).padEnd(2, "0");
2164
+ fraction = fraction.slice(2);
2165
+ }
2166
+ return /^0*$/.test(whole) && /^0*$/.test(fraction.slice(0, places)) && (fraction[places] ?? "0") < "5";
2167
+ }
2168
+ function signOf(sent, raw, highlight) {
2169
+ const shown = contentOf(sent);
2170
+ let n = numberOf(shown);
2171
+ const format = shownAs(sent);
2172
+ if (!Number.isNaN(n) && format && showsAsZero(n, format.places, format.percent)) {
2173
+ n = 0;
2174
+ }
2175
+ const fallsBack = shown !== null && shown !== void 0 && (typeof shown !== "string" || /\d/.test(shown));
2176
+ if (Number.isNaN(n) && fallsBack) n = numberOf(contentOf(raw));
1915
2177
  if (Number.isNaN(n)) return null;
1916
2178
  if (n > 0) return highlight.positive ?? null;
1917
2179
  if (n < 0) return highlight.negative ?? null;
@@ -2000,6 +2262,13 @@ var TableBuilder = class {
2000
2262
  /**
2001
2263
  * Colours a column's numbers (records) or a row (objects) by sign:
2002
2264
  * `.signHighlight('saving', { positive: 'success', negative: 'danger' })`.
2265
+ * A row takes the sign of the number shown, after `transformValues()`,
2266
+ * `.round()` and a format's places, as Recount shows it: money, number and
2267
+ * percent to 2 places unless given, an integer to none, and a percent times
2268
+ * 100. So -0.004 as money is zero, and 0.004 as a percent (0.40%) positive.
2269
+ * When what's shown is text with a number in it, such as `'$-5'`, it
2270
+ * takes the raw value's sign; a blank row, or text with no number such as
2271
+ * `'N/A'`, takes none.
2003
2272
  */
2004
2273
  signHighlight(key, highlight) {
2005
2274
  this._signHighlights[key] = highlight;
@@ -2124,24 +2393,25 @@ var TableBuilder = class {
2124
2393
  const order = this._pick;
2125
2394
  rows = order.filter((k) => rows.some(([rk]) => rk === k)).map((k) => rows.find(([rk]) => rk === k));
2126
2395
  }
2396
+ const sentValues = [];
2127
2397
  const finalRows = rows.map(([k, v]) => {
2128
2398
  const key = k;
2129
2399
  if (this._shouldRedact(key)) {
2400
+ sentValues.push(REDACTED);
2130
2401
  return [this._resolveLabel(key), REDACTED];
2131
2402
  }
2132
2403
  const transformed = this._valueTransforms[key] ? this._valueTransforms[key](v) : v;
2133
2404
  const format = this._formats[key];
2134
- if (format !== void 0 && isCellValue(transformed)) {
2135
- return [this._resolveLabel(key), cell(transformed, format)];
2136
- }
2137
- return [this._resolveLabel(key), this._finalValue(key, transformed)];
2405
+ const sent = format !== void 0 && isCellValue(transformed) ? cell(transformed, format) : this._finalValue(key, transformed);
2406
+ sentValues.push(sent);
2407
+ return [this._resolveLabel(key), sent];
2138
2408
  });
2139
2409
  const allRows = this._extraRows.length ? [...finalRows, ...this._extraRows.map((r) => r.cells)] : finalRows;
2140
2410
  const hasSigns = Object.keys(this._signHighlights).length > 0;
2141
- const rowHighlights = this._rowHighlightFn || hasSigns ? rows.map(([k, v]) => {
2411
+ const rowHighlights = this._rowHighlightFn || hasSigns ? rows.map(([k, v], i) => {
2142
2412
  const fromFn = this._rowHighlightFn ? this._rowHighlightFn({ [k]: v }) : null;
2143
2413
  const sign = this._signHighlights[k];
2144
- return fromFn ?? (sign && !this._shouldRedact(k) ? signOf(v, sign) : null);
2414
+ return fromFn ?? (sign && !this._shouldRedact(k) ? signOf(sentValues[i], v, sign) : null);
2145
2415
  }) : void 0;
2146
2416
  const allRowHighlights = this._mergeRowHighlights(
2147
2417
  rowHighlights,