Skip to content
Merged
Changes from all 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
17 changes: 16 additions & 1 deletion src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, string> = {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
Expand All @@ -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) {
Expand Down