Skip to content
Open
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
12 changes: 11 additions & 1 deletion src/internal/utils/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ export const toBase64 = (data: string | Uint8Array | null | undefined): string =
}

if (typeof btoa !== 'undefined') {
return btoa(String.fromCharCode.apply(null, data as any));
// Process in chunks to avoid stack overflow with large inputs.
// String.fromCharCode.apply() passes all bytes as arguments,
// which exceeds the JS engine's max argument limit (~65536) for
// inputs larger than ~100KB (e.g. image uploads).
const CHUNK_SIZE = 0x8000; // 32KB chunks
const parts: string[] = [];
for (let i = 0; i < data.length; i += CHUNK_SIZE) {
const chunk = data.subarray(i, i + CHUNK_SIZE);
parts.push(String.fromCharCode.apply(null, chunk as any));
}
return btoa(parts.join(''));
}

throw new AnthropicError('Cannot generate base64 string; Expected `Buffer` or `btoa` to be defined');
Expand Down