(req: IncomingMessage, limitBytes = 2 * 1024 * 1024)
| 29 | } |
| 30 | |
| 31 | function readBody(req: IncomingMessage, limitBytes = 2 * 1024 * 1024): Promise<string> { |
| 32 | return new Promise((resolve, reject) => { |
| 33 | let total = 0 |
| 34 | const chunks: Buffer[] = [] |
| 35 | req.on('data', (chunk: Buffer) => { |
| 36 | total += chunk.length |
| 37 | if (total > limitBytes) { |
| 38 | reject(new Error('Request body too large')) |
| 39 | req.destroy() |
| 40 | return |
| 41 | } |
| 42 | chunks.push(chunk) |
| 43 | }) |
| 44 | req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8'))) |
| 45 | req.on('error', reject) |
| 46 | }) |
| 47 | } |
| 48 | |
| 49 | function writeJson(res: ServerResponse, status: number, payload: unknown) { |
| 50 | res.statusCode = status |
no test coverage detected