diff --git a/packages/engine/src/services/frameCapture-scriptLoadCause.test.ts b/packages/engine/src/services/frameCapture-scriptLoadCause.test.ts index fa6caf3014..7595ff857d 100644 --- a/packages/engine/src/services/frameCapture-scriptLoadCause.test.ts +++ b/packages/engine/src/services/frameCapture-scriptLoadCause.test.ts @@ -1,6 +1,10 @@ import { describe, expect, it, vi } from "vitest"; import type { CaptureSession } from "./frameCapture.js"; -import { classifyScriptLoadCause, recordSubTimelineWarning } from "./frameCapture.js"; +import { + classifyScriptLoadCause, + recordFailedRequest, + recordSubTimelineWarning, +} from "./frameCapture.js"; const SERVER = "http://localhost:4100"; @@ -41,6 +45,42 @@ describe("classifyScriptLoadCause", () => { }); }); +// fails without the fix: Node's URL parser threw on a host the browser had accepted, from inside a page listener +describe("a script URL Node cannot parse", () => { + const unparseable = ["http://xn--a.com/lib.js", "https://xn--ab-m1t.com/lib.js"]; + + it.each(unparseable)("classifies %s as remote instead of throwing", (url) => { + expect(() => new URL(url)).toThrow(); + expect(classifyScriptLoadCause(url, SERVER, "net::ERR_NAME_NOT_RESOLVED")).toEqual({ + origin: "remote", + code: "net::ERR_NAME_NOT_RESOLVED", + }); + expect(classifyScriptLoadCause(url, SERVER, 404)).toEqual({ + origin: "remote", + code: "http-404", + }); + }); + + it.each(unparseable)("records the failed request %s and keeps going", (url) => { + const session = { + serverUrl: SERVER, + scriptLoadFailures: [], + pageErrors: [], + browserConsoleBuffer: [], + } as unknown as CaptureSession; + recordFailedRequest(session, { + resourceType: () => "script", + url: () => url, + method: () => "GET", + failure: () => ({ errorText: "net::ERR_NAME_NOT_RESOLVED" }), + }); + expect(session.scriptLoadFailures).toEqual([url]); + expect(session.scriptLoadCauses).toEqual([ + { origin: "remote", code: "net::ERR_NAME_NOT_RESOLVED" }, + ]); + }); +}); + describe("the script-failure warning", () => { it("names the closed origin and code of each failed script before the free text, so a cut message keeps them", () => { vi.spyOn(console, "warn").mockImplementation(() => {}); diff --git a/packages/engine/src/services/frameCapture.ts b/packages/engine/src/services/frameCapture.ts index b1ce9e7be2..21c6d04a30 100644 --- a/packages/engine/src/services/frameCapture.ts +++ b/packages/engine/src/services/frameCapture.ts @@ -3000,9 +3000,10 @@ export function classifyScriptLoadCause( failure: string | number, ): ScriptLoadCause { let origin: ScriptLoadOrigin = "unknown"; + const parsed = URL.canParse(url) ? new URL(url) : undefined; if (url.startsWith(`${serverUrl}/`)) origin = "local"; else if (/^https?:\/\//.test(url)) { - origin = CDN_HOSTS.test(new URL(url).hostname) ? "cdn" : "remote"; + origin = parsed && CDN_HOSTS.test(parsed.hostname) ? "cdn" : "remote"; } const code = typeof failure === "number" @@ -3010,7 +3011,7 @@ export function classifyScriptLoadCause( : /^net::ERR_[A-Z0-9_]{1,40}$/.test(failure) ? failure : "unknown"; - const lib = origin === "cdn" ? cdnLib(new URL(url)) : undefined; + const lib = origin === "cdn" && parsed ? cdnLib(parsed) : undefined; return lib ? { origin, code, lib } : { origin, code }; }