( req: http.IncomingMessage, maxBytes: number = DEFAULT_MAX_BODY_BYTES, )
| 1201 | const DEFAULT_MAX_BODY_BYTES = 10 * 1024 * 1024; // 10 MB |
| 1202 | |
| 1203 | export function readBody( |
| 1204 | req: http.IncomingMessage, |
| 1205 | maxBytes: number = DEFAULT_MAX_BODY_BYTES, |
| 1206 | ): Promise<string> { |
| 1207 | return new Promise((resolve, reject) => { |
| 1208 | const chunks: Buffer[] = []; |
| 1209 | let totalBytes = 0; |
| 1210 | let settled = false; |
| 1211 | req.on("data", (chunk: Buffer) => { |
| 1212 | if (settled) return; |
| 1213 | totalBytes += chunk.length; |
| 1214 | if (totalBytes > maxBytes) { |
| 1215 | settled = true; |
| 1216 | req.destroy(); |
| 1217 | reject(new Error(`Request body exceeded size limit of ${maxBytes} bytes`)); |
| 1218 | return; |
| 1219 | } |
| 1220 | chunks.push(chunk); |
| 1221 | }); |
| 1222 | req.on("end", () => { |
| 1223 | if (!settled) { |
| 1224 | settled = true; |
| 1225 | resolve(Buffer.concat(chunks).toString()); |
| 1226 | } |
| 1227 | }); |
| 1228 | req.on("error", (err) => { |
| 1229 | if (!settled) { |
| 1230 | settled = true; |
| 1231 | reject(err); |
| 1232 | } |
| 1233 | }); |
| 1234 | }); |
| 1235 | } |
| 1236 | |
| 1237 | // ─── Pattern matching ───────────────────────────────────────────────────── |
| 1238 |
no test coverage detected
searching dependent graphs…