@watchforge/browser 0.1.18 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@watchforge/browser",
3
- "version": "0.1.18",
3
+ "version": "0.1.21",
4
4
  "main": "./src/index.js",
5
5
  "types": "./src/index.d.ts",
6
6
  "description": "WatchForge JavaScript SDK for browser JavaScript, Next.js, React, Node.js, and Express.js",
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;
package/src/stacktrace.js CHANGED
@@ -139,7 +139,7 @@ function isJsonParseError(message) {
139
139
 
140
140
  function isUriError(message) {
141
141
  if (!message || typeof message !== "string") return false;
142
- return /URI(?:Error| malformed)/i.test(message) || /decodeURIComponent/i.test(message);
142
+ return /URI\s+malformed/i.test(message) || /URIError/i.test(message);
143
143
  }
144
144
 
145
145
  function isInvalidArrayLengthError(message) {
@@ -717,13 +717,19 @@ function resolveFrameLine(frame, lines) {
717
717
  return lineno;
718
718
  }
719
719
 
720
+ function applyResolvedSourceContext(frame, sourceLines) {
721
+ if (!sourceLines?.length) return false;
722
+ const lineno = resolveFrameLine(frame, sourceLines);
723
+ applySourceContext(frame, sourceLines, lineno);
724
+ return Boolean(frame.context_line);
725
+ }
726
+
720
727
  async function enrichFrameWithNodeSource(frame) {
721
728
  if (!frame.in_app || !frame.lineno) return;
722
729
  const lines = await readNodeSourceLines(frame.raw_abs_path || frame.abs_path);
723
730
  if (!lines) return;
724
731
 
725
- const lineno = resolveFrameLine(frame, lines);
726
- applySourceContext(frame, lines, lineno);
732
+ applyResolvedSourceContext(frame, lines);
727
733
  }
728
734
 
729
735
  function getBrowserSourceFetchCandidates(absPath, rawAbsPath) {
@@ -917,12 +923,10 @@ function applyOriginalSourceFrameContext(frame, sourceMap) {
917
923
 
918
924
  const sourceContent = getSourceMapContentForFrame(frame, sourceMap);
919
925
  if (!sourceContent?.lines?.length) return false;
920
- if (frame.lineno > sourceContent.lines.length) return false;
921
926
 
922
927
  frame.abs_path = sourceContent.source;
923
928
  frame.filename = normalizeSourcePath(sourceContent.source).split("/").pop() || frame.filename;
924
- applySourceContext(frame, sourceContent.lines, frame.lineno);
925
- return Boolean(frame.context_line);
929
+ return applyResolvedSourceContext(frame, sourceContent.lines);
926
930
  }
927
931
 
928
932
  async function findSourceMapForFrame(frame) {
@@ -1081,12 +1085,9 @@ function getWebpackFrameArtifacts(frame) {
1081
1085
  async function enrichFrameWithBrowserSource(frame) {
1082
1086
  if (!frame.in_app || !frame.lineno) return;
1083
1087
 
1084
- if (isOriginalSourceFrame(frame)) {
1085
- const lines = await fetchBrowserSourceLines(frame.abs_path, frame.raw_abs_path);
1086
- if (lines) {
1087
- applySourceContext(frame, lines, frame.lineno);
1088
- if (frame.context_line) return;
1089
- }
1088
+ const lines = await fetchBrowserSourceLines(frame.abs_path, frame.raw_abs_path);
1089
+ if (lines && applyResolvedSourceContext(frame, lines)) {
1090
+ return;
1090
1091
  }
1091
1092
 
1092
1093
  const webpackArtifacts = getWebpackFrameArtifacts(frame);
@@ -1096,16 +1097,7 @@ async function enrichFrameWithBrowserSource(frame) {
1096
1097
  }
1097
1098
 
1098
1099
  if (webpackArtifacts?.lines && !webpackArtifacts?.sourceMap) {
1099
- const lineno = resolveFrameLine(frame, webpackArtifacts.lines);
1100
- applySourceContext(frame, webpackArtifacts.lines, lineno);
1101
- if (frame.context_line) return;
1102
- }
1103
-
1104
- const lines = await fetchBrowserSourceLines(frame.abs_path, frame.raw_abs_path);
1105
- if (lines) {
1106
- const lineno = resolveFrameLine(frame, lines);
1107
- applySourceContext(frame, lines, lineno);
1108
- if (frame.context_line) return;
1100
+ if (applyResolvedSourceContext(frame, webpackArtifacts.lines)) return;
1109
1101
  }
1110
1102
 
1111
1103
  await enrichFrameWithSourceMap(frame);