(options: { url: string; headers: Record<string, string> }, output: Log)
| 51 | |
| 52 | // HTTP HEAD request that returns status code. |
| 53 | export async function headRequest(options: { url: string; headers: Record<string, string> }, output: Log) { |
| 54 | const secureContext = await secureContextWithExtraCerts(output); |
| 55 | return new Promise<number>((resolve, reject) => { |
| 56 | const parsed = new url.URL(options.url); |
| 57 | const reqOptions: RequestOptions & tls.CommonConnectionOptions = { |
| 58 | hostname: parsed.hostname, |
| 59 | port: parsed.port, |
| 60 | path: parsed.pathname + parsed.search, |
| 61 | method: 'HEAD', |
| 62 | headers: options.headers, |
| 63 | agent: new ProxyAgent(), |
| 64 | secureContext, |
| 65 | }; |
| 66 | |
| 67 | const plainHTTP = parsed.protocol === 'http:' || parsed.hostname === 'localhost'; |
| 68 | if (plainHTTP) { |
| 69 | output.write('Sending as plain HTTP request', LogLevel.Warning); |
| 70 | } |
| 71 | |
| 72 | const req = (plainHTTP ? http : https).request(reqOptions, res => { |
| 73 | res.on('error', reject); |
| 74 | output.write(`HEAD ${options.url} -> ${res.statusCode}`, LogLevel.Trace); |
| 75 | resolve(res.statusCode!); |
| 76 | }); |
| 77 | req.on('error', reject); |
| 78 | req.end(); |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | // Send HTTP Request. |
| 83 | // Does not throw on status code, but rather always returns 'statusCode', 'resHeaders', and 'resBody'. |
nothing calls this directly
no test coverage detected