(req, limit)
| 14 | * @returns {Promise<Buffer>} |
| 15 | */ |
| 16 | export function readRequestBody(req, limit) { |
| 17 | return new Promise((resolve, reject) => { |
| 18 | const chunks = []; |
| 19 | let size = 0; |
| 20 | req.on("data", (chunk) => { |
| 21 | size += chunk.length; |
| 22 | if (size > limit) { |
| 23 | reject(new Error("request body too large")); |
| 24 | req.destroy(); |
| 25 | return; |
| 26 | } |
| 27 | chunks.push(chunk); |
| 28 | }); |
| 29 | req.on("end", () => resolve(Buffer.concat(chunks))); |
| 30 | req.on("error", reject); |
| 31 | req.on("close", () => { |
| 32 | if (!req.complete) reject(new Error("request aborted")); |
| 33 | }); |
| 34 | }); |
| 35 | } |
no test coverage detected