Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2023 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | import { createWriteStream } from 'fs'; |
| 17 | import * as http from 'http'; |
| 18 | import * as https from 'https'; |
| 19 | import { URL, urlToHttpOptions } from 'url'; |
Alex Rudenko | 763f344 | 2023-07-13 07:25:53 | [diff] [blame] | 20 | import { HttpProxyAgent } from 'http-proxy-agent'; |
| 21 | import { HttpsProxyAgent } from 'https-proxy-agent'; |
| 22 | import { getProxyForUrl } from 'proxy-from-env'; |
| 23 | import { SocksProxyAgent } from 'socks-proxy-agent'; |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 24 | export function headHttpRequest(url) { |
| 25 | return new Promise(resolve => { |
| 26 | const request = httpRequest(url, 'HEAD', response => { |
| 27 | resolve(response.statusCode === 200); |
| 28 | }, false); |
| 29 | request.on('error', () => { |
| 30 | resolve(false); |
| 31 | }); |
| 32 | }); |
| 33 | } |
| 34 | export function httpRequest(url, method, response, keepAlive = true) { |
Alex Rudenko | 763f344 | 2023-07-13 07:25:53 | [diff] [blame] | 35 | const proxy = getProxyForUrl(url.toString()); |
| 36 | let agent; |
| 37 | if (proxy) { |
| 38 | const proxyUrl = new URL(proxy); |
| 39 | if (proxyUrl.protocol === 'http:') { |
| 40 | agent = new HttpProxyAgent(proxyUrl); |
| 41 | } |
| 42 | else if (proxyUrl.protocol === 'https:') { |
| 43 | agent = new HttpsProxyAgent(proxyUrl); |
| 44 | } |
| 45 | else if (proxyUrl.protocol.startsWith('socks')) { |
| 46 | agent = new SocksProxyAgent(proxyUrl); |
| 47 | } |
| 48 | } |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 49 | const options = { |
| 50 | protocol: url.protocol, |
| 51 | hostname: url.hostname, |
| 52 | port: url.port, |
| 53 | path: url.pathname + url.search, |
| 54 | method, |
| 55 | headers: keepAlive ? { Connection: 'keep-alive' } : undefined, |
| 56 | auth: urlToHttpOptions(url).auth, |
Alex Rudenko | 763f344 | 2023-07-13 07:25:53 | [diff] [blame] | 57 | agent, |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 58 | }; |
| 59 | const requestCallback = (res) => { |
| 60 | if (res.statusCode && |
| 61 | res.statusCode >= 300 && |
| 62 | res.statusCode < 400 && |
| 63 | res.headers.location) { |
| 64 | httpRequest(new URL(res.headers.location), method, response); |
| 65 | } |
| 66 | else { |
| 67 | response(res); |
| 68 | } |
| 69 | }; |
| 70 | const request = options.protocol === 'https:' |
| 71 | ? https.request(options, requestCallback) |
| 72 | : http.request(options, requestCallback); |
| 73 | request.end(); |
| 74 | return request; |
| 75 | } |
| 76 | /** |
| 77 | * @internal |
| 78 | */ |
| 79 | export function downloadFile(url, destinationPath, progressCallback) { |
| 80 | return new Promise((resolve, reject) => { |
| 81 | let downloadedBytes = 0; |
| 82 | let totalBytes = 0; |
| 83 | function onData(chunk) { |
| 84 | downloadedBytes += chunk.length; |
| 85 | progressCallback(downloadedBytes, totalBytes); |
| 86 | } |
| 87 | const request = httpRequest(url, 'GET', response => { |
| 88 | if (response.statusCode !== 200) { |
| 89 | const error = new Error(`Download failed: server returned code ${response.statusCode}. URL: ${url}`); |
| 90 | // consume response data to free up memory |
| 91 | response.resume(); |
| 92 | reject(error); |
| 93 | return; |
| 94 | } |
| 95 | const file = createWriteStream(destinationPath); |
| 96 | file.on('finish', () => { |
| 97 | return resolve(); |
| 98 | }); |
| 99 | file.on('error', error => { |
| 100 | return reject(error); |
| 101 | }); |
| 102 | response.pipe(file); |
| 103 | totalBytes = parseInt(response.headers['content-length'], 10); |
| 104 | if (progressCallback) { |
| 105 | response.on('data', onData); |
| 106 | } |
| 107 | }); |
| 108 | request.on('error', error => { |
| 109 | return reject(error); |
| 110 | }); |
| 111 | }); |
| 112 | } |
| 113 | export async function getJSON(url) { |
| 114 | const text = await getText(url); |
| 115 | try { |
| 116 | return JSON.parse(text); |
| 117 | } |
| 118 | catch { |
| 119 | throw new Error('Could not parse JSON from ' + url.toString()); |
| 120 | } |
| 121 | } |
| 122 | export function getText(url) { |
| 123 | return new Promise((resolve, reject) => { |
| 124 | const request = httpRequest(url, 'GET', response => { |
| 125 | let data = ''; |
| 126 | if (response.statusCode && response.statusCode >= 400) { |
| 127 | return reject(new Error(`Got status code ${response.statusCode}`)); |
| 128 | } |
| 129 | response.on('data', chunk => { |
| 130 | data += chunk; |
| 131 | }); |
| 132 | response.on('end', () => { |
| 133 | try { |
| 134 | return resolve(String(data)); |
| 135 | } |
| 136 | catch { |
| 137 | return reject(new Error('Chrome version not found')); |
| 138 | } |
| 139 | }); |
| 140 | }, false); |
| 141 | request.on('error', err => { |
| 142 | reject(err); |
| 143 | }); |
| 144 | }); |
| 145 | } |
| 146 | //# sourceMappingURL=httpUtil.js.map |