@watchforge/browser 0.1.20 → 0.1.21
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/package.json +1 -1
- package/src/client.js +73 -0
- package/src/index.d.ts +5 -0
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -97,6 +97,71 @@ function captureGlobalException(error, context = {}) {
|
|
|
97
97
|
void captureException(normalizeException(error, context.message), context);
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
+
function normalizeStackFramePath(path) {
|
|
101
|
+
if (!path) return "";
|
|
102
|
+
try {
|
|
103
|
+
if (isBrowser) {
|
|
104
|
+
const url = new URL(String(path), window.location.href);
|
|
105
|
+
if (url.origin === window.location.origin) {
|
|
106
|
+
return url.pathname.replace(/^\/+/, "");
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
} catch {
|
|
110
|
+
// keep original path
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return String(path)
|
|
114
|
+
.replace(/^webpack-internal:\/\/\/?/, "")
|
|
115
|
+
.replace(/^webpack:\/\/(?:\([^)]*\)\/)?/, "")
|
|
116
|
+
.replace(/^webpack:\/\/[^/]+\//, "")
|
|
117
|
+
.replace(/^\([^)]*\)\//, "")
|
|
118
|
+
.replace(/^file:\/\//, "")
|
|
119
|
+
.split("?")[0]
|
|
120
|
+
.split("#")[0]
|
|
121
|
+
.replace(/\\/g, "/")
|
|
122
|
+
.replace(/^\/+/, "")
|
|
123
|
+
.replace(/^\.\//, "");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function isOriginalSourcePath(path) {
|
|
127
|
+
const normalized = normalizeStackFramePath(path);
|
|
128
|
+
return /(?:^|\/)src\/.+\.(?:tsx?|jsx?)$/.test(normalized);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function applyBrowserLocationOverride(stacktrace, location) {
|
|
132
|
+
if (!stacktrace?.frames?.length || !location?.filename || !location?.lineno) {
|
|
133
|
+
return stacktrace;
|
|
134
|
+
}
|
|
135
|
+
if (!isOriginalSourcePath(location.filename)) return stacktrace;
|
|
136
|
+
|
|
137
|
+
const normalizedLocation = normalizeStackFramePath(location.filename);
|
|
138
|
+
const matchingFrames = stacktrace.frames.filter((frame) => {
|
|
139
|
+
const framePath = normalizeStackFramePath(
|
|
140
|
+
frame.abs_path || frame.module || frame.raw_abs_path || frame.filename
|
|
141
|
+
);
|
|
142
|
+
return (
|
|
143
|
+
frame.in_app &&
|
|
144
|
+
(framePath === normalizedLocation ||
|
|
145
|
+
framePath.endsWith(`/${normalizedLocation}`) ||
|
|
146
|
+
normalizedLocation.endsWith(`/${framePath}`))
|
|
147
|
+
);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
const target = matchingFrames[matchingFrames.length - 1] || stacktrace.frames.find((f) => f.in_app);
|
|
151
|
+
if (!target) return stacktrace;
|
|
152
|
+
|
|
153
|
+
target.abs_path = normalizedLocation;
|
|
154
|
+
target.raw_abs_path = location.filename;
|
|
155
|
+
target.module = normalizedLocation;
|
|
156
|
+
target.filename = normalizedLocation.split("/").pop() || target.filename;
|
|
157
|
+
target.lineno = Number(location.lineno);
|
|
158
|
+
if (location.colno != null) target.colno = Number(location.colno);
|
|
159
|
+
target.function = target.function || "<anonymous>";
|
|
160
|
+
target.browser_location_override = true;
|
|
161
|
+
|
|
162
|
+
return stacktrace;
|
|
163
|
+
}
|
|
164
|
+
|
|
100
165
|
function getElementDescriptor(element) {
|
|
101
166
|
if (!element || typeof element !== "object") return null;
|
|
102
167
|
const tag = element.tagName ? String(element.tagName).toLowerCase() : "unknown";
|
|
@@ -236,6 +301,11 @@ function setupBrowserInstrumentation() {
|
|
|
236
301
|
mechanism: "window.onerror",
|
|
237
302
|
},
|
|
238
303
|
extra: { url, line, col },
|
|
304
|
+
stackFrame: {
|
|
305
|
+
filename: url,
|
|
306
|
+
lineno: line,
|
|
307
|
+
colno: col,
|
|
308
|
+
},
|
|
239
309
|
message: String(msg),
|
|
240
310
|
});
|
|
241
311
|
};
|
|
@@ -742,6 +812,9 @@ export async function captureException(error, context = {}) {
|
|
|
742
812
|
if (!DSN) return;
|
|
743
813
|
|
|
744
814
|
let stacktrace = buildStacktraceFromError(error);
|
|
815
|
+
if (stacktrace && context.stackFrame) {
|
|
816
|
+
stacktrace = applyBrowserLocationOverride(stacktrace, context.stackFrame);
|
|
817
|
+
}
|
|
745
818
|
if (stacktrace) {
|
|
746
819
|
stacktrace = await enrichStacktraceAsync(
|
|
747
820
|
stacktrace,
|
package/src/index.d.ts
CHANGED
|
@@ -23,6 +23,11 @@ export interface WatchForgeCaptureContext {
|
|
|
23
23
|
tags?: Record<string, unknown>;
|
|
24
24
|
extra?: Record<string, unknown>;
|
|
25
25
|
contexts?: Record<string, unknown>;
|
|
26
|
+
stackFrame?: {
|
|
27
|
+
filename?: string;
|
|
28
|
+
lineno?: number;
|
|
29
|
+
colno?: number;
|
|
30
|
+
};
|
|
26
31
|
}
|
|
27
32
|
|
|
28
33
|
export function register(options: WatchForgeRegisterOptions): void;
|