| 574 | const nodeRes = res |
| 575 | const body = new ReadableStream<Uint8Array>({ |
| 576 | start(controller) { |
| 577 | nodeRes.on('data', (chunk: Buffer) => { |
| 578 | totalBytes += chunk.length |
| 579 | if ( |
| 580 | typeof maxResponseBytes === 'number' && |
| 581 | maxResponseBytes > 0 && |
| 582 | totalBytes > maxResponseBytes |
| 583 | ) { |
| 584 | cleanupAbort() |
| 585 | controller.error( |
| 586 | new PayloadSizeLimitError({ |
| 587 | label: 'response body', |
| 588 | maxBytes: maxResponseBytes, |
| 589 | observedBytes: totalBytes, |
| 590 | }) |
| 591 | ) |
| 592 | nodeRes.destroy() |
| 593 | return |
| 594 | } |
| 595 | controller.enqueue(new Uint8Array(chunk)) |
| 596 | }) |
| 597 | nodeRes.on('end', () => { |
| 598 | cleanupAbort() |
| 599 | controller.close() |
| 600 | }) |
| 601 | nodeRes.on('error', (err) => { |
| 602 | cleanupAbort() |
| 603 | controller.error(err) |
| 604 | }) |
| 605 | }, |
| 606 | cancel() { |
| 607 | cleanupAbort() |
| 608 | nodeRes.destroy() |