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