bugstack-sdk 2.2.2 → 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 +30 -0
- package/README.md +75 -2
- package/dist/adapters/express/index.d.mts +2 -2
- package/dist/adapters/express/index.d.ts +2 -2
- package/dist/adapters/express/index.js +408 -42
- package/dist/adapters/express/index.js.map +1 -1
- package/dist/adapters/express/index.mjs +408 -42
- 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 +456 -42
- package/dist/adapters/generic/index.js.map +1 -1
- package/dist/adapters/generic/index.mjs +453 -43
- 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 +408 -42
- package/dist/adapters/nextjs/index.js.map +1 -1
- package/dist/adapters/nextjs/index.mjs +408 -42
- package/dist/adapters/nextjs/index.mjs.map +1 -1
- package/dist/{client-DK_ItP55.d.mts → client-B9WaTEYj.d.mts} +148 -1
- package/dist/{client-DK_ItP55.d.ts → client-B9WaTEYj.d.ts} +148 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +468 -54
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +465 -55
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -4
|
@@ -421,8 +421,439 @@ var RetryQueue = class {
|
|
|
421
421
|
}
|
|
422
422
|
};
|
|
423
423
|
|
|
424
|
+
// src/core/utils/stack-parser.ts
|
|
425
|
+
var V8_STACK_FRAME_REGEX = /^\s*at\s+(?:async\s+)?(?:(.+?)\s+\()?(?:(.+?):(\d+):(\d+))\)?$/;
|
|
426
|
+
function isNodeModule(filePath) {
|
|
427
|
+
return filePath.includes("node_modules");
|
|
428
|
+
}
|
|
429
|
+
function isInternalCode(filePath) {
|
|
430
|
+
return filePath.startsWith("node:") || filePath.startsWith("internal/") || // A bare name ("native", "<anonymous>") has no directory. A Windows
|
|
431
|
+
// path has backslashes and is app code.
|
|
432
|
+
!filePath.includes("/") && !filePath.includes("\\");
|
|
433
|
+
}
|
|
434
|
+
function cleanFunctionName(name) {
|
|
435
|
+
if (!name) return void 0;
|
|
436
|
+
return name.replace(/^Object\./, "").replace(/^Module\./, "").replace(/^__webpack_require__\./, "").replace(/^webpack_require\./, "").trim();
|
|
437
|
+
}
|
|
438
|
+
function parseStackTrace(stack) {
|
|
439
|
+
if (!stack) return [];
|
|
440
|
+
const lines = stack.split("\n");
|
|
441
|
+
const frames = [];
|
|
442
|
+
for (const line of lines) {
|
|
443
|
+
const match = V8_STACK_FRAME_REGEX.exec(line);
|
|
444
|
+
if (match) {
|
|
445
|
+
const [, functionName, file, lineStr, columnStr] = match;
|
|
446
|
+
const frame = {
|
|
447
|
+
function: cleanFunctionName(functionName),
|
|
448
|
+
file: file ?? void 0,
|
|
449
|
+
line: lineStr ? parseInt(lineStr, 10) : void 0,
|
|
450
|
+
column: columnStr ? parseInt(columnStr, 10) : void 0
|
|
451
|
+
};
|
|
452
|
+
if (frame.file) {
|
|
453
|
+
frame.isNodeModule = isNodeModule(frame.file);
|
|
454
|
+
frame.isInternal = isInternalCode(frame.file);
|
|
455
|
+
}
|
|
456
|
+
frames.push(frame);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
return frames;
|
|
460
|
+
}
|
|
461
|
+
function getFirstAppFrame(frames) {
|
|
462
|
+
return frames.find((frame) => !frame.isNodeModule && !frame.isInternal);
|
|
463
|
+
}
|
|
464
|
+
|
|
424
465
|
// src/core/sdk-version.ts
|
|
425
|
-
var SDK_VERSION = "2.
|
|
466
|
+
var SDK_VERSION = "2.3.0" ;
|
|
467
|
+
|
|
468
|
+
// src/core/logged-errors.ts
|
|
469
|
+
var DEFAULT_MAX_PER_MINUTE = 20;
|
|
470
|
+
var DEFAULT_REPEAT_WINDOW_MS = 5 * 6e4;
|
|
471
|
+
var MAX_REMEMBERED = 500;
|
|
472
|
+
var MESSAGE_MAX = 300;
|
|
473
|
+
var ANCHORED_MESSAGE_MAX = 500;
|
|
474
|
+
var CAUSE_STACK_MAX = 2e3;
|
|
475
|
+
function resolveLoggedErrorOptions(value) {
|
|
476
|
+
const opts = typeof value === "object" && value !== null ? value : {};
|
|
477
|
+
return {
|
|
478
|
+
console: value !== false,
|
|
479
|
+
messages: opts.messages ?? false,
|
|
480
|
+
maxPerMinute: opts.maxPerMinute ?? DEFAULT_MAX_PER_MINUTE,
|
|
481
|
+
repeatWindowMs: opts.repeatWindowMs ?? DEFAULT_REPEAT_WINDOW_MS,
|
|
482
|
+
ignore: opts.ignore ?? []
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
var SHARED_KEY = /* @__PURE__ */ Symbol.for("bugstack-sdk.logged-errors");
|
|
486
|
+
function emptyStats() {
|
|
487
|
+
return {
|
|
488
|
+
forwarded: 0,
|
|
489
|
+
messageOnly: 0,
|
|
490
|
+
dropped: {
|
|
491
|
+
no_error_argument: 0,
|
|
492
|
+
sdk_line: 0,
|
|
493
|
+
node_warning: 0,
|
|
494
|
+
ignored: 0,
|
|
495
|
+
already_reported: 0,
|
|
496
|
+
repeat: 0,
|
|
497
|
+
rate_limited: 0,
|
|
498
|
+
no_client: 0
|
|
499
|
+
}
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
function shared() {
|
|
503
|
+
const g = globalThis;
|
|
504
|
+
let s = g[SHARED_KEY];
|
|
505
|
+
if (!s) {
|
|
506
|
+
s = {
|
|
507
|
+
reported: /* @__PURE__ */ new WeakSet(),
|
|
508
|
+
sink: null,
|
|
509
|
+
consoleOriginal: null,
|
|
510
|
+
consoleWrapper: null,
|
|
511
|
+
busy: false,
|
|
512
|
+
windowStart: 0,
|
|
513
|
+
inWindow: 0,
|
|
514
|
+
lastSent: /* @__PURE__ */ new Map(),
|
|
515
|
+
stats: emptyStats(),
|
|
516
|
+
now: Date.now
|
|
517
|
+
};
|
|
518
|
+
g[SHARED_KEY] = s;
|
|
519
|
+
}
|
|
520
|
+
return s;
|
|
521
|
+
}
|
|
522
|
+
var defer = typeof setImmediate === "function" ? (fn) => void setImmediate(fn) : (fn) => void setTimeout(fn, 0);
|
|
523
|
+
function markErrorReported(error) {
|
|
524
|
+
try {
|
|
525
|
+
if (error !== null && typeof error === "object") shared().reported.add(error);
|
|
526
|
+
} catch {
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
function loggedErrorStats() {
|
|
530
|
+
const s = shared();
|
|
531
|
+
return {
|
|
532
|
+
consoleInstalled: s.consoleWrapper !== null && s.sink !== null && s.sink.options.console,
|
|
533
|
+
forwarded: s.stats.forwarded,
|
|
534
|
+
messageOnly: s.stats.messageOnly,
|
|
535
|
+
dropped: { ...s.stats.dropped }
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
function isError(value) {
|
|
539
|
+
if (value instanceof Error) return true;
|
|
540
|
+
if (value === null || typeof value !== "object") return false;
|
|
541
|
+
try {
|
|
542
|
+
const v = value;
|
|
543
|
+
return typeof v.message === "string" && typeof v.stack === "string";
|
|
544
|
+
} catch {
|
|
545
|
+
return false;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
function findError(args) {
|
|
549
|
+
for (const a of args) if (isError(a)) return a;
|
|
550
|
+
for (const a of args) {
|
|
551
|
+
if (a === null || typeof a !== "object" || Array.isArray(a)) continue;
|
|
552
|
+
try {
|
|
553
|
+
const o = a;
|
|
554
|
+
for (const k of ["err", "error", "cause"]) if (isError(o[k])) return o[k];
|
|
555
|
+
} catch {
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
return void 0;
|
|
559
|
+
}
|
|
560
|
+
var STACK_TEXT = /(^|\n)\s*([A-Za-z_$][\w$.]*(?:Error|Exception)\b[^\n]*)\n\s+at \S/;
|
|
561
|
+
function findStackText(args) {
|
|
562
|
+
for (const a of args) {
|
|
563
|
+
if (typeof a !== "string") continue;
|
|
564
|
+
const m = STACK_TEXT.exec(a);
|
|
565
|
+
if (!m) continue;
|
|
566
|
+
const header = m[2].trim();
|
|
567
|
+
const colon = header.indexOf(":");
|
|
568
|
+
const err = new Error(colon > 0 ? header.slice(colon + 1).trim() : header);
|
|
569
|
+
err.name = colon > 0 ? header.slice(0, colon).trim() : "Error";
|
|
570
|
+
err.stack = a.slice(m.index + m[1].length).trim();
|
|
571
|
+
return err;
|
|
572
|
+
}
|
|
573
|
+
return void 0;
|
|
574
|
+
}
|
|
575
|
+
function messageOfArgs(args) {
|
|
576
|
+
const parts = [];
|
|
577
|
+
for (const a of args) {
|
|
578
|
+
if (typeof a === "string") parts.push(a);
|
|
579
|
+
else if (typeof a === "number" || typeof a === "boolean") parts.push(String(a));
|
|
580
|
+
else if (a !== null && typeof a === "object" && !Array.isArray(a) && !isError(a)) {
|
|
581
|
+
try {
|
|
582
|
+
const o = a;
|
|
583
|
+
for (const k of ["error", "message", "reason", "err", "msg"]) {
|
|
584
|
+
if (typeof o[k] === "string" && o[k].length > 0) {
|
|
585
|
+
parts.push(o[k]);
|
|
586
|
+
break;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
} catch {
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
return parts.join(" ").replace(/\s+/g, " ").trim().slice(0, MESSAGE_MAX);
|
|
594
|
+
}
|
|
595
|
+
function logRoute(label) {
|
|
596
|
+
const tag = label?.match(/^\s*\[([A-Za-z0-9_.:/-]{1,60})\]/)?.[1];
|
|
597
|
+
return tag ? `log:${tag}` : "log";
|
|
598
|
+
}
|
|
599
|
+
var SDK_LINE = /^\s*\[bugstack[^\]]*\]/i;
|
|
600
|
+
var NODE_WARNING_LINE = /^\s*\(node:\d+\)\s+(?:\[[A-Z0-9_]+\]\s+)?\w*Warning:/;
|
|
601
|
+
function escapeRegExp(s) {
|
|
602
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
603
|
+
}
|
|
604
|
+
function redactText(text, redactFields) {
|
|
605
|
+
let out = text.replace(/\b(Bearer|Basic)\s+[A-Za-z0-9._~+/=-]{8,}/gi, "$1 [REDACTED]");
|
|
606
|
+
if (redactFields.length > 0) {
|
|
607
|
+
const names = redactFields.filter((f) => f.length > 0).map(escapeRegExp).join("|");
|
|
608
|
+
if (names) {
|
|
609
|
+
const re = new RegExp(
|
|
610
|
+
`(["']?\\b\\w*(?:${names})\\w*["']?\\s*[=:]\\s*)("[^"]*"|'[^']*'|[^\\s,;&)}\\]]+)`,
|
|
611
|
+
"gi"
|
|
612
|
+
);
|
|
613
|
+
out = out.replace(re, "$1[REDACTED]");
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
return out;
|
|
617
|
+
}
|
|
618
|
+
function firstFrame(stack) {
|
|
619
|
+
return (stack ?? "").split("\n").find((l) => /^\s+at /.test(l))?.trim() ?? "";
|
|
620
|
+
}
|
|
621
|
+
function trimLibraryFrames(stack) {
|
|
622
|
+
const lines = stack.split("\n");
|
|
623
|
+
const header = [];
|
|
624
|
+
let i = 0;
|
|
625
|
+
while (i < lines.length && !/^\s+at /.test(lines[i])) header.push(lines[i++]);
|
|
626
|
+
let j = i;
|
|
627
|
+
while (j < lines.length && /node_modules|[/\\]bugstack-sdk[/\\]|\(node:internal|^\s+at node:internal/.test(lines[j])) j++;
|
|
628
|
+
if (j >= lines.length) return stack;
|
|
629
|
+
return [...header, ...lines.slice(j)].join("\n");
|
|
630
|
+
}
|
|
631
|
+
function siteOf(label, cause) {
|
|
632
|
+
if (label === void 0) return "";
|
|
633
|
+
const first = label.split("\n")[0].trim();
|
|
634
|
+
if (!first || first === cause.message || first === `${cause.name}: ${cause.message}`) return "";
|
|
635
|
+
return first.slice(0, 200);
|
|
636
|
+
}
|
|
637
|
+
function matchesIgnore(text, ignore) {
|
|
638
|
+
for (const p of ignore) {
|
|
639
|
+
try {
|
|
640
|
+
if (typeof p === "string" ? text.includes(p) : p.test(text)) return true;
|
|
641
|
+
} catch {
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
return false;
|
|
645
|
+
}
|
|
646
|
+
function captureLoggedCall(args, source, boundary) {
|
|
647
|
+
const s = shared();
|
|
648
|
+
if (s.busy) return;
|
|
649
|
+
try {
|
|
650
|
+
const sink = s.sink;
|
|
651
|
+
if (!sink) {
|
|
652
|
+
s.stats.dropped.no_client += 1;
|
|
653
|
+
return;
|
|
654
|
+
}
|
|
655
|
+
const label = args.find((a) => typeof a === "string");
|
|
656
|
+
if (label !== void 0 && SDK_LINE.test(label)) {
|
|
657
|
+
s.stats.dropped.sdk_line += 1;
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
if (label !== void 0 && NODE_WARNING_LINE.test(label)) {
|
|
661
|
+
s.stats.dropped.node_warning += 1;
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
const cause = findError(args) ?? findStackText(args) ?? null;
|
|
665
|
+
if (sink.options.ignore.length > 0 && matchesIgnore(`${messageOfArgs(args)} ${cause ? `${cause.name}: ${cause.message}` : ""}`, sink.options.ignore)) {
|
|
666
|
+
s.stats.dropped.ignored += 1;
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
let message;
|
|
670
|
+
if (cause) {
|
|
671
|
+
message = `${siteOf(label, cause) || source}: ${cause.message}`;
|
|
672
|
+
} else {
|
|
673
|
+
message = sink.options.messages ? messageOfArgs(args) : "";
|
|
674
|
+
if (!message) {
|
|
675
|
+
s.stats.dropped.no_error_argument += 1;
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
const anchored = new Error(redactText(message, sink.redactFields).slice(0, ANCHORED_MESSAGE_MAX));
|
|
680
|
+
anchored.name = "LoggedError";
|
|
681
|
+
const capture = Error.captureStackTrace;
|
|
682
|
+
if (capture) capture(anchored, boundary);
|
|
683
|
+
if (anchored.stack) anchored.stack = trimLibraryFrames(anchored.stack);
|
|
684
|
+
if (cause) anchored.cause = cause;
|
|
685
|
+
const pending = { anchored, cause, label, source, messageOnly: !cause };
|
|
686
|
+
defer(() => forward(pending));
|
|
687
|
+
} catch {
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
function forward(p) {
|
|
691
|
+
const s = shared();
|
|
692
|
+
const sink = s.sink;
|
|
693
|
+
if (!sink) {
|
|
694
|
+
s.stats.dropped.no_client += 1;
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
try {
|
|
698
|
+
if (p.cause && s.reported.has(p.cause)) {
|
|
699
|
+
s.stats.dropped.already_reported += 1;
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
const now = s.now();
|
|
703
|
+
const key = p.messageOnly ? `${firstFrame(p.anchored.stack)}|message-only` : `${firstFrame(p.anchored.stack)}|${p.anchored.message}`;
|
|
704
|
+
const last = s.lastSent.get(key);
|
|
705
|
+
if (last !== void 0 && now - last < sink.options.repeatWindowMs) {
|
|
706
|
+
s.stats.dropped.repeat += 1;
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
if (now - s.windowStart >= 6e4) {
|
|
710
|
+
s.windowStart = now;
|
|
711
|
+
s.inWindow = 0;
|
|
712
|
+
}
|
|
713
|
+
if (s.inWindow >= sink.options.maxPerMinute) {
|
|
714
|
+
s.stats.dropped.rate_limited += 1;
|
|
715
|
+
return;
|
|
716
|
+
}
|
|
717
|
+
s.inWindow += 1;
|
|
718
|
+
s.lastSent.set(key, now);
|
|
719
|
+
if (s.lastSent.size > MAX_REMEMBERED) {
|
|
720
|
+
const oldest = s.lastSent.keys().next().value;
|
|
721
|
+
if (oldest !== void 0) s.lastSent.delete(oldest);
|
|
722
|
+
}
|
|
723
|
+
if (p.cause) s.reported.add(p.cause);
|
|
724
|
+
const captured = buildLoggedCapturedError(p, sink);
|
|
725
|
+
s.busy = true;
|
|
726
|
+
try {
|
|
727
|
+
const result = sink.report(captured);
|
|
728
|
+
if (result && typeof result.catch === "function") {
|
|
729
|
+
result.catch(() => {
|
|
730
|
+
});
|
|
731
|
+
}
|
|
732
|
+
s.stats.forwarded += 1;
|
|
733
|
+
if (p.messageOnly) s.stats.messageOnly += 1;
|
|
734
|
+
} finally {
|
|
735
|
+
s.busy = false;
|
|
736
|
+
}
|
|
737
|
+
} catch {
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
function nodeVersion() {
|
|
741
|
+
try {
|
|
742
|
+
return typeof process !== "undefined" && process.version ? process.version : "unknown";
|
|
743
|
+
} catch {
|
|
744
|
+
return "unknown";
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
function buildLoggedCapturedError(p, sink) {
|
|
748
|
+
const stackFrames = parseStackTrace(p.anchored.stack);
|
|
749
|
+
const firstAppFrame = getFirstAppFrame(stackFrames);
|
|
750
|
+
const route = logRoute(p.label);
|
|
751
|
+
const metadata = {
|
|
752
|
+
source: p.source,
|
|
753
|
+
handled: true,
|
|
754
|
+
...firstAppFrame && {
|
|
755
|
+
sourceLocation: {
|
|
756
|
+
file: firstAppFrame.file,
|
|
757
|
+
line: firstAppFrame.line,
|
|
758
|
+
column: firstAppFrame.column,
|
|
759
|
+
function: firstAppFrame.function
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
};
|
|
763
|
+
if (p.cause) {
|
|
764
|
+
metadata.causeName = p.cause.name;
|
|
765
|
+
if (p.cause.stack) metadata.causeStack = redactText(p.cause.stack, sink.redactFields).slice(0, CAUSE_STACK_MAX);
|
|
766
|
+
} else {
|
|
767
|
+
metadata.messageOnly = true;
|
|
768
|
+
}
|
|
769
|
+
return {
|
|
770
|
+
id: `err_${Date.now().toString(36)}_${Math.random().toString(36).substring(2, 9)}`,
|
|
771
|
+
name: p.anchored.name,
|
|
772
|
+
message: p.anchored.message,
|
|
773
|
+
stack: p.anchored.stack,
|
|
774
|
+
stackFrames,
|
|
775
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
776
|
+
route,
|
|
777
|
+
method: "LOG",
|
|
778
|
+
statusCode: 500,
|
|
779
|
+
url: route,
|
|
780
|
+
environment: sink.environment,
|
|
781
|
+
projectId: sink.projectId,
|
|
782
|
+
sdkVersion: SDK_VERSION,
|
|
783
|
+
nodeVersion: nodeVersion(),
|
|
784
|
+
metadata
|
|
785
|
+
};
|
|
786
|
+
}
|
|
787
|
+
function attachLoggedErrorSink(sink) {
|
|
788
|
+
try {
|
|
789
|
+
const s = shared();
|
|
790
|
+
s.sink = sink;
|
|
791
|
+
if (sink.options.console) installConsoleWrapper(s);
|
|
792
|
+
} catch {
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
function detachLoggedErrorSink(sink) {
|
|
796
|
+
try {
|
|
797
|
+
const s = shared();
|
|
798
|
+
if (s.sink !== sink) return;
|
|
799
|
+
s.sink = null;
|
|
800
|
+
if (s.consoleWrapper && typeof console !== "undefined" && console.error === s.consoleWrapper && s.consoleOriginal) {
|
|
801
|
+
console.error = s.consoleOriginal;
|
|
802
|
+
s.consoleWrapper = null;
|
|
803
|
+
s.consoleOriginal = null;
|
|
804
|
+
}
|
|
805
|
+
} catch {
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
function installConsoleWrapper(s) {
|
|
809
|
+
if (typeof console === "undefined" || typeof console.error !== "function") return;
|
|
810
|
+
if (s.consoleWrapper && console.error === s.consoleWrapper) return;
|
|
811
|
+
const original = console.error;
|
|
812
|
+
const wrapper = function consoleErrorCapturedByBugstack(...args) {
|
|
813
|
+
original.apply(this === void 0 ? console : this, args);
|
|
814
|
+
const st = shared();
|
|
815
|
+
if (!st.sink || !st.sink.options.console) return;
|
|
816
|
+
captureLoggedCall(args, "console-error", wrapper);
|
|
817
|
+
};
|
|
818
|
+
s.consoleOriginal = original;
|
|
819
|
+
s.consoleWrapper = wrapper;
|
|
820
|
+
console.error = wrapper;
|
|
821
|
+
}
|
|
822
|
+
function bugstackPinoHooks() {
|
|
823
|
+
const logMethod = function bugstackPinoLogMethod(inputArgs, method, level) {
|
|
824
|
+
if (typeof level === "number" && level >= 50) {
|
|
825
|
+
captureLoggedCall(inputArgs, "pino", logMethod);
|
|
826
|
+
}
|
|
827
|
+
return method.apply(this, inputArgs);
|
|
828
|
+
};
|
|
829
|
+
return { logMethod };
|
|
830
|
+
}
|
|
831
|
+
var WINSTON_LEVEL = /* @__PURE__ */ Symbol.for("level");
|
|
832
|
+
var WINSTON_SPLAT = /* @__PURE__ */ Symbol.for("splat");
|
|
833
|
+
var WINSTON_ERROR_LEVELS = /* @__PURE__ */ new Set(["error", "crit", "alert", "emerg", "fatal"]);
|
|
834
|
+
function bugstackWinstonFormat() {
|
|
835
|
+
const transform = function bugstackWinstonTransform(info) {
|
|
836
|
+
try {
|
|
837
|
+
const level = info[WINSTON_LEVEL] ?? info.level;
|
|
838
|
+
if (typeof level === "string" && WINSTON_ERROR_LEVELS.has(level)) {
|
|
839
|
+
const splat = Array.isArray(info[WINSTON_SPLAT]) ? info[WINSTON_SPLAT] : [];
|
|
840
|
+
const args = [];
|
|
841
|
+
if (typeof info.message === "string") args.push(info.message);
|
|
842
|
+
if (typeof info.stack === "string" && typeof info.message === "string") {
|
|
843
|
+
const e = new Error(info.message);
|
|
844
|
+
e.name = typeof info.name === "string" ? info.name : "Error";
|
|
845
|
+
e.stack = info.stack;
|
|
846
|
+
args.push(e);
|
|
847
|
+
}
|
|
848
|
+
args.push(...splat, info);
|
|
849
|
+
captureLoggedCall(args, "winston", transform);
|
|
850
|
+
}
|
|
851
|
+
} catch {
|
|
852
|
+
}
|
|
853
|
+
return info;
|
|
854
|
+
};
|
|
855
|
+
return { transform };
|
|
856
|
+
}
|
|
426
857
|
|
|
427
858
|
// src/core/client.ts
|
|
428
859
|
var DEFAULT_ENDPOINT = "https://dashboard.bugstack.ai/api/ingest/sdk";
|
|
@@ -452,6 +883,7 @@ var ErrorCaptureClient = class _ErrorCaptureClient {
|
|
|
452
883
|
static instance = null;
|
|
453
884
|
deduplicator;
|
|
454
885
|
retryQueue;
|
|
886
|
+
loggedErrorSink = null;
|
|
455
887
|
constructor(config) {
|
|
456
888
|
const isProduction = process.env.NODE_ENV === "production";
|
|
457
889
|
this.config = {
|
|
@@ -478,7 +910,8 @@ var ErrorCaptureClient = class _ErrorCaptureClient {
|
|
|
478
910
|
dropChunkErrors: config.dropChunkErrors ?? false,
|
|
479
911
|
chunkErrorsEndpoint: config.chunkErrorsEndpoint ?? deriveChunkErrorsEndpoint(
|
|
480
912
|
config.endpoint ?? process.env.BUGSTACK_ENDPOINT ?? DEFAULT_ENDPOINT
|
|
481
|
-
)
|
|
913
|
+
),
|
|
914
|
+
captureConsoleErrors: resolveLoggedErrorOptions(config.captureConsoleErrors)
|
|
482
915
|
};
|
|
483
916
|
this.deduplicator = new ErrorDeduplicator(this.config.deduplicationWindow);
|
|
484
917
|
this.retryQueue = new RetryQueue(
|
|
@@ -490,12 +923,23 @@ var ErrorCaptureClient = class _ErrorCaptureClient {
|
|
|
490
923
|
debug: this.config.debug
|
|
491
924
|
}
|
|
492
925
|
);
|
|
926
|
+
if (this.config.enabled) {
|
|
927
|
+
this.loggedErrorSink = {
|
|
928
|
+
options: this.config.captureConsoleErrors,
|
|
929
|
+
environment: this.config.environment,
|
|
930
|
+
projectId: this.config.projectId,
|
|
931
|
+
redactFields: this.config.redactFields,
|
|
932
|
+
report: (error) => this.reportError(error)
|
|
933
|
+
};
|
|
934
|
+
attachLoggedErrorSink(this.loggedErrorSink);
|
|
935
|
+
}
|
|
493
936
|
this.log("BugStack client initialized", {
|
|
494
937
|
endpoint: this.config.endpoint,
|
|
495
938
|
projectId: this.config.projectId,
|
|
496
939
|
environment: this.config.environment,
|
|
497
940
|
autoFix: this.config.autoFix,
|
|
498
|
-
deduplicationWindow: this.config.deduplicationWindow
|
|
941
|
+
deduplicationWindow: this.config.deduplicationWindow,
|
|
942
|
+
captureConsoleErrors: this.config.captureConsoleErrors.console
|
|
499
943
|
});
|
|
500
944
|
}
|
|
501
945
|
/**
|
|
@@ -694,6 +1138,10 @@ var ErrorCaptureClient = class _ErrorCaptureClient {
|
|
|
694
1138
|
* Should be called when shutting down to prevent memory leaks
|
|
695
1139
|
*/
|
|
696
1140
|
destroy() {
|
|
1141
|
+
if (this.loggedErrorSink) {
|
|
1142
|
+
detachLoggedErrorSink(this.loggedErrorSink);
|
|
1143
|
+
this.loggedErrorSink = null;
|
|
1144
|
+
}
|
|
697
1145
|
this.deduplicator.destroy();
|
|
698
1146
|
this.retryQueue.destroy();
|
|
699
1147
|
}
|
|
@@ -733,45 +1181,6 @@ function deriveChunkErrorsEndpoint(captureEndpoint) {
|
|
|
733
1181
|
return captureEndpoint.replace(/\/?$/, "/chunk-errors");
|
|
734
1182
|
}
|
|
735
1183
|
|
|
736
|
-
// src/core/utils/stack-parser.ts
|
|
737
|
-
var V8_STACK_FRAME_REGEX = /^\s*at\s+(?:async\s+)?(?:(.+?)\s+\()?(?:(.+?):(\d+):(\d+))\)?$/;
|
|
738
|
-
function isNodeModule(filePath) {
|
|
739
|
-
return filePath.includes("node_modules");
|
|
740
|
-
}
|
|
741
|
-
function isInternalCode(filePath) {
|
|
742
|
-
return filePath.startsWith("node:") || filePath.startsWith("internal/") || !filePath.includes("/");
|
|
743
|
-
}
|
|
744
|
-
function cleanFunctionName(name) {
|
|
745
|
-
if (!name) return void 0;
|
|
746
|
-
return name.replace(/^Object\./, "").replace(/^Module\./, "").replace(/^__webpack_require__\./, "").replace(/^webpack_require\./, "").trim();
|
|
747
|
-
}
|
|
748
|
-
function parseStackTrace(stack) {
|
|
749
|
-
if (!stack) return [];
|
|
750
|
-
const lines = stack.split("\n");
|
|
751
|
-
const frames = [];
|
|
752
|
-
for (const line of lines) {
|
|
753
|
-
const match = V8_STACK_FRAME_REGEX.exec(line);
|
|
754
|
-
if (match) {
|
|
755
|
-
const [, functionName, file, lineStr, columnStr] = match;
|
|
756
|
-
const frame = {
|
|
757
|
-
function: cleanFunctionName(functionName),
|
|
758
|
-
file: file ?? void 0,
|
|
759
|
-
line: lineStr ? parseInt(lineStr, 10) : void 0,
|
|
760
|
-
column: columnStr ? parseInt(columnStr, 10) : void 0
|
|
761
|
-
};
|
|
762
|
-
if (frame.file) {
|
|
763
|
-
frame.isNodeModule = isNodeModule(frame.file);
|
|
764
|
-
frame.isInternal = isInternalCode(frame.file);
|
|
765
|
-
}
|
|
766
|
-
frames.push(frame);
|
|
767
|
-
}
|
|
768
|
-
}
|
|
769
|
-
return frames;
|
|
770
|
-
}
|
|
771
|
-
function getFirstAppFrame(frames) {
|
|
772
|
-
return frames.find((frame) => !frame.isNodeModule && !frame.isInternal);
|
|
773
|
-
}
|
|
774
|
-
|
|
775
1184
|
// src/adapters/generic/index.ts
|
|
776
1185
|
function generateErrorId() {
|
|
777
1186
|
const timestamp = Date.now().toString(36);
|
|
@@ -835,6 +1244,7 @@ async function captureError(error, metadata) {
|
|
|
835
1244
|
);
|
|
836
1245
|
return false;
|
|
837
1246
|
}
|
|
1247
|
+
markErrorReported(error);
|
|
838
1248
|
const err = error instanceof Error ? error : new Error(String(error));
|
|
839
1249
|
const capturedError = buildCapturedError(err, client, metadata);
|
|
840
1250
|
return client.reportError(capturedError);
|
|
@@ -880,6 +1290,6 @@ function createErrorCapture(baseMetadata) {
|
|
|
880
1290
|
};
|
|
881
1291
|
}
|
|
882
1292
|
|
|
883
|
-
export { BugStackClient, ErrorCaptureClient, captureError, captureException, createErrorCapture, initBugStack, installGlobalHandlers, wrapFunction };
|
|
1293
|
+
export { BugStackClient, ErrorCaptureClient, bugstackPinoHooks, bugstackWinstonFormat, captureError, captureException, createErrorCapture, initBugStack, installGlobalHandlers, loggedErrorStats, markErrorReported, wrapFunction };
|
|
884
1294
|
//# sourceMappingURL=index.mjs.map
|
|
885
1295
|
//# sourceMappingURL=index.mjs.map
|