@refactorlabs/recount 0.11.1 → 0.11.2
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/README.md +1 -0
- package/dist/{chunk-XKR3GXTH.js → chunk-GLYQDK5L.js} +11 -3
- package/dist/chunk-GLYQDK5L.js.map +1 -0
- package/dist/index.cjs +331 -55
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +15 -3
- package/dist/index.d.ts +15 -3
- package/dist/index.js +324 -56
- package/dist/index.js.map +1 -1
- package/dist/integrations/anthropic.js +1 -1
- package/dist/integrations/mongodb.js +1 -1
- package/dist/integrations/openai.js +1 -1
- package/llms.md +6 -3
- package/package.json +1 -1
- package/dist/chunk-XKR3GXTH.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,20 @@
|
|
|
1
|
-
import { __privateAdd, __privateGet, __privateSet, runWithTrace, getActiveTrace, runWithSpanName, __privateMethod } from './chunk-
|
|
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
|
|
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) : {
|
|
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
|
-
`${
|
|
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)
|
|
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
|
|
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(
|
|
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
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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(
|
|
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,30 @@ 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).
|
|
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
|
-
|
|
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 ===
|
|
1571
|
-
__privateGet(this, _spans).
|
|
1732
|
+
if (__privateGet(this, _spans)[__privateGet(this, _spans).length - 1] === group) {
|
|
1733
|
+
__privateGet(this, _spans).pop();
|
|
1572
1734
|
__privateGet(this, _log).call(this, `span type=group name=${name} dropped (no children)`);
|
|
1573
1735
|
}
|
|
1574
1736
|
}
|
|
@@ -1597,7 +1759,8 @@ var Trace = class {
|
|
|
1597
1759
|
let opts;
|
|
1598
1760
|
try {
|
|
1599
1761
|
opts = calculationSpanOptions(calcs, options, __privateGet(this, _log), name);
|
|
1600
|
-
} catch {
|
|
1762
|
+
} catch (err) {
|
|
1763
|
+
__privateGet(this, _log).call(this, `calculation span=${name} couldn't be built (${errorText(err)})`);
|
|
1601
1764
|
return this;
|
|
1602
1765
|
}
|
|
1603
1766
|
return this.span("table", name, opts);
|
|
@@ -1652,6 +1815,7 @@ _log = new WeakMap();
|
|
|
1652
1815
|
_metadata = new WeakMap();
|
|
1653
1816
|
_environment = new WeakMap();
|
|
1654
1817
|
_lastSpan2 = new WeakMap();
|
|
1818
|
+
_nonGroupSpans = new WeakMap();
|
|
1655
1819
|
_Trace_instances = new WeakSet();
|
|
1656
1820
|
pushSpan_fn = function(type, name, options, parentOrder) {
|
|
1657
1821
|
if (type !== "group" && type !== "message") {
|
|
@@ -1660,6 +1824,25 @@ pushSpan_fn = function(type, name, options, parentOrder) {
|
|
|
1660
1824
|
return;
|
|
1661
1825
|
}
|
|
1662
1826
|
}
|
|
1827
|
+
if (type !== "group" && __privateGet(this, _nonGroupSpans) >= MAX_SPANS) {
|
|
1828
|
+
__privateGet(this, _log).call(this, `span type=${type} name=${name} dropped (a trace holds ${MAX_SPANS} spans)`);
|
|
1829
|
+
__privateSet(this, _lastSpan2, void 0);
|
|
1830
|
+
return;
|
|
1831
|
+
}
|
|
1832
|
+
let span;
|
|
1833
|
+
try {
|
|
1834
|
+
span = __privateMethod(this, _Trace_instances, makeSpan_fn).call(this, type, name, options, parentOrder);
|
|
1835
|
+
} catch (err) {
|
|
1836
|
+
__privateGet(this, _log).call(this, `span type=${type} name=${name} dropped (couldn't be recorded: ${errorText(err)})`);
|
|
1837
|
+
__privateSet(this, _lastSpan2, void 0);
|
|
1838
|
+
return;
|
|
1839
|
+
}
|
|
1840
|
+
__privateGet(this, _spans).push(span);
|
|
1841
|
+
if (type !== "group") __privateWrapper(this, _nonGroupSpans)._++;
|
|
1842
|
+
__privateSet(this, _lastSpan2, span);
|
|
1843
|
+
__privateGet(this, _log).call(this, `span type=${type} name=${name}`);
|
|
1844
|
+
};
|
|
1845
|
+
makeSpan_fn = function(type, name, options, parentOrder) {
|
|
1663
1846
|
let span;
|
|
1664
1847
|
if (type === "message") {
|
|
1665
1848
|
span = {
|
|
@@ -1687,9 +1870,7 @@ pushSpan_fn = function(type, name, options, parentOrder) {
|
|
|
1687
1870
|
...__privateMethod(this, _Trace_instances, applyRedaction_fn).call(this, type, options)
|
|
1688
1871
|
};
|
|
1689
1872
|
}
|
|
1690
|
-
|
|
1691
|
-
__privateSet(this, _lastSpan2, span);
|
|
1692
|
-
__privateGet(this, _log).call(this, `span type=${type} name=${name}`);
|
|
1873
|
+
return span;
|
|
1693
1874
|
};
|
|
1694
1875
|
applyRedaction_fn = function(type, options) {
|
|
1695
1876
|
if (!options) return {};
|
|
@@ -1733,15 +1914,6 @@ applyRedaction_fn = function(type, options) {
|
|
|
1733
1914
|
return { ...rest, input, output };
|
|
1734
1915
|
};
|
|
1735
1916
|
|
|
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
1917
|
// src/client.ts
|
|
1746
1918
|
var DEFAULT_API_URL = "https://api.recount.refactor.gg";
|
|
1747
1919
|
var NOOP_RESULT2 = { traceId: "" };
|
|
@@ -1767,6 +1939,59 @@ function retryDelayMs(res, attempt) {
|
|
|
1767
1939
|
}
|
|
1768
1940
|
return BASE_RETRY_MS * 2 ** (attempt - 1);
|
|
1769
1941
|
}
|
|
1942
|
+
function bigintAsString(_key2, value) {
|
|
1943
|
+
return typeof value === "bigint" ? value.toString() : value;
|
|
1944
|
+
}
|
|
1945
|
+
function serialize(payload, log) {
|
|
1946
|
+
try {
|
|
1947
|
+
return JSON.stringify(payload);
|
|
1948
|
+
} catch {
|
|
1949
|
+
}
|
|
1950
|
+
try {
|
|
1951
|
+
return JSON.stringify(payload, bigintAsString);
|
|
1952
|
+
} catch {
|
|
1953
|
+
}
|
|
1954
|
+
const sendable = payload.spans.filter((span) => {
|
|
1955
|
+
try {
|
|
1956
|
+
JSON.stringify(span, bigintAsString);
|
|
1957
|
+
return true;
|
|
1958
|
+
} catch (err) {
|
|
1959
|
+
log(
|
|
1960
|
+
`span type=${span.type} name=${span.name} dropped (can't be sent as JSON: ${errorText(err)})`
|
|
1961
|
+
);
|
|
1962
|
+
return false;
|
|
1963
|
+
}
|
|
1964
|
+
});
|
|
1965
|
+
const spans = sendable.filter(
|
|
1966
|
+
(span) => span.type !== "group" || sendable.some((child) => child.parentOrder === span.order)
|
|
1967
|
+
);
|
|
1968
|
+
if (spans.length === 0) {
|
|
1969
|
+
log("ingest serialize error: no span can be sent as JSON");
|
|
1970
|
+
return void 0;
|
|
1971
|
+
}
|
|
1972
|
+
let error;
|
|
1973
|
+
try {
|
|
1974
|
+
return JSON.stringify({ ...payload, spans }, bigintAsString);
|
|
1975
|
+
} catch (err) {
|
|
1976
|
+
error = err;
|
|
1977
|
+
}
|
|
1978
|
+
if (payload.metadata !== void 0) {
|
|
1979
|
+
try {
|
|
1980
|
+
const body = JSON.stringify(
|
|
1981
|
+
{ ...payload, spans, metadata: void 0 },
|
|
1982
|
+
bigintAsString
|
|
1983
|
+
);
|
|
1984
|
+
log(
|
|
1985
|
+
`ingest metadata dropped (can't be sent as JSON: ${errorText(error)})`
|
|
1986
|
+
);
|
|
1987
|
+
return body;
|
|
1988
|
+
} catch (err) {
|
|
1989
|
+
error = err;
|
|
1990
|
+
}
|
|
1991
|
+
}
|
|
1992
|
+
log(`ingest serialize error: ${errorText(error)}`);
|
|
1993
|
+
return void 0;
|
|
1994
|
+
}
|
|
1770
1995
|
var _apiKey, _apiUrl, _environment2, _disabled2, _timeoutMs, _log2;
|
|
1771
1996
|
var Recount = class {
|
|
1772
1997
|
constructor(options = {}) {
|
|
@@ -1810,11 +2035,8 @@ var Recount = class {
|
|
|
1810
2035
|
return NOOP_RESULT2;
|
|
1811
2036
|
}
|
|
1812
2037
|
__privateGet(this, _log2).call(this, `ingest name=${payload.name} spans=${payload.spans.length}`);
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
body = JSON.stringify(payload);
|
|
1816
|
-
} catch (err) {
|
|
1817
|
-
__privateGet(this, _log2).call(this, `ingest serialize error: ${err instanceof Error ? err.message : String(err)}`);
|
|
2038
|
+
const body = serialize(payload, __privateGet(this, _log2));
|
|
2039
|
+
if (body === void 0) {
|
|
1818
2040
|
return NOOP_RESULT2;
|
|
1819
2041
|
}
|
|
1820
2042
|
const headers = {
|
|
@@ -1847,7 +2069,8 @@ var Recount = class {
|
|
|
1847
2069
|
}
|
|
1848
2070
|
if (!res.ok) {
|
|
1849
2071
|
const text2 = await res.text().catch(() => res.statusText);
|
|
1850
|
-
|
|
2072
|
+
const size = res.status === 413 ? ` bytes=${new TextEncoder().encode(body).length}` : "";
|
|
2073
|
+
__privateGet(this, _log2).call(this, `ingest error status=${res.status}${size} message=${text2}`);
|
|
1851
2074
|
return NOOP_RESULT2;
|
|
1852
2075
|
}
|
|
1853
2076
|
let json;
|
|
@@ -1910,8 +2133,45 @@ function cleanCellValue(value) {
|
|
|
1910
2133
|
}
|
|
1911
2134
|
return value;
|
|
1912
2135
|
}
|
|
1913
|
-
function
|
|
1914
|
-
|
|
2136
|
+
function contentOf(value) {
|
|
2137
|
+
return typeof value === "object" && value !== null && (value.__cell === true || value.__cellSpan === true) ? value.value : value;
|
|
2138
|
+
}
|
|
2139
|
+
function numberOf(value) {
|
|
2140
|
+
if (typeof value === "number") return value;
|
|
2141
|
+
if (typeof value === "string" && value.trim() !== "") return Number(value);
|
|
2142
|
+
return NaN;
|
|
2143
|
+
}
|
|
2144
|
+
function shownAs(sent) {
|
|
2145
|
+
if (typeof sent !== "object" || sent === null || sent.__cell !== true) {
|
|
2146
|
+
return void 0;
|
|
2147
|
+
}
|
|
2148
|
+
const { format, currency, unit, places } = sent;
|
|
2149
|
+
const kind = format ?? (currency ? "money" : unit !== void 0 || places !== void 0 ? "number" : void 0);
|
|
2150
|
+
if (kind === void 0 || kind === "text") return void 0;
|
|
2151
|
+
return {
|
|
2152
|
+
places: kind === "integer" ? 0 : typeof places === "number" && Number.isInteger(places) && places >= 0 ? places : 2,
|
|
2153
|
+
percent: kind === "percent" || unit === "%" && !(kind === "money" && currency)
|
|
2154
|
+
};
|
|
2155
|
+
}
|
|
2156
|
+
function showsAsZero(n, places, percent) {
|
|
2157
|
+
const text2 = decimalText(n);
|
|
2158
|
+
if (text2 === void 0) return false;
|
|
2159
|
+
let [whole, fraction = ""] = text2.replace("-", "").split(".");
|
|
2160
|
+
if (percent) {
|
|
2161
|
+
whole += fraction.slice(0, 2).padEnd(2, "0");
|
|
2162
|
+
fraction = fraction.slice(2);
|
|
2163
|
+
}
|
|
2164
|
+
return /^0*$/.test(whole) && /^0*$/.test(fraction.slice(0, places)) && (fraction[places] ?? "0") < "5";
|
|
2165
|
+
}
|
|
2166
|
+
function signOf(sent, raw, highlight) {
|
|
2167
|
+
const shown = contentOf(sent);
|
|
2168
|
+
let n = numberOf(shown);
|
|
2169
|
+
const format = shownAs(sent);
|
|
2170
|
+
if (!Number.isNaN(n) && format && showsAsZero(n, format.places, format.percent)) {
|
|
2171
|
+
n = 0;
|
|
2172
|
+
}
|
|
2173
|
+
const fallsBack = shown !== null && shown !== void 0 && (typeof shown !== "string" || /\d/.test(shown));
|
|
2174
|
+
if (Number.isNaN(n) && fallsBack) n = numberOf(contentOf(raw));
|
|
1915
2175
|
if (Number.isNaN(n)) return null;
|
|
1916
2176
|
if (n > 0) return highlight.positive ?? null;
|
|
1917
2177
|
if (n < 0) return highlight.negative ?? null;
|
|
@@ -2000,6 +2260,13 @@ var TableBuilder = class {
|
|
|
2000
2260
|
/**
|
|
2001
2261
|
* Colours a column's numbers (records) or a row (objects) by sign:
|
|
2002
2262
|
* `.signHighlight('saving', { positive: 'success', negative: 'danger' })`.
|
|
2263
|
+
* A row takes the sign of the number shown, after `transformValues()`,
|
|
2264
|
+
* `.round()` and a format's places, as Recount shows it: money, number and
|
|
2265
|
+
* percent to 2 places unless given, an integer to none, and a percent times
|
|
2266
|
+
* 100. So -0.004 as money is zero, and 0.004 as a percent (0.40%) positive.
|
|
2267
|
+
* When what's shown is text with a number in it, such as `'$-5'`, it
|
|
2268
|
+
* takes the raw value's sign; a blank row, or text with no number such as
|
|
2269
|
+
* `'N/A'`, takes none.
|
|
2003
2270
|
*/
|
|
2004
2271
|
signHighlight(key, highlight) {
|
|
2005
2272
|
this._signHighlights[key] = highlight;
|
|
@@ -2124,24 +2391,25 @@ var TableBuilder = class {
|
|
|
2124
2391
|
const order = this._pick;
|
|
2125
2392
|
rows = order.filter((k) => rows.some(([rk]) => rk === k)).map((k) => rows.find(([rk]) => rk === k));
|
|
2126
2393
|
}
|
|
2394
|
+
const sentValues = [];
|
|
2127
2395
|
const finalRows = rows.map(([k, v]) => {
|
|
2128
2396
|
const key = k;
|
|
2129
2397
|
if (this._shouldRedact(key)) {
|
|
2398
|
+
sentValues.push(REDACTED);
|
|
2130
2399
|
return [this._resolveLabel(key), REDACTED];
|
|
2131
2400
|
}
|
|
2132
2401
|
const transformed = this._valueTransforms[key] ? this._valueTransforms[key](v) : v;
|
|
2133
2402
|
const format = this._formats[key];
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
return [this._resolveLabel(key), this._finalValue(key, transformed)];
|
|
2403
|
+
const sent = format !== void 0 && isCellValue(transformed) ? cell(transformed, format) : this._finalValue(key, transformed);
|
|
2404
|
+
sentValues.push(sent);
|
|
2405
|
+
return [this._resolveLabel(key), sent];
|
|
2138
2406
|
});
|
|
2139
2407
|
const allRows = this._extraRows.length ? [...finalRows, ...this._extraRows.map((r) => r.cells)] : finalRows;
|
|
2140
2408
|
const hasSigns = Object.keys(this._signHighlights).length > 0;
|
|
2141
|
-
const rowHighlights = this._rowHighlightFn || hasSigns ? rows.map(([k, v]) => {
|
|
2409
|
+
const rowHighlights = this._rowHighlightFn || hasSigns ? rows.map(([k, v], i) => {
|
|
2142
2410
|
const fromFn = this._rowHighlightFn ? this._rowHighlightFn({ [k]: v }) : null;
|
|
2143
2411
|
const sign = this._signHighlights[k];
|
|
2144
|
-
return fromFn ?? (sign && !this._shouldRedact(k) ? signOf(v, sign) : null);
|
|
2412
|
+
return fromFn ?? (sign && !this._shouldRedact(k) ? signOf(sentValues[i], v, sign) : null);
|
|
2145
2413
|
}) : void 0;
|
|
2146
2414
|
const allRowHighlights = this._mergeRowHighlights(
|
|
2147
2415
|
rowHighlights,
|