Skip to content

Commit de5b6c6

Browse files
author
SentienceDEV
committed
Merge pull request #67 from SentienceAPI/cap_max_elements
cap request payload to 10MB
2 parents bc91c00 + a6e8f8f commit de5b6c6

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/snapshot.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { Snapshot } from './types';
77
import * as fs from 'fs';
88
import * as path from 'path';
99

10+
// Maximum payload size for API requests (10MB server limit)
11+
const MAX_PAYLOAD_BYTES = 10 * 1024 * 1024;
12+
1013
export interface SnapshotOptions {
1114
screenshot?: boolean | { format: 'png' | 'jpeg'; quality?: number };
1215
limit?: number;
@@ -183,6 +186,18 @@ async function snapshotViaApi(
183186
},
184187
};
185188

189+
// Check payload size before sending (server has 10MB limit)
190+
const payloadJson = JSON.stringify(payload);
191+
const payloadSize = new TextEncoder().encode(payloadJson).length;
192+
if (payloadSize > MAX_PAYLOAD_BYTES) {
193+
const sizeMB = (payloadSize / 1024 / 1024).toFixed(2);
194+
const limitMB = (MAX_PAYLOAD_BYTES / 1024 / 1024).toFixed(0);
195+
throw new Error(
196+
`Payload size (${sizeMB}MB) exceeds server limit (${limitMB}MB). ` +
197+
`Try reducing the number of elements on the page or filtering elements.`
198+
);
199+
}
200+
186201
const headers: Record<string, string> = {
187202
'Authorization': `Bearer ${apiKey}`,
188203
'Content-Type': 'application/json',
@@ -192,7 +207,7 @@ async function snapshotViaApi(
192207
const response = await fetch(`${apiUrl}/v1/snapshot`, {
193208
method: 'POST',
194209
headers,
195-
body: JSON.stringify(payload),
210+
body: payloadJson,
196211
});
197212

198213
if (!response.ok) {

0 commit comments

Comments
 (0)