feat(engine): a failed CDN script's report names its library and version, never its URL (#7394)

This commit is contained in:
Miguel Ángel
2026-11-08 03:03:07 +00:00
committed by GitHub
parent 3151fd2133
commit adf14d7324
2 changed files with 56 additions and 3 deletions
@@ -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] (",
);
});
});
+25 -2
View File
@@ -2901,10 +2901,29 @@ export interface ScriptLoadCause {
origin: ScriptLoadOrigin;
/** `net::ERR_*`, `http-<status>` 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";