| 536 | * @param {(Error | null)=} err err |
| 537 | */ |
| 538 | const batchWrite = (err) => { |
| 539 | // will be handled in "on" error handler |
| 540 | if (err) return; |
| 541 | |
| 542 | if (i === len) { |
| 543 | stream.end(); |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | // queue up a batch of chunks up to the write limit |
| 548 | // end is exclusive |
| 549 | let end = i; |
| 550 | let sum = chunks[end++].length; |
| 551 | while (end < len) { |
| 552 | sum += chunks[end].length; |
| 553 | if (sum > WRITE_LIMIT_TOTAL) break; |
| 554 | end++; |
| 555 | } |
| 556 | while (i < end - 1) { |
| 557 | stream.write(chunks[i++]); |
| 558 | } |
| 559 | stream.write(chunks[i++], batchWrite); |
| 560 | }; |
| 561 | batchWrite(); |
| 562 | } |
| 563 | ); |