78521fe11 days ago21 lines · 711 B| 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 |
| 6 | const chunks: |
| 7 | let current: |
| 8 | let size = |
| 9 | for (const item |
| 10 | const itemSize = |
| 11 | if (current.length |
| 12 | chunks.push(current |
| 13 | current = []; |
| 14 | size = 0; |
| 15 | } |
| 16 | current.push(item |
| 17 | size += itemSize; |
| 18 | } |
| 19 | if (current.length) |
| 20 | return chunks; |
| 21 | } |