From adf14d7324109694614955f32ba6cf2b97ad79d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sun, 8 Nov 2026 03:03:07 +0000 Subject: [PATCH] feat(engine): a failed CDN script's report names its library and version, never its URL (#7394) --- .../frameCapture-scriptLoadCause.test.ts | 32 ++++++++++++++++++- packages/engine/src/services/frameCapture.ts | 27 ++++++++++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/packages/engine/src/services/frameCapture-scriptLoadCause.test.ts b/packages/engine/src/services/frameCapture-scriptLoadCause.test.ts index 71db78e3d7..fa6caf3014 100644 --- a/packages/engine/src/services/frameCapture-scriptLoadCause.test.ts +++ b/packages/engine/src/services/frameCapture-scriptLoadCause.test.ts @@ -24,7 +24,20 @@ describe("classifyScriptLoadCause", () => { ["runtime-error:subresource-integrity", "unknown", "unknown", "unknown"], [`${SERVER}/a.js`, "something the browser worded freely: /Users/me/x.js", "local", "unknown"], ] as const)("%s + %s", (url, failure, origin, code) => { - expect(classifyScriptLoadCause(url, SERVER, failure)).toEqual({ origin, code }); + expect(classifyScriptLoadCause(url, SERVER, failure)).toMatchObject({ origin, code }); + }); + + // fails without the fix: a report said a CDN script failed but never which library. + it.each([ + ["https://cdn.jsdelivr.net/npm/gsap@3.14.2/dist/gsap.min.js", "gsap@3.14.2"], + ["https://unpkg.com/three@0.147.0/build/three.min.js", "three@0.147.0"], + ["https://cdnjs.cloudflare.com/ajax/libs/lottie-web/5.12.2/lottie.min.js", "lottie-web@5.12.2"], + ["https://cdn.jsdelivr.net/npm/gsap@3/dist/gsap.min.js", undefined], + ["https://cdn.jsdelivr.net/npm/@fontsource/roboto@5.2.8/x.css", undefined], + ["https://assets.example.test/gsap@3.14.2/gsap.js", undefined], + ["https://cdn.jsdelivr.net/npm/gsap@3.14.2/../../evil@1.0.0/x.js", undefined], + ] as const)("names the library of %s as %s", (url, lib) => { + expect(classifyScriptLoadCause(url, SERVER, "net::ERR_INTERNET_DISCONNECTED").lib).toBe(lib); }); }); @@ -49,4 +62,21 @@ describe("the script-failure warning", () => { ), ).toBe(true); }); + + it("names the failed libraries in a bracket of their own, after the first one", () => { + vi.spyOn(console, "warn").mockImplementation(() => {}); + const session = { + subTimelineWaitOutcome: "script_failure", + scriptLoadFailures: ["https://cdn.jsdelivr.net/npm/gsap@3.14.2/dist/gsap.min.js"], + scriptLoadCauses: [ + { origin: "cdn", code: "net::ERR_INTERNET_DISCONNECTED", lib: "gsap@3.14.2" }, + ], + pageErrors: [], + warnings: [], + } as unknown as CaptureSession; + recordSubTimelineWarning(session, 45_000); + expect(session.warnings[0].message).toContain( + "[script_load: cdn/net::ERR_INTERNET_DISCONNECTED] [script_lib: gsap@3.14.2] (", + ); + }); }); diff --git a/packages/engine/src/services/frameCapture.ts b/packages/engine/src/services/frameCapture.ts index 71b38038e9..890811d827 100644 --- a/packages/engine/src/services/frameCapture.ts +++ b/packages/engine/src/services/frameCapture.ts @@ -2901,10 +2901,29 @@ export interface ScriptLoadCause { origin: ScriptLoadOrigin; /** `net::ERR_*`, `http-` or `unknown`. */ code: string; + /** `name@x.y.z` of a public CDN's npm package, so a report says which library failed; never the URL. */ + lib?: string; } const CDN_HOSTS = /^(?:cdn\.jsdelivr\.net|unpkg\.com|cdnjs\.cloudflare\.com)$/; +const LIB = "([a-z0-9][a-z0-9._-]{0,48})"; +const VERSION = "(\\d+\\.\\d+\\.\\d+)"; +const LIB_PATHS = [ + ["cdn.jsdelivr.net", new RegExp(`^/npm/${LIB}@${VERSION}/`)], + ["unpkg.com", new RegExp(`^/${LIB}@${VERSION}/`)], + ["cdnjs.cloudflare.com", new RegExp(`^/ajax/libs/${LIB}/${VERSION}/`)], +] as const; + +/** `name@x.y.z` for an unscoped package at one exact version on a public CDN, else undefined. */ +function cdnLib(url: URL): string | undefined { + for (const [host, path] of LIB_PATHS) { + const match = url.hostname === host ? path.exec(url.pathname) : null; + if (match) return `${match[1]}@${match[2]}`; + } + return undefined; +} + /** Where a failed script came from and why, never its URL, host or path. */ export function classifyScriptLoadCause( url: string, @@ -2922,12 +2941,16 @@ export function classifyScriptLoadCause( : /^net::ERR_[A-Z0-9_]{1,40}$/.test(failure) ? failure : "unknown"; - return { origin, code }; + const lib = origin === "cdn" ? cdnLib(new URL(url)) : undefined; + return lib ? { origin, code, lib } : { origin, code }; } +/** Each cause's origin and code, then the failed libraries in a bracket the first bracket's readers ignore. */ function scriptLoadCauseSuffix(causes: readonly ScriptLoadCause[]): string { const shown = causes.slice(0, 4).map((c) => `${c.origin}/${c.code}`); - return shown.length === 0 ? "" : ` [script_load: ${shown.join(", ")}]`; + if (shown.length === 0) return ""; + const libs = [...new Set(causes.slice(0, 4).flatMap((c) => (c.lib ? [c.lib] : [])))]; + return ` [script_load: ${shown.join(", ")}]${libs.length > 0 ? ` [script_lib: ${libs.join(", ")}]` : ""}`; } const CANCELLED_BY_THE_PAGE_NOT_REFUSED = "net::ERR_ABORTED";