( response: Response | SecureFetchResponse, maxBytes: number )
| 124 | * Returns null when the cap is exceeded. |
| 125 | */ |
| 126 | export async function readBodyWithLimit( |
| 127 | response: Response | SecureFetchResponse, |
| 128 | maxBytes: number |
| 129 | ): Promise<Buffer | null> { |
| 130 | if (!response.body) { |
| 131 | const buffer = Buffer.from(await response.arrayBuffer()) |
| 132 | return buffer.byteLength > maxBytes ? null : buffer |
| 133 | } |
| 134 | |
| 135 | const reader = response.body.getReader() |
| 136 | const chunks: Uint8Array[] = [] |
| 137 | let total = 0 |
| 138 | while (true) { |
| 139 | const { done, value } = await reader.read() |
| 140 | if (done) break |
| 141 | total += value.byteLength |
| 142 | if (total > maxBytes) { |
| 143 | await reader.cancel().catch(() => {}) |
| 144 | return null |
| 145 | } |
| 146 | chunks.push(value) |
| 147 | } |
| 148 | return Buffer.concat(chunks) |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Marks a listed document stub as intentionally skipped — for example because it |
no test coverage detected