| 1 | // Under the API's 2 MB body limit with room for the envelope; a single item larger than this travels alone. |
| 2 | const MAX_CHUNK_BYTES = 1_000_000; |
| 3 | |
| 4 | /** Splits `items` so each chunk's JSON stays under `maxBytes`. Order is kept. */ |
| 5 | export function chunkBySize<T>(items: T[], maxBytes = MAX_CHUNK_BYTES): T[][] { |
| 6 | const chunks: T[][] = []; |
| 7 | let current: T[] = []; |
| 8 | let size = 0; |
| 9 | for (const item of items) { |
| 10 | const itemSize = Buffer.byteLength(JSON.stringify(item)) + 1; |
| 11 | if (current.length && size + itemSize > maxBytes) { |
| 12 | chunks.push(current); |
| 13 | current = []; |
| 14 | size = 0; |
| 15 | } |
| 16 | current.push(item); |
| 17 | size += itemSize; |
| 18 | } |
| 19 | if (current.length) chunks.push(current); |
| 20 | return chunks; |
| 21 | } |