Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
fix(cli): a page-load timeout names the script or stylesheet the page was waiting on (#5727)
Miguel Ángeladb7a6c994bf
diff --git a/packages/cli/src/capture/navigateForCapture.browser.test.ts b/packages/cli/src/capture/navigateForCapture.browser.test.tsindex 28d9507248..89fdc59fb9 100644--- a/packages/cli/src/capture/navigateForCapture.browser.test.ts+++ b/packages/cli/src/capture/navigateForCapture.browser.test.ts@@ -4,12 +4,16 @@ import type { AddressInfo } from "node:net";import puppeteer, { type Browser } from "puppeteer-core";import { afterAll, beforeAll, expect, it } from "vitest";import { ensureBrowser } from "../browser/manager.js";-import { documentRequestSent } from "./navigateForCapture.js";+import { documentRequestSent, unfinishedPageLoadRequests } from "./navigateForCapture.js";let browser: Browser;let held: ServerResponse | undefined;let requested: () => void = () => undefined;-const server = createServer((_req, res) => {+const server = createServer((req, res) => {+ if (req.url === "/with-script") {+ res.end('<script src="/held.js"></script><p>page</p>');+ return;+ }held = res;requested();});@@ -58,3 +62,32 @@ it("settles for a page answered by interception, which never reaches the networkexpect(await page.content()).toContain("answered");await page.close();});++it("lists a page's script while it is still loading, and drops it once it arrives", async () => {+ const page = await browser.newPage();+ const cdp = await page.createCDPSession();+ await cdp.send("Network.enable");+ const scriptRequested = new Promise<void>((resolve) => (requested = resolve));+ const unfinished = unfinishedPageLoadRequests(cdp);+ const scriptSeenByChrome = new Promise<void>((resolve) =>+ cdp.on("Network.requestWillBeSent", ({ request }) => {+ if (request.url.endsWith("/held.js")) resolve();+ }),+ );+ const origin = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;+ const navigation = page.goto(`${origin}/with-script`, {+ waitUntil: "domcontentloaded",+ timeout: 0,+ });+ await Promise.all([scriptRequested, scriptSeenByChrome]);+ expect(unfinished()).toContain(`${origin}/held.js`);+ const scriptDone = new Promise<void>((resolve) =>+ cdp.on("Network.loadingFinished", () => {+ if (!unfinished().includes(`${origin}/held.js`)) resolve();+ }),+ );+ held?.end("window.__held = true;");+ await Promise.all([navigation, scriptDone]);+ expect(unfinished()).not.toContain(`${origin}/held.js`);+ await page.close();+});diff --git a/packages/cli/src/capture/navigateForCapture.test.ts b/packages/cli/src/capture/navigateForCapture.test.tsindex aa65f0b821..73ba7007b9 100644--- a/packages/cli/src/capture/navigateForCapture.test.ts+++ b/packages/cli/src/capture/navigateForCapture.test.ts@@ -144,7 +144,14 @@ function fakeChromePage() {page,session,navigate: () =>- session.emit("Network.requestWillBeSent", { requestId: "doc", type: "Document" }),+ session.emit("Network.requestWillBeSent", {+ requestId: "doc",+ type: "Document",+ request: { url: "http://127.0.0.1:1/" },+ }),+ request: (requestId: string, type: string, url: string) =>+ session.emit("Network.requestWillBeSent", { requestId, type, request: { url } }),+ finish: (requestId: string) => session.emit("Network.loadingFinished", { requestId }),send: () => session.emit("Network.requestWillBeSentExtraInfo", { requestId: "doc" }),load: (response: unknown) => load(response),};@@ -196,4 +203,54 @@ describe("gotoWhenSent", () => {await vi.advanceTimersByTimeAsync(10_000);await failed;});++ it.each([+ [+ "names the other server's script it is still waiting on",+ "https://cdn.example/gsap.min.js",+ "https://cdn.example/gsap.min.js",+ true,+ ],+ [+ "names a stalled script of its own without advising to move it",+ "http://127.0.0.1:1/app.js",+ "http://127.0.0.1:1/app.js",+ false,+ ],+ [+ "keeps a signed URL's secrets out of the message for another server's script",+ "https://user:[email protected]/gsap.min.js?sig=SECRET#SECRET",+ "https://cdn.example/gsap.min.js?…",+ true,+ ],+ [+ "keeps a signed URL's secrets out of the message for a script of its own",+ "http://127.0.0.1:1/app.js?token=SECRET",+ "http://127.0.0.1:1/app.js?…",+ false,+ ],+ ])("%s", async (_name, script, shown, advises) => {+ vi.useFakeTimers();+ const chrome = fakeChromePage();+ const opened = gotoWhenSent(chrome.page, "http://127.0.0.1:1/", 10_000);+ const failed = expect(opened).rejects.toSatisfy((error: unknown) => {+ const message = String(error);+ return (+ isNavigationTimeoutError(error) &&+ message.includes(`while the page was still loading ${shown}.`) &&+ !message.includes("SECRET") &&+ !message.includes("styles.css") &&+ message.includes("from another server") === advises+ );+ });+ await vi.advanceTimersByTimeAsync(0);+ chrome.navigate();+ chrome.send();+ chrome.request("css", "Stylesheet", "http://127.0.0.1:1/styles.css");+ chrome.request("js", "Script", script);+ chrome.finish("doc");+ chrome.finish("css");+ await vi.advanceTimersByTimeAsync(10_000);+ await failed;+ });});diff --git a/packages/cli/src/capture/navigateForCapture.ts b/packages/cli/src/capture/navigateForCapture.tsindex 68ff4793b8..29b16fa8f4 100644--- a/packages/cli/src/capture/navigateForCapture.ts+++ b/packages/cli/src/capture/navigateForCapture.ts@@ -64,6 +64,8 @@ export async function navigateForCapture<TResponse>(}const PUPPETEER_LAUNCH_DEFAULT_TIMEOUT_MS = 30_000;+const PAGE_LOAD_BLOCKING_TYPES = new Set(["Document", "Script", "Stylesheet"]);+const NAMED_BLOCKING_REQUESTS = 3;const SENT_OR_ANSWERED_WITHOUT_NETWORK = ["Network.requestWillBeSentExtraInfo","Network.responseReceived",@@ -79,6 +81,7 @@ export async function gotoWhenSent(try {await cdp.send("Network.enable");const sent = documentRequestSent(cdp);+ const unfinished = unfinishedPageLoadRequests(cdp);const navigation = page.goto(url, { waitUntil: "domcontentloaded", timeout: 0 });const navigationAwaitedBelow = navigation.catch(() => undefined);await withRemainingBudget(@@ -94,7 +97,7 @@ export async function gotoWhenSent(navigation,timeoutMs,"loading the page",- () => new TimeoutError(`Navigation timeout of ${timeoutMs} ms exceeded`),+ () => new TimeoutError(pageLoadTimeoutMessage(timeoutMs, url, unfinished())),);} finally {await cdp.detach().catch(() => undefined);@@ -120,3 +123,46 @@ export function documentRequestSent(cdp: CDPSession): Promise<void> {}});}++export function unfinishedPageLoadRequests(cdp: CDPSession): () => string[] {+ const unfinished = new Map<string, string>();+ cdp.on("Network.requestWillBeSent", ({ requestId, request, type }) => {+ if (type && PAGE_LOAD_BLOCKING_TYPES.has(type)) unfinished.set(requestId, request.url);+ });+ for (const event of ["Network.loadingFinished", "Network.loadingFailed"] as const) {+ cdp.on(event, ({ requestId }) => unfinished.delete(requestId));+ }+ return () => [...unfinished.values()];+}++function urlWithoutSecrets(raw: string): string {+ try {+ const url = new URL(raw);+ if (url.protocol !== "http:" && url.protocol !== "https:") return `${url.protocol}…`;+ return `${url.origin}${url.pathname}${url.search ? "?…" : ""}`;+ } catch {+ return "(unreadable URL)";+ }+}++function originOf(raw: string): string | null {+ try {+ return new URL(raw).origin;+ } catch {+ return null;+ }+}++function pageLoadTimeoutMessage(timeoutMs: number, pageUrl: string, waitingOn: string[]): string {+ const exceeded = `Navigation timeout of ${timeoutMs} ms exceeded`;+ if (waitingOn.length === 0) return exceeded;+ const named = waitingOn.slice(0, NAMED_BLOCKING_REQUESTS).map(urlWithoutSecrets).join(", ");+ const unnamed = waitingOn.length - NAMED_BLOCKING_REQUESTS;+ const more = unnamed > 0 ? ` and ${unnamed} more` : "";+ const pageOrigin = new URL(pageUrl).origin;+ const fromAnotherServer = waitingOn.some((request) => originOf(request) !== pageOrigin);+ const advice = fromAnotherServer+ ? " A <script> or stylesheet from another server holds the page until it arrives; serve it from the project instead."+ : "";+ return `${exceeded} while the page was still loading ${named}${more}.${advice}`;+}