diff --git a/packages/engine/src/services/frameCapture.test.ts b/packages/engine/src/services/frameCapture.test.ts index 0f54395e38..f3cc3b4c27 100644 --- a/packages/engine/src/services/frameCapture.test.ts +++ b/packages/engine/src/services/frameCapture.test.ts @@ -220,6 +220,104 @@ describe("classifyPageError", () => { } }); + // fails without the fix: the page's text was logged and kept at whatever length it sent, in unbounded lists + it("logs and keeps the page's console and error text only up to a fixed size", async () => { + const stderr = vi.spyOn(console, "error").mockImplementation(() => {}); + try { + const { session, pageListeners, runtimeListeners } = await listenedSession(); + const huge = (n: number) => `${n}:${"x".repeat(10_000_000)}`; + pageListeners.get("response")?.({ + status: () => 200, + url: () => comp, + request: () => ({ resourceType: () => "script" }), + }); + for (let n = 0; n < 1_000; n++) { + const big = n < 5 ? huge(n) : `${n}:${"y".repeat(5_000)}`; + pageListeners.get("console")?.({ + type: () => "error", + text: () => `[HyperFrames] composition script error: ${big}`, + location: () => ({ url: big }), + }); + pageListeners.get("pageerror")?.(new Error(big)); + runtimeListeners.get("Runtime.exceptionThrown")?.({ + exceptionDetails: thrown(`Error: ${big}`, comp, [comp]), + }); + const url = `${server}/${big}.js`; + const request = { resourceType: () => "script", url: () => url, method: () => "GET" }; + pageListeners.get("requestfailed")?.({ + ...request, + failure: () => ({ errorText: "net::ERR_FAILED" }), + }); + pageListeners.get("response")?.({ + status: () => 404, + url: () => url, + statusText: () => big, + request: () => request, + }); + } + + const kept = [ + ...stderr.mock.calls.map((call) => call.join(" ")), + ...session.browserConsoleBuffer, + ...session.pageErrors, + ...session.scriptLoadFailures, + ]; + expect(Math.max(...kept.map((text) => text.length))).toBeLessThanOrEqual(2_000); + expect(session.pageErrors).toHaveLength(50); + expect(session.pageErrors[0]).toMatch(/^runtime-error:0:x/); + expect(session.scriptLoadFailures).toHaveLength(50); + expect(session.scriptLoadFailures[0]).toMatch(new RegExp(`^${server}/0:x`)); + expect(session.browserConsoleBuffer).toHaveLength(200); + } finally { + stderr.mockRestore(); + } + }); + + it("classifies a script by its whole URL before keeping a cut copy", async () => { + const { session, pageListeners } = await listenedSession(); + const url = `https://${"a".repeat(2_100)}.example.com/x.js`; + const request = { resourceType: () => "script", url: () => url, method: () => "GET" }; + pageListeners.get("requestfailed")?.({ + ...request, + failure: () => ({ errorText: "net::ERR_NAME_NOT_RESOLVED" }), + }); + pageListeners.get("response")?.({ + status: () => 404, + url: () => `${url}?again`, + statusText: () => "Not Found", + request: () => request, + }); + expect(session.scriptLoadFailures).toHaveLength(2); + expect(session.scriptLoadCauses?.map((cause) => cause.origin)).toEqual(["remote", "remote"]); + }); + + it("still fails fast on a blocked integrity check whose URL is long", async () => { + const { session, pageListeners } = await listenedSession(); + const url = `https://cdn.example.com/lib.js?signature=${"s".repeat(1_900)}`; + pageListeners.get("console")?.({ + type: () => "error", + text: () => + `Failed to find a valid digest in the 'integrity' attribute for resource '${url}' with computed SHA-384 integrity 'abc'. The resource has been blocked.`, + location: () => ({ url }), + }); + expect(session.scriptLoadFailures).toEqual(["runtime-error:subresource-integrity"]); + }); + + it("keeps a long font URL's load error off stderr", async () => { + const stderr = vi.spyOn(console, "error").mockImplementation(() => {}); + try { + const { pageListeners } = await listenedSession(); + pageListeners.get("console")?.({ + type: () => "error", + text: () => "Failed to load resource: the server responded with a status of 404", + location: () => ({ url: `https://fonts.example.com/${"f".repeat(2_100)}.woff2` }), + }); + expect(stderr).not.toHaveBeenCalled(); + } finally { + stderr.mockRestore(); + } + }); + it("records the page's uncaught errors from scripts it loaded from the server", async () => { const { session, pageListeners, runtimeListeners } = await listenedSession(); const loaded = (url: string, resourceType: string) => ({ diff --git a/packages/engine/src/services/frameCapture.ts b/packages/engine/src/services/frameCapture.ts index 21c6d04a30..2ebe6700bd 100644 --- a/packages/engine/src/services/frameCapture.ts +++ b/packages/engine/src/services/frameCapture.ts @@ -594,8 +594,21 @@ export async function decodeDynamicCssBackgroundImages(page: Page): Promise= 0xd800 && high <= 0xdbff) end -= 1; + return `${text.slice(0, end)}… (${text.length - end} more characters)`; +} + function appendBrowserDiagnostic(session: CaptureSession, text: string): void { - session.browserConsoleBuffer.push(text); + session.browserConsoleBuffer.push(boundedPageText(text)); if (session.browserConsoleBuffer.length > BROWSER_CONSOLE_BUFFER_SIZE) { session.browserConsoleBuffer.shift(); } @@ -2926,7 +2939,8 @@ function assertVfxIntact(session: CaptureSession): void { } // A `vfx:` detail is a chain that cannot paint, so every frame is wrong; `vfx-frame:` is one frame, a page error. -function recordConsoleScriptError(session: CaptureSession, error: string | null): void { +function recordConsoleScriptError(session: CaptureSession, detail: string | null): void { + const error = detail && boundedPageText(detail); if (!error?.startsWith("runtime-error:vfx:")) { recordPageError(session, error); return; @@ -2941,13 +2955,16 @@ function recordConsoleScriptError(session: CaptureSession, error: string | null) function loggedEarlierInRender(session: CaptureSession, text: string): boolean { if (!text.startsWith(SCENE_CALLBACK_ERROR_PREFIX)) return false; const logged = (session.subTimelineWaitMemo.loggedSceneCallbackErrors ??= []); - if (logged.includes(text)) return true; - logged.push(text); + const kept = boundedPageText(text); + if (logged.includes(kept) || logged.length >= PAGE_LIST_MAX) return true; + logged.push(kept); return false; } function recordPageError(session: CaptureSession, error: string | null): void { - if (error && !session.pageErrors.includes(error)) session.pageErrors.push(error); + const kept = error && boundedPageText(error); + if (kept && session.pageErrors.length < PAGE_LIST_MAX && !session.pageErrors.includes(kept)) + session.pageErrors.push(kept); } // A 4xx `response` and a `requestfailed` can both fire for the same script @@ -2959,8 +2976,12 @@ function recordScriptLoadFailure( url: string, cause: ScriptLoadCause, ): void { - if (!session.scriptLoadFailures.includes(url)) { - session.scriptLoadFailures.push(url); + const kept = boundedPageText(url); + if ( + session.scriptLoadFailures.length < PAGE_LIST_MAX && + !session.scriptLoadFailures.includes(kept) + ) { + session.scriptLoadFailures.push(kept); (session.scriptLoadCauses ??= []).push(cause); } } @@ -3148,7 +3169,7 @@ export async function initializeSession(session: CaptureSession): Promise !quietRuntimeDetail(text) && !loggedEarlierInRender(session, text) ) { - console.error(terminalText(diagnostic.text)); + console.error(terminalText(boundedPageText(diagnostic.text))); } appendBrowserDiagnostic(session, diagnostic.text); @@ -3164,7 +3185,7 @@ export async function initializeSession(session: CaptureSession): Promise const text = `[Browser:PAGEERROR] ${message}`; // Benign play/pause race during frame capture — suppress terminal noise, keep in buffer. - if (!isPlayPauseAbort(message)) console.error(terminalText(text)); + if (!isPlayPauseAbort(message)) console.error(terminalText(boundedPageText(text))); appendBrowserDiagnostic(session, text); }); @@ -3211,11 +3232,13 @@ export async function initializeSession(session: CaptureSession): Promise recordPageError(session, error); if (!error) return; // Recorded first so fail-fast sees it; the throw site is appended once the source is fetched. + const kept = boundedPageText(error); void describeThrowSite(runtimeClient, exceptionDetails, projectScripts).then((site) => { - const i = session.pageErrors.indexOf(error); + const i = session.pageErrors.indexOf(kept); if (!site || i < 0) return; - if (session.pageErrors.includes(`${error}${site}`)) session.pageErrors.splice(i, 1); - else session.pageErrors[i] = `${error}${site}`; + const sited = boundedPageText(`${kept}${site}`); + if (session.pageErrors.includes(sited)) session.pageErrors.splice(i, 1); + else session.pageErrors[i] = sited; }); }); await runtimeClient.send("Runtime.enable");