@ultimat3/core 31.23.0 → 31.25.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/CLAUDE.md +5 -2
- package/README.md +15 -3
- package/package.json +2 -2
- package/src/adapter.ts +2 -0
- package/src/boot-warning.ts +142 -0
- package/src/core-error-codes.ts +2 -0
- package/src/define-error.ts +34 -1
- package/src/error-reporter-sentry.ts +10 -3
- package/src/exports/observability.ts +3 -1
- package/src/failure-capture-mask.ts +3 -0
- package/src/index.ts +30 -3
- package/src/logger-redact.ts +23 -3
- package/src/logger.ts +6 -2
- package/src/map-bounded.ts +49 -20
- package/src/metrics-text.ts +2 -1
- package/src/otlp.ts +4 -0
- package/src/outage-log.ts +94 -3
- package/src/outbound-errors.ts +29 -6
- package/src/outbound-hops.ts +12 -4
- package/src/outbound-proxy.ts +72 -0
- package/src/outbound-subpath.ts +3 -0
- package/src/outbound-types.ts +34 -1
- package/src/outbound.ts +1 -0
- package/src/problem-meta-declared.ts +68 -0
- package/src/redact-values.ts +131 -0
- package/src/telemetry.ts +4 -2
- package/src/type-pins.ts +15 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// Single responsibility: redaction by VALUE — the app's registered `{ pattern, replace }` matchers,
|
|
2
|
+
// the screen each passes at registration, and the one rewrite every sink applies to a string leaf.
|
|
3
|
+
// Mechanism only: the framework registers no pattern. `logger-redact.ts` owns the public door
|
|
4
|
+
// (`redactValues`) so the redaction generation stays in one place.
|
|
5
|
+
|
|
6
|
+
import { type CodedErrorInit, UltimateError } from './errors';
|
|
7
|
+
|
|
8
|
+
/** One value matcher: every match of `pattern` (which carries `g`) becomes `replace`. */
|
|
9
|
+
export interface RedactValueMatcher {
|
|
10
|
+
readonly pattern: RegExp;
|
|
11
|
+
/** `String.prototype.replace`'s replacement: `$1`, `$&` and friends work. */
|
|
12
|
+
readonly replace: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class RedactPatternUnsafeError extends UltimateError {
|
|
16
|
+
static readonly code = 'X_REDACT_PATTERN_UNSAFE';
|
|
17
|
+
override readonly name = 'RedactPatternUnsafeError';
|
|
18
|
+
constructor(init: CodedErrorInit) {
|
|
19
|
+
super({ ...init, code: RedactPatternUnsafeError.code });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const matchers: RedactValueMatcher[] = [];
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A REPEATING quantifier at `index`: `*`, `+`, or a brace count whose upper bound exceeds one —
|
|
27
|
+
* `{2}`, `{2,}`, `{2,40}`. A bounded count is no defence: `(a+){2,40}b` took 2 s on 44 characters.
|
|
28
|
+
* Only `?`, `{0,1}` and `{1}` are non-repeating.
|
|
29
|
+
*/
|
|
30
|
+
function repeatingAt(source: string, index: number): boolean {
|
|
31
|
+
const char = source[index];
|
|
32
|
+
if (char === '*' || char === '+') return true;
|
|
33
|
+
if (char !== '{') return false;
|
|
34
|
+
const count = /^\{(\d+)(,(\d*))?\}/.exec(source.slice(index));
|
|
35
|
+
if (count === null) return false;
|
|
36
|
+
const [, low, comma, high] = count;
|
|
37
|
+
const upper = comma === undefined ? Number(low) : high === '' ? Infinity : Number(high);
|
|
38
|
+
return upper > 1;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Whether a repeated group itself contains a repeating quantifier — `(a+)+`, `(\w+\s?)*`,
|
|
43
|
+
* `((ab)*c)+`, `(a+){2,40}`: the shape that backtracks exponentially on a near-miss. A scan, not a
|
|
44
|
+
* proof: overlapping alternation (`(a|a)*`) passes. It refuses the obvious, at registration, where
|
|
45
|
+
* the alternative was a log line that never finishes rendering.
|
|
46
|
+
*/
|
|
47
|
+
export function hasNestedQuantifier(source: string): boolean {
|
|
48
|
+
const groups: boolean[] = [];
|
|
49
|
+
let inClass = false;
|
|
50
|
+
let closedQuantified = false;
|
|
51
|
+
for (let index = 0; index < source.length; index += 1) {
|
|
52
|
+
const char = source[index];
|
|
53
|
+
if (char === '\\') {
|
|
54
|
+
index += 1;
|
|
55
|
+
closedQuantified = false;
|
|
56
|
+
} else if (inClass) {
|
|
57
|
+
if (char === ']') inClass = false;
|
|
58
|
+
} else if (char === '[') {
|
|
59
|
+
inClass = true;
|
|
60
|
+
closedQuantified = false;
|
|
61
|
+
} else if (char === '(') {
|
|
62
|
+
groups.push(false);
|
|
63
|
+
closedQuantified = false;
|
|
64
|
+
} else if (char === ')') {
|
|
65
|
+
closedQuantified = groups.pop() ?? false;
|
|
66
|
+
if (closedQuantified && groups.length > 0) groups[groups.length - 1] = true;
|
|
67
|
+
} else if (repeatingAt(source, index)) {
|
|
68
|
+
if (closedQuantified) return true;
|
|
69
|
+
if (groups.length > 0) groups[groups.length - 1] = true;
|
|
70
|
+
} else {
|
|
71
|
+
closedQuantified = false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Why `matcher` cannot be registered, or `undefined` when it can. */
|
|
78
|
+
function refusal(matcher: RedactValueMatcher): string | undefined {
|
|
79
|
+
const { pattern } = matcher;
|
|
80
|
+
if (!pattern.flags.includes('g')) return 'has no g flag, so only its first match is replaced';
|
|
81
|
+
if (pattern.flags.includes('y')) return 'is sticky (y), so it matches only at a value’s start';
|
|
82
|
+
if (hasNestedQuantifier(pattern.source)) {
|
|
83
|
+
return 'repeats a group that holds a repeating quantifier, which backtracks exponentially';
|
|
84
|
+
}
|
|
85
|
+
if (new RegExp(pattern.source, pattern.flags.replace('g', '')).test('')) {
|
|
86
|
+
return 'matches the empty string, so its replacement lands between every character';
|
|
87
|
+
}
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function fixFor(pattern: RegExp): string {
|
|
92
|
+
const flags = pattern.flags.replace('y', '');
|
|
93
|
+
const withG = flags.includes('g') ? flags : `${flags}g`;
|
|
94
|
+
return `in the redactValues([...]) call, use /${pattern.source}/${withG} with no group that is quantified and holds a quantifier (write (?:a+)+ as a+), and require at least one character`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Screen every matcher, then add them all — one refusal registers none of the list. */
|
|
98
|
+
export function addValueMatchers(list: readonly RedactValueMatcher[]): void {
|
|
99
|
+
for (const matcher of list) {
|
|
100
|
+
const why = refusal(matcher);
|
|
101
|
+
if (why === undefined) continue;
|
|
102
|
+
throw new RedactPatternUnsafeError({
|
|
103
|
+
cause: `the redaction pattern /${matcher.pattern.source}/${matcher.pattern.flags} ${why}`,
|
|
104
|
+
fix: fixFor(matcher.pattern),
|
|
105
|
+
meta: { pattern: matcher.pattern.source, flags: matcher.pattern.flags },
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
matchers.push(...list);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** `text` with every registered match replaced. No matcher registered: `text`, at one compare. */
|
|
112
|
+
export function redactedValue(text: string): string {
|
|
113
|
+
if (matchers.length === 0) return text;
|
|
114
|
+
let out = text;
|
|
115
|
+
for (const { pattern, replace } of matchers) out = out.replace(pattern, replace);
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Every string leaf of an already JSON-safe value (an error's `toJSON()`), rewritten. */
|
|
120
|
+
export function redactedStrings(value: unknown, depth = 0): unknown {
|
|
121
|
+
if (matchers.length === 0) return value;
|
|
122
|
+
if (typeof value === 'string') return redactedValue(value);
|
|
123
|
+
if (value === null || typeof value !== 'object') return value;
|
|
124
|
+
// Cut, never passed through: a string below the limit would leave unredacted. Same marker and
|
|
125
|
+
// the same limit as `serialise` (`logger-redact.ts`).
|
|
126
|
+
if (depth >= 6) return '[depth-limit]';
|
|
127
|
+
if (Array.isArray(value)) return value.map((item) => redactedStrings(item, depth + 1));
|
|
128
|
+
const out: Record<string, unknown> = {};
|
|
129
|
+
for (const [key, entry] of Object.entries(value)) out[key] = redactedStrings(entry, depth + 1);
|
|
130
|
+
return out;
|
|
131
|
+
}
|
package/src/telemetry.ts
CHANGED
|
@@ -11,6 +11,7 @@ import { isUltimateError } from './errors';
|
|
|
11
11
|
import { isSpanId, isTraceId, spanId as newSpanId, traceId as newTraceId } from './ids';
|
|
12
12
|
import { instrument, subscribed } from './instrument';
|
|
13
13
|
import { installTraceHeaders } from './outbound-headers';
|
|
14
|
+
import { redactedValue } from './redact-values';
|
|
14
15
|
import { defaultSampler, resetDefaultSampler, type Sampler } from './sampler';
|
|
15
16
|
|
|
16
17
|
export type SpanKind = 'internal' | 'server' | 'client' | 'producer' | 'consumer';
|
|
@@ -309,7 +310,8 @@ export function startSpan(initialName: string, options?: StartSpanOptions): Span
|
|
|
309
310
|
// `withSpan`'s catch — around `cache.invalidate`, `db.<verb>` and every HTTP and job span.
|
|
310
311
|
// A throw here substitutes the tracer's own TypeError for the caller's real failure and
|
|
311
312
|
// leaves the span it was annotating unended.
|
|
312
|
-
|
|
313
|
+
// Redacted by value: the event and the status leave the box through every OTLP exporter.
|
|
314
|
+
const message = redactedValue(renderThrowable(error));
|
|
313
315
|
if (sampled) {
|
|
314
316
|
pushEvent({
|
|
315
317
|
name: 'exception',
|
|
@@ -321,7 +323,7 @@ export function startSpan(initialName: string, options?: StartSpanOptions): Span
|
|
|
321
323
|
return span;
|
|
322
324
|
},
|
|
323
325
|
setStatus(code, message) {
|
|
324
|
-
status = { code, message };
|
|
326
|
+
status = { code, message: message === undefined ? message : redactedValue(message) };
|
|
325
327
|
return span;
|
|
326
328
|
},
|
|
327
329
|
updateName(next) {
|
package/src/type-pins.ts
CHANGED
|
@@ -18,6 +18,7 @@ import type { CtxPatch } from './context';
|
|
|
18
18
|
import type { Page } from './cursor-page';
|
|
19
19
|
import type { ErrorReporter, ErrorReportingOptions } from './error-reporter';
|
|
20
20
|
import type { SentryReporterHooks, SentryReporterOptions } from './error-reporter-sentry-hooks';
|
|
21
|
+
import { mapBounded } from './map-bounded';
|
|
21
22
|
import type { HydrateStrategy, OfflineStrategy, RenderMode, Surface } from './route-vocabulary';
|
|
22
23
|
|
|
23
24
|
/** Fails to compile when `T` is anything but `true`. The whole mechanism. */
|
|
@@ -282,3 +283,17 @@ export const _HooksSpreadIntoOptions = (hooks: SentryReporterHooks): SentryRepor
|
|
|
282
283
|
dsn: 'https://public@o0.ingest.sentry.io/0',
|
|
283
284
|
...hooks,
|
|
284
285
|
});
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* `mapBounded`'s two answers, by `settle`: the default is `R[]` (fail-fast), `settle: 'all'` is
|
|
289
|
+
* `PromiseSettledResult<R>[]`. Swapped overloads, or either widened to the union, would hand a
|
|
290
|
+
* fail-fast caller outcome objects it never unwraps. Real calls, so overload resolution is pinned.
|
|
291
|
+
*/
|
|
292
|
+
const settleAll = (items: readonly string[]) =>
|
|
293
|
+
mapBounded(items, (item) => item.length, { width: 2, settle: 'all' });
|
|
294
|
+
const settleFirst = (items: readonly string[]) =>
|
|
295
|
+
mapBounded(items, (item) => item.length, { width: 2 });
|
|
296
|
+
type _SettleAllAnswersOutcomes = Assert<
|
|
297
|
+
Exact<Awaited<ReturnType<typeof settleAll>>, PromiseSettledResult<number>[]>
|
|
298
|
+
>;
|
|
299
|
+
type _DefaultAnswersValues = Assert<Exact<Awaited<ReturnType<typeof settleFirst>>, number[]>>;
|