(url, options = {})
| 88 | } |
| 89 | |
| 90 | async function buildRequestOptions(url, options = {}) { |
| 91 | const parsedUrl = new URL(url) |
| 92 | const reqOptions = { |
| 93 | hostname: parsedUrl.hostname, |
| 94 | port: parsedUrl.port || 443, |
| 95 | path: parsedUrl.pathname + parsedUrl.search, |
| 96 | headers: { |
| 97 | 'User-Agent': userAgent, |
| 98 | ...options.headers, |
| 99 | }, |
| 100 | } |
| 101 | |
| 102 | const proxyUrl = getProxyUrl() |
| 103 | if (!proxyUrl || shouldBypassProxy(parsedUrl.hostname)) { |
| 104 | return reqOptions |
| 105 | } |
| 106 | |
| 107 | const tunnelSocket = await connectThroughProxy( |
| 108 | proxyUrl, |
| 109 | parsedUrl.hostname, |
| 110 | parsedUrl.port || 443, |
| 111 | ) |
| 112 | |
| 113 | class TunnelAgent extends httpsModule.Agent { |
| 114 | createConnection(_options, callback) { |
| 115 | const secureSocket = tlsModule.connect({ |
| 116 | socket: tunnelSocket, |
| 117 | servername: parsedUrl.hostname, |
| 118 | }) |
| 119 | |
| 120 | if (typeof callback === 'function') { |
| 121 | if (typeof secureSocket.once === 'function') { |
| 122 | let settled = false |
| 123 | const finish = (error) => { |
| 124 | if (settled) return |
| 125 | settled = true |
| 126 | callback(error || null, error ? undefined : secureSocket) |
| 127 | } |
| 128 | |
| 129 | secureSocket.once('secureConnect', () => finish(null)) |
| 130 | secureSocket.once('error', (error) => finish(error)) |
| 131 | } else { |
| 132 | callback(null, secureSocket) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return secureSocket |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | reqOptions.agent = new TunnelAgent({ keepAlive: false }) |
| 141 | return reqOptions |
| 142 | } |
| 143 | |
| 144 | async function httpGet(url, options = {}) { |
| 145 | const reqOptions = await buildRequestOptions(url, options) |
no test coverage detected