(request: ClientRequest)
| 15 | } |
| 16 | |
| 17 | export async function waitForClientRequest(request: ClientRequest): Promise<{ |
| 18 | response: IncomingMessage |
| 19 | responseText: string |
| 20 | }> { |
| 21 | return new Promise((resolve, reject) => { |
| 22 | request.once('error', (error) => { |
| 23 | /** |
| 24 | * @note Since Node.js v20, Node.js may throw an AggregateError |
| 25 | * that doesn't have the `message` property and thus won't be handled |
| 26 | * here correctly. Instead, use the error's `code` as the rejection reason. |
| 27 | * The code stays consistent across Node.js versions. |
| 28 | */ |
| 29 | reject('code' in error ? error.code : error) |
| 30 | }) |
| 31 | request.once('abort', () => reject(new Error('Request was aborted'))) |
| 32 | |
| 33 | request.on('response', (response) => { |
| 34 | response.once('error', reject) |
| 35 | |
| 36 | const responseChunks: Array<Buffer> = [] |
| 37 | |
| 38 | response.on('data', (chunk) => { |
| 39 | responseChunks.push(Buffer.from(chunk)) |
| 40 | }) |
| 41 | response.once('end', () => { |
| 42 | resolve({ |
| 43 | response, |
| 44 | responseText: Buffer.concat(responseChunks).toString('utf8'), |
| 45 | }) |
| 46 | }) |
| 47 | }) |
| 48 | }) |
| 49 | } |
no test coverage detected
searching dependent graphs…