catchfn 0.0.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 +280 -0
- package/dist/index.cjs +815 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +462 -0
- package/dist/index.d.ts +462 -0
- package/dist/index.js +773 -0
- package/dist/index.js.map +1 -0
- package/package.json +60 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,815 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
BreadcrumbCollector: () => BreadcrumbCollector,
|
|
24
|
+
CatchFn: () => CatchFn,
|
|
25
|
+
ConsoleCollector: () => ConsoleCollector,
|
|
26
|
+
DeviceCollector: () => DeviceCollector,
|
|
27
|
+
NetworkCollector: () => NetworkCollector,
|
|
28
|
+
catchFn: () => catchFn,
|
|
29
|
+
default: () => index_default,
|
|
30
|
+
estimateSize: () => estimateSize,
|
|
31
|
+
limitPayloadSize: () => limitPayloadSize,
|
|
32
|
+
redactHeaders: () => redactHeaders,
|
|
33
|
+
redactObject: () => redactObject,
|
|
34
|
+
redactString: () => redactString,
|
|
35
|
+
redactUrl: () => redactUrl,
|
|
36
|
+
safeSerialize: () => safeSerialize,
|
|
37
|
+
serializeError: () => serializeError,
|
|
38
|
+
trimArray: () => trimArray,
|
|
39
|
+
truncateString: () => truncateString
|
|
40
|
+
});
|
|
41
|
+
module.exports = __toCommonJS(index_exports);
|
|
42
|
+
|
|
43
|
+
// src/collectors/breadcrumbs.ts
|
|
44
|
+
var BreadcrumbCollector = class {
|
|
45
|
+
breadcrumbs = [];
|
|
46
|
+
maxSize;
|
|
47
|
+
constructor(maxSize = 50) {
|
|
48
|
+
this.maxSize = maxSize;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Add a breadcrumb
|
|
52
|
+
*/
|
|
53
|
+
add(breadcrumb) {
|
|
54
|
+
const entry = {
|
|
55
|
+
...breadcrumb,
|
|
56
|
+
timestamp: Date.now()
|
|
57
|
+
};
|
|
58
|
+
this.breadcrumbs.push(entry);
|
|
59
|
+
if (this.breadcrumbs.length > this.maxSize) {
|
|
60
|
+
this.breadcrumbs.shift();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Add a navigation breadcrumb
|
|
65
|
+
*/
|
|
66
|
+
addNavigation(from, to) {
|
|
67
|
+
this.add({
|
|
68
|
+
type: "navigation",
|
|
69
|
+
message: `${from} \u2192 ${to}`,
|
|
70
|
+
data: { from, to },
|
|
71
|
+
category: "navigation"
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Add a console breadcrumb
|
|
76
|
+
*/
|
|
77
|
+
addConsole(level, message) {
|
|
78
|
+
this.add({
|
|
79
|
+
type: "console",
|
|
80
|
+
message,
|
|
81
|
+
level,
|
|
82
|
+
category: "console"
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Add a network breadcrumb
|
|
87
|
+
*/
|
|
88
|
+
addNetwork(method, url, status) {
|
|
89
|
+
this.add({
|
|
90
|
+
type: "network",
|
|
91
|
+
message: `${method} ${url} ${status ? `(${status})` : ""}`,
|
|
92
|
+
data: { method, url, status },
|
|
93
|
+
category: "network",
|
|
94
|
+
level: status && status >= 400 ? "error" : "info"
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Add a user interaction breadcrumb
|
|
99
|
+
*/
|
|
100
|
+
addUser(action, target, data) {
|
|
101
|
+
this.add({
|
|
102
|
+
type: "user",
|
|
103
|
+
message: `${action} on ${target}`,
|
|
104
|
+
data: { action, target, ...data },
|
|
105
|
+
category: "user"
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Collect all breadcrumbs
|
|
110
|
+
*/
|
|
111
|
+
collect() {
|
|
112
|
+
return [...this.breadcrumbs];
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Clear all breadcrumbs
|
|
116
|
+
*/
|
|
117
|
+
clear() {
|
|
118
|
+
this.breadcrumbs = [];
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Get the current number of breadcrumbs
|
|
122
|
+
*/
|
|
123
|
+
size() {
|
|
124
|
+
return this.breadcrumbs.length;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// src/collectors/console.ts
|
|
129
|
+
var ConsoleCollector = class {
|
|
130
|
+
logs = [];
|
|
131
|
+
maxSize;
|
|
132
|
+
originalMethods = /* @__PURE__ */ new Map();
|
|
133
|
+
isActive = false;
|
|
134
|
+
constructor(maxSize = 100) {
|
|
135
|
+
this.maxSize = maxSize;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Start intercepting console methods
|
|
139
|
+
*/
|
|
140
|
+
start() {
|
|
141
|
+
if (this.isActive) return;
|
|
142
|
+
const methods = ["log", "info", "warn", "error", "debug"];
|
|
143
|
+
methods.forEach((method) => {
|
|
144
|
+
this.originalMethods.set(method, console[method].bind(console));
|
|
145
|
+
console[method] = (...args) => {
|
|
146
|
+
this.originalMethods.get(method)?.(...args);
|
|
147
|
+
this.capture(method, args);
|
|
148
|
+
};
|
|
149
|
+
});
|
|
150
|
+
this.isActive = true;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Stop intercepting console methods
|
|
154
|
+
*/
|
|
155
|
+
stop() {
|
|
156
|
+
if (!this.isActive) return;
|
|
157
|
+
this.originalMethods.forEach((original, method) => {
|
|
158
|
+
console[method] = original;
|
|
159
|
+
});
|
|
160
|
+
this.originalMethods.clear();
|
|
161
|
+
this.isActive = false;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Capture a console log entry
|
|
165
|
+
*/
|
|
166
|
+
capture(level, args) {
|
|
167
|
+
const entry = {
|
|
168
|
+
level,
|
|
169
|
+
args: this.serializeArgs(args),
|
|
170
|
+
timestamp: Date.now()
|
|
171
|
+
};
|
|
172
|
+
this.logs.push(entry);
|
|
173
|
+
if (this.logs.length > this.maxSize) {
|
|
174
|
+
this.logs.shift();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Serialize console arguments
|
|
179
|
+
*/
|
|
180
|
+
serializeArgs(args) {
|
|
181
|
+
return args.map((arg) => {
|
|
182
|
+
if (arg === null) return null;
|
|
183
|
+
if (arg === void 0) return void 0;
|
|
184
|
+
if (typeof arg === "string") return arg;
|
|
185
|
+
if (typeof arg === "number") return arg;
|
|
186
|
+
if (typeof arg === "boolean") return arg;
|
|
187
|
+
if (arg instanceof Error) {
|
|
188
|
+
return {
|
|
189
|
+
name: arg.name,
|
|
190
|
+
message: arg.message,
|
|
191
|
+
stack: arg.stack
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
try {
|
|
195
|
+
return JSON.parse(JSON.stringify(arg));
|
|
196
|
+
} catch {
|
|
197
|
+
return String(arg);
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Collect all console logs
|
|
203
|
+
*/
|
|
204
|
+
collect() {
|
|
205
|
+
return [...this.logs];
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Clear all console logs
|
|
209
|
+
*/
|
|
210
|
+
clear() {
|
|
211
|
+
this.logs = [];
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Get the current number of logs
|
|
215
|
+
*/
|
|
216
|
+
size() {
|
|
217
|
+
return this.logs.length;
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
// src/collectors/network.ts
|
|
222
|
+
var NetworkCollector = class {
|
|
223
|
+
requests = [];
|
|
224
|
+
maxSize;
|
|
225
|
+
originalFetch;
|
|
226
|
+
originalXHROpen;
|
|
227
|
+
originalXHRSend;
|
|
228
|
+
isActive = false;
|
|
229
|
+
sensitiveHeaders = /* @__PURE__ */ new Set([
|
|
230
|
+
"authorization",
|
|
231
|
+
"cookie",
|
|
232
|
+
"set-cookie",
|
|
233
|
+
"x-api-key",
|
|
234
|
+
"x-auth-token"
|
|
235
|
+
]);
|
|
236
|
+
constructor(maxSize = 20) {
|
|
237
|
+
this.maxSize = maxSize;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Start intercepting network requests
|
|
241
|
+
*/
|
|
242
|
+
start() {
|
|
243
|
+
if (this.isActive) return;
|
|
244
|
+
this.interceptFetch();
|
|
245
|
+
this.interceptXHR();
|
|
246
|
+
this.isActive = true;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Stop intercepting network requests
|
|
250
|
+
*/
|
|
251
|
+
stop() {
|
|
252
|
+
if (!this.isActive) return;
|
|
253
|
+
if (this.originalFetch) {
|
|
254
|
+
globalThis.fetch = this.originalFetch;
|
|
255
|
+
}
|
|
256
|
+
if (this.originalXHROpen) {
|
|
257
|
+
XMLHttpRequest.prototype.open = this.originalXHROpen;
|
|
258
|
+
}
|
|
259
|
+
if (this.originalXHRSend) {
|
|
260
|
+
XMLHttpRequest.prototype.send = this.originalXHRSend;
|
|
261
|
+
}
|
|
262
|
+
this.isActive = false;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Intercept fetch API
|
|
266
|
+
*/
|
|
267
|
+
interceptFetch() {
|
|
268
|
+
if (typeof fetch === "undefined") return;
|
|
269
|
+
this.originalFetch = fetch;
|
|
270
|
+
globalThis.fetch = async (...args) => {
|
|
271
|
+
const startTime = Date.now();
|
|
272
|
+
const url = typeof args[0] === "string" ? args[0] : args[0] instanceof URL ? args[0].href : args[0].url;
|
|
273
|
+
const method = args[1]?.method || "GET";
|
|
274
|
+
try {
|
|
275
|
+
const response = await this.originalFetch(...args);
|
|
276
|
+
const duration = Date.now() - startTime;
|
|
277
|
+
this.addRequest({
|
|
278
|
+
url,
|
|
279
|
+
method,
|
|
280
|
+
status: response.status,
|
|
281
|
+
statusText: response.statusText,
|
|
282
|
+
duration,
|
|
283
|
+
timestamp: startTime,
|
|
284
|
+
requestHeaders: this.extractHeaders(args[1]?.headers),
|
|
285
|
+
responseHeaders: this.extractHeaders(response.headers)
|
|
286
|
+
});
|
|
287
|
+
return response;
|
|
288
|
+
} catch (error) {
|
|
289
|
+
const duration = Date.now() - startTime;
|
|
290
|
+
this.addRequest({
|
|
291
|
+
url,
|
|
292
|
+
method,
|
|
293
|
+
duration,
|
|
294
|
+
timestamp: startTime,
|
|
295
|
+
requestHeaders: this.extractHeaders(args[1]?.headers)
|
|
296
|
+
});
|
|
297
|
+
throw error;
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Intercept XMLHttpRequest
|
|
303
|
+
*/
|
|
304
|
+
interceptXHR() {
|
|
305
|
+
if (typeof XMLHttpRequest === "undefined") return;
|
|
306
|
+
this.originalXHROpen = XMLHttpRequest.prototype.open;
|
|
307
|
+
this.originalXHRSend = XMLHttpRequest.prototype.send;
|
|
308
|
+
const collector = this;
|
|
309
|
+
XMLHttpRequest.prototype.open = function(method, url, async, username, password) {
|
|
310
|
+
this.__catchfn_method = method;
|
|
311
|
+
this.__catchfn_url = String(url);
|
|
312
|
+
this.__catchfn_startTime = Date.now();
|
|
313
|
+
return collector.originalXHROpen.call(
|
|
314
|
+
this,
|
|
315
|
+
method,
|
|
316
|
+
url,
|
|
317
|
+
async ?? true,
|
|
318
|
+
username,
|
|
319
|
+
password
|
|
320
|
+
);
|
|
321
|
+
};
|
|
322
|
+
XMLHttpRequest.prototype.send = function(...args) {
|
|
323
|
+
this.addEventListener("loadend", function() {
|
|
324
|
+
const duration = Date.now() - this.__catchfn_startTime;
|
|
325
|
+
collector.addRequest({
|
|
326
|
+
url: this.__catchfn_url,
|
|
327
|
+
method: this.__catchfn_method,
|
|
328
|
+
status: this.status,
|
|
329
|
+
statusText: this.statusText,
|
|
330
|
+
duration,
|
|
331
|
+
timestamp: this.__catchfn_startTime
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
return collector.originalXHRSend.call(this, ...args);
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Extract and sanitize headers
|
|
339
|
+
*/
|
|
340
|
+
extractHeaders(headers) {
|
|
341
|
+
if (!headers) return void 0;
|
|
342
|
+
const result = {};
|
|
343
|
+
if (headers instanceof Headers) {
|
|
344
|
+
headers.forEach((value, key) => {
|
|
345
|
+
if (!this.sensitiveHeaders.has(key.toLowerCase())) {
|
|
346
|
+
result[key] = value;
|
|
347
|
+
} else {
|
|
348
|
+
result[key] = "[REDACTED]";
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
} else if (Array.isArray(headers)) {
|
|
352
|
+
headers.forEach(([key, value]) => {
|
|
353
|
+
if (!this.sensitiveHeaders.has(key.toLowerCase())) {
|
|
354
|
+
result[key] = value;
|
|
355
|
+
} else {
|
|
356
|
+
result[key] = "[REDACTED]";
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
} else {
|
|
360
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
361
|
+
if (!this.sensitiveHeaders.has(key.toLowerCase())) {
|
|
362
|
+
result[key] = String(value);
|
|
363
|
+
} else {
|
|
364
|
+
result[key] = "[REDACTED]";
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
return result;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Add a network request
|
|
372
|
+
*/
|
|
373
|
+
addRequest(request) {
|
|
374
|
+
this.requests.push(request);
|
|
375
|
+
if (this.requests.length > this.maxSize) {
|
|
376
|
+
this.requests.shift();
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Collect all network requests
|
|
381
|
+
*/
|
|
382
|
+
collect() {
|
|
383
|
+
return [...this.requests];
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Clear all network requests
|
|
387
|
+
*/
|
|
388
|
+
clear() {
|
|
389
|
+
this.requests = [];
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Get the current number of requests
|
|
393
|
+
*/
|
|
394
|
+
size() {
|
|
395
|
+
return this.requests.length;
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
// src/collectors/device.ts
|
|
400
|
+
var DeviceCollector = class {
|
|
401
|
+
/**
|
|
402
|
+
* Collect device and environment information
|
|
403
|
+
*/
|
|
404
|
+
static collect() {
|
|
405
|
+
const info = {};
|
|
406
|
+
if (typeof navigator !== "undefined") {
|
|
407
|
+
info.userAgent = navigator.userAgent;
|
|
408
|
+
info.platform = navigator.platform;
|
|
409
|
+
info.language = navigator.language;
|
|
410
|
+
if (typeof globalThis !== "undefined" && "screen" in globalThis) {
|
|
411
|
+
const screen = globalThis.screen;
|
|
412
|
+
info.screenResolution = `${screen.width}x${screen.height}`;
|
|
413
|
+
}
|
|
414
|
+
if (typeof globalThis !== "undefined" && "window" in globalThis) {
|
|
415
|
+
const window = globalThis.window;
|
|
416
|
+
info.viewportSize = `${window.innerWidth}x${window.innerHeight}`;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
if (typeof process !== "undefined") {
|
|
420
|
+
info.platform = process.platform;
|
|
421
|
+
info.nodeVersion = process.version;
|
|
422
|
+
if (process.memoryUsage) {
|
|
423
|
+
const mem = process.memoryUsage();
|
|
424
|
+
info.memoryUsage = {
|
|
425
|
+
rss: mem.rss,
|
|
426
|
+
heapTotal: mem.heapTotal,
|
|
427
|
+
heapUsed: mem.heapUsed,
|
|
428
|
+
external: mem.external
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
try {
|
|
433
|
+
info.timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
434
|
+
} catch {
|
|
435
|
+
}
|
|
436
|
+
return info;
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
// src/utils/redaction.ts
|
|
441
|
+
var SENSITIVE_PATTERNS = [
|
|
442
|
+
/password/i,
|
|
443
|
+
/passwd/i,
|
|
444
|
+
/pwd/i,
|
|
445
|
+
/secret/i,
|
|
446
|
+
/token/i,
|
|
447
|
+
/api[_-]?key/i,
|
|
448
|
+
/access[_-]?key/i,
|
|
449
|
+
/auth/i,
|
|
450
|
+
/credit[_-]?card/i,
|
|
451
|
+
/card[_-]?number/i,
|
|
452
|
+
/cvv/i,
|
|
453
|
+
/ssn/i,
|
|
454
|
+
/social[_-]?security/i
|
|
455
|
+
];
|
|
456
|
+
var CREDIT_CARD_PATTERN = /\b\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}\b/g;
|
|
457
|
+
function redactObject(obj, customPatterns = []) {
|
|
458
|
+
if (obj === null || obj === void 0) return obj;
|
|
459
|
+
if (typeof obj !== "object") return obj;
|
|
460
|
+
const patterns = [...SENSITIVE_PATTERNS, ...customPatterns];
|
|
461
|
+
if (Array.isArray(obj)) {
|
|
462
|
+
return obj.map((item) => redactObject(item, customPatterns));
|
|
463
|
+
}
|
|
464
|
+
const result = {};
|
|
465
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
466
|
+
const isSensitive = patterns.some((pattern) => pattern.test(key));
|
|
467
|
+
if (isSensitive) {
|
|
468
|
+
result[key] = "[REDACTED]";
|
|
469
|
+
} else if (value && typeof value === "object") {
|
|
470
|
+
result[key] = redactObject(value, customPatterns);
|
|
471
|
+
} else {
|
|
472
|
+
result[key] = value;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
return result;
|
|
476
|
+
}
|
|
477
|
+
function redactString(str) {
|
|
478
|
+
let result = str;
|
|
479
|
+
result = result.replace(CREDIT_CARD_PATTERN, "XXXX-XXXX-XXXX-XXXX");
|
|
480
|
+
return result;
|
|
481
|
+
}
|
|
482
|
+
function redactUrl(url, sensitiveParams = []) {
|
|
483
|
+
try {
|
|
484
|
+
const urlObj = new URL(url);
|
|
485
|
+
const defaultSensitiveParams = [
|
|
486
|
+
"token",
|
|
487
|
+
"key",
|
|
488
|
+
"secret",
|
|
489
|
+
"password",
|
|
490
|
+
"api_key",
|
|
491
|
+
"access_token",
|
|
492
|
+
"auth"
|
|
493
|
+
];
|
|
494
|
+
const allSensitiveParams = [...defaultSensitiveParams, ...sensitiveParams];
|
|
495
|
+
allSensitiveParams.forEach((param) => {
|
|
496
|
+
if (urlObj.searchParams.has(param)) {
|
|
497
|
+
urlObj.searchParams.set(param, "[REDACTED]");
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
return urlObj.toString();
|
|
501
|
+
} catch {
|
|
502
|
+
return url;
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
function redactHeaders(headers) {
|
|
506
|
+
const sensitiveHeaders = [
|
|
507
|
+
"authorization",
|
|
508
|
+
"cookie",
|
|
509
|
+
"set-cookie",
|
|
510
|
+
"x-api-key",
|
|
511
|
+
"x-auth-token",
|
|
512
|
+
"x-access-token"
|
|
513
|
+
];
|
|
514
|
+
const result = {};
|
|
515
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
516
|
+
if (sensitiveHeaders.includes(key.toLowerCase())) {
|
|
517
|
+
result[key] = "[REDACTED]";
|
|
518
|
+
} else {
|
|
519
|
+
result[key] = value;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
return result;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// src/utils/serialization.ts
|
|
526
|
+
function serializeError(error) {
|
|
527
|
+
return {
|
|
528
|
+
name: error.name,
|
|
529
|
+
message: error.message,
|
|
530
|
+
stack: error.stack,
|
|
531
|
+
...Object.getOwnPropertyNames(error).reduce(
|
|
532
|
+
(acc, key) => {
|
|
533
|
+
if (key !== "name" && key !== "message" && key !== "stack") {
|
|
534
|
+
acc[key] = error[key];
|
|
535
|
+
}
|
|
536
|
+
return acc;
|
|
537
|
+
},
|
|
538
|
+
{}
|
|
539
|
+
)
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
function safeSerialize(value, maxDepth = 10) {
|
|
543
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
544
|
+
function serialize(val, depth) {
|
|
545
|
+
if (depth > maxDepth) {
|
|
546
|
+
return "[Max Depth Reached]";
|
|
547
|
+
}
|
|
548
|
+
if (val === null) return null;
|
|
549
|
+
if (val === void 0) return void 0;
|
|
550
|
+
if (typeof val === "string") return val;
|
|
551
|
+
if (typeof val === "number") return val;
|
|
552
|
+
if (typeof val === "boolean") return val;
|
|
553
|
+
if (typeof val === "function") return "[Function]";
|
|
554
|
+
if (typeof val === "symbol") return val.toString();
|
|
555
|
+
if (val instanceof Error) {
|
|
556
|
+
return serializeError(val);
|
|
557
|
+
}
|
|
558
|
+
if (val instanceof Date) {
|
|
559
|
+
return val.toISOString();
|
|
560
|
+
}
|
|
561
|
+
if (val instanceof RegExp) {
|
|
562
|
+
return val.toString();
|
|
563
|
+
}
|
|
564
|
+
if (typeof val === "object") {
|
|
565
|
+
if (seen.has(val)) {
|
|
566
|
+
return "[Circular Reference]";
|
|
567
|
+
}
|
|
568
|
+
seen.add(val);
|
|
569
|
+
if (Array.isArray(val)) {
|
|
570
|
+
return val.map((item) => serialize(item, depth + 1));
|
|
571
|
+
}
|
|
572
|
+
const result = {};
|
|
573
|
+
for (const key in val) {
|
|
574
|
+
if (Object.prototype.hasOwnProperty.call(val, key)) {
|
|
575
|
+
result[key] = serialize(val[key], depth + 1);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
return result;
|
|
579
|
+
}
|
|
580
|
+
return String(val);
|
|
581
|
+
}
|
|
582
|
+
return serialize(value, 0);
|
|
583
|
+
}
|
|
584
|
+
function estimateSize(obj) {
|
|
585
|
+
try {
|
|
586
|
+
const str = JSON.stringify(safeSerialize(obj));
|
|
587
|
+
return new Blob([str]).size;
|
|
588
|
+
} catch {
|
|
589
|
+
return 0;
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
function truncateString(str, maxLength) {
|
|
593
|
+
if (str.length <= maxLength) return str;
|
|
594
|
+
return str.substring(0, maxLength - 3) + "...";
|
|
595
|
+
}
|
|
596
|
+
function trimArray(arr, maxSize) {
|
|
597
|
+
if (arr.length <= maxSize) return arr;
|
|
598
|
+
return arr.slice(-maxSize);
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
// src/utils/size-limiter.ts
|
|
602
|
+
function limitPayloadSize(payload, maxSize) {
|
|
603
|
+
const currentSize = estimateSize(payload);
|
|
604
|
+
if (currentSize <= maxSize) {
|
|
605
|
+
return payload;
|
|
606
|
+
}
|
|
607
|
+
const limited = { ...payload };
|
|
608
|
+
if (Array.isArray(limited.breadcrumbs) && limited.breadcrumbs.length > 10) {
|
|
609
|
+
limited.breadcrumbs = trimArray(limited.breadcrumbs, 10);
|
|
610
|
+
}
|
|
611
|
+
if (Array.isArray(limited.consoleLogs) && limited.consoleLogs.length > 20) {
|
|
612
|
+
limited.consoleLogs = trimArray(limited.consoleLogs, 20);
|
|
613
|
+
}
|
|
614
|
+
if (Array.isArray(limited.networkRequests) && limited.networkRequests.length > 5) {
|
|
615
|
+
limited.networkRequests = trimArray(limited.networkRequests, 5);
|
|
616
|
+
}
|
|
617
|
+
if (typeof limited.description === "string" && limited.description.length > 1e3) {
|
|
618
|
+
limited.description = truncateString(limited.description, 1e3);
|
|
619
|
+
}
|
|
620
|
+
const newSize = estimateSize(limited);
|
|
621
|
+
if (newSize <= maxSize) {
|
|
622
|
+
return limited;
|
|
623
|
+
}
|
|
624
|
+
delete limited.consoleLogs;
|
|
625
|
+
delete limited.networkRequests;
|
|
626
|
+
return limited;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
// src/core/catch-fn.ts
|
|
630
|
+
function generateId() {
|
|
631
|
+
return `bug_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`;
|
|
632
|
+
}
|
|
633
|
+
var CatchFn = class {
|
|
634
|
+
config;
|
|
635
|
+
breadcrumbCollector;
|
|
636
|
+
consoleCollector;
|
|
637
|
+
networkCollector;
|
|
638
|
+
beforeSendHooks = [];
|
|
639
|
+
ignorePatterns = [];
|
|
640
|
+
constructor(config) {
|
|
641
|
+
this.config = {
|
|
642
|
+
...config,
|
|
643
|
+
maxBreadcrumbs: config.maxBreadcrumbs ?? 50,
|
|
644
|
+
maxConsoleLogs: config.maxConsoleLogs ?? 100,
|
|
645
|
+
maxNetworkRequests: config.maxNetworkRequests ?? 20,
|
|
646
|
+
defaultSeverity: config.defaultSeverity ?? "medium",
|
|
647
|
+
ignorePatterns: config.ignorePatterns ?? [],
|
|
648
|
+
maxPayloadSize: config.maxPayloadSize ?? 262144,
|
|
649
|
+
// 256KB
|
|
650
|
+
sensitiveFields: config.sensitiveFields ?? [],
|
|
651
|
+
enableBreadcrumbs: config.enableBreadcrumbs ?? true,
|
|
652
|
+
enableConsoleCapture: config.enableConsoleCapture ?? false,
|
|
653
|
+
enableNetworkCapture: config.enableNetworkCapture ?? false
|
|
654
|
+
};
|
|
655
|
+
this.breadcrumbCollector = new BreadcrumbCollector(
|
|
656
|
+
this.config.maxBreadcrumbs
|
|
657
|
+
);
|
|
658
|
+
if (this.config.enableConsoleCapture) {
|
|
659
|
+
this.consoleCollector = new ConsoleCollector(this.config.maxConsoleLogs);
|
|
660
|
+
this.consoleCollector.start();
|
|
661
|
+
}
|
|
662
|
+
if (this.config.enableNetworkCapture) {
|
|
663
|
+
this.networkCollector = new NetworkCollector(
|
|
664
|
+
this.config.maxNetworkRequests
|
|
665
|
+
);
|
|
666
|
+
this.networkCollector.start();
|
|
667
|
+
}
|
|
668
|
+
if (this.config.beforeSend) {
|
|
669
|
+
this.beforeSendHooks.push(this.config.beforeSend);
|
|
670
|
+
}
|
|
671
|
+
this.ignorePatterns = [...this.config.ignorePatterns];
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* Add a breadcrumb
|
|
675
|
+
*/
|
|
676
|
+
addBreadcrumb(breadcrumb) {
|
|
677
|
+
if (!this.config.enableBreadcrumbs) return;
|
|
678
|
+
this.breadcrumbCollector.add(breadcrumb);
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Register a beforeSend hook
|
|
682
|
+
*/
|
|
683
|
+
beforeSend(hook) {
|
|
684
|
+
this.beforeSendHooks.push(hook);
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Add an ignore pattern
|
|
688
|
+
*/
|
|
689
|
+
addIgnorePattern(pattern) {
|
|
690
|
+
this.ignorePatterns.push(pattern);
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* Check if an event should be ignored
|
|
694
|
+
*/
|
|
695
|
+
shouldIgnore(event) {
|
|
696
|
+
const message = event.title + " " + (event.description || "");
|
|
697
|
+
return this.ignorePatterns.some((pattern) => {
|
|
698
|
+
if (typeof pattern === "string") {
|
|
699
|
+
return message.includes(pattern);
|
|
700
|
+
}
|
|
701
|
+
return pattern.test(message);
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
/**
|
|
705
|
+
* Apply beforeSend hooks
|
|
706
|
+
*/
|
|
707
|
+
async applyHooks(event) {
|
|
708
|
+
let current = event;
|
|
709
|
+
for (const hook of this.beforeSendHooks) {
|
|
710
|
+
if (!current) break;
|
|
711
|
+
try {
|
|
712
|
+
current = await hook(current);
|
|
713
|
+
} catch (error) {
|
|
714
|
+
console.error("CatchFn: beforeSend hook failed:", error);
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
return current;
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Capture a bug report
|
|
721
|
+
*/
|
|
722
|
+
async captureBug(options) {
|
|
723
|
+
let event = {
|
|
724
|
+
id: generateId(),
|
|
725
|
+
timestamp: Date.now(),
|
|
726
|
+
title: options.title,
|
|
727
|
+
description: options.description,
|
|
728
|
+
severity: options.severity || this.config.defaultSeverity,
|
|
729
|
+
tags: options.tags,
|
|
730
|
+
environment: options.environment || this.config.environment,
|
|
731
|
+
release: options.release || this.config.release,
|
|
732
|
+
user: options.user || this.config.user,
|
|
733
|
+
metadata: options.metadata,
|
|
734
|
+
attachments: options.attachments
|
|
735
|
+
};
|
|
736
|
+
if (this.config.enableBreadcrumbs) {
|
|
737
|
+
event.breadcrumbs = this.breadcrumbCollector.collect();
|
|
738
|
+
}
|
|
739
|
+
if (this.consoleCollector) {
|
|
740
|
+
event.consoleLogs = this.consoleCollector.collect();
|
|
741
|
+
}
|
|
742
|
+
if (this.networkCollector) {
|
|
743
|
+
event.networkRequests = this.networkCollector.collect();
|
|
744
|
+
}
|
|
745
|
+
event.deviceInfo = DeviceCollector.collect();
|
|
746
|
+
event = redactObject(
|
|
747
|
+
event,
|
|
748
|
+
this.config.sensitiveFields.map((f) => new RegExp(f, "i"))
|
|
749
|
+
);
|
|
750
|
+
event = safeSerialize(event);
|
|
751
|
+
event = limitPayloadSize(event, this.config.maxPayloadSize);
|
|
752
|
+
if (this.shouldIgnore(event)) {
|
|
753
|
+
console.log("CatchFn: Event ignored by ignore patterns");
|
|
754
|
+
return event;
|
|
755
|
+
}
|
|
756
|
+
const processedEvent = await this.applyHooks(event);
|
|
757
|
+
if (!processedEvent) {
|
|
758
|
+
console.log("CatchFn: Event dropped by beforeSend hook");
|
|
759
|
+
return event;
|
|
760
|
+
}
|
|
761
|
+
console.log("CatchFn: Bug captured", {
|
|
762
|
+
id: processedEvent.id,
|
|
763
|
+
title: processedEvent.title,
|
|
764
|
+
severity: processedEvent.severity
|
|
765
|
+
});
|
|
766
|
+
return processedEvent;
|
|
767
|
+
}
|
|
768
|
+
/**
|
|
769
|
+
* Clear all collected context
|
|
770
|
+
*/
|
|
771
|
+
clear() {
|
|
772
|
+
this.breadcrumbCollector.clear();
|
|
773
|
+
this.consoleCollector?.clear();
|
|
774
|
+
this.networkCollector?.clear();
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* Stop all collectors
|
|
778
|
+
*/
|
|
779
|
+
stop() {
|
|
780
|
+
this.consoleCollector?.stop();
|
|
781
|
+
this.networkCollector?.stop();
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* Get current configuration
|
|
785
|
+
*/
|
|
786
|
+
getConfig() {
|
|
787
|
+
return { ...this.config };
|
|
788
|
+
}
|
|
789
|
+
};
|
|
790
|
+
|
|
791
|
+
// src/index.ts
|
|
792
|
+
function catchFn(config) {
|
|
793
|
+
return new CatchFn(config);
|
|
794
|
+
}
|
|
795
|
+
var index_default = catchFn;
|
|
796
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
797
|
+
0 && (module.exports = {
|
|
798
|
+
BreadcrumbCollector,
|
|
799
|
+
CatchFn,
|
|
800
|
+
ConsoleCollector,
|
|
801
|
+
DeviceCollector,
|
|
802
|
+
NetworkCollector,
|
|
803
|
+
catchFn,
|
|
804
|
+
estimateSize,
|
|
805
|
+
limitPayloadSize,
|
|
806
|
+
redactHeaders,
|
|
807
|
+
redactObject,
|
|
808
|
+
redactString,
|
|
809
|
+
redactUrl,
|
|
810
|
+
safeSerialize,
|
|
811
|
+
serializeError,
|
|
812
|
+
trimArray,
|
|
813
|
+
truncateString
|
|
814
|
+
});
|
|
815
|
+
//# sourceMappingURL=index.cjs.map
|