From 5d412b6ff173661a4d19d753fbce833e42158a09 Mon Sep 17 00:00:00 2001 From: rcholic Date: Sun, 28 Dec 2025 14:03:34 -0800 Subject: [PATCH] cap request payload to 10MB --- src/snapshot.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/snapshot.ts b/src/snapshot.ts index 2b3b12b0..3d8cbe6c 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -7,6 +7,9 @@ import { Snapshot } from './types'; import * as fs from 'fs'; import * as path from 'path'; +// Maximum payload size for API requests (10MB server limit) +const MAX_PAYLOAD_BYTES = 10 * 1024 * 1024; + export interface SnapshotOptions { screenshot?: boolean | { format: 'png' | 'jpeg'; quality?: number }; limit?: number; @@ -183,6 +186,18 @@ async function snapshotViaApi( }, }; + // Check payload size before sending (server has 10MB limit) + const payloadJson = JSON.stringify(payload); + const payloadSize = new TextEncoder().encode(payloadJson).length; + if (payloadSize > MAX_PAYLOAD_BYTES) { + const sizeMB = (payloadSize / 1024 / 1024).toFixed(2); + const limitMB = (MAX_PAYLOAD_BYTES / 1024 / 1024).toFixed(0); + throw new Error( + `Payload size (${sizeMB}MB) exceeds server limit (${limitMB}MB). ` + + `Try reducing the number of elements on the page or filtering elements.` + ); + } + const headers: Record = { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json', @@ -192,7 +207,7 @@ async function snapshotViaApi( const response = await fetch(`${apiUrl}/v1/snapshot`, { method: 'POST', headers, - body: JSON.stringify(payload), + body: payloadJson, }); if (!response.ok) {