feat(browser): clamp screenshots under 5MB

This commit is contained in:
Peter Steinberger
2025-12-13 18:08:00 +00:00
parent 867d7e5d25
commit 56fe23549c
4 changed files with 173 additions and 4 deletions

View File

@@ -81,6 +81,19 @@ async function fetchJson<T>(url: string, timeoutMs = 1500): Promise<T> {
export async function captureScreenshotPng(opts: {
wsUrl: string;
fullPage?: boolean;
}): Promise<Buffer> {
return await captureScreenshot({
wsUrl: opts.wsUrl,
fullPage: opts.fullPage,
format: "png",
});
}
export async function captureScreenshot(opts: {
wsUrl: string;
fullPage?: boolean;
format?: "png" | "jpeg";
quality?: number; // jpeg only (0..100)
}): Promise<Buffer> {
const ws = new WebSocket(opts.wsUrl, { handshakeTimeout: 5000 });
const { send, closeWithError } = createCdpSender(ws);
@@ -110,8 +123,15 @@ export async function captureScreenshotPng(opts: {
}
}
const format = opts.format ?? "png";
const quality =
format === "jpeg"
? Math.max(0, Math.min(100, Math.round(opts.quality ?? 85)))
: undefined;
const result = (await send("Page.captureScreenshot", {
format: "png",
format,
...(quality !== undefined ? { quality } : {}),
fromSurface: true,
captureBeyondViewport: true,
...(clip ? { clip } : {}),