catchfn 0.0.1 → 0.1.1
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 +313 -278
- package/dist/index.cjs +1736 -635
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +501 -388
- package/dist/index.d.ts +501 -388
- package/dist/index.js +1736 -616
- package/dist/index.js.map +1 -1
- package/package.json +37 -25
package/README.md
CHANGED
|
@@ -1,280 +1,315 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
environment: "production",
|
|
33
|
-
enableBreadcrumbs: true,
|
|
34
|
-
enableConsoleCapture: true,
|
|
35
|
-
enableNetworkCapture: false,
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
// Add breadcrumbs manually
|
|
39
|
-
catcher.addBreadcrumb({
|
|
40
|
-
type: "navigation",
|
|
41
|
-
message: "User navigated to dashboard",
|
|
42
|
-
data: { from: "/home", to: "/dashboard" },
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
// Capture a bug report
|
|
46
|
-
const report = await catcher.captureBug({
|
|
47
|
-
title: "Payment failed",
|
|
48
|
-
description: "User encountered error during checkout",
|
|
49
|
-
severity: "high",
|
|
50
|
-
tags: ["payment", "checkout"],
|
|
51
|
-
user: {
|
|
52
|
-
id: "user-123",
|
|
53
|
-
email: "user@example.com",
|
|
54
|
-
},
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
console.log("Bug report ID:", report.id);
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
## Configuration
|
|
61
|
-
|
|
62
|
-
```typescript
|
|
63
|
-
interface CatchFnConfig {
|
|
64
|
-
// Required
|
|
65
|
-
name: string;
|
|
66
|
-
|
|
67
|
-
// Breadcrumbs
|
|
68
|
-
enableBreadcrumbs?: boolean; // default: true
|
|
69
|
-
maxBreadcrumbs?: number; // default: 50
|
|
70
|
-
|
|
71
|
-
// Console capture
|
|
72
|
-
enableConsoleCapture?: boolean; // default: false
|
|
73
|
-
maxConsoleLogs?: number; // default: 100
|
|
74
|
-
|
|
75
|
-
// Network capture
|
|
76
|
-
enableNetworkCapture?: boolean; // default: false
|
|
77
|
-
maxNetworkRequests?: number; // default: 20
|
|
78
|
-
|
|
79
|
-
// Bug settings
|
|
80
|
-
defaultSeverity?: "low" | "medium" | "high" | "critical"; // default: "medium"
|
|
81
|
-
environment?: string;
|
|
82
|
-
release?: string;
|
|
83
|
-
user?: { id?: string; email?: string; [key: string]: any };
|
|
84
|
-
|
|
85
|
-
// Hooks and filters
|
|
86
|
-
beforeSend?: (event: BugReportEvent) => BugReportEvent | null;
|
|
87
|
-
ignorePatterns?: (string | RegExp)[];
|
|
88
|
-
|
|
89
|
-
// Size limits
|
|
90
|
-
maxPayloadSize?: number; // default: 262144 (256KB)
|
|
91
|
-
sensitiveFields?: string[]; // Additional fields to redact
|
|
92
|
-
}
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
## Usage Examples
|
|
96
|
-
|
|
97
|
-
### Basic Bug Capture
|
|
98
|
-
|
|
99
|
-
```typescript
|
|
100
|
-
await catcher.captureBug({
|
|
101
|
-
title: "API request failed",
|
|
102
|
-
description: "Failed to fetch user data",
|
|
103
|
-
severity: "medium",
|
|
104
|
-
tags: ["api", "network"],
|
|
105
|
-
});
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
### With User Context
|
|
109
|
-
|
|
110
|
-
```typescript
|
|
111
|
-
await catcher.captureBug({
|
|
112
|
-
title: "Feature access denied",
|
|
113
|
-
description: "User tried to access premium feature",
|
|
114
|
-
severity: "low",
|
|
115
|
-
user: {
|
|
116
|
-
id: "user-123",
|
|
117
|
-
email: "user@example.com",
|
|
118
|
-
plan: "free",
|
|
119
|
-
},
|
|
120
|
-
metadata: {
|
|
121
|
-
feature: "analytics-dashboard",
|
|
122
|
-
requestedAt: new Date().toISOString(),
|
|
123
|
-
},
|
|
124
|
-
});
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
### Using beforeSend Hook
|
|
128
|
-
|
|
129
|
-
```typescript
|
|
130
|
-
const catcher = catchFn({
|
|
131
|
-
name: "my-app",
|
|
132
|
-
beforeSend: (event) => {
|
|
133
|
-
// Add custom data
|
|
134
|
-
event.metadata = {
|
|
135
|
-
...event.metadata,
|
|
136
|
-
buildNumber: "1.2.3",
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
// Filter internal users
|
|
140
|
-
if (event.user?.email?.endsWith("@company.com")) {
|
|
141
|
-
return null; // Drop the event
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
return event;
|
|
145
|
-
},
|
|
146
|
-
});
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
### Ignore Patterns
|
|
150
|
-
|
|
151
|
-
```typescript
|
|
152
|
-
const catcher = catchFn({
|
|
153
|
-
name: "my-app",
|
|
154
|
-
ignorePatterns: ["ResizeObserver loop", /network.*timeout/i],
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
// This will be ignored
|
|
158
|
-
await catcher.captureBug({
|
|
159
|
-
title: "ResizeObserver loop limit exceeded",
|
|
160
|
-
description: "Browser warning",
|
|
161
|
-
});
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
### Manual Breadcrumbs
|
|
165
|
-
|
|
166
|
-
```typescript
|
|
167
|
-
// Navigation
|
|
168
|
-
catcher.addBreadcrumb({
|
|
169
|
-
type: "navigation",
|
|
170
|
-
message: "User navigated to settings",
|
|
171
|
-
data: { from: "/dashboard", to: "/settings" },
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
// User interaction
|
|
175
|
-
catcher.addBreadcrumb({
|
|
176
|
-
type: "user",
|
|
177
|
-
message: "Button clicked",
|
|
178
|
-
data: { button: "submit-form" },
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
// Custom
|
|
182
|
-
catcher.addBreadcrumb({
|
|
183
|
-
type: "custom",
|
|
184
|
-
message: "Payment processed",
|
|
185
|
-
data: { amount: 99.99, currency: "USD" },
|
|
186
|
-
level: "info",
|
|
187
|
-
});
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
## API Reference
|
|
191
|
-
|
|
192
|
-
### `catchFn(config: CatchFnConfig)`
|
|
193
|
-
|
|
194
|
-
Factory function to create a new CatchFn instance.
|
|
195
|
-
|
|
196
|
-
### `CatchFn.captureBug(options: CaptureBugOptions)`
|
|
197
|
-
|
|
198
|
-
Capture a bug report with context.
|
|
199
|
-
|
|
200
|
-
**Returns:** `Promise<BugReportEvent>`
|
|
201
|
-
|
|
202
|
-
### `CatchFn.addBreadcrumb(breadcrumb: Breadcrumb)`
|
|
203
|
-
|
|
204
|
-
Manually add a breadcrumb.
|
|
205
|
-
|
|
206
|
-
### `CatchFn.beforeSend(hook: BeforeSendHook)`
|
|
207
|
-
|
|
208
|
-
Register a beforeSend hook.
|
|
209
|
-
|
|
210
|
-
### `CatchFn.addIgnorePattern(pattern: IgnorePattern)`
|
|
211
|
-
|
|
212
|
-
Add a pattern to ignore.
|
|
213
|
-
|
|
214
|
-
### `CatchFn.clear()`
|
|
215
|
-
|
|
216
|
-
Clear all collected context (breadcrumbs, logs, network requests).
|
|
217
|
-
|
|
218
|
-
### `CatchFn.stop()`
|
|
219
|
-
|
|
220
|
-
Stop all collectors (console, network interceptors).
|
|
221
|
-
|
|
222
|
-
### `CatchFn.getConfig()`
|
|
223
|
-
|
|
224
|
-
Get the current configuration.
|
|
225
|
-
|
|
226
|
-
## Advanced Usage
|
|
227
|
-
|
|
228
|
-
### Using Collectors Directly
|
|
229
|
-
|
|
230
|
-
```typescript
|
|
231
|
-
import { BreadcrumbCollector } from "catchfn";
|
|
232
|
-
|
|
233
|
-
const breadcrumbs = new BreadcrumbCollector(100);
|
|
234
|
-
|
|
235
|
-
breadcrumbs.addNavigation("/home", "/profile");
|
|
236
|
-
breadcrumbs.addUser("click", "edit-button");
|
|
237
|
-
|
|
238
|
-
const collected = breadcrumbs.collect();
|
|
239
|
-
console.log(collected);
|
|
240
|
-
```
|
|
241
|
-
|
|
242
|
-
### Data Redaction
|
|
243
|
-
|
|
244
|
-
```typescript
|
|
245
|
-
import { redactObject, redactUrl } from "catchfn";
|
|
246
|
-
|
|
247
|
-
const data = {
|
|
248
|
-
username: "john",
|
|
249
|
-
password: "secret123", // Will be redacted
|
|
250
|
-
apiKey: "abc123", // Will be redacted
|
|
251
|
-
};
|
|
252
|
-
|
|
253
|
-
const redacted = redactObject(data);
|
|
254
|
-
// { username: "john", password: "[REDACTED]", apiKey: "[REDACTED]" }
|
|
255
|
-
|
|
256
|
-
const url = redactUrl("https://api.example.com?token=secret&id=123");
|
|
257
|
-
// "https://api.example.com?token=[REDACTED]&id=123"
|
|
1
|
+
# catchfn
|
|
2
|
+
|
|
3
|
+
Console, network, breadcrumb, error and WebSocket collectors that record what happened in a
|
|
4
|
+
browser or Node process as versioned **evidence events**. The collectors run on
|
|
5
|
+
their own: no reporter client, widget or server is needed.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { BreadcrumbCollector, ConsoleCollector, ErrorCollector, NetworkCollector, WebSocketCollector, validateEvidenceEvent } from "catchfn";
|
|
9
|
+
|
|
10
|
+
const consoleCollector = new ConsoleCollector(100); // keeps the newest 100 events
|
|
11
|
+
const network = new NetworkCollector(20, { blockUrls: ["/health"] });
|
|
12
|
+
const breadcrumbs = new BreadcrumbCollector(50);
|
|
13
|
+
const errors = new ErrorCollector(50);
|
|
14
|
+
const sockets = new WebSocketCollector(100);
|
|
15
|
+
|
|
16
|
+
consoleCollector.start();
|
|
17
|
+
network.start();
|
|
18
|
+
errors.start();
|
|
19
|
+
sockets.start();
|
|
20
|
+
breadcrumbs.addNavigation("/cart", "/checkout");
|
|
21
|
+
|
|
22
|
+
// Later: the last 30 seconds of evidence, oldest first across collectors.
|
|
23
|
+
const since = Date.now() - 30_000;
|
|
24
|
+
const events = [consoleCollector, network, breadcrumbs, errors, sockets]
|
|
25
|
+
.flatMap((collector) => collector.collect({ since }))
|
|
26
|
+
.sort((a, b) => a.timestamp - b.timestamp || a.seq - b.seq);
|
|
27
|
+
|
|
28
|
+
consoleCollector.stop();
|
|
29
|
+
network.stop();
|
|
30
|
+
errors.stop();
|
|
31
|
+
sockets.stop();
|
|
258
32
|
```
|
|
259
33
|
|
|
260
|
-
##
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
34
|
+
## Evidence events v1
|
|
35
|
+
|
|
36
|
+
Every event has `schemaVersion: 1`, a `kind` (`"console"`, `"network"`,
|
|
37
|
+
`"breadcrumb"`, `"error"` or `"websocket"`), a `seq` capture-order number and a `timestamp` in epoch
|
|
38
|
+
milliseconds, plus the fields of its kind. The format is closed: extra fields
|
|
39
|
+
are invalid. Check an event with `validateEvidenceEvent(event)` (returns a list
|
|
40
|
+
of problems, empty when valid) or with any JSON Schema 2020-12 validator and
|
|
41
|
+
the exported `evidenceEventSchema`. Integer fields (`seq`, `timestamp`,
|
|
42
|
+
`status`, `lineno`, `colno`, `size`, `code`) are safe integers in both, and nested records (`data`, header
|
|
43
|
+
records, objects in `args`) must be plain or null-prototype objects without a
|
|
44
|
+
callable `toJSON`.
|
|
45
|
+
|
|
46
|
+
| kind | fields |
|
|
47
|
+
| --- | --- |
|
|
48
|
+
| `console` | `level` (`log`, `info`, `warn`, `error`, `debug`), `args` |
|
|
49
|
+
| `network` | `transport` (`fetch`, `xhr`), `method`, `url`, optional `status`, `statusText`, `duration`, `requestHeaders`, `responseHeaders` |
|
|
50
|
+
| `breadcrumb` | `type` (`navigation`, `console`, `network`, `user`, `custom`), optional `message`, `data`, `category`, `level` (`debug`, `info`, `warning`, `error`) |
|
|
51
|
+
| `error` | `source` (`error`, `unhandledrejection`), `message`, optional `name`, `stack`, `filename`, `lineno`, `colno`, `value` |
|
|
52
|
+
| `websocket` | `type` (`open`, `message`, `close`, `error`), `url`, optional `direction` (`sent`, `received`) and `size` (messages), `code` and `wasClean` (close) |
|
|
53
|
+
|
|
54
|
+
Captured events are frozen.
|
|
55
|
+
|
|
56
|
+
## Time-window queries
|
|
57
|
+
|
|
58
|
+
Each collector keeps its events in a fixed-size ring buffer (the constructor's
|
|
59
|
+
`maxSize`; `0` keeps nothing) and `collect({ since, until })` returns the
|
|
60
|
+
buffered events whose `timestamp` lies in the **inclusive** window, oldest
|
|
61
|
+
first. Either bound may be omitted, `since > until` matches nothing, and a
|
|
62
|
+
`NaN` or non-number bound throws a `RangeError`.
|
|
63
|
+
|
|
64
|
+
## Patching and `stop()`
|
|
65
|
+
|
|
66
|
+
`ConsoleCollector` wraps `console.log/info/warn/error/debug`; `NetworkCollector`
|
|
67
|
+
wraps `fetch` and `XMLHttpRequest.prototype.open/send`. Overlapping collectors
|
|
68
|
+
share one set of wrappers. `stop()` on the last running collector restores
|
|
69
|
+
exactly the functions that were replaced, but only where its own wrapper is
|
|
70
|
+
still installed: if another library wrapped them after `start()`, the catchfn
|
|
71
|
+
wrapper stays in that chain, passes calls through and records nothing.
|
|
72
|
+
|
|
73
|
+
The network collector never changes the application's request: the original
|
|
74
|
+
arguments pass through untouched (a `null` or `undefined` receiver of `fetch`
|
|
75
|
+
becomes `globalThis`), and observation never runs the application's
|
|
76
|
+
iterators, getters or conversions (except the fulfilled value's `then` read
|
|
77
|
+
described below). The page's `fetch` and
|
|
78
|
+
`XMLHttpRequest.prototype.open` run first. Before they run, observation reads
|
|
79
|
+
only a string URL argument, or a platform `URL` argument through the `href`
|
|
80
|
+
getter captured when catchfn loads. The `URL` is read only where that
|
|
81
|
+
getter's brand check runs no Proxy trap, which a probe checks at load.
|
|
82
|
+
Browsers and current Node check an internal slot, which runs none. On Node
|
|
83
|
+
18.17 the brand check runs a trap, so there a `URL` argument is recorded as
|
|
84
|
+
`[UNREAD_URL]`. A `Request`, `init` or headers
|
|
85
|
+
argument is read only after the call returns, and `send` reads nothing from
|
|
86
|
+
its arguments. A native `fetch`, or a wrapper that calls one synchronously,
|
|
87
|
+
has read the request by then. The application's own arguments and results
|
|
88
|
+
are read through the `URL`, `Request`, `Response`, `Headers`, `Promise`,
|
|
89
|
+
`XMLHttpRequest` and `EventTarget` getters and methods captured at load, so a
|
|
90
|
+
patch the page makes to those later never runs on them. Two exceptions:
|
|
91
|
+
|
|
92
|
+
- An `XMLHttpRequest` defined or replaced after catchfn loads has its getters
|
|
93
|
+
captured when `start()` installs the wrappers.
|
|
94
|
+
- Redacting the captured URL string after the call uses the live `URL` and
|
|
95
|
+
`URLSearchParams` API (see Redaction), so a page that patches it can run
|
|
96
|
+
code at that point.
|
|
97
|
+
|
|
98
|
+
Known limit, deferred to SFNS-60 (SFNS-56 owner scope amendment of
|
|
99
|
+
2026-11-13): no script can tell a Proxy apart, so a stateful Proxy `init` or
|
|
100
|
+
headers object can observe the reads made after the call. Attribution can be
|
|
101
|
+
wrong after the input is consumed through a `fetch` installed before `start()`
|
|
102
|
+
that defers the native call, or through a reused `init` or `Request`,
|
|
103
|
+
including values an earlier wrapper changed.
|
|
104
|
+
|
|
105
|
+
When the page's `fetch` returns a plain platform `Promise` (not a subclass,
|
|
106
|
+
with no own properties, while `Promise.prototype.constructor` and
|
|
107
|
+
`Promise[Symbol.species]` are still the platform's), `fetch` returns a
|
|
108
|
+
promise derived from it through the `Promise.prototype.then` captured at load
|
|
109
|
+
(SFNS-56 owner amendment of 2026-11-14). It settles with the same value or
|
|
110
|
+
reason one microtask later and carries the application's rejection handling:
|
|
111
|
+
a rejection the application leaves unhandled is reported once, with the
|
|
112
|
+
original reason, and a handled one is not reported. Observation records
|
|
113
|
+
`status`, `statusText`, `duration` and response headers from it and never
|
|
114
|
+
raises. Only that promise's identity changes. Two limits follow from deriving
|
|
115
|
+
a native promise, and no script can avoid them:
|
|
116
|
+
|
|
117
|
+
- Resolving the derived promise reads the page value's `then`, as resolving
|
|
118
|
+
any promise does. A platform `Response` has none, unless the page adds one
|
|
119
|
+
(on the response, `Response.prototype` or `Object.prototype`); a callable
|
|
120
|
+
one is then adopted.
|
|
121
|
+
- Only handlers on the returned promise count. A rejection handled solely on
|
|
122
|
+
the page's own promise (by a `fetch` wrapper installed before `start()`, or
|
|
123
|
+
on a shared promise) is reported as unhandled if the caller drops the
|
|
124
|
+
returned promise.
|
|
125
|
+
|
|
126
|
+
Any other result (a custom thenable, a `Promise` subclass, a promise with own
|
|
127
|
+
properties, or any promise once the page replaced Promise's `constructor` or
|
|
128
|
+
species) is returned unchanged, its `then` is never called by catchfn, and
|
|
129
|
+
it is recorded when the call is made, without `status` or `duration`. A
|
|
130
|
+
stopped wrapper reads nothing at all. What observation then records:
|
|
131
|
+
|
|
132
|
+
- `init.headers` is captured only from a platform `Headers` object (not a
|
|
133
|
+
subclass, and only while neither it nor `Headers.prototype` has a replaced
|
|
134
|
+
iterator), a plain array of `[name, value]` string pairs or a plain record
|
|
135
|
+
of string data properties. Any other form (a generator, `Map.entries()`, an
|
|
136
|
+
accessor, a custom iterator, a non-string value, more than 200 pairs) is
|
|
137
|
+
not read and the request is recorded without `requestHeaders`. The headers
|
|
138
|
+
of a `Request` input and of the response are read from the platform's own
|
|
139
|
+
header list, so a replaced `Headers` iterator does not affect them. On a
|
|
140
|
+
runtime whose `Headers.prototype.forEach` runs the iterator (Node 18.17,
|
|
141
|
+
probed at load), no headers are recorded while the iterator is replaced.
|
|
142
|
+
- `init.method` and `init.headers` are read only from own data properties.
|
|
143
|
+
- The URL is read from a string, a platform `URL` (only where the load-time
|
|
144
|
+
probe found its `href` brand check trap-free) or a platform `Request`;
|
|
145
|
+
any other input is recorded as `"[UNREAD_URL]"` (`UNREAD_URL`), and a method
|
|
146
|
+
that is not a string as `"[UNREAD_METHOD]"` (`UNREAD_METHOD`).
|
|
147
|
+
|
|
148
|
+
The captured URL goes through `redactUrl` and the captured headers through
|
|
149
|
+
`redactHeaders`.
|
|
150
|
+
|
|
151
|
+
### Errors
|
|
152
|
+
|
|
153
|
+
`ErrorCollector` adds `error` and `unhandledrejection` listeners to the
|
|
154
|
+
global (the window, or a worker's global scope) and removes them on the last
|
|
155
|
+
`stop()`; it replaces nothing and never cancels an event, so the page's own
|
|
156
|
+
handlers and the browser's console report run as before. Where the global is
|
|
157
|
+
not an event target (plain Node) it records nothing. The listeners go on
|
|
158
|
+
through the `EventTarget` methods captured at load, or, where the global is
|
|
159
|
+
not a platform event target but has its own `addEventListener` (a test
|
|
160
|
+
environment such as Vitest's jsdom, which copies a window's members onto
|
|
161
|
+
Node's global), through that method. There a running collector counts as a
|
|
162
|
+
page error listener, so the environment no longer fails a test on an
|
|
163
|
+
uncaught error while it runs.
|
|
164
|
+
|
|
165
|
+
- An uncaught error is read from a platform `ErrorEvent` through the getters
|
|
166
|
+
captured at load: `message` is the browser's text (`"Uncaught Error: boom"`)
|
|
167
|
+
when it has one, `filename` goes through `redactUrl`, and `lineno` and
|
|
168
|
+
`colno` are kept. An `error` event that is not an `ErrorEvent` (one the page
|
|
169
|
+
dispatches itself) is ignored.
|
|
170
|
+
- An unhandled rejection's reason is read through the captured
|
|
171
|
+
`PromiseRejectionEvent` getter, and an `unhandledrejection` event that is
|
|
172
|
+
not a `PromiseRejectionEvent` is ignored. Only on a runtime without that
|
|
173
|
+
class is the reason read from the event's own `reason` data property.
|
|
174
|
+
- A thrown value or reason that is an `Error` gives `name`, `message` and
|
|
175
|
+
`stack`, serialized as one entry; reading them can run the error's own
|
|
176
|
+
getters, as for a console argument. Each URL in the stack goes through
|
|
177
|
+
`redactUrl`, keeping its `:line:column` suffix. Any other value is serialized into
|
|
178
|
+
`value`, and `message` holds it as text (a string as is, anything else as
|
|
179
|
+
its JSON). An uncaught error whose `ErrorEvent.error` is `null` or
|
|
180
|
+
`undefined` has no `value`: a cross-origin `"Script error."` and an
|
|
181
|
+
uncaught `throw null` or `throw undefined` look the same there, and only the
|
|
182
|
+
browser's `message` tells them apart. A rejection keeps a `null` or
|
|
183
|
+
`undefined` reason in `value`.
|
|
184
|
+
|
|
185
|
+
### WebSockets
|
|
186
|
+
|
|
187
|
+
`WebSocketCollector` replaces the global `WebSocket` with a `Proxy` of the
|
|
188
|
+
page's class (statics, `prototype`, `instanceof` and subclassing keep
|
|
189
|
+
working) and wraps the `send` its sockets inherit, on the prototype that
|
|
190
|
+
defines it (`WebSocket.prototype`, also when the global is a subclass); the last `stop()` restores
|
|
191
|
+
both where its own wrapper is still installed, as above. Every target is
|
|
192
|
+
resolved before anything is replaced, so `start()` never throws: a global it
|
|
193
|
+
cannot replace (or a failure part way) is rolled back, a wrapper it cannot
|
|
194
|
+
remove is left inert, and the collector records nothing. Each restore runs on its own, so one that throws
|
|
195
|
+
does not stop the others. Only sockets constructed while a collector runs
|
|
196
|
+
are observed: the constructor adds `open`, `message`, `close` and `error`
|
|
197
|
+
listeners before the page can add its own. They are removed after the
|
|
198
|
+
socket's `close` event and from every socket on the last `stop()`; the
|
|
199
|
+
page's own listeners stay and the socket is never closed. Sockets are held
|
|
200
|
+
only weakly, so a running collector never keeps one alive, also one that
|
|
201
|
+
fails without a `close` event. A restart observes
|
|
202
|
+
new sockets only. A class that extended `WebSocket` before `start()`
|
|
203
|
+
constructs through the page's own class and is not observed.
|
|
204
|
+
|
|
205
|
+
- Messages record their direction and their size in bytes: a string's UTF-8
|
|
206
|
+
length (counted with the `String.prototype.charCodeAt` captured at load),
|
|
207
|
+
or the size of an `ArrayBuffer`, typed array, `DataView` or `Blob`
|
|
208
|
+
through the platform getters captured at load, each used only when a
|
|
209
|
+
probe at load shows its brand check runs no `Proxy` trap. A browser's
|
|
210
|
+
`Blob` getter qualifies; Node's reads a symbol property, which runs a
|
|
211
|
+
trap, so on Node a `Blob` is recorded without `size`. Another payload
|
|
212
|
+
type is recorded without `size`. The payload itself is never recorded.
|
|
213
|
+
- `send()` on a socket constructed while no collector ran is passed straight
|
|
214
|
+
to the page's `send`, unobserved. On an observed socket the page's `send`
|
|
215
|
+
always receives the caller's arguments unchanged, and observing it runs
|
|
216
|
+
no page code. Before it only the socket's membership and `readyState`,
|
|
217
|
+
the clock, and the length of an `ArrayBuffer`, typed array or `DataView`
|
|
218
|
+
are read (a library's `send` may transfer the buffer, detaching it). A
|
|
219
|
+
`Blob` and a string are sized after the `send` returns; `readyState` is
|
|
220
|
+
read again only for an object the platform's own `send` converts (below).
|
|
221
|
+
- Only a `send()` on an open socket is recorded, its `readyState` read
|
|
222
|
+
before the call through the getter captured at load: the platform throws
|
|
223
|
+
while connecting (it still throws) and discards the data once the socket
|
|
224
|
+
is closing or closed (nothing is recorded). A `send` that transmits and
|
|
225
|
+
then closes the socket is recorded. An object other than binary data (a
|
|
226
|
+
bare `SharedArrayBuffer` included) is converted by the `send` itself,
|
|
227
|
+
which can run page code that closes the socket and discards the data, and
|
|
228
|
+
is recorded without `size`. Through the platform's own `send` (the one the
|
|
229
|
+
`WebSocket` at load defines; a replacement installed on its prototype
|
|
230
|
+
before catchfn loads counts as it), which never closes a socket itself,
|
|
231
|
+
such an object is recorded only if the socket is still open after the call.
|
|
232
|
+
Through a library's or subclass's `send` the collector cannot tell that
|
|
233
|
+
discard from a library that transmits and then closes, so the state
|
|
234
|
+
before the call decides and such a discard is recorded as sent.
|
|
235
|
+
- Event attributes are read only through the platform getters. A `message`
|
|
236
|
+
or `close` event the page dispatches on a socket itself, which is not a
|
|
237
|
+
platform `MessageEvent` or `CloseEvent`, is ignored where the runtime has
|
|
238
|
+
that class. Only on a runtime without the class (or for a stand-in socket
|
|
239
|
+
class without a `url` or `readyState` getter) is the attribute read from
|
|
240
|
+
an own data property; a getter there is never run.
|
|
241
|
+
- In a browser window or worker, which fires socket events trusted, an
|
|
242
|
+
untrusted event (`isTrusted` false, read through the platform getter) is
|
|
243
|
+
ignored, whatever its type. Node fires its socket events untrusted, so
|
|
244
|
+
there `isTrusted` is not checked; instead, on every runtime, an `open` is
|
|
245
|
+
recorded once and only on a socket whose `readyState` is open (or
|
|
246
|
+
closing, when an earlier listener closed it during the event), and a
|
|
247
|
+
`close` only on a socket whose `readyState` is closed. A page-dispatched
|
|
248
|
+
`error` event is not detected on Node.
|
|
249
|
+
- `url` is the socket's `url` attribute (through the getter captured at load
|
|
250
|
+
when the global class inherits from the platform's) through `redactUrl`. A close
|
|
251
|
+
event records `code` and `wasClean` through the `CloseEvent` getters
|
|
252
|
+
captured at load. On a runtime without a global `CloseEvent` (Node 22),
|
|
253
|
+
whose close events keep them in getters, they are omitted rather than read
|
|
254
|
+
through a getter that cannot be verified. Its `reason` is not recorded.
|
|
255
|
+
- A refused handshake fires `error`; browsers and Node 24 then fire a
|
|
256
|
+
`close` with code 1006, which Node 22 omits. The collector records what
|
|
257
|
+
the runtime emits.
|
|
258
|
+
- A recorded `size` is always a nonnegative safe integer; a size a platform
|
|
259
|
+
getter reports as anything else is omitted and the event kept.
|
|
260
|
+
- `start()` reentered while it installs (page code run by reading the global
|
|
261
|
+
or adding a listener) is a no-op, and a `stop()` during that install leaves
|
|
262
|
+
the session at once: a socket constructed meanwhile is never observed, also
|
|
263
|
+
after a restart. Each collector holds at most one subscription.
|
|
264
|
+
- Timestamps of the error and WebSocket collectors come from the `Date.now`
|
|
265
|
+
captured at load. An observation whose clock gives anything but a
|
|
266
|
+
nonnegative safe integer is dropped; the event or send itself is unchanged.
|
|
267
|
+
Loading never throws: with no `Date.now` to capture then (a throwing
|
|
268
|
+
accessor, or none), every observation is dropped.
|
|
269
|
+
|
|
270
|
+
Known limit, deferred to SFNS-60 (SFNS-57 owner decision of 2026-11-19):
|
|
271
|
+
against hostile same-realm page code (patched intrinsics such as `Set`
|
|
272
|
+
methods, accessor-backed sockets or payloads, Proxies, forwarding
|
|
273
|
+
library `send` methods), WebSocket send evidence is best-effort. Such code
|
|
274
|
+
can make a `send` the platform discarded be recorded as sent, run while a
|
|
275
|
+
send is observed, start a collector that then receives a send already under
|
|
276
|
+
way, or report a binary size that differs from what the platform sent. These
|
|
277
|
+
stay guaranteed: `send()` behaves as without the collector, collectors never
|
|
278
|
+
throw, every event is valid v1, `stop()` restores everything, and each
|
|
279
|
+
instance holds one subscription.
|
|
280
|
+
|
|
281
|
+
## Serialization
|
|
282
|
+
|
|
283
|
+
Captured values are snapshotted under a closed set of forms:
|
|
284
|
+
|
|
285
|
+
- JSON primitives (strings, finite numbers, booleans, `null`) are kept;
|
|
286
|
+
- `Error` becomes `{ name, message, stack, ...own properties }`;
|
|
287
|
+
- plain objects and arrays are copied at most 5 container levels deep (deeper
|
|
288
|
+
containers become `"[Max Depth]"`) with at most 200 keys or items each,
|
|
289
|
+
a truncation marker included;
|
|
290
|
+
- a container that refers back to one of its ancestors becomes `"[Circular]"`;
|
|
291
|
+
- DOM nodes become `"<tag#id.class>"`;
|
|
292
|
+
- everything else becomes `String(value)`, or `"[Unserializable]"` when that
|
|
293
|
+
conversion throws.
|
|
294
|
+
|
|
295
|
+
One entry is at most 10 KB of UTF-8 JSON; strings are cut and containers stop
|
|
296
|
+
early with a `"[Truncated]"` marker (the last array item, or a record key
|
|
297
|
+
whose value counts the omitted keys; `"[Truncated 2]"`, ... when the record
|
|
298
|
+
already holds that key). A console event's argument list is one
|
|
299
|
+
entry, and each argument counts its depth from 1. A breadcrumb's `data` and
|
|
300
|
+
`message`, a network event's `url` and each header record, an error event's
|
|
301
|
+
`message`, `filename` and thrown value (an `Error`'s name, message and stack
|
|
302
|
+
together, or `value`), and a websocket event's `url`, are entries.
|
|
303
|
+
|
|
304
|
+
## Redaction
|
|
305
|
+
|
|
306
|
+
- `redactUrl(url)` removes authority credentials, replaces sensitive query
|
|
307
|
+
values (`token`, `key`, `secret`, `password`, `api_key`, `access_token`,
|
|
308
|
+
`auth`, case-insensitive) with `[REDACTED]`, redacts a fragment that names a
|
|
309
|
+
token, secret, password or API key, keeps relative URLs relative and returns
|
|
310
|
+
`[REDACTED_URL]` for a URL it cannot parse.
|
|
311
|
+
- `redactHeaders(headers)` redacts `authorization`, `cookie`, `set-cookie`,
|
|
312
|
+
`x-api-key`, `x-auth-token` and `x-access-token`, case-insensitive.
|
|
313
|
+
- `redactObject(value)` snapshots a value and redacts keys that name secrets,
|
|
314
|
+
plus URLs under `url`/`href` keys; the redacted result keeps the 10 KB bound.
|
|
315
|
+
- `redactString(text)` masks card numbers.
|