* Reads the JSON body while enforcing a byte cap. The body is read through a * size-limited stream so chunked/streamed bodies are bounded even when the * `content-length` header is absent or lies about the true size. When no * readable stream is available (e.g. a mocked request) the content-lengt
(request: Request, maxBytes: number)
| 104 | * "UTF-8 decode" behavior of `request.json()`. |
| 105 | */ |
| 106 | async function readJsonBodyWithLimit(request: Request, maxBytes: number): Promise<unknown> { |
| 107 | assertContentLengthWithinLimit(request.headers, maxBytes, REQUEST_BODY_LABEL) |
| 108 | |
| 109 | const stream = request.body |
| 110 | if (!stream) { |
| 111 | return request.json() |
| 112 | } |
| 113 | |
| 114 | const buffer = await readStreamToBufferWithLimit(stream, { |
| 115 | maxBytes, |
| 116 | label: REQUEST_BODY_LABEL, |
| 117 | }) |
| 118 | return JSON.parse(new TextDecoder().decode(buffer)) |
| 119 | } |
| 120 | |
| 121 | export async function parseJsonBody( |
| 122 | request: Request, |
no test coverage detected