(options: { type: string; url: string; headers: Record<string, string>; data?: Buffer }, output: Log)
| 82 | // Send HTTP Request. |
| 83 | // Does not throw on status code, but rather always returns 'statusCode', 'resHeaders', and 'resBody'. |
| 84 | export async function requestResolveHeaders(options: { type: string; url: string; headers: Record<string, string>; data?: Buffer }, output: Log) { |
| 85 | const secureContext = await secureContextWithExtraCerts(output); |
| 86 | return new Promise<{ statusCode: number; resHeaders: Record<string, string>; resBody: Buffer }>((resolve, reject) => { |
| 87 | const parsed = new url.URL(options.url); |
| 88 | const reqOptions: RequestOptions & tls.CommonConnectionOptions & FollowOptions<any> = { |
| 89 | hostname: parsed.hostname, |
| 90 | maxBodyLength: Infinity, |
| 91 | port: parsed.port, |
| 92 | path: parsed.pathname + parsed.search, |
| 93 | method: options.type, |
| 94 | headers: options.headers, |
| 95 | agent: new ProxyAgent(), |
| 96 | secureContext, |
| 97 | }; |
| 98 | |
| 99 | const plainHTTP = parsed.protocol === 'http:' || parsed.hostname === 'localhost'; |
| 100 | if (plainHTTP) { |
| 101 | output.write('Sending as plain HTTP request', LogLevel.Warning); |
| 102 | } |
| 103 | |
| 104 | const req = (plainHTTP ? http : https).request(reqOptions, res => { |
| 105 | res.on('error', reject); |
| 106 | |
| 107 | // Resolve response body |
| 108 | const chunks: Buffer[] = []; |
| 109 | res.on('data', chunk => chunks.push(chunk as Buffer)); |
| 110 | res.on('end', () => { |
| 111 | resolve({ |
| 112 | statusCode: res.statusCode!, |
| 113 | resHeaders: res.headers! as Record<string, string>, |
| 114 | resBody: Buffer.concat(chunks) |
| 115 | }); |
| 116 | }); |
| 117 | }); |
| 118 | |
| 119 | if (options.data) { |
| 120 | req.write(options.data); |
| 121 | } |
| 122 | |
| 123 | req.on('error', reject); |
| 124 | req.end(); |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | let _secureContextWithExtraCerts: Promise<tls.SecureContext | undefined> | undefined; |
| 129 |
no test coverage detected