bugstack-sdk 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +310 -317
- package/dist/adapters/express/index.d.mts +80 -0
- package/dist/adapters/express/index.d.ts +80 -0
- package/dist/adapters/express/index.js +887 -0
- package/dist/adapters/express/index.js.map +1 -0
- package/dist/adapters/express/index.mjs +881 -0
- package/dist/adapters/express/index.mjs.map +1 -0
- package/dist/adapters/generic/index.d.mts +127 -0
- package/dist/adapters/generic/index.d.ts +127 -0
- package/dist/adapters/generic/index.js +833 -0
- package/dist/adapters/generic/index.js.map +1 -0
- package/dist/adapters/generic/index.mjs +824 -0
- package/dist/adapters/generic/index.mjs.map +1 -0
- package/dist/adapters/nextjs/index.d.mts +50 -0
- package/dist/adapters/nextjs/index.d.ts +50 -0
- package/dist/adapters/nextjs/index.js +1003 -0
- package/dist/adapters/nextjs/index.js.map +1 -0
- package/dist/adapters/nextjs/index.mjs +998 -0
- package/dist/adapters/nextjs/index.mjs.map +1 -0
- package/dist/client-9EMqVMOl.d.mts +622 -0
- package/dist/client-9EMqVMOl.d.ts +622 -0
- package/dist/index.d.mts +44 -360
- package/dist/index.d.ts +44 -360
- package/dist/index.js +535 -150
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +529 -149
- package/dist/index.mjs.map +1 -1
- package/package.json +90 -71
package/dist/index.js
CHANGED
|
@@ -1,17 +1,439 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
3
|
+
// src/core/fingerprint.ts
|
|
4
|
+
function generateErrorFingerprint(error) {
|
|
5
|
+
const errorType = error.name || "Error";
|
|
6
|
+
const route = error.route || "unknown";
|
|
7
|
+
const stackFirstLine = getStackFirstLine(error.stack);
|
|
8
|
+
const key = `${errorType}:${route}:${stackFirstLine}`;
|
|
9
|
+
return simpleHash(key);
|
|
10
|
+
}
|
|
11
|
+
function getStackFirstLine(stack) {
|
|
12
|
+
if (!stack) return "unknown";
|
|
13
|
+
const lines = stack.split("\n");
|
|
14
|
+
for (const line of lines) {
|
|
15
|
+
const trimmed = line.trim();
|
|
16
|
+
if (trimmed.startsWith("at ")) {
|
|
17
|
+
return trimmed;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
for (const line of lines) {
|
|
21
|
+
const trimmed = line.trim();
|
|
22
|
+
if (trimmed) {
|
|
23
|
+
return trimmed;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return "unknown";
|
|
27
|
+
}
|
|
28
|
+
function simpleHash(str) {
|
|
29
|
+
let hash = 5381;
|
|
30
|
+
for (let i = 0; i < str.length; i++) {
|
|
31
|
+
hash = (hash << 5) + hash + str.charCodeAt(i);
|
|
32
|
+
hash = hash & hash;
|
|
33
|
+
}
|
|
34
|
+
const unsignedHash = hash >>> 0;
|
|
35
|
+
return unsignedHash.toString(16).padStart(8, "0");
|
|
36
|
+
}
|
|
37
|
+
function generateFingerprintFromParts(errorName, route, stack) {
|
|
38
|
+
const stackFirstLine = getStackFirstLine(stack);
|
|
39
|
+
const key = `${errorName}:${route}:${stackFirstLine}`;
|
|
40
|
+
return simpleHash(key);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// src/core/deduplicator.ts
|
|
44
|
+
var ErrorDeduplicator = class {
|
|
45
|
+
/**
|
|
46
|
+
* Cache of fingerprint → timestamp mapping
|
|
47
|
+
* Key: error fingerprint (hex string)
|
|
48
|
+
* Value: timestamp (ms) when error was last sent
|
|
49
|
+
*/
|
|
50
|
+
cache;
|
|
51
|
+
/**
|
|
52
|
+
* Time window in milliseconds
|
|
53
|
+
* Errors with the same fingerprint within this window are considered duplicates
|
|
54
|
+
*/
|
|
55
|
+
window;
|
|
56
|
+
/**
|
|
57
|
+
* Interval ID for cleanup task
|
|
58
|
+
*/
|
|
59
|
+
cleanupIntervalId;
|
|
60
|
+
/**
|
|
61
|
+
* Create a new deduplicator
|
|
62
|
+
*
|
|
63
|
+
* @param window - Deduplication window in milliseconds (default: 5 minutes)
|
|
64
|
+
*/
|
|
65
|
+
constructor(window = 5 * 60 * 1e3) {
|
|
66
|
+
this.cache = /* @__PURE__ */ new Map();
|
|
67
|
+
this.window = window;
|
|
68
|
+
this.startCleanup();
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Check if an error should be sent
|
|
72
|
+
*
|
|
73
|
+
* @param fingerprint - Error fingerprint
|
|
74
|
+
* @returns true if error should be sent, false if it's a duplicate
|
|
75
|
+
*/
|
|
76
|
+
shouldSend(fingerprint) {
|
|
77
|
+
const now = Date.now();
|
|
78
|
+
const lastSent = this.cache.get(fingerprint);
|
|
79
|
+
if (lastSent !== void 0) {
|
|
80
|
+
const elapsed = now - lastSent;
|
|
81
|
+
if (elapsed < this.window) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
this.cache.set(fingerprint, now);
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Mark an error as sent (manually)
|
|
90
|
+
*
|
|
91
|
+
* This can be used to pre-populate the cache or update the timestamp
|
|
92
|
+
* after a successful send.
|
|
93
|
+
*
|
|
94
|
+
* @param fingerprint - Error fingerprint
|
|
95
|
+
*/
|
|
96
|
+
markSent(fingerprint) {
|
|
97
|
+
this.cache.set(fingerprint, Date.now());
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Check if an error fingerprint is in the cache
|
|
101
|
+
*
|
|
102
|
+
* @param fingerprint - Error fingerprint
|
|
103
|
+
* @returns true if fingerprint exists and is within the window
|
|
104
|
+
*/
|
|
105
|
+
has(fingerprint) {
|
|
106
|
+
const lastSent = this.cache.get(fingerprint);
|
|
107
|
+
if (lastSent === void 0) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
const elapsed = Date.now() - lastSent;
|
|
111
|
+
return elapsed < this.window;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Get the number of entries in the cache
|
|
115
|
+
*/
|
|
116
|
+
get size() {
|
|
117
|
+
return this.cache.size;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Clear all entries from the cache
|
|
121
|
+
*/
|
|
122
|
+
clear() {
|
|
123
|
+
this.cache.clear();
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Remove expired entries from the cache
|
|
127
|
+
*
|
|
128
|
+
* This is called automatically every minute, but can also be
|
|
129
|
+
* called manually to free up memory.
|
|
130
|
+
*/
|
|
131
|
+
cleanup() {
|
|
132
|
+
const now = Date.now();
|
|
133
|
+
const toDelete = [];
|
|
134
|
+
for (const [fingerprint, timestamp] of this.cache.entries()) {
|
|
135
|
+
const elapsed = now - timestamp;
|
|
136
|
+
if (elapsed >= this.window) {
|
|
137
|
+
toDelete.push(fingerprint);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
for (const fingerprint of toDelete) {
|
|
141
|
+
this.cache.delete(fingerprint);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Start automatic cleanup task
|
|
146
|
+
*
|
|
147
|
+
* Runs cleanup every minute to remove expired entries
|
|
148
|
+
*/
|
|
149
|
+
startCleanup() {
|
|
150
|
+
this.cleanupIntervalId = setInterval(() => {
|
|
151
|
+
this.cleanup();
|
|
152
|
+
}, 60 * 1e3);
|
|
153
|
+
if (this.cleanupIntervalId.unref) {
|
|
154
|
+
this.cleanupIntervalId.unref();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Stop automatic cleanup task
|
|
159
|
+
*
|
|
160
|
+
* This should be called when shutting down to prevent memory leaks
|
|
161
|
+
*/
|
|
162
|
+
destroy() {
|
|
163
|
+
if (this.cleanupIntervalId) {
|
|
164
|
+
clearInterval(this.cleanupIntervalId);
|
|
165
|
+
this.cleanupIntervalId = void 0;
|
|
166
|
+
}
|
|
167
|
+
this.cache.clear();
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Get cache statistics (for debugging)
|
|
171
|
+
*/
|
|
172
|
+
getStats() {
|
|
173
|
+
const now = Date.now();
|
|
174
|
+
const entries = Array.from(this.cache.entries()).map(([fingerprint, timestamp]) => ({
|
|
175
|
+
fingerprint,
|
|
176
|
+
age: now - timestamp
|
|
177
|
+
}));
|
|
178
|
+
return {
|
|
179
|
+
size: this.cache.size,
|
|
180
|
+
window: this.window,
|
|
181
|
+
entries
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
// src/core/queue.ts
|
|
187
|
+
var RetryQueue = class {
|
|
188
|
+
/**
|
|
189
|
+
* Queue of errors waiting to be sent
|
|
190
|
+
*/
|
|
191
|
+
queue = [];
|
|
192
|
+
/**
|
|
193
|
+
* Whether the queue is currently processing
|
|
194
|
+
*/
|
|
195
|
+
processing = false;
|
|
196
|
+
/**
|
|
197
|
+
* Maximum number of retry attempts
|
|
198
|
+
*/
|
|
199
|
+
maxRetries = 5;
|
|
200
|
+
/**
|
|
201
|
+
* Base backoff delay in milliseconds
|
|
202
|
+
*/
|
|
203
|
+
baseDelay = 1e3;
|
|
204
|
+
// 1 second
|
|
205
|
+
/**
|
|
206
|
+
* Maximum backoff delay in milliseconds
|
|
207
|
+
*/
|
|
208
|
+
maxDelay = 16e3;
|
|
209
|
+
// 16 seconds
|
|
210
|
+
/**
|
|
211
|
+
* Function to send errors
|
|
212
|
+
*/
|
|
213
|
+
sendFn;
|
|
214
|
+
/**
|
|
215
|
+
* Debug mode
|
|
216
|
+
*/
|
|
217
|
+
debug;
|
|
218
|
+
/**
|
|
219
|
+
* Processing interval ID
|
|
220
|
+
*/
|
|
221
|
+
intervalId;
|
|
222
|
+
/**
|
|
223
|
+
* Create a new retry queue
|
|
224
|
+
*
|
|
225
|
+
* @param sendFn - Function to send errors
|
|
226
|
+
* @param options - Configuration options
|
|
227
|
+
*/
|
|
228
|
+
constructor(sendFn, options) {
|
|
229
|
+
this.sendFn = sendFn;
|
|
230
|
+
this.maxRetries = options?.maxRetries ?? 5;
|
|
231
|
+
this.baseDelay = options?.baseDelay ?? 1e3;
|
|
232
|
+
this.maxDelay = options?.maxDelay ?? 16e3;
|
|
233
|
+
this.debug = options?.debug ?? false;
|
|
234
|
+
this.startProcessing();
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Enqueue an error for sending
|
|
238
|
+
*
|
|
239
|
+
* This is non-blocking and returns immediately.
|
|
240
|
+
*
|
|
241
|
+
* @param error - The error to send
|
|
242
|
+
* @param sourceContext - Source context (optional)
|
|
243
|
+
*/
|
|
244
|
+
enqueue(error, sourceContext) {
|
|
245
|
+
const queuedError = {
|
|
246
|
+
error,
|
|
247
|
+
sourceContext,
|
|
248
|
+
attempts: 0,
|
|
249
|
+
nextRetry: Date.now()
|
|
250
|
+
// Send immediately on first attempt
|
|
251
|
+
};
|
|
252
|
+
this.queue.push(queuedError);
|
|
253
|
+
this.log(`Enqueued error: ${error.id} (queue size: ${this.queue.length})`);
|
|
254
|
+
this.processQueue();
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Get the current queue size
|
|
258
|
+
*/
|
|
259
|
+
get size() {
|
|
260
|
+
return this.queue.length;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Clear all items from the queue
|
|
264
|
+
*/
|
|
265
|
+
clear() {
|
|
266
|
+
this.queue = [];
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Destroy the queue and stop processing
|
|
270
|
+
*/
|
|
271
|
+
destroy() {
|
|
272
|
+
if (this.intervalId) {
|
|
273
|
+
clearInterval(this.intervalId);
|
|
274
|
+
this.intervalId = void 0;
|
|
275
|
+
}
|
|
276
|
+
this.queue = [];
|
|
277
|
+
this.processing = false;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Start periodic queue processing
|
|
281
|
+
*/
|
|
282
|
+
startProcessing() {
|
|
283
|
+
this.intervalId = setInterval(() => {
|
|
284
|
+
this.processQueue();
|
|
285
|
+
}, 500);
|
|
286
|
+
if (this.intervalId.unref) {
|
|
287
|
+
this.intervalId.unref();
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Process the queue
|
|
292
|
+
*
|
|
293
|
+
* This is called periodically and also immediately after enqueue.
|
|
294
|
+
* Non-blocking - errors are caught and logged.
|
|
295
|
+
*/
|
|
296
|
+
async processQueue() {
|
|
297
|
+
if (this.processing || this.queue.length === 0) {
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
this.processing = true;
|
|
301
|
+
try {
|
|
302
|
+
const now = Date.now();
|
|
303
|
+
const readyErrors = this.queue.filter((item) => item.nextRetry <= now);
|
|
304
|
+
if (readyErrors.length === 0) {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
this.log(`Processing ${readyErrors.length} ready errors`);
|
|
308
|
+
for (const item of readyErrors) {
|
|
309
|
+
await this.processItem(item);
|
|
310
|
+
}
|
|
311
|
+
} catch (err) {
|
|
312
|
+
this.logError("Queue processing error:", err);
|
|
313
|
+
} finally {
|
|
314
|
+
this.processing = false;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Process a single queued error
|
|
319
|
+
*
|
|
320
|
+
* @param item - The queued error item
|
|
321
|
+
*/
|
|
322
|
+
async processItem(item) {
|
|
323
|
+
try {
|
|
324
|
+
item.attempts++;
|
|
325
|
+
item.lastAttempt = Date.now();
|
|
326
|
+
this.log(
|
|
327
|
+
`Sending error ${item.error.id} (attempt ${item.attempts}/${this.maxRetries})`
|
|
328
|
+
);
|
|
329
|
+
const success = await this.sendFn(item.error, item.sourceContext);
|
|
330
|
+
if (success) {
|
|
331
|
+
this.log(`Successfully sent error: ${item.error.id}`);
|
|
332
|
+
this.removeFromQueue(item);
|
|
333
|
+
} else {
|
|
334
|
+
this.handleFailure(item);
|
|
335
|
+
}
|
|
336
|
+
} catch (err) {
|
|
337
|
+
this.logError(`Error sending ${item.error.id}:`, err);
|
|
338
|
+
this.handleFailure(item);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Handle a failed send attempt
|
|
343
|
+
*
|
|
344
|
+
* @param item - The queued error item
|
|
345
|
+
*/
|
|
346
|
+
handleFailure(item) {
|
|
347
|
+
if (item.attempts >= this.maxRetries) {
|
|
348
|
+
this.logError(
|
|
349
|
+
`Max retries exceeded for error ${item.error.id}, dropping`
|
|
350
|
+
);
|
|
351
|
+
this.removeFromQueue(item);
|
|
352
|
+
} else {
|
|
353
|
+
const delay = this.calculateBackoff(item.attempts);
|
|
354
|
+
item.nextRetry = Date.now() + delay;
|
|
355
|
+
this.log(
|
|
356
|
+
`Scheduling retry for error ${item.error.id} in ${delay}ms (attempt ${item.attempts}/${this.maxRetries})`
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Calculate exponential backoff delay
|
|
362
|
+
*
|
|
363
|
+
* Backoff formula: min(baseDelay * 2^(attempts - 1), maxDelay)
|
|
364
|
+
* - Attempt 1: 1s
|
|
365
|
+
* - Attempt 2: 2s
|
|
366
|
+
* - Attempt 3: 4s
|
|
367
|
+
* - Attempt 4: 8s
|
|
368
|
+
* - Attempt 5: 16s
|
|
369
|
+
*
|
|
370
|
+
* @param attempts - Number of attempts
|
|
371
|
+
* @returns Delay in milliseconds
|
|
372
|
+
*/
|
|
373
|
+
calculateBackoff(attempts) {
|
|
374
|
+
const delay = this.baseDelay * Math.pow(2, attempts - 1);
|
|
375
|
+
return Math.min(delay, this.maxDelay);
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Remove an item from the queue
|
|
379
|
+
*
|
|
380
|
+
* @param item - The item to remove
|
|
381
|
+
*/
|
|
382
|
+
removeFromQueue(item) {
|
|
383
|
+
const index = this.queue.indexOf(item);
|
|
384
|
+
if (index !== -1) {
|
|
385
|
+
this.queue.splice(index, 1);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Log a message (if debug mode is enabled)
|
|
390
|
+
*
|
|
391
|
+
* @param message - Message to log
|
|
392
|
+
*/
|
|
393
|
+
log(message) {
|
|
394
|
+
if (this.debug) {
|
|
395
|
+
console.log(`[BugStack Queue] ${message}`);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Log an error (if debug mode is enabled)
|
|
400
|
+
*
|
|
401
|
+
* @param message - Message to log
|
|
402
|
+
* @param error - Error object (optional)
|
|
403
|
+
*/
|
|
404
|
+
logError(message, error) {
|
|
405
|
+
if (this.debug) {
|
|
406
|
+
if (error) {
|
|
407
|
+
console.error(`[BugStack Queue] ${message}`, error);
|
|
408
|
+
} else {
|
|
409
|
+
console.error(`[BugStack Queue] ${message}`);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Get queue statistics (for debugging)
|
|
415
|
+
*/
|
|
416
|
+
getStats() {
|
|
417
|
+
const now = Date.now();
|
|
418
|
+
return {
|
|
419
|
+
size: this.queue.length,
|
|
420
|
+
processing: this.processing,
|
|
421
|
+
items: this.queue.map((item) => ({
|
|
422
|
+
errorId: item.error.id,
|
|
423
|
+
attempts: item.attempts,
|
|
424
|
+
nextRetry: item.nextRetry,
|
|
425
|
+
timeUntilRetry: Math.max(0, item.nextRetry - now)
|
|
426
|
+
}))
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
};
|
|
9
430
|
|
|
10
|
-
// src/client.ts
|
|
11
|
-
var SDK_VERSION = "
|
|
431
|
+
// src/core/client.ts
|
|
432
|
+
var SDK_VERSION = "0.2.0";
|
|
12
433
|
var DEFAULT_ENDPOINT = "http://localhost:3001/api/capture";
|
|
13
434
|
var DEFAULT_TIMEOUT = 5e3;
|
|
14
435
|
var DEFAULT_MAX_BODY_SIZE = 1e4;
|
|
436
|
+
var DEFAULT_DEDUPLICATION_WINDOW = 5 * 60 * 1e3;
|
|
15
437
|
var DEFAULT_REDACT_FIELDS = [
|
|
16
438
|
"password",
|
|
17
439
|
"secret",
|
|
@@ -33,6 +455,8 @@ var DEFAULT_REDACT_FIELDS = [
|
|
|
33
455
|
var ErrorCaptureClient = class _ErrorCaptureClient {
|
|
34
456
|
config;
|
|
35
457
|
static instance = null;
|
|
458
|
+
deduplicator;
|
|
459
|
+
retryQueue;
|
|
36
460
|
constructor(config) {
|
|
37
461
|
const isProduction = process.env.NODE_ENV === "production";
|
|
38
462
|
this.config = {
|
|
@@ -40,6 +464,7 @@ var ErrorCaptureClient = class _ErrorCaptureClient {
|
|
|
40
464
|
endpoint: config.endpoint ?? process.env.BUGSTACK_ENDPOINT ?? DEFAULT_ENDPOINT,
|
|
41
465
|
projectId: config.projectId ?? process.env.BUGSTACK_PROJECT_ID ?? "",
|
|
42
466
|
environment: config.environment ?? process.env.NODE_ENV ?? "development",
|
|
467
|
+
autoFix: config.autoFix ?? false,
|
|
43
468
|
enabled: config.enabled ?? true,
|
|
44
469
|
captureRequestBody: config.captureRequestBody ?? true,
|
|
45
470
|
captureQueryParams: config.captureQueryParams ?? true,
|
|
@@ -51,15 +476,36 @@ var ErrorCaptureClient = class _ErrorCaptureClient {
|
|
|
51
476
|
],
|
|
52
477
|
metadata: config.metadata ?? {},
|
|
53
478
|
timeout: config.timeout ?? DEFAULT_TIMEOUT,
|
|
479
|
+
deduplicationWindow: config.deduplicationWindow ?? DEFAULT_DEDUPLICATION_WINDOW,
|
|
54
480
|
debug: config.debug ?? !isProduction,
|
|
55
481
|
shouldCapture: config.shouldCapture,
|
|
56
482
|
beforeSend: config.beforeSend
|
|
57
483
|
};
|
|
484
|
+
this.deduplicator = new ErrorDeduplicator(this.config.deduplicationWindow);
|
|
485
|
+
this.retryQueue = new RetryQueue(
|
|
486
|
+
(error, sourceContext) => this.sendErrorToService(error, sourceContext),
|
|
487
|
+
{
|
|
488
|
+
maxRetries: 5,
|
|
489
|
+
baseDelay: 1e3,
|
|
490
|
+
maxDelay: 16e3,
|
|
491
|
+
debug: this.config.debug
|
|
492
|
+
}
|
|
493
|
+
);
|
|
494
|
+
this.log("BugStack client initialized", {
|
|
495
|
+
endpoint: this.config.endpoint,
|
|
496
|
+
projectId: this.config.projectId,
|
|
497
|
+
environment: this.config.environment,
|
|
498
|
+
autoFix: this.config.autoFix,
|
|
499
|
+
deduplicationWindow: this.config.deduplicationWindow
|
|
500
|
+
});
|
|
58
501
|
}
|
|
59
502
|
/**
|
|
60
503
|
* Initialize the singleton client instance
|
|
61
504
|
*/
|
|
62
505
|
static init(config) {
|
|
506
|
+
if (_ErrorCaptureClient.instance) {
|
|
507
|
+
_ErrorCaptureClient.instance.destroy();
|
|
508
|
+
}
|
|
63
509
|
_ErrorCaptureClient.instance = new _ErrorCaptureClient(config);
|
|
64
510
|
return _ErrorCaptureClient.instance;
|
|
65
511
|
}
|
|
@@ -79,10 +525,19 @@ var ErrorCaptureClient = class _ErrorCaptureClient {
|
|
|
79
525
|
* Reset the client (mainly for testing)
|
|
80
526
|
*/
|
|
81
527
|
static reset() {
|
|
528
|
+
if (_ErrorCaptureClient.instance) {
|
|
529
|
+
_ErrorCaptureClient.instance.destroy();
|
|
530
|
+
}
|
|
82
531
|
_ErrorCaptureClient.instance = null;
|
|
83
532
|
}
|
|
84
533
|
/**
|
|
85
534
|
* Report an error to the BugStack service
|
|
535
|
+
*
|
|
536
|
+
* This method includes deduplication and queuing for resilience.
|
|
537
|
+
*
|
|
538
|
+
* @param error - The captured error
|
|
539
|
+
* @param sourceContext - Source code context (optional)
|
|
540
|
+
* @returns Promise that resolves to true if error was accepted (not deduplicated)
|
|
86
541
|
*/
|
|
87
542
|
async reportError(error, sourceContext) {
|
|
88
543
|
if (!this.config.enabled) {
|
|
@@ -98,16 +553,35 @@ var ErrorCaptureClient = class _ErrorCaptureClient {
|
|
|
98
553
|
}
|
|
99
554
|
errorToSend = modified;
|
|
100
555
|
}
|
|
556
|
+
const fingerprint = generateErrorFingerprint(errorToSend);
|
|
557
|
+
errorToSend.fingerprint = fingerprint;
|
|
558
|
+
if (!this.deduplicator.shouldSend(fingerprint)) {
|
|
559
|
+
this.log(`Error deduplicated: ${error.id} (fingerprint: ${fingerprint})`);
|
|
560
|
+
return false;
|
|
561
|
+
}
|
|
562
|
+
this.log(`Reporting error: ${error.id} (fingerprint: ${fingerprint})`);
|
|
563
|
+
errorToSend.projectId = this.config.projectId || errorToSend.projectId;
|
|
564
|
+
errorToSend.environment = this.config.environment;
|
|
565
|
+
errorToSend.metadata = {
|
|
566
|
+
...this.config.metadata,
|
|
567
|
+
...errorToSend.metadata,
|
|
568
|
+
autoFix: this.config.autoFix
|
|
569
|
+
};
|
|
570
|
+
this.retryQueue.enqueue(errorToSend, sourceContext);
|
|
571
|
+
return true;
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Send an error to the service (internal method)
|
|
575
|
+
*
|
|
576
|
+
* This is used by the retry queue. Returns true on success, false on failure.
|
|
577
|
+
*
|
|
578
|
+
* @param error - The error to send
|
|
579
|
+
* @param sourceContext - Source context (optional)
|
|
580
|
+
* @returns Promise that resolves to true on success
|
|
581
|
+
*/
|
|
582
|
+
async sendErrorToService(error, sourceContext) {
|
|
101
583
|
const payload = {
|
|
102
|
-
error
|
|
103
|
-
...errorToSend,
|
|
104
|
-
projectId: this.config.projectId || errorToSend.projectId,
|
|
105
|
-
environment: this.config.environment,
|
|
106
|
-
metadata: {
|
|
107
|
-
...this.config.metadata,
|
|
108
|
-
...errorToSend.metadata
|
|
109
|
-
}
|
|
110
|
-
},
|
|
584
|
+
error,
|
|
111
585
|
sourceContext
|
|
112
586
|
};
|
|
113
587
|
try {
|
|
@@ -129,17 +603,17 @@ var ErrorCaptureClient = class _ErrorCaptureClient {
|
|
|
129
603
|
clearTimeout(timeoutId);
|
|
130
604
|
if (!response.ok) {
|
|
131
605
|
this.logError(
|
|
132
|
-
`Failed to
|
|
606
|
+
`Failed to send error: HTTP ${response.status} ${response.statusText}`
|
|
133
607
|
);
|
|
134
608
|
return false;
|
|
135
609
|
}
|
|
136
|
-
this.log(`Error
|
|
610
|
+
this.log(`Error sent successfully: ${error.id}`);
|
|
137
611
|
return true;
|
|
138
612
|
} catch (err) {
|
|
139
613
|
if (err instanceof Error && err.name === "AbortError") {
|
|
140
|
-
this.logError(`Error
|
|
614
|
+
this.logError(`Error send timed out after ${this.config.timeout}ms`);
|
|
141
615
|
} else {
|
|
142
|
-
this.logError("Failed to send error
|
|
616
|
+
this.logError("Failed to send error:", err);
|
|
143
617
|
}
|
|
144
618
|
return false;
|
|
145
619
|
}
|
|
@@ -165,9 +639,31 @@ var ErrorCaptureClient = class _ErrorCaptureClient {
|
|
|
165
639
|
static getVersion() {
|
|
166
640
|
return SDK_VERSION;
|
|
167
641
|
}
|
|
168
|
-
|
|
642
|
+
/**
|
|
643
|
+
* Get client statistics (for debugging)
|
|
644
|
+
*/
|
|
645
|
+
getStats() {
|
|
646
|
+
return {
|
|
647
|
+
deduplicator: this.deduplicator.getStats(),
|
|
648
|
+
retryQueue: this.retryQueue.getStats()
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
/**
|
|
652
|
+
* Clean up resources
|
|
653
|
+
*
|
|
654
|
+
* Should be called when shutting down to prevent memory leaks
|
|
655
|
+
*/
|
|
656
|
+
destroy() {
|
|
657
|
+
this.deduplicator.destroy();
|
|
658
|
+
this.retryQueue.destroy();
|
|
659
|
+
}
|
|
660
|
+
log(message, data) {
|
|
169
661
|
if (this.config.debug) {
|
|
170
|
-
|
|
662
|
+
if (data) {
|
|
663
|
+
console.log(`[BugStack] ${message}`, data);
|
|
664
|
+
} else {
|
|
665
|
+
console.log(`[BugStack] ${message}`);
|
|
666
|
+
}
|
|
171
667
|
}
|
|
172
668
|
}
|
|
173
669
|
logError(message, error) {
|
|
@@ -182,7 +678,7 @@ var ErrorCaptureClient = class _ErrorCaptureClient {
|
|
|
182
678
|
};
|
|
183
679
|
var BugStackClient = ErrorCaptureClient;
|
|
184
680
|
|
|
185
|
-
// src/sanitize.ts
|
|
681
|
+
// src/core/utils/sanitize.ts
|
|
186
682
|
var REDACT_PLACEHOLDER = "[REDACTED]";
|
|
187
683
|
var TRUNCATE_PLACEHOLDER = "[TRUNCATED]";
|
|
188
684
|
var SENSITIVE_HEADERS = /* @__PURE__ */ new Set([
|
|
@@ -333,7 +829,7 @@ async function safeParseBody(request, maxSize, client) {
|
|
|
333
829
|
}
|
|
334
830
|
}
|
|
335
831
|
|
|
336
|
-
// src/stack-parser.ts
|
|
832
|
+
// src/core/utils/stack-parser.ts
|
|
337
833
|
var V8_STACK_FRAME_REGEX = /^\s*at\s+(?:async\s+)?(?:(.+?)\s+\()?(?:(.+?):(\d+):(\d+))\)?$/;
|
|
338
834
|
function isNodeModule(filePath) {
|
|
339
835
|
return filePath.includes("node_modules");
|
|
@@ -374,141 +870,30 @@ function getFirstAppFrame(frames) {
|
|
|
374
870
|
function getAppFrames(frames) {
|
|
375
871
|
return frames.filter((frame) => !frame.isNodeModule && !frame.isInternal);
|
|
376
872
|
}
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
const random = Math.random().toString(36).substring(2, 9);
|
|
383
|
-
return `err_${timestamp}_${random}`;
|
|
384
|
-
}
|
|
385
|
-
function extractRoute(request) {
|
|
386
|
-
try {
|
|
387
|
-
const url = new URL(request.url);
|
|
388
|
-
return url.pathname;
|
|
389
|
-
} catch {
|
|
390
|
-
return "unknown";
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
function getNodeVersion() {
|
|
394
|
-
try {
|
|
395
|
-
return process.version;
|
|
396
|
-
} catch {
|
|
397
|
-
return "unknown";
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
function getNextVersion() {
|
|
401
|
-
try {
|
|
402
|
-
const pkg = __require("next/package.json");
|
|
403
|
-
return pkg.version;
|
|
404
|
-
} catch {
|
|
405
|
-
return void 0;
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
async function buildCapturedError(error, request, context, client, routeOptions) {
|
|
409
|
-
const config = client.getConfig();
|
|
410
|
-
const url = new URL(request.url);
|
|
411
|
-
const stackFrames = parseStackTrace(error.stack);
|
|
412
|
-
const firstAppFrame = getFirstAppFrame(stackFrames);
|
|
413
|
-
const capturedError = {
|
|
414
|
-
id: generateErrorId(),
|
|
415
|
-
name: error.name || "Error",
|
|
416
|
-
message: error.message,
|
|
417
|
-
stack: error.stack,
|
|
418
|
-
stackFrames,
|
|
419
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
420
|
-
route: extractRoute(request),
|
|
421
|
-
method: request.method,
|
|
422
|
-
statusCode: routeOptions?.statusCode ?? 500,
|
|
423
|
-
url: request.url,
|
|
424
|
-
environment: config.environment,
|
|
425
|
-
projectId: config.projectId,
|
|
426
|
-
sdkVersion: SDK_VERSION2,
|
|
427
|
-
nodeVersion: getNodeVersion(),
|
|
428
|
-
nextVersion: getNextVersion()
|
|
429
|
-
};
|
|
430
|
-
if (context?.params && Object.keys(context.params).length > 0) {
|
|
431
|
-
capturedError.routeParams = context.params;
|
|
432
|
-
}
|
|
433
|
-
if (config.captureQueryParams && url.searchParams.toString()) {
|
|
434
|
-
capturedError.queryParams = sanitizeQueryParams(url, client);
|
|
435
|
-
}
|
|
436
|
-
if (config.captureHeaders) {
|
|
437
|
-
capturedError.requestHeaders = sanitizeHeaders(request.headers, client);
|
|
438
|
-
}
|
|
439
|
-
if (config.captureRequestBody && hasBody(request.method)) {
|
|
440
|
-
const bodyResult = await safeParseBody(request, config.maxBodySize, client);
|
|
441
|
-
if (bodyResult) {
|
|
442
|
-
capturedError.requestBody = bodyResult.body;
|
|
443
|
-
if (bodyResult.truncated) {
|
|
444
|
-
capturedError.metadata = {
|
|
445
|
-
...capturedError.metadata,
|
|
446
|
-
bodyTruncated: true
|
|
447
|
-
};
|
|
448
|
-
}
|
|
873
|
+
function formatStackFrames(frames) {
|
|
874
|
+
return frames.map((frame) => {
|
|
875
|
+
const location = [frame.file, frame.line, frame.column].filter(Boolean).join(":");
|
|
876
|
+
if (frame.function) {
|
|
877
|
+
return ` at ${frame.function} (${location})`;
|
|
449
878
|
}
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
capturedError.metadata = {
|
|
453
|
-
...capturedError.metadata,
|
|
454
|
-
sourceLocation: {
|
|
455
|
-
file: firstAppFrame.file,
|
|
456
|
-
line: firstAppFrame.line,
|
|
457
|
-
column: firstAppFrame.column,
|
|
458
|
-
function: firstAppFrame.function
|
|
459
|
-
}
|
|
460
|
-
};
|
|
461
|
-
}
|
|
462
|
-
if (routeOptions?.metadata) {
|
|
463
|
-
capturedError.metadata = {
|
|
464
|
-
...capturedError.metadata,
|
|
465
|
-
...routeOptions.metadata
|
|
466
|
-
};
|
|
467
|
-
}
|
|
468
|
-
return capturedError;
|
|
469
|
-
}
|
|
470
|
-
function hasBody(method) {
|
|
471
|
-
return ["POST", "PUT", "PATCH", "DELETE"].includes(method.toUpperCase());
|
|
472
|
-
}
|
|
473
|
-
function withErrorCapture(handler, config, routeOptions) {
|
|
474
|
-
if (config && !ErrorCaptureClient.isInitialized()) {
|
|
475
|
-
ErrorCaptureClient.init(config);
|
|
476
|
-
}
|
|
477
|
-
return async (request, context) => {
|
|
478
|
-
const client = ErrorCaptureClient.getInstance();
|
|
479
|
-
try {
|
|
480
|
-
const response = await handler(request, context);
|
|
481
|
-
return response;
|
|
482
|
-
} catch (error) {
|
|
483
|
-
const err = error instanceof Error ? error : new Error(String(error));
|
|
484
|
-
if (client) {
|
|
485
|
-
const clientConfig = client.getConfig();
|
|
486
|
-
if (clientConfig.shouldCapture && !clientConfig.shouldCapture(err, request)) {
|
|
487
|
-
throw error;
|
|
488
|
-
}
|
|
489
|
-
if (routeOptions?.shouldCapture && !routeOptions.shouldCapture(err)) {
|
|
490
|
-
throw error;
|
|
491
|
-
}
|
|
492
|
-
buildCapturedError(err, request, context, client, routeOptions).then((capturedError) => {
|
|
493
|
-
return client.reportError(capturedError);
|
|
494
|
-
}).catch(() => {
|
|
495
|
-
});
|
|
496
|
-
}
|
|
497
|
-
throw error;
|
|
498
|
-
}
|
|
499
|
-
};
|
|
879
|
+
return ` at ${location}`;
|
|
880
|
+
}).join("\n");
|
|
500
881
|
}
|
|
501
|
-
var withBugStack = withErrorCapture;
|
|
502
882
|
|
|
503
883
|
exports.BugStackClient = BugStackClient;
|
|
504
884
|
exports.ErrorCaptureClient = ErrorCaptureClient;
|
|
885
|
+
exports.ErrorDeduplicator = ErrorDeduplicator;
|
|
886
|
+
exports.RetryQueue = RetryQueue;
|
|
887
|
+
exports.formatStackFrames = formatStackFrames;
|
|
888
|
+
exports.generateErrorFingerprint = generateErrorFingerprint;
|
|
889
|
+
exports.generateFingerprintFromParts = generateFingerprintFromParts;
|
|
505
890
|
exports.getAppFrames = getAppFrames;
|
|
506
891
|
exports.getFirstAppFrame = getFirstAppFrame;
|
|
507
892
|
exports.parseStackTrace = parseStackTrace;
|
|
893
|
+
exports.safeParseBody = safeParseBody;
|
|
508
894
|
exports.sanitizeHeaders = sanitizeHeaders;
|
|
509
895
|
exports.sanitizeObject = sanitizeObject;
|
|
510
896
|
exports.sanitizeQueryParams = sanitizeQueryParams;
|
|
511
|
-
exports.
|
|
512
|
-
exports.withErrorCapture = withErrorCapture;
|
|
897
|
+
exports.truncateBody = truncateBody;
|
|
513
898
|
//# sourceMappingURL=index.js.map
|
|
514
899
|
//# sourceMappingURL=index.js.map
|