bugstack-sdk 2.2.1 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +139 -96
- package/README.md +496 -421
- package/bin/bugstack.cjs +6 -6
- package/dist/adapters/express/index.d.mts +2 -2
- package/dist/adapters/express/index.d.ts +2 -2
- package/dist/adapters/express/index.js +409 -43
- package/dist/adapters/express/index.js.map +1 -1
- package/dist/adapters/express/index.mjs +409 -43
- package/dist/adapters/express/index.mjs.map +1 -1
- package/dist/adapters/generic/index.d.mts +2 -2
- package/dist/adapters/generic/index.d.ts +2 -2
- package/dist/adapters/generic/index.js +457 -43
- package/dist/adapters/generic/index.js.map +1 -1
- package/dist/adapters/generic/index.mjs +454 -44
- package/dist/adapters/generic/index.mjs.map +1 -1
- package/dist/adapters/nextjs/index.d.mts +2 -2
- package/dist/adapters/nextjs/index.d.ts +2 -2
- package/dist/adapters/nextjs/index.js +409 -43
- package/dist/adapters/nextjs/index.js.map +1 -1
- package/dist/adapters/nextjs/index.mjs +409 -43
- package/dist/adapters/nextjs/index.mjs.map +1 -1
- package/dist/build/cli.js.map +1 -1
- package/dist/build/cli.mjs.map +1 -1
- package/dist/build/upload.js.map +1 -1
- package/dist/build/upload.mjs.map +1 -1
- package/dist/{client-DoqE7sNh.d.mts → client-B9WaTEYj.d.mts} +149 -2
- package/dist/{client-DoqE7sNh.d.ts → client-B9WaTEYj.d.ts} +149 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +469 -55
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +466 -56
- package/dist/index.mjs.map +1 -1
- package/dist/next-config/index.js.map +1 -1
- package/dist/next-config/index.mjs.map +1 -1
- package/package.json +107 -104
package/dist/index.js
CHANGED
|
@@ -435,11 +435,454 @@ var RetryQueue = class {
|
|
|
435
435
|
}
|
|
436
436
|
};
|
|
437
437
|
|
|
438
|
+
// src/core/utils/stack-parser.ts
|
|
439
|
+
var V8_STACK_FRAME_REGEX = /^\s*at\s+(?:async\s+)?(?:(.+?)\s+\()?(?:(.+?):(\d+):(\d+))\)?$/;
|
|
440
|
+
function isNodeModule(filePath) {
|
|
441
|
+
return filePath.includes("node_modules");
|
|
442
|
+
}
|
|
443
|
+
function isInternalCode(filePath) {
|
|
444
|
+
return filePath.startsWith("node:") || filePath.startsWith("internal/") || // A bare name ("native", "<anonymous>") has no directory. A Windows
|
|
445
|
+
// path has backslashes and is app code.
|
|
446
|
+
!filePath.includes("/") && !filePath.includes("\\");
|
|
447
|
+
}
|
|
448
|
+
function cleanFunctionName(name) {
|
|
449
|
+
if (!name) return void 0;
|
|
450
|
+
return name.replace(/^Object\./, "").replace(/^Module\./, "").replace(/^__webpack_require__\./, "").replace(/^webpack_require\./, "").trim();
|
|
451
|
+
}
|
|
452
|
+
function parseStackTrace(stack) {
|
|
453
|
+
if (!stack) return [];
|
|
454
|
+
const lines = stack.split("\n");
|
|
455
|
+
const frames = [];
|
|
456
|
+
for (const line of lines) {
|
|
457
|
+
const match = V8_STACK_FRAME_REGEX.exec(line);
|
|
458
|
+
if (match) {
|
|
459
|
+
const [, functionName, file, lineStr, columnStr] = match;
|
|
460
|
+
const frame = {
|
|
461
|
+
function: cleanFunctionName(functionName),
|
|
462
|
+
file: file ?? void 0,
|
|
463
|
+
line: lineStr ? parseInt(lineStr, 10) : void 0,
|
|
464
|
+
column: columnStr ? parseInt(columnStr, 10) : void 0
|
|
465
|
+
};
|
|
466
|
+
if (frame.file) {
|
|
467
|
+
frame.isNodeModule = isNodeModule(frame.file);
|
|
468
|
+
frame.isInternal = isInternalCode(frame.file);
|
|
469
|
+
}
|
|
470
|
+
frames.push(frame);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
return frames;
|
|
474
|
+
}
|
|
475
|
+
function getFirstAppFrame(frames) {
|
|
476
|
+
return frames.find((frame) => !frame.isNodeModule && !frame.isInternal);
|
|
477
|
+
}
|
|
478
|
+
function getAppFrames(frames) {
|
|
479
|
+
return frames.filter((frame) => !frame.isNodeModule && !frame.isInternal);
|
|
480
|
+
}
|
|
481
|
+
function formatStackFrames(frames) {
|
|
482
|
+
return frames.map((frame) => {
|
|
483
|
+
const location = [frame.file, frame.line, frame.column].filter(Boolean).join(":");
|
|
484
|
+
if (frame.function) {
|
|
485
|
+
return ` at ${frame.function} (${location})`;
|
|
486
|
+
}
|
|
487
|
+
return ` at ${location}`;
|
|
488
|
+
}).join("\n");
|
|
489
|
+
}
|
|
490
|
+
|
|
438
491
|
// src/core/sdk-version.ts
|
|
439
|
-
var SDK_VERSION = "2.
|
|
492
|
+
var SDK_VERSION = "2.3.0" ;
|
|
493
|
+
|
|
494
|
+
// src/core/logged-errors.ts
|
|
495
|
+
var DEFAULT_MAX_PER_MINUTE = 20;
|
|
496
|
+
var DEFAULT_REPEAT_WINDOW_MS = 5 * 6e4;
|
|
497
|
+
var MAX_REMEMBERED = 500;
|
|
498
|
+
var MESSAGE_MAX = 300;
|
|
499
|
+
var ANCHORED_MESSAGE_MAX = 500;
|
|
500
|
+
var CAUSE_STACK_MAX = 2e3;
|
|
501
|
+
function resolveLoggedErrorOptions(value) {
|
|
502
|
+
const opts = typeof value === "object" && value !== null ? value : {};
|
|
503
|
+
return {
|
|
504
|
+
console: value !== false,
|
|
505
|
+
messages: opts.messages ?? false,
|
|
506
|
+
maxPerMinute: opts.maxPerMinute ?? DEFAULT_MAX_PER_MINUTE,
|
|
507
|
+
repeatWindowMs: opts.repeatWindowMs ?? DEFAULT_REPEAT_WINDOW_MS,
|
|
508
|
+
ignore: opts.ignore ?? []
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
var SHARED_KEY = /* @__PURE__ */ Symbol.for("bugstack-sdk.logged-errors");
|
|
512
|
+
function emptyStats() {
|
|
513
|
+
return {
|
|
514
|
+
forwarded: 0,
|
|
515
|
+
messageOnly: 0,
|
|
516
|
+
dropped: {
|
|
517
|
+
no_error_argument: 0,
|
|
518
|
+
sdk_line: 0,
|
|
519
|
+
node_warning: 0,
|
|
520
|
+
ignored: 0,
|
|
521
|
+
already_reported: 0,
|
|
522
|
+
repeat: 0,
|
|
523
|
+
rate_limited: 0,
|
|
524
|
+
no_client: 0
|
|
525
|
+
}
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
function shared() {
|
|
529
|
+
const g = globalThis;
|
|
530
|
+
let s = g[SHARED_KEY];
|
|
531
|
+
if (!s) {
|
|
532
|
+
s = {
|
|
533
|
+
reported: /* @__PURE__ */ new WeakSet(),
|
|
534
|
+
sink: null,
|
|
535
|
+
consoleOriginal: null,
|
|
536
|
+
consoleWrapper: null,
|
|
537
|
+
busy: false,
|
|
538
|
+
windowStart: 0,
|
|
539
|
+
inWindow: 0,
|
|
540
|
+
lastSent: /* @__PURE__ */ new Map(),
|
|
541
|
+
stats: emptyStats(),
|
|
542
|
+
now: Date.now
|
|
543
|
+
};
|
|
544
|
+
g[SHARED_KEY] = s;
|
|
545
|
+
}
|
|
546
|
+
return s;
|
|
547
|
+
}
|
|
548
|
+
var defer = typeof setImmediate === "function" ? (fn) => void setImmediate(fn) : (fn) => void setTimeout(fn, 0);
|
|
549
|
+
function markErrorReported(error) {
|
|
550
|
+
try {
|
|
551
|
+
if (error !== null && typeof error === "object") shared().reported.add(error);
|
|
552
|
+
} catch {
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
function loggedErrorStats() {
|
|
556
|
+
const s = shared();
|
|
557
|
+
return {
|
|
558
|
+
consoleInstalled: s.consoleWrapper !== null && s.sink !== null && s.sink.options.console,
|
|
559
|
+
forwarded: s.stats.forwarded,
|
|
560
|
+
messageOnly: s.stats.messageOnly,
|
|
561
|
+
dropped: { ...s.stats.dropped }
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
function isError(value) {
|
|
565
|
+
if (value instanceof Error) return true;
|
|
566
|
+
if (value === null || typeof value !== "object") return false;
|
|
567
|
+
try {
|
|
568
|
+
const v = value;
|
|
569
|
+
return typeof v.message === "string" && typeof v.stack === "string";
|
|
570
|
+
} catch {
|
|
571
|
+
return false;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
function findError(args) {
|
|
575
|
+
for (const a of args) if (isError(a)) return a;
|
|
576
|
+
for (const a of args) {
|
|
577
|
+
if (a === null || typeof a !== "object" || Array.isArray(a)) continue;
|
|
578
|
+
try {
|
|
579
|
+
const o = a;
|
|
580
|
+
for (const k of ["err", "error", "cause"]) if (isError(o[k])) return o[k];
|
|
581
|
+
} catch {
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
return void 0;
|
|
585
|
+
}
|
|
586
|
+
var STACK_TEXT = /(^|\n)\s*([A-Za-z_$][\w$.]*(?:Error|Exception)\b[^\n]*)\n\s+at \S/;
|
|
587
|
+
function findStackText(args) {
|
|
588
|
+
for (const a of args) {
|
|
589
|
+
if (typeof a !== "string") continue;
|
|
590
|
+
const m = STACK_TEXT.exec(a);
|
|
591
|
+
if (!m) continue;
|
|
592
|
+
const header = m[2].trim();
|
|
593
|
+
const colon = header.indexOf(":");
|
|
594
|
+
const err = new Error(colon > 0 ? header.slice(colon + 1).trim() : header);
|
|
595
|
+
err.name = colon > 0 ? header.slice(0, colon).trim() : "Error";
|
|
596
|
+
err.stack = a.slice(m.index + m[1].length).trim();
|
|
597
|
+
return err;
|
|
598
|
+
}
|
|
599
|
+
return void 0;
|
|
600
|
+
}
|
|
601
|
+
function messageOfArgs(args) {
|
|
602
|
+
const parts = [];
|
|
603
|
+
for (const a of args) {
|
|
604
|
+
if (typeof a === "string") parts.push(a);
|
|
605
|
+
else if (typeof a === "number" || typeof a === "boolean") parts.push(String(a));
|
|
606
|
+
else if (a !== null && typeof a === "object" && !Array.isArray(a) && !isError(a)) {
|
|
607
|
+
try {
|
|
608
|
+
const o = a;
|
|
609
|
+
for (const k of ["error", "message", "reason", "err", "msg"]) {
|
|
610
|
+
if (typeof o[k] === "string" && o[k].length > 0) {
|
|
611
|
+
parts.push(o[k]);
|
|
612
|
+
break;
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
} catch {
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
return parts.join(" ").replace(/\s+/g, " ").trim().slice(0, MESSAGE_MAX);
|
|
620
|
+
}
|
|
621
|
+
function logRoute(label) {
|
|
622
|
+
const tag = label?.match(/^\s*\[([A-Za-z0-9_.:/-]{1,60})\]/)?.[1];
|
|
623
|
+
return tag ? `log:${tag}` : "log";
|
|
624
|
+
}
|
|
625
|
+
var SDK_LINE = /^\s*\[bugstack[^\]]*\]/i;
|
|
626
|
+
var NODE_WARNING_LINE = /^\s*\(node:\d+\)\s+(?:\[[A-Z0-9_]+\]\s+)?\w*Warning:/;
|
|
627
|
+
function escapeRegExp(s) {
|
|
628
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
629
|
+
}
|
|
630
|
+
function redactText(text, redactFields) {
|
|
631
|
+
let out = text.replace(/\b(Bearer|Basic)\s+[A-Za-z0-9._~+/=-]{8,}/gi, "$1 [REDACTED]");
|
|
632
|
+
if (redactFields.length > 0) {
|
|
633
|
+
const names = redactFields.filter((f) => f.length > 0).map(escapeRegExp).join("|");
|
|
634
|
+
if (names) {
|
|
635
|
+
const re = new RegExp(
|
|
636
|
+
`(["']?\\b\\w*(?:${names})\\w*["']?\\s*[=:]\\s*)("[^"]*"|'[^']*'|[^\\s,;&)}\\]]+)`,
|
|
637
|
+
"gi"
|
|
638
|
+
);
|
|
639
|
+
out = out.replace(re, "$1[REDACTED]");
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
return out;
|
|
643
|
+
}
|
|
644
|
+
function firstFrame(stack) {
|
|
645
|
+
return (stack ?? "").split("\n").find((l) => /^\s+at /.test(l))?.trim() ?? "";
|
|
646
|
+
}
|
|
647
|
+
function trimLibraryFrames(stack) {
|
|
648
|
+
const lines = stack.split("\n");
|
|
649
|
+
const header = [];
|
|
650
|
+
let i = 0;
|
|
651
|
+
while (i < lines.length && !/^\s+at /.test(lines[i])) header.push(lines[i++]);
|
|
652
|
+
let j = i;
|
|
653
|
+
while (j < lines.length && /node_modules|[/\\]bugstack-sdk[/\\]|\(node:internal|^\s+at node:internal/.test(lines[j])) j++;
|
|
654
|
+
if (j >= lines.length) return stack;
|
|
655
|
+
return [...header, ...lines.slice(j)].join("\n");
|
|
656
|
+
}
|
|
657
|
+
function siteOf(label, cause) {
|
|
658
|
+
if (label === void 0) return "";
|
|
659
|
+
const first = label.split("\n")[0].trim();
|
|
660
|
+
if (!first || first === cause.message || first === `${cause.name}: ${cause.message}`) return "";
|
|
661
|
+
return first.slice(0, 200);
|
|
662
|
+
}
|
|
663
|
+
function matchesIgnore(text, ignore) {
|
|
664
|
+
for (const p of ignore) {
|
|
665
|
+
try {
|
|
666
|
+
if (typeof p === "string" ? text.includes(p) : p.test(text)) return true;
|
|
667
|
+
} catch {
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
return false;
|
|
671
|
+
}
|
|
672
|
+
function captureLoggedCall(args, source, boundary) {
|
|
673
|
+
const s = shared();
|
|
674
|
+
if (s.busy) return;
|
|
675
|
+
try {
|
|
676
|
+
const sink = s.sink;
|
|
677
|
+
if (!sink) {
|
|
678
|
+
s.stats.dropped.no_client += 1;
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
const label = args.find((a) => typeof a === "string");
|
|
682
|
+
if (label !== void 0 && SDK_LINE.test(label)) {
|
|
683
|
+
s.stats.dropped.sdk_line += 1;
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
686
|
+
if (label !== void 0 && NODE_WARNING_LINE.test(label)) {
|
|
687
|
+
s.stats.dropped.node_warning += 1;
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
const cause = findError(args) ?? findStackText(args) ?? null;
|
|
691
|
+
if (sink.options.ignore.length > 0 && matchesIgnore(`${messageOfArgs(args)} ${cause ? `${cause.name}: ${cause.message}` : ""}`, sink.options.ignore)) {
|
|
692
|
+
s.stats.dropped.ignored += 1;
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
let message;
|
|
696
|
+
if (cause) {
|
|
697
|
+
message = `${siteOf(label, cause) || source}: ${cause.message}`;
|
|
698
|
+
} else {
|
|
699
|
+
message = sink.options.messages ? messageOfArgs(args) : "";
|
|
700
|
+
if (!message) {
|
|
701
|
+
s.stats.dropped.no_error_argument += 1;
|
|
702
|
+
return;
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
const anchored = new Error(redactText(message, sink.redactFields).slice(0, ANCHORED_MESSAGE_MAX));
|
|
706
|
+
anchored.name = "LoggedError";
|
|
707
|
+
const capture = Error.captureStackTrace;
|
|
708
|
+
if (capture) capture(anchored, boundary);
|
|
709
|
+
if (anchored.stack) anchored.stack = trimLibraryFrames(anchored.stack);
|
|
710
|
+
if (cause) anchored.cause = cause;
|
|
711
|
+
const pending = { anchored, cause, label, source, messageOnly: !cause };
|
|
712
|
+
defer(() => forward(pending));
|
|
713
|
+
} catch {
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
function forward(p) {
|
|
717
|
+
const s = shared();
|
|
718
|
+
const sink = s.sink;
|
|
719
|
+
if (!sink) {
|
|
720
|
+
s.stats.dropped.no_client += 1;
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
try {
|
|
724
|
+
if (p.cause && s.reported.has(p.cause)) {
|
|
725
|
+
s.stats.dropped.already_reported += 1;
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
const now = s.now();
|
|
729
|
+
const key = p.messageOnly ? `${firstFrame(p.anchored.stack)}|message-only` : `${firstFrame(p.anchored.stack)}|${p.anchored.message}`;
|
|
730
|
+
const last = s.lastSent.get(key);
|
|
731
|
+
if (last !== void 0 && now - last < sink.options.repeatWindowMs) {
|
|
732
|
+
s.stats.dropped.repeat += 1;
|
|
733
|
+
return;
|
|
734
|
+
}
|
|
735
|
+
if (now - s.windowStart >= 6e4) {
|
|
736
|
+
s.windowStart = now;
|
|
737
|
+
s.inWindow = 0;
|
|
738
|
+
}
|
|
739
|
+
if (s.inWindow >= sink.options.maxPerMinute) {
|
|
740
|
+
s.stats.dropped.rate_limited += 1;
|
|
741
|
+
return;
|
|
742
|
+
}
|
|
743
|
+
s.inWindow += 1;
|
|
744
|
+
s.lastSent.set(key, now);
|
|
745
|
+
if (s.lastSent.size > MAX_REMEMBERED) {
|
|
746
|
+
const oldest = s.lastSent.keys().next().value;
|
|
747
|
+
if (oldest !== void 0) s.lastSent.delete(oldest);
|
|
748
|
+
}
|
|
749
|
+
if (p.cause) s.reported.add(p.cause);
|
|
750
|
+
const captured = buildLoggedCapturedError(p, sink);
|
|
751
|
+
s.busy = true;
|
|
752
|
+
try {
|
|
753
|
+
const result = sink.report(captured);
|
|
754
|
+
if (result && typeof result.catch === "function") {
|
|
755
|
+
result.catch(() => {
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
s.stats.forwarded += 1;
|
|
759
|
+
if (p.messageOnly) s.stats.messageOnly += 1;
|
|
760
|
+
} finally {
|
|
761
|
+
s.busy = false;
|
|
762
|
+
}
|
|
763
|
+
} catch {
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
function nodeVersion() {
|
|
767
|
+
try {
|
|
768
|
+
return typeof process !== "undefined" && process.version ? process.version : "unknown";
|
|
769
|
+
} catch {
|
|
770
|
+
return "unknown";
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
function buildLoggedCapturedError(p, sink) {
|
|
774
|
+
const stackFrames = parseStackTrace(p.anchored.stack);
|
|
775
|
+
const firstAppFrame = getFirstAppFrame(stackFrames);
|
|
776
|
+
const route = logRoute(p.label);
|
|
777
|
+
const metadata = {
|
|
778
|
+
source: p.source,
|
|
779
|
+
handled: true,
|
|
780
|
+
...firstAppFrame && {
|
|
781
|
+
sourceLocation: {
|
|
782
|
+
file: firstAppFrame.file,
|
|
783
|
+
line: firstAppFrame.line,
|
|
784
|
+
column: firstAppFrame.column,
|
|
785
|
+
function: firstAppFrame.function
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
};
|
|
789
|
+
if (p.cause) {
|
|
790
|
+
metadata.causeName = p.cause.name;
|
|
791
|
+
if (p.cause.stack) metadata.causeStack = redactText(p.cause.stack, sink.redactFields).slice(0, CAUSE_STACK_MAX);
|
|
792
|
+
} else {
|
|
793
|
+
metadata.messageOnly = true;
|
|
794
|
+
}
|
|
795
|
+
return {
|
|
796
|
+
id: `err_${Date.now().toString(36)}_${Math.random().toString(36).substring(2, 9)}`,
|
|
797
|
+
name: p.anchored.name,
|
|
798
|
+
message: p.anchored.message,
|
|
799
|
+
stack: p.anchored.stack,
|
|
800
|
+
stackFrames,
|
|
801
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
802
|
+
route,
|
|
803
|
+
method: "LOG",
|
|
804
|
+
statusCode: 500,
|
|
805
|
+
url: route,
|
|
806
|
+
environment: sink.environment,
|
|
807
|
+
projectId: sink.projectId,
|
|
808
|
+
sdkVersion: SDK_VERSION,
|
|
809
|
+
nodeVersion: nodeVersion(),
|
|
810
|
+
metadata
|
|
811
|
+
};
|
|
812
|
+
}
|
|
813
|
+
function attachLoggedErrorSink(sink) {
|
|
814
|
+
try {
|
|
815
|
+
const s = shared();
|
|
816
|
+
s.sink = sink;
|
|
817
|
+
if (sink.options.console) installConsoleWrapper(s);
|
|
818
|
+
} catch {
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
function detachLoggedErrorSink(sink) {
|
|
822
|
+
try {
|
|
823
|
+
const s = shared();
|
|
824
|
+
if (s.sink !== sink) return;
|
|
825
|
+
s.sink = null;
|
|
826
|
+
if (s.consoleWrapper && typeof console !== "undefined" && console.error === s.consoleWrapper && s.consoleOriginal) {
|
|
827
|
+
console.error = s.consoleOriginal;
|
|
828
|
+
s.consoleWrapper = null;
|
|
829
|
+
s.consoleOriginal = null;
|
|
830
|
+
}
|
|
831
|
+
} catch {
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
function installConsoleWrapper(s) {
|
|
835
|
+
if (typeof console === "undefined" || typeof console.error !== "function") return;
|
|
836
|
+
if (s.consoleWrapper && console.error === s.consoleWrapper) return;
|
|
837
|
+
const original = console.error;
|
|
838
|
+
const wrapper = function consoleErrorCapturedByBugstack(...args) {
|
|
839
|
+
original.apply(this === void 0 ? console : this, args);
|
|
840
|
+
const st = shared();
|
|
841
|
+
if (!st.sink || !st.sink.options.console) return;
|
|
842
|
+
captureLoggedCall(args, "console-error", wrapper);
|
|
843
|
+
};
|
|
844
|
+
s.consoleOriginal = original;
|
|
845
|
+
s.consoleWrapper = wrapper;
|
|
846
|
+
console.error = wrapper;
|
|
847
|
+
}
|
|
848
|
+
function bugstackPinoHooks() {
|
|
849
|
+
const logMethod = function bugstackPinoLogMethod(inputArgs, method, level) {
|
|
850
|
+
if (typeof level === "number" && level >= 50) {
|
|
851
|
+
captureLoggedCall(inputArgs, "pino", logMethod);
|
|
852
|
+
}
|
|
853
|
+
return method.apply(this, inputArgs);
|
|
854
|
+
};
|
|
855
|
+
return { logMethod };
|
|
856
|
+
}
|
|
857
|
+
var WINSTON_LEVEL = /* @__PURE__ */ Symbol.for("level");
|
|
858
|
+
var WINSTON_SPLAT = /* @__PURE__ */ Symbol.for("splat");
|
|
859
|
+
var WINSTON_ERROR_LEVELS = /* @__PURE__ */ new Set(["error", "crit", "alert", "emerg", "fatal"]);
|
|
860
|
+
function bugstackWinstonFormat() {
|
|
861
|
+
const transform = function bugstackWinstonTransform(info) {
|
|
862
|
+
try {
|
|
863
|
+
const level = info[WINSTON_LEVEL] ?? info.level;
|
|
864
|
+
if (typeof level === "string" && WINSTON_ERROR_LEVELS.has(level)) {
|
|
865
|
+
const splat = Array.isArray(info[WINSTON_SPLAT]) ? info[WINSTON_SPLAT] : [];
|
|
866
|
+
const args = [];
|
|
867
|
+
if (typeof info.message === "string") args.push(info.message);
|
|
868
|
+
if (typeof info.stack === "string" && typeof info.message === "string") {
|
|
869
|
+
const e = new Error(info.message);
|
|
870
|
+
e.name = typeof info.name === "string" ? info.name : "Error";
|
|
871
|
+
e.stack = info.stack;
|
|
872
|
+
args.push(e);
|
|
873
|
+
}
|
|
874
|
+
args.push(...splat, info);
|
|
875
|
+
captureLoggedCall(args, "winston", transform);
|
|
876
|
+
}
|
|
877
|
+
} catch {
|
|
878
|
+
}
|
|
879
|
+
return info;
|
|
880
|
+
};
|
|
881
|
+
return { transform };
|
|
882
|
+
}
|
|
440
883
|
|
|
441
884
|
// src/core/client.ts
|
|
442
|
-
var DEFAULT_ENDPOINT = "
|
|
885
|
+
var DEFAULT_ENDPOINT = "https://dashboard.bugstack.ai/api/ingest/sdk";
|
|
443
886
|
var DEFAULT_TIMEOUT = 5e3;
|
|
444
887
|
var DEFAULT_MAX_BODY_SIZE = 1e4;
|
|
445
888
|
var DEFAULT_DEDUPLICATION_WINDOW = 5 * 60 * 1e3;
|
|
@@ -466,6 +909,7 @@ var ErrorCaptureClient = class _ErrorCaptureClient {
|
|
|
466
909
|
static instance = null;
|
|
467
910
|
deduplicator;
|
|
468
911
|
retryQueue;
|
|
912
|
+
loggedErrorSink = null;
|
|
469
913
|
constructor(config) {
|
|
470
914
|
const isProduction = process.env.NODE_ENV === "production";
|
|
471
915
|
this.config = {
|
|
@@ -492,7 +936,8 @@ var ErrorCaptureClient = class _ErrorCaptureClient {
|
|
|
492
936
|
dropChunkErrors: config.dropChunkErrors ?? false,
|
|
493
937
|
chunkErrorsEndpoint: config.chunkErrorsEndpoint ?? deriveChunkErrorsEndpoint(
|
|
494
938
|
config.endpoint ?? process.env.BUGSTACK_ENDPOINT ?? DEFAULT_ENDPOINT
|
|
495
|
-
)
|
|
939
|
+
),
|
|
940
|
+
captureConsoleErrors: resolveLoggedErrorOptions(config.captureConsoleErrors)
|
|
496
941
|
};
|
|
497
942
|
this.deduplicator = new ErrorDeduplicator(this.config.deduplicationWindow);
|
|
498
943
|
this.retryQueue = new RetryQueue(
|
|
@@ -504,12 +949,23 @@ var ErrorCaptureClient = class _ErrorCaptureClient {
|
|
|
504
949
|
debug: this.config.debug
|
|
505
950
|
}
|
|
506
951
|
);
|
|
952
|
+
if (this.config.enabled) {
|
|
953
|
+
this.loggedErrorSink = {
|
|
954
|
+
options: this.config.captureConsoleErrors,
|
|
955
|
+
environment: this.config.environment,
|
|
956
|
+
projectId: this.config.projectId,
|
|
957
|
+
redactFields: this.config.redactFields,
|
|
958
|
+
report: (error) => this.reportError(error)
|
|
959
|
+
};
|
|
960
|
+
attachLoggedErrorSink(this.loggedErrorSink);
|
|
961
|
+
}
|
|
507
962
|
this.log("BugStack client initialized", {
|
|
508
963
|
endpoint: this.config.endpoint,
|
|
509
964
|
projectId: this.config.projectId,
|
|
510
965
|
environment: this.config.environment,
|
|
511
966
|
autoFix: this.config.autoFix,
|
|
512
|
-
deduplicationWindow: this.config.deduplicationWindow
|
|
967
|
+
deduplicationWindow: this.config.deduplicationWindow,
|
|
968
|
+
captureConsoleErrors: this.config.captureConsoleErrors.console
|
|
513
969
|
});
|
|
514
970
|
}
|
|
515
971
|
/**
|
|
@@ -708,6 +1164,10 @@ var ErrorCaptureClient = class _ErrorCaptureClient {
|
|
|
708
1164
|
* Should be called when shutting down to prevent memory leaks
|
|
709
1165
|
*/
|
|
710
1166
|
destroy() {
|
|
1167
|
+
if (this.loggedErrorSink) {
|
|
1168
|
+
detachLoggedErrorSink(this.loggedErrorSink);
|
|
1169
|
+
this.loggedErrorSink = null;
|
|
1170
|
+
}
|
|
711
1171
|
this.deduplicator.destroy();
|
|
712
1172
|
this.retryQueue.destroy();
|
|
713
1173
|
}
|
|
@@ -902,57 +1362,6 @@ async function safeParseBody(request, maxSize, client) {
|
|
|
902
1362
|
}
|
|
903
1363
|
}
|
|
904
1364
|
|
|
905
|
-
// src/core/utils/stack-parser.ts
|
|
906
|
-
var V8_STACK_FRAME_REGEX = /^\s*at\s+(?:async\s+)?(?:(.+?)\s+\()?(?:(.+?):(\d+):(\d+))\)?$/;
|
|
907
|
-
function isNodeModule(filePath) {
|
|
908
|
-
return filePath.includes("node_modules");
|
|
909
|
-
}
|
|
910
|
-
function isInternalCode(filePath) {
|
|
911
|
-
return filePath.startsWith("node:") || filePath.startsWith("internal/") || !filePath.includes("/");
|
|
912
|
-
}
|
|
913
|
-
function cleanFunctionName(name) {
|
|
914
|
-
if (!name) return void 0;
|
|
915
|
-
return name.replace(/^Object\./, "").replace(/^Module\./, "").replace(/^__webpack_require__\./, "").replace(/^webpack_require\./, "").trim();
|
|
916
|
-
}
|
|
917
|
-
function parseStackTrace(stack) {
|
|
918
|
-
if (!stack) return [];
|
|
919
|
-
const lines = stack.split("\n");
|
|
920
|
-
const frames = [];
|
|
921
|
-
for (const line of lines) {
|
|
922
|
-
const match = V8_STACK_FRAME_REGEX.exec(line);
|
|
923
|
-
if (match) {
|
|
924
|
-
const [, functionName, file, lineStr, columnStr] = match;
|
|
925
|
-
const frame = {
|
|
926
|
-
function: cleanFunctionName(functionName),
|
|
927
|
-
file: file ?? void 0,
|
|
928
|
-
line: lineStr ? parseInt(lineStr, 10) : void 0,
|
|
929
|
-
column: columnStr ? parseInt(columnStr, 10) : void 0
|
|
930
|
-
};
|
|
931
|
-
if (frame.file) {
|
|
932
|
-
frame.isNodeModule = isNodeModule(frame.file);
|
|
933
|
-
frame.isInternal = isInternalCode(frame.file);
|
|
934
|
-
}
|
|
935
|
-
frames.push(frame);
|
|
936
|
-
}
|
|
937
|
-
}
|
|
938
|
-
return frames;
|
|
939
|
-
}
|
|
940
|
-
function getFirstAppFrame(frames) {
|
|
941
|
-
return frames.find((frame) => !frame.isNodeModule && !frame.isInternal);
|
|
942
|
-
}
|
|
943
|
-
function getAppFrames(frames) {
|
|
944
|
-
return frames.filter((frame) => !frame.isNodeModule && !frame.isInternal);
|
|
945
|
-
}
|
|
946
|
-
function formatStackFrames(frames) {
|
|
947
|
-
return frames.map((frame) => {
|
|
948
|
-
const location = [frame.file, frame.line, frame.column].filter(Boolean).join(":");
|
|
949
|
-
if (frame.function) {
|
|
950
|
-
return ` at ${frame.function} (${location})`;
|
|
951
|
-
}
|
|
952
|
-
return ` at ${location}`;
|
|
953
|
-
}).join("\n");
|
|
954
|
-
}
|
|
955
|
-
|
|
956
1365
|
// src/adapters/nextjs/index.ts
|
|
957
1366
|
function generateErrorId() {
|
|
958
1367
|
const timestamp = Date.now().toString(36);
|
|
@@ -1057,6 +1466,7 @@ function withErrorCapture(handler, config, routeOptions) {
|
|
|
1057
1466
|
const response = await handler(request, context);
|
|
1058
1467
|
return response;
|
|
1059
1468
|
} catch (error) {
|
|
1469
|
+
markErrorReported(error);
|
|
1060
1470
|
const err = error instanceof Error ? error : new Error(String(error));
|
|
1061
1471
|
if (client) {
|
|
1062
1472
|
const clientConfig = client.getConfig();
|
|
@@ -1081,12 +1491,16 @@ exports.BugStackClient = BugStackClient;
|
|
|
1081
1491
|
exports.ErrorCaptureClient = ErrorCaptureClient;
|
|
1082
1492
|
exports.ErrorDeduplicator = ErrorDeduplicator;
|
|
1083
1493
|
exports.RetryQueue = RetryQueue;
|
|
1494
|
+
exports.bugstackPinoHooks = bugstackPinoHooks;
|
|
1495
|
+
exports.bugstackWinstonFormat = bugstackWinstonFormat;
|
|
1084
1496
|
exports.formatStackFrames = formatStackFrames;
|
|
1085
1497
|
exports.generateErrorFingerprint = generateErrorFingerprint;
|
|
1086
1498
|
exports.generateFingerprintFromParts = generateFingerprintFromParts;
|
|
1087
1499
|
exports.getAppFrames = getAppFrames;
|
|
1088
1500
|
exports.getFirstAppFrame = getFirstAppFrame;
|
|
1089
1501
|
exports.isChunkLoadError = isChunkLoadError;
|
|
1502
|
+
exports.loggedErrorStats = loggedErrorStats;
|
|
1503
|
+
exports.markErrorReported = markErrorReported;
|
|
1090
1504
|
exports.parseStackTrace = parseStackTrace;
|
|
1091
1505
|
exports.safeParseBody = safeParseBody;
|
|
1092
1506
|
exports.sanitizeHeaders = sanitizeHeaders;
|