@fixback/sdk 0.1.0 → 0.3.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.
@@ -0,0 +1,448 @@
1
+ /**
2
+ * The thin trace **breadcrumb ring buffer** and its capture instrumentation
3
+ * (spec 0003 §C; research `sentry-error-capture-findings.md` §7.5).
4
+ *
5
+ * A fixed-size FIFO buffer of the most recent activity — console, navigation,
6
+ * network metadata, masked user actions, and the failing error — that rides on a
7
+ * report as Evidence. It is trimmed exactly like Sentry (`crumbs.slice(-N)`),
8
+ * defaults to a deliberately thin `N ≈ 30` because it ships on **every** payload,
9
+ * and takes an optional age cap. A `beforeBreadcrumb(crumb) => crumb | null`
10
+ * filter lets a Project mute or edit crumbs before they enter the buffer.
11
+ *
12
+ * Everything private is kept out **at the source**: `ui.input` records that an
13
+ * input changed, never its value; `fetch`/`xhr` crumbs carry method + URL +
14
+ * status only, **never** bodies; URLs are scrubbed as the crumb is built. The
15
+ * `beforeSend` choke point (`scrub.ts`) is the final gate over the whole report.
16
+ *
17
+ * The SDK stays dependency-free and must never throw into the host page, so every
18
+ * instrumentation hook is wrapped: a capture failure is swallowed and the original
19
+ * behaviour (the real `console`, `fetch`, navigation) always runs.
20
+ */
21
+ /** Console-style severity a `console` crumb records. */
22
+ export type BreadcrumbLevel = "log" | "info" | "warn" | "error" | "assert" | "debug";
23
+ /** The kind of activity a crumb records. */
24
+ export type BreadcrumbCategory = "console" | "navigation" | "fetch" | "xhr" | "beacon" | "ui.click" | "ui.input" | "error";
25
+ /**
26
+ * Which browser API issued a captured network request (spec #122 §D, ticket #139).
27
+ * Every network crumb's `category` is one of these too, so a stream router and the
28
+ * read model agree on what is a network entry.
29
+ */
30
+ export type NetworkApi = "fetch" | "xhr" | "beacon";
31
+ /**
32
+ * A network request's failure classification (spec #122 §D, ticket #139). `ok` and
33
+ * the HTTP status classes come from a settled response; `network-error` / `timeout`
34
+ * / `aborted` from how a request failed; `opaque-cors` from a cross-origin response
35
+ * whose status is unreadable. The Network tab flags every non-`ok` outcome.
36
+ */
37
+ export type NetworkOutcome = "ok" | "http-4xx" | "http-5xx" | "network-error" | "timeout" | "aborted" | "opaque-cors";
38
+ /**
39
+ * A crumb's structured detail. Deliberately narrow: there is **no** field for a
40
+ * request/response body or an input value, so those can never be recorded.
41
+ */
42
+ export interface BreadcrumbData {
43
+ readonly url?: string;
44
+ readonly method?: string;
45
+ readonly status?: number;
46
+ /** A masked CSS selector for a `ui.*` target — never its text or value. */
47
+ readonly target?: string;
48
+ readonly from?: string;
49
+ readonly to?: string;
50
+ readonly errorType?: string;
51
+ }
52
+ /**
53
+ * The type tag on a structured console argument (spec #122 §C, decision D7). A
54
+ * console call's arguments are preserved **type-tagged** rather than flattened to a
55
+ * string, so an object/array argument can be inspected in the Console tab rather
56
+ * than read as `[object Object]`: `string`/`number`/`bool`/`null` carry the value
57
+ * directly, `json` a depth-/byte-capped JSON-safe clone, and `error` an Error's
58
+ * `{ name, message, stack }`.
59
+ */
60
+ export type ConsoleArgType = "string" | "number" | "bool" | "null" | "json" | "error";
61
+ /**
62
+ * One structured console argument (spec #122 §C): a type tag plus a JSON-safe value.
63
+ * `v` is always serializable — a `json` arg is depth-, breadth-, and string-capped at
64
+ * assembly, and exotic values (bigint, symbol, function, circular refs) are rendered
65
+ * to safe text — so a console crumb can never carry an unserializable or unbounded
66
+ * value onto the wire.
67
+ */
68
+ export interface ConsoleArg {
69
+ readonly t: ConsoleArgType;
70
+ readonly v: unknown;
71
+ }
72
+ /** A `file:line` source location (spec #122 §C) — where a console call was made. */
73
+ export interface SourceLocation {
74
+ readonly file: string;
75
+ readonly line: number;
76
+ }
77
+ /**
78
+ * One entry in a trace stream. Alongside its semantic fields every entry carries a
79
+ * stable {@link id} and a high-res monotonic {@link mono} timestamp — both stamped
80
+ * by the buffer on `add` — so entries from the three independent streams order and
81
+ * cross-link exactly (spec #122 §B, decision D9). `timestamp` stays epoch ms.
82
+ */
83
+ export interface Breadcrumb {
84
+ readonly category: BreadcrumbCategory;
85
+ readonly message?: string;
86
+ readonly level?: BreadcrumbLevel;
87
+ /** Epoch milliseconds when the crumb was recorded. */
88
+ readonly timestamp: number;
89
+ /** A stable id, unique within the buffer — assigned on `add` when not already set. */
90
+ readonly id?: string;
91
+ /** A high-res monotonic timestamp (`performance.now()`) — assigned on `add`. */
92
+ readonly mono?: number;
93
+ readonly data?: BreadcrumbData;
94
+ /**
95
+ * For a `console` crumb (spec #122 §C): the call's arguments preserved as
96
+ * structured, type-tagged values (see {@link ConsoleArg}), so the Console tab can
97
+ * render each argument expandably instead of a flattened string. `message` stays
98
+ * the one-line preview. Absent on non-console crumbs.
99
+ */
100
+ readonly args?: readonly ConsoleArg[];
101
+ /**
102
+ * For a `console` crumb (spec #122 §C): the `file:line` the call was made from,
103
+ * parsed best-effort from the call stack. Captured only for the levels that keep a
104
+ * source — `warn`/`error`/`assert` (ticket #159; see {@link SOURCE_CAPTURE_LEVELS}) —
105
+ * and only when the stack yields a usable location; a chatty app's `log`/`info`/`debug`
106
+ * omit it so the hot-page capture cost stays low. Absent on non-console crumbs.
107
+ */
108
+ readonly source?: SourceLocation;
109
+ /**
110
+ * For an auto-captured `error` crumb only (spec #122 §F): the ids of the entries
111
+ * immediately preceding the throw — a causal pointer into the same trace, so a
112
+ * machine-filed crash names its lead-up. Absent on every other crumb.
113
+ */
114
+ readonly causedBy?: readonly string[];
115
+ /**
116
+ * For a network crumb (`fetch` / `xhr` / `beacon`) — spec #122 §D, ticket #139.
117
+ * The rich request metadata the Network tab renders, lifted to the top level (the
118
+ * console-enrichment precedent) so the read model shapes each into a
119
+ * `NetworkTraceEntry`. **No field carries a request/response body or an arbitrary
120
+ * header** — `respSize` derives from the `content-length` response header only and
121
+ * `contentType` from `content-type`; nothing else is read. Absent on every other crumb.
122
+ */
123
+ readonly api?: NetworkApi;
124
+ /** The request method (network crumb) — e.g. `GET`, `POST`. */
125
+ readonly method?: string;
126
+ /** The scrubbed request URL (network crumb) — query dropped, path PII redacted. */
127
+ readonly url?: string;
128
+ /** The final HTTP status (network crumb), when one was known; absent on a network error/beacon. */
129
+ readonly status?: number;
130
+ /** The HTTP status text (network crumb), when the response carried one. */
131
+ readonly statusText?: string;
132
+ /** Wall-clock duration of the request in ms (network crumb) — a `performance.now()` delta. */
133
+ readonly durationMs?: number;
134
+ /** Request body size in bytes (network crumb) — only when trivially known (string/Blob/ArrayBuffer), never by reading a stream. */
135
+ readonly reqSize?: number;
136
+ /** Response body size in bytes (network crumb) — from the `content-length` response header only. */
137
+ readonly respSize?: number;
138
+ /** Response content type (network crumb) — the `content-type` header's media type. */
139
+ readonly contentType?: string;
140
+ /** The request's failure classification (network crumb) — see {@link NetworkOutcome}. */
141
+ readonly outcome?: NetworkOutcome;
142
+ }
143
+ /**
144
+ * The three independent trace streams (spec #122 §A, decision D6). Each is its own
145
+ * FIFO ring with its own size budget, so a chatty stream can't evict another's
146
+ * lead-up. Every {@link BreadcrumbCategory} maps to exactly one stream via
147
+ * {@link streamOf}.
148
+ */
149
+ export type TraceStream = "network" | "console" | "breadcrumbs";
150
+ /**
151
+ * Per-stream ring budgets — starting points from the grill (D6): network is the
152
+ * chattiest, breadcrumbs the sparsest. Per-project tunable, never frozen.
153
+ */
154
+ export declare const DEFAULT_STREAM_BUDGETS: Readonly<Record<TraceStream, number>>;
155
+ /**
156
+ * The shared age cap (spec #122 §A): entries older than this are pruned from every
157
+ * stream, whichever trims first. ~3 minutes and — unlike the thin buffer's
158
+ * off-by-default cap — **on** by default, so an idle tab never ships stale context.
159
+ */
160
+ export declare const DEFAULT_MAX_AGE_MS: number;
161
+ /**
162
+ * Console levels captured by default (spec #122 §C, decision D7): **all** of them, so
163
+ * the Console tab is a real console and not just an error log. Eviction priority (not
164
+ * capture) is what keeps a chatty app's `log`/`info`/`debug` from burying the signal —
165
+ * see {@link PINNED_CONSOLE_LEVELS}.
166
+ */
167
+ export declare const DEFAULT_CONSOLE_LEVELS: readonly BreadcrumbLevel[];
168
+ /**
169
+ * The console levels **pinned** against eviction (spec #122 §C, decision D7). When the
170
+ * console stream overflows its budget, `warn`/`error`/`assert` are retained ahead of
171
+ * `log`/`info`/`debug`, so an error is never evicted by a burst of chatter — the
172
+ * low-priority levels fill the remainder and are dropped first.
173
+ */
174
+ export declare const PINNED_CONSOLE_LEVELS: readonly BreadcrumbLevel[];
175
+ /**
176
+ * The console levels for which `source` (`file:line`) is captured (ticket #159).
177
+ *
178
+ * Capturing a call site constructs a `new Error()` to read its stack on **every** console
179
+ * call — a measurable hot-page cost when a chatty app logs in a tight loop (spec #122's
180
+ * "hot-page performance budget" open question, handed onward from the grill). The
181
+ * spec-sanctioned mitigation (decision D7) narrows source capture to the **levels that
182
+ * matter for debugging** — the same `warn`/`error`/`assert` that are pinned against
183
+ * eviction — so `log`/`info`/`debug` pay no per-call stack cost and simply omit `source`.
184
+ * Full-fidelity `source` is preserved exactly where it earns its keep: on warnings,
185
+ * errors, and failed assertions. Per-project tunability of this set is future work; today
186
+ * it deliberately mirrors {@link PINNED_CONSOLE_LEVELS} so "what survives eviction" and
187
+ * "what carries a call site" stay one idea.
188
+ */
189
+ export declare const SOURCE_CAPTURE_LEVELS: readonly BreadcrumbLevel[];
190
+ /**
191
+ * Which stream a crumb's category belongs to (spec #122 §A). Network APIs
192
+ * (`fetch` / `xhr` / `beacon`) form the network stream, `console` its own, and
193
+ * everything else (navigation, masked UI events, the failing error) breadcrumbs.
194
+ */
195
+ export declare function streamOf(category: string): TraceStream;
196
+ /** Filters or edits each crumb before it enters the buffer; `null` drops it. */
197
+ export type BeforeBreadcrumb = (crumb: Breadcrumb) => Breadcrumb | null;
198
+ /** Configuration for {@link createBreadcrumbBuffer}. All values are optional. */
199
+ export interface BreadcrumbBufferConfig {
200
+ /**
201
+ * Per-stream size budgets (oldest drop, independently per stream). Any stream
202
+ * omitted falls back to {@link DEFAULT_STREAM_BUDGETS}.
203
+ */
204
+ readonly budgets?: Partial<Record<TraceStream, number>>;
205
+ /**
206
+ * Shared age cap in ms: entries older than this are dropped from every stream.
207
+ * Defaults to {@link DEFAULT_MAX_AGE_MS} (on); pass `0` (or a non-positive value)
208
+ * to disable age pruning.
209
+ */
210
+ readonly maxAgeMs?: number;
211
+ /** A per-crumb filter (mute a category, edit, or drop by returning `null`). */
212
+ readonly beforeBreadcrumb?: BeforeBreadcrumb | null;
213
+ /** Epoch clock source, injectable for tests. Defaults to `Date.now`. */
214
+ readonly now?: () => number;
215
+ /** High-res monotonic clock, injectable for tests. Defaults to `performance.now`. */
216
+ readonly mono?: () => number;
217
+ /** Stable id generator, injectable for tests. Defaults to a per-buffer sequence. */
218
+ readonly nextId?: () => string;
219
+ }
220
+ /** A live trace buffer. */
221
+ export interface BreadcrumbBuffer {
222
+ /** Record a crumb (subject to `beforeBreadcrumb`, id/mono stamping, size, and age trimming). */
223
+ add(crumb: Breadcrumb): void;
224
+ /** The current crumbs, merged across streams and ordered in time — a fresh array. */
225
+ snapshot(): Breadcrumb[];
226
+ /** Drop every crumb. */
227
+ clear(): void;
228
+ }
229
+ /**
230
+ * Create the per-stream trace buffer (spec #122 §A/§B). It holds **three
231
+ * independent FIFO rings** — `network`, `console`, `breadcrumbs` — each trimmed to
232
+ * its own budget so a chatty stream never evicts another's lead-up, plus a shared
233
+ * age cap. On `add`, a crumb passes through `beforeBreadcrumb`, is stamped with a
234
+ * stable `id` and a high-res `mono` timestamp (and an epoch `timestamp` if it has
235
+ * none), routed to its stream, then that stream is pruned by age and trimmed to its
236
+ * budget. `snapshot` prunes by age again, then merges the three streams into one
237
+ * `mono`-ordered array for transport — so the wire keeps its single `trace` field.
238
+ */
239
+ export declare function createBreadcrumbBuffer(config?: BreadcrumbBufferConfig): BreadcrumbBuffer;
240
+ /**
241
+ * Longest scrubbed URL kept on a crumb (spec #122 §F): capped at assembly so an
242
+ * over-long URL is truncated, never dropped and never allowed to bloat a report.
243
+ */
244
+ export declare const MAX_URL_LENGTH = 2048;
245
+ /**
246
+ * Structured console argument caps (spec #122 §C/§F), all applied **at assembly** so a
247
+ * single console crumb can never bloat a report:
248
+ * - {@link MAX_ARG_DEPTH} bounds how deep a `json` argument is cloned (deeper nodes
249
+ * collapse to an `[Object]`/`[Array]` marker);
250
+ * - {@link MAX_ARG_ITEMS} bounds how many keys/elements are kept at each level;
251
+ * - {@link MAX_ARG_STRING_LENGTH} truncates a single over-long string value;
252
+ * - {@link MAX_CONSOLE_ARGS_BYTES} bounds the serialized size of the whole args array
253
+ * (trailing args are dropped to fit), kept well under ingest's per-entry byte cap.
254
+ */
255
+ export declare const MAX_ARG_DEPTH = 4;
256
+ export declare const MAX_ARG_ITEMS = 100;
257
+ export declare const MAX_ARG_STRING_LENGTH = 1024;
258
+ export declare const MAX_CONSOLE_ARGS_BYTES = 4096;
259
+ /** Classify one console argument into a type-tagged {@link ConsoleArg} (spec #122 §C). */
260
+ export declare function toConsoleArg(value: unknown): ConsoleArg;
261
+ /**
262
+ * A `console` crumb from a captured call's level and arguments (spec #122 §C). The
263
+ * `message` is the one-line preview (flattened, truncated); `args` preserves each
264
+ * argument as a structured, type-tagged, size-capped value so the Console tab can
265
+ * render objects/errors expandably; `source` is the best-effort `file:line`, attached
266
+ * only when the stack yielded one. Args are omitted entirely for a no-argument call.
267
+ */
268
+ export declare function consoleCrumb(level: BreadcrumbLevel, args: readonly unknown[], timestamp: number, source?: SourceLocation): Breadcrumb;
269
+ /** A `navigation` crumb; both URLs are scrubbed and length-capped as the crumb is built. */
270
+ export declare function navigationCrumb(from: string, to: string, timestamp: number): Breadcrumb;
271
+ /**
272
+ * Classify an HTTP status into a {@link NetworkOutcome} (spec #122 §D). A missing or
273
+ * `0` status is a `network-error` (a request that never got a response); otherwise
274
+ * the 4xx/5xx classes flag failures and everything else is `ok`. Used both as the
275
+ * fallback outcome and by the thin {@link fetchCrumb} / {@link xhrCrumb} builders.
276
+ */
277
+ export declare function outcomeFromStatus(status: number | undefined): NetworkOutcome;
278
+ /**
279
+ * The trivially-known byte size of a request body (spec #122 §D) — a string's UTF-8
280
+ * length, a `Blob`'s `.size`, or an `ArrayBuffer`/typed-array `.byteLength`. Anything
281
+ * that would require **reading** the body (a `ReadableStream`, `FormData`, a `Request`
282
+ * with a stream body) returns `undefined` — the "never read the body" line holds.
283
+ */
284
+ export declare function trivialBodySize(body: unknown): number | undefined;
285
+ /** Parse a `content-length` header into a non-negative byte count, or `undefined`. */
286
+ export declare function parseContentLength(value: string | null | undefined): number | undefined;
287
+ /** The media type from a `content-type` header (the part before any `;` parameters). */
288
+ export declare function contentTypeOf(value: string | null | undefined): string | undefined;
289
+ /** The rich metadata a network crumb records (spec #122 §D) — never a body or header. */
290
+ export interface NetworkCrumbInput {
291
+ readonly api: NetworkApi;
292
+ readonly method: string;
293
+ /** The raw request URL — scrubbed (query dropped, path PII redacted) as the crumb is built. */
294
+ readonly url: string;
295
+ readonly status?: number;
296
+ readonly statusText?: string;
297
+ readonly durationMs?: number;
298
+ readonly reqSize?: number;
299
+ readonly respSize?: number;
300
+ readonly contentType?: string;
301
+ readonly outcome: NetworkOutcome;
302
+ }
303
+ /**
304
+ * A network crumb (`fetch` / `xhr` / `beacon`) with the rich, metadata-only fields
305
+ * the Network tab renders (spec #122 §D, ticket #139): api, method, scrubbed URL,
306
+ * status + statusText, duration, request/response sizes, content type, and a failure
307
+ * outcome. The URL is scrubbed and length-capped at assembly. The shape has **no field
308
+ * for a request/response body or an arbitrary header**, so neither can ever be
309
+ * recorded; only a positive status, and fields that were actually supplied, ride along.
310
+ */
311
+ export declare function networkCrumb(input: NetworkCrumbInput, timestamp: number): Breadcrumb;
312
+ /**
313
+ * A thin `fetch` crumb from method + URL + status (outcome derived from the status).
314
+ * The instrumentation uses {@link networkCrumb} directly to carry the full metadata;
315
+ * this convenience builder seeds a network crumb from a status alone.
316
+ */
317
+ export declare function fetchCrumb(method: string, url: string, status: number | undefined, timestamp: number): Breadcrumb;
318
+ /** A thin `xhr` crumb from method + URL + status (outcome derived from the status). */
319
+ export declare function xhrCrumb(method: string, url: string, status: number | undefined, timestamp: number): Breadcrumb;
320
+ /** A `ui.click` crumb: a masked target selector only — no text or value. */
321
+ export declare function clickCrumb(target: Element, timestamp: number): Breadcrumb;
322
+ /**
323
+ * A `ui.input` crumb: records **that** an input changed and which field, never
324
+ * the value typed into it. The `target` element's `.value` is never read.
325
+ */
326
+ export declare function inputCrumb(target: Element, timestamp: number): Breadcrumb;
327
+ /**
328
+ * An `error` crumb for the failing exception or rejection that ends the trace. When
329
+ * `causedBy` is given (an auto-captured error), it rides on the crumb as the causal
330
+ * pointer to the ids of the entries immediately preceding the throw (spec #122 §F).
331
+ */
332
+ export declare function errorCrumb(error: unknown, timestamp: number, causedBy?: readonly string[]): Breadcrumb;
333
+ /** Detaches an installed instrumentation, restoring the original behaviour. */
334
+ export type Teardown = () => void;
335
+ type AnyFn = (...args: unknown[]) => unknown;
336
+ type ConsoleLike = Partial<Record<BreadcrumbLevel, AnyFn>>;
337
+ type FetchFn = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
338
+ interface HistoryLike {
339
+ pushState(data: unknown, unused: string, url?: string | URL | null): void;
340
+ replaceState(data: unknown, unused: string, url?: string | URL | null): void;
341
+ }
342
+ interface XhrInstance {
343
+ status: number;
344
+ statusText?: string;
345
+ open(method: string, url: string | URL, ...rest: unknown[]): void;
346
+ send(body?: unknown): void;
347
+ getResponseHeader?(name: string): string | null;
348
+ addEventListener(type: string, listener: () => void): void;
349
+ removeEventListener(type: string, listener: () => void): void;
350
+ }
351
+ interface XhrConstructor {
352
+ new (): XhrInstance;
353
+ prototype: XhrInstance;
354
+ }
355
+ /** The `navigator.sendBeacon` surface the beacon instrumentation wraps. */
356
+ export interface BeaconNavigator {
357
+ sendBeacon?: (url: string | URL, data?: BodyInit | null) => boolean;
358
+ }
359
+ /** The structural window surface the instrumentation reaches into. */
360
+ export interface InstrumentWindow {
361
+ fetch?: FetchFn;
362
+ history?: HistoryLike;
363
+ location?: {
364
+ href: string;
365
+ };
366
+ XMLHttpRequest?: XhrConstructor;
367
+ navigator?: BeaconNavigator;
368
+ addEventListener(type: string, listener: (event: Event) => void, options?: boolean | AddEventListenerOptions): void;
369
+ removeEventListener(type: string, listener: (event: Event) => void, options?: boolean | EventListenerOptions): void;
370
+ }
371
+ /** Options for {@link instrumentBreadcrumbs} and the individual installers. */
372
+ export interface InstrumentOptions {
373
+ readonly win?: InstrumentWindow;
374
+ readonly doc?: Document;
375
+ readonly consoleObj?: ConsoleLike;
376
+ readonly consoleLevels?: readonly BreadcrumbLevel[];
377
+ /** Skip URLs (e.g. the SDK's own ingest calls) so they never become crumbs. */
378
+ readonly ignoreUrl?: (url: string) => boolean;
379
+ readonly now?: () => number;
380
+ /**
381
+ * High-res monotonic clock (spec #122 §D) — used to measure a request's duration as
382
+ * a delta around the wrapped call. Defaults to `performance.now`; injectable for tests.
383
+ */
384
+ readonly mono?: () => number;
385
+ /**
386
+ * Whether to instrument the **console** stream (spec #122 §L; ticket #138).
387
+ * Defaults to `true`; pass `false` and `console` is never wrapped, so no console
388
+ * entry is ever recorded — the per-project toggle stops the stream at its source.
389
+ */
390
+ readonly captureConsole?: boolean;
391
+ /**
392
+ * Whether to instrument the **network** stream — `fetch` and `XHR` (spec #122 §L;
393
+ * ticket #138). Defaults to `true`; pass `false` and neither is wrapped, so no
394
+ * network entry is ever recorded. Navigation and masked UI events are unaffected.
395
+ */
396
+ readonly captureNetwork?: boolean;
397
+ }
398
+ /**
399
+ * The best-effort `file:line` a console call was made from (spec #122 §C). Parses the
400
+ * frames of a stack, skipping `skipFrames` leading (SDK-internal) frames so the source
401
+ * points at the host code that called `console.*`. Returns `undefined` when no frame
402
+ * yields a usable location — the crumb then simply omits `source`.
403
+ */
404
+ export declare function sourceFromStack(stack: string | undefined, skipFrames?: number): SourceLocation | undefined;
405
+ /** Wrap `console` methods so calls at the captured levels become crumbs. */
406
+ export declare function instrumentConsole(buffer: BreadcrumbBuffer, consoleObj: ConsoleLike, levels: readonly BreadcrumbLevel[], now: () => number): Teardown;
407
+ /**
408
+ * Wrap `fetch` to record a rich network crumb on settlement (spec #122 §D, ticket
409
+ * #139): method, scrubbed URL, status + statusText, a `performance.now()` duration,
410
+ * request size (only when the body is trivially sized), response size (from the
411
+ * `content-length` header only), content type, and a failure outcome. The **original**
412
+ * outcome is returned untouched — the response is passed through **without its body
413
+ * being read** (only two metadata headers are inspected), and a rejection is re-thrown
414
+ * so the caller's `unhandledrejection` semantics are preserved.
415
+ */
416
+ export declare function instrumentFetch(buffer: BreadcrumbBuffer, win: InstrumentWindow, ignoreUrl: (url: string) => boolean, now: () => number, mono?: () => number): Teardown;
417
+ /**
418
+ * Patch `XMLHttpRequest` to record a rich network crumb when a request settles (spec
419
+ * #122 §D, ticket #139): method, scrubbed URL, status + statusText, a `performance.now()`
420
+ * duration, request size (only when the body is trivially sized), response size (from
421
+ * the `content-length` header only), content type, and a failure outcome distinguished
422
+ * by which terminal event fired (`error` / `timeout` / `abort` / `load`). The `send`
423
+ * body argument is **never read for content** — only its trivially-known size — so a body
424
+ * can never reach the buffer.
425
+ */
426
+ export declare function instrumentXhr(buffer: BreadcrumbBuffer, win: InstrumentWindow, ignoreUrl: (url: string) => boolean, now: () => number, mono?: () => number): Teardown;
427
+ /**
428
+ * Wrap `navigator.sendBeacon` to record a network crumb for each beacon (spec #122 §D,
429
+ * ticket #139). `sendBeacon` is fire-and-forget with a **synchronous boolean** return —
430
+ * `true` when the user agent queued the beacon, `false` when it declined — so wrapping
431
+ * must return that boolean unchanged: the original is called first and its result both
432
+ * classifies the crumb (`ok` / `network-error`) and is returned to the caller. Only the
433
+ * URL and the trivially-known request size are recorded (no response exists to read);
434
+ * capture failure is swallowed and the boolean still returned, and if the original itself
435
+ * throws we record best-effort and re-throw so the contract is preserved exactly.
436
+ */
437
+ export declare function instrumentBeacon(buffer: BreadcrumbBuffer, win: InstrumentWindow, ignoreUrl: (url: string) => boolean, now: () => number): Teardown;
438
+ /** Record a `navigation` crumb on `pushState`/`replaceState`/pop/hash changes. */
439
+ export declare function instrumentNavigation(buffer: BreadcrumbBuffer, win: InstrumentWindow, now: () => number): Teardown;
440
+ /** Listen (capture-phase) for clicks and input changes as masked crumbs. */
441
+ export declare function instrumentUiEvents(buffer: BreadcrumbBuffer, doc: Document, now: () => number): Teardown;
442
+ /**
443
+ * Install every capture hook onto a window/document and return a single teardown
444
+ * that removes them all. Each hook is independent and defensive: a failure in one
445
+ * never blocks the others, and none can throw into the host page.
446
+ */
447
+ export declare function instrumentBreadcrumbs(buffer: BreadcrumbBuffer, options?: InstrumentOptions): Teardown;
448
+ export {};
package/dist/dom.d.ts CHANGED
@@ -10,6 +10,13 @@
10
10
  */
11
11
  /** Marker attributes on the SDK's own host elements in the light DOM. */
12
12
  export declare const FIXBACK_HOST_MARKERS: readonly ["data-fixback-root", "data-fixback-overlay"];
13
+ /**
14
+ * Is this node itself one of the SDK's own host elements (the launcher / overlay
15
+ * roots)? A self-only check — the screenshot's clone walk excludes a node's whole
16
+ * subtree once the host is matched, so catching the host is enough to keep all of
17
+ * Fixback's chrome (light DOM and its shadow tree) out of a capture.
18
+ */
19
+ export declare function isFixbackHostElement(node: Node | null | undefined): boolean;
13
20
  /**
14
21
  * Is this node part of the SDK's own UI? Walks up parents and out through any
15
22
  * Shadow DOM boundary (via `getRootNode().host`), so a click inside the overlay's
@@ -0,0 +1,51 @@
1
+ /**
2
+ * The draw surface — arrow / box / pen / text marking over the captured frame
3
+ * (spec 0003 §B, the Reporter prototype's draw toolbar). It is the interactive
4
+ * editor behind the overlay's draw mode: a tool, a live preview, a committed list
5
+ * of {@link Mark}s with undo, and an SVG it renders into. Marks are recorded in
6
+ * **screenshot (viewport) coordinates** so they compose over the full masked
7
+ * screenshot; the overlay reads {@link DrawSurface.marks} when the Reporter
8
+ * attaches.
9
+ *
10
+ * Pointer listeners are capture-phase on the document, so a stroke is intercepted
11
+ * before the host page; a stroke that begins on interactive chrome (the toolbar,
12
+ * the label input) is ignored so the toolbar stays clickable. The surface draws
13
+ * nothing into the host page — only into the SVG the overlay hands it inside its
14
+ * own Shadow DOM.
15
+ */
16
+ import { type DrawTool, type Mark } from "./annotation";
17
+ /** Options for {@link startDrawSurface}. */
18
+ export interface DrawSurfaceOptions {
19
+ /** Document to attach to. Defaults to the global `document`. */
20
+ readonly doc?: Document;
21
+ /** The (viewport-filling) SVG the surface renders committed marks + preview into. */
22
+ readonly svg: SVGSVGElement;
23
+ /** The inline label input the text tool shows, positions, and reads. */
24
+ readonly textInput: HTMLInputElement;
25
+ /** Mark colour. Defaults to {@link MARK_COLOR}. */
26
+ readonly color?: string;
27
+ /** Marks to seed from, so re-entering draw continues an existing annotation. */
28
+ readonly initialMarks?: ReadonlyArray<Mark>;
29
+ /** Notified with a fresh snapshot whenever the committed marks change. */
30
+ readonly onChange?: (marks: ReadonlyArray<Mark>) => void;
31
+ }
32
+ /** A running draw surface. */
33
+ export interface DrawSurface {
34
+ /** Switch the active tool. */
35
+ setTool(tool: DrawTool): void;
36
+ /** The active tool. */
37
+ getTool(): DrawTool;
38
+ /** Remove the most recently committed mark. */
39
+ undo(): void;
40
+ /** A snapshot of the committed marks, in screenshot coordinates. */
41
+ marks(): ReadonlyArray<Mark>;
42
+ /** Detach all listeners and hide the label input. */
43
+ stop(): void;
44
+ }
45
+ /**
46
+ * Begin drawing. The returned surface tracks a tool, a live preview stroke, and the
47
+ * committed marks; `marks()` snapshots them for the overlay's Send assembly. Text
48
+ * labels are entered through the provided input (Enter commits, Escape/blur-empty
49
+ * discards). `stop()` detaches everything.
50
+ */
51
+ export declare function startDrawSurface(options: DrawSurfaceOptions): DrawSurface;
@@ -0,0 +1,135 @@
1
+ /**
2
+ * Automatic error capture — the SDK's signature capability: **errors report
3
+ * themselves, no prompt** (spec 0003 §E, ADR-0011; research
4
+ * `sentry-error-capture-findings.md` §7.1–7.3, §7.6).
5
+ *
6
+ * Exactly **two capture-phase listeners** (`window` `error` +
7
+ * `unhandledrejection`) turn uncaught exceptions and unhandled rejections into
8
+ * `source: auto`, `Kind = bug` Feedback for the current session's Reporter — no
9
+ * native-API monkeypatching, no library. Each firing is deduped by a per-session
10
+ * fingerprint, rate-limited by a token-bucket burst limiter and a per-session cap,
11
+ * scrubbed through the same `beforeSend` choke point as manual reports (§C), and
12
+ * shipped through the same transport (which honours ingest's `429` / `Retry-After`
13
+ * backpressure, ticket #89). `console.error` is **not** promoted — it stays
14
+ * breadcrumb-only Evidence.
15
+ *
16
+ * Per-firing order (research §7.1): **`canSubmit`/Gate → dedup → rate-limit/cap →
17
+ * `beforeSend` scrub → enqueue Feedback**. The Gate is honoured by construction:
18
+ * `init` installs this only when boot returned `canSubmit`, so an auto-error is
19
+ * never filed where a manual report would be refused, and it inherits the session
20
+ * Reporter's server-derived tier.
21
+ *
22
+ * The whole module is defensive — every handler is wrapped so a Fixback problem
23
+ * (or an error thrown while capturing an error) never surfaces on the host page.
24
+ * The fingerprint/`normalize` shape and the limiter numbers are **starting points**
25
+ * from research (ticket #92), exposed as config — tunable, not frozen.
26
+ */
27
+ import type { IdentityInputs } from "./boot";
28
+ import { type BreadcrumbBuffer, type Teardown } from "./breadcrumbs";
29
+ import { type BeforeSend } from "./scrub";
30
+ import { type Capture, type CaptureOptions } from "./screenshot";
31
+ import { type SubmitInput, type SubmitResult } from "./submit";
32
+ export type { Teardown };
33
+ /** Burst limiter capacity — how many auto-reports may fire back-to-back (§E). */
34
+ export declare const DEFAULT_BURST_CAPACITY = 5;
35
+ /** Burst limiter refill — one token returns every this-many ms (§E). */
36
+ export declare const DEFAULT_BURST_REFILL_MS = 2000;
37
+ /** Per-session ceiling on distinct auto-Feedback; beyond it, only a dropped-count (§E). */
38
+ export declare const DEFAULT_MAX_DISTINCT_AUTO = 20;
39
+ /**
40
+ * Collapse the volatile parts of an error message so a changing string doesn't
41
+ * split one bug: UUIDs, URLs, `0x…` and long hex runs, and long digit runs are
42
+ * replaced with stable placeholders. Short numbers and stable text are kept so
43
+ * genuinely distinct bugs stay distinct. A small, dependency-free regex set —
44
+ * tunable per ticket #92, never a frozen magic set.
45
+ */
46
+ export declare function normalize(value: string): string;
47
+ /**
48
+ * A dependency-free FNV-1a hash rendered in base-36. It only has to be stable and
49
+ * well-distributed within one session (the client key is a flood guard; the server
50
+ * does canonical cross-session clustering), so a non-cryptographic hash is right.
51
+ */
52
+ export declare function hashString(input: string): string;
53
+ /**
54
+ * Extract a compact, stable signature of the top in-app frames of a stack: up to
55
+ * {@link FINGERPRINT_FRAME_LIMIT} frames as `function@basename:line:col`, origin
56
+ * and cache-busting query stripped so a per-deploy asset hash doesn't matter within
57
+ * a session. Returns `""` when there is no usable stack (message-only fallback).
58
+ */
59
+ export declare function extractTopFrames(stack: string | undefined, limit?: number): string;
60
+ /**
61
+ * The per-session fingerprint (research §7.2):
62
+ * `hash(errorType + "|" + normalize(value) + "|" + topFrames)`. Stack frames
63
+ * dominate when present; otherwise it falls back to type + normalized value.
64
+ */
65
+ export declare function computeFingerprint(type: string, value: string, stack?: string): string;
66
+ /** Configuration for {@link TokenBucket}. */
67
+ export interface TokenBucketOptions {
68
+ readonly capacity: number;
69
+ readonly refillIntervalMs: number;
70
+ /** Clock source, injectable for tests. Defaults to `Date.now`. */
71
+ readonly now?: () => number;
72
+ }
73
+ /**
74
+ * A token bucket: starts full at `capacity`, refills one token every
75
+ * `refillIntervalMs`, and refuses (`take() === false`) when empty. So a fast error
76
+ * loop that dodges dedup with distinct fingerprints still can't machine-gun ingest.
77
+ * The clock is injectable so the window is unit-tested deterministically.
78
+ */
79
+ export declare class TokenBucket {
80
+ private readonly options;
81
+ private tokens;
82
+ private last;
83
+ private readonly now;
84
+ constructor(options: TokenBucketOptions);
85
+ /** Consume a token if one is available (refilling first), else refuse. */
86
+ take(): boolean;
87
+ }
88
+ /** Injectable collaborators, defaulted to the real implementations. */
89
+ export interface AutoCaptureDeps {
90
+ readonly captureView: (options?: CaptureOptions) => Promise<Capture | null>;
91
+ readonly submitReport: (apiUrl: string, input: SubmitInput, fetchImpl?: typeof fetch) => Promise<SubmitResult>;
92
+ }
93
+ /** Configuration for {@link installErrorCapture}. */
94
+ export interface AutoCaptureConfig {
95
+ readonly apiUrl: string;
96
+ readonly key: string;
97
+ readonly identity?: IdentityInputs;
98
+ /** The window whose global handlers are installed. Defaults to `window`. */
99
+ readonly win?: Window;
100
+ /** The document used for capture + environment. Defaults to the window's. */
101
+ readonly doc?: Document;
102
+ readonly sdkVersion?: string;
103
+ /** The shared trace buffer; the failing error is added to it before filing. */
104
+ readonly buffer?: BreadcrumbBuffer | null;
105
+ /** Per-project client scrub hook, run at the `beforeSend` choke point (§C). */
106
+ readonly beforeSend?: BeforeSend;
107
+ /** Run the built-in default scrubbers. Defaults to `true` (private-by-default). */
108
+ readonly scrub?: boolean;
109
+ readonly deps?: Partial<AutoCaptureDeps>;
110
+ /** Burst limiter capacity. Defaults to {@link DEFAULT_BURST_CAPACITY}. */
111
+ readonly burstCapacity?: number;
112
+ /** Burst limiter refill interval (ms). Defaults to {@link DEFAULT_BURST_REFILL_MS}. */
113
+ readonly burstRefillMs?: number;
114
+ /** Per-session distinct-Feedback cap. Defaults to {@link DEFAULT_MAX_DISTINCT_AUTO}. */
115
+ readonly maxDistinct?: number;
116
+ /** Clock source, injectable for tests. Defaults to `Date.now`. */
117
+ readonly now?: () => number;
118
+ }
119
+ /** A running auto-capture: its teardown, plus the local dropped-count (spec §E). */
120
+ export interface AutoCaptureHandle {
121
+ /** Remove the two global handlers and the flush hooks. Safe to call repeatedly. */
122
+ readonly destroy: Teardown;
123
+ /**
124
+ * Distinct auto-Feedback dropped by the burst limiter or the per-session cap —
125
+ * the "keep only a local dropped-count" fallback beyond the guardrails (spec §E).
126
+ */
127
+ droppedCount(): number;
128
+ }
129
+ /**
130
+ * Install automatic error capture on a window and return a handle whose `destroy`
131
+ * removes every listener it added (the two global handlers plus the flush hooks).
132
+ * The caller (`init`) installs this only after boot returned `canSubmit`, so the
133
+ * Gate is respected and the auto-error inherits the session Reporter's tier.
134
+ */
135
+ export declare function installErrorCapture(config: AutoCaptureConfig): AutoCaptureHandle;