Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion apps/server/src/preview/ServerBrowserPage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ import {
type CDPSession,
type Page,
} from "playwright-core";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "vite-plus/test";
import {
afterAll,
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
vi,
} from "vite-plus/test";

import * as ServerBrowserPage from "./ServerBrowserPage.ts";

Expand Down Expand Up @@ -388,3 +397,20 @@ describe("server browser drag", () => {
}
});
});

describe("server browser capture", () => {
it("fails a capture the page never answers instead of waiting for it", async () => {
vi.useFakeTimers();
try {
const cdp = { send: () => new Promise(() => {}) } as unknown as CDPSession;
const capture = ServerBrowserPage.captureViewport(cdp, { format: "jpeg" }).then(
() => null,
(error: unknown) => error,
);
await vi.advanceTimersByTimeAsync(5_000);
expect(await capture).toMatchObject({ tag: "PreviewAutomationTimeoutError" });
} finally {
vi.useRealTimers();
}
});
});
20 changes: 18 additions & 2 deletions apps/server/src/preview/ServerBrowserPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ const withoutTreeDataPayloads = (tree: string) =>
// The broker waits 15s for the host; a snapshot, retry included, answers well before.
const SNAPSHOT_TIMEOUT_MS = 10_000;
const FRAME_ANSWER_MS = 1_000;
// A tab that draws no frames never answers a screenshot, and callers hold the tab's queue while waiting.
const CAPTURE_TIMEOUT_MS = 5_000;

// Include password and CSS-masked text in open shadow roots, normalized like accessible text.
const DOTTED_SCRIPT = `(() => {
Expand Down Expand Up @@ -238,6 +240,12 @@ const tooBusy = () =>
"The page was too busy to capture a snapshot in time. Wait for it to settle and take another snapshot.",
);

const noFrame = () =>
new ServerBrowserOperationError(
"PreviewAutomationTimeoutError",
"The page drew no frame to capture in time. Try again once the tab is showing or has settled.",
);

const VALUE_TOO_LONG = "A password field holds a value too long to hide, so no snapshot was taken.";

/** Reuse one CDP session per out-of-process frame across snapshots. */
Expand Down Expand Up @@ -450,11 +458,19 @@ export const captureViewport = async (
cdp: CDPSession,
options: { readonly format: "png" | "jpeg"; readonly quality?: number },
) => {
const { data } = await cdp.send("Page.captureScreenshot", {
let timer: ReturnType<typeof setTimeout> | undefined;
const expired = new Promise<never>((_, reject) => {
timer = setTimeout(() => reject(noFrame()), CAPTURE_TIMEOUT_MS);
});
const captured = cdp.send("Page.captureScreenshot", {
format: options.format,
...(options.quality === undefined ? {} : { quality: options.quality }),
});
return data;
try {
return (await Promise.race([captured, expired])).data;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
} finally {
clearTimeout(timer);
}
};

/** Runs in the page's own isolated world, as the server has no image library. */
Expand Down
Loading