blob: d67dc09cfe35ac58d408f85b8d994a569fe46634 [file] [log] [blame]
Randolf Jungbcb3bc82023-06-26 16:30:141/**
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 */
16import { createWriteStream } from 'fs';
17import * as http from 'http';
18import * as https from 'https';
19import { URL, urlToHttpOptions } from 'url';
Alex Rudenko763f3442023-07-13 07:25:5320import { HttpProxyAgent } from 'http-proxy-agent';
21import { HttpsProxyAgent } from 'https-proxy-agent';
22import { getProxyForUrl } from 'proxy-from-env';
23import { SocksProxyAgent } from 'socks-proxy-agent';
Randolf Jungbcb3bc82023-06-26 16:30:1424export 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}
34export function httpRequest(url, method, response, keepAlive = true) {
Alex Rudenko763f3442023-07-13 07:25:5335 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 Jungbcb3bc82023-06-26 16:30:1449 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 Rudenko763f3442023-07-13 07:25:5357 agent,
Randolf Jungbcb3bc82023-06-26 16:30:1458 };
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 */
79export 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}
113export 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}
122export 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