2022-10-01 01:06:36 +02:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import fs from 'fs';
|
|
|
|
import { progress as ProgressBar } from '../../utilsBundle';
|
2023-01-13 13:50:38 -08:00
|
|
|
import { httpRequest } from '../../utils/network';
|
2022-10-01 01:06:36 +02:00
|
|
|
import { ManualPromise } from '../../utils/manualPromise';
|
2023-06-16 20:40:15 +02:00
|
|
|
import { extract } from '../../zipBundle';
|
|
|
|
import { getUserAgent } from '../../utils/userAgent';
|
|
|
|
import { browserDirectoryToMarkerFilePath } from '.';
|
2022-10-01 01:06:36 +02:00
|
|
|
|
|
|
|
type OnProgressCallback = (downloadedBytes: number, totalBytes: number) => void;
|
|
|
|
type DownloadFileLogger = (message: string) => void;
|
|
|
|
type DownloadFileOptions = {
|
2022-10-19 13:06:35 -07:00
|
|
|
progressCallback: OnProgressCallback,
|
|
|
|
log: DownloadFileLogger,
|
|
|
|
userAgent: string,
|
|
|
|
connectionTimeout: number,
|
2022-10-01 01:06:36 +02:00
|
|
|
};
|
|
|
|
|
2022-10-19 13:06:35 -07:00
|
|
|
function downloadFile(url: string, destinationPath: string, options: DownloadFileOptions): Promise<void> {
|
2022-10-01 01:06:36 +02:00
|
|
|
const {
|
|
|
|
progressCallback,
|
|
|
|
log = () => { },
|
|
|
|
} = options;
|
|
|
|
log(`running download:`);
|
|
|
|
log(`-- from url: ${url}`);
|
|
|
|
log(`-- to location: ${destinationPath}`);
|
|
|
|
let downloadedBytes = 0;
|
|
|
|
let totalBytes = 0;
|
|
|
|
|
|
|
|
const promise = new ManualPromise<void>();
|
|
|
|
|
|
|
|
httpRequest({
|
|
|
|
url,
|
2022-10-19 13:06:35 -07:00
|
|
|
headers: {
|
2022-10-01 01:06:36 +02:00
|
|
|
'User-Agent': options.userAgent,
|
2022-10-19 13:06:35 -07:00
|
|
|
},
|
|
|
|
timeout: options.connectionTimeout,
|
2022-10-01 01:06:36 +02:00
|
|
|
}, response => {
|
|
|
|
log(`-- response status code: ${response.statusCode}`);
|
|
|
|
if (response.statusCode !== 200) {
|
|
|
|
let content = '';
|
|
|
|
const handleError = () => {
|
|
|
|
const error = new Error(`Download failed: server returned code ${response.statusCode} body '${content}'. URL: ${url}`);
|
|
|
|
// consume response data to free up memory
|
|
|
|
response.resume();
|
|
|
|
promise.reject(error);
|
|
|
|
};
|
|
|
|
response
|
|
|
|
.on('data', chunk => content += chunk)
|
|
|
|
.on('end', handleError)
|
|
|
|
.on('error', handleError);
|
|
|
|
return;
|
|
|
|
}
|
2023-06-14 15:33:06 +02:00
|
|
|
totalBytes = parseInt(response.headers['content-length'] || '0', 10);
|
|
|
|
log(`-- total bytes: ${totalBytes}`);
|
2022-10-01 01:06:36 +02:00
|
|
|
const file = fs.createWriteStream(destinationPath);
|
2023-06-14 15:33:06 +02:00
|
|
|
file.on('finish', () => {
|
|
|
|
if (downloadedBytes !== totalBytes) {
|
|
|
|
log(`-- download failed, size mismatch: ${downloadedBytes} != ${totalBytes}`);
|
|
|
|
promise.reject(new Error(`Download failed: size mismatch, file size: ${downloadedBytes}, expected size: ${totalBytes} URL: ${url}`));
|
|
|
|
} else {
|
|
|
|
log(`-- download complete, size: ${downloadedBytes}`);
|
|
|
|
promise.resolve();
|
|
|
|
}
|
|
|
|
});
|
2022-10-01 01:06:36 +02:00
|
|
|
file.on('error', error => promise.reject(error));
|
|
|
|
response.pipe(file);
|
2022-10-19 13:06:35 -07:00
|
|
|
response.on('data', onData);
|
2022-10-01 01:06:36 +02:00
|
|
|
}, (error: any) => promise.reject(error));
|
|
|
|
return promise;
|
|
|
|
|
|
|
|
function onData(chunk: string) {
|
|
|
|
downloadedBytes += chunk.length;
|
|
|
|
progressCallback!(downloadedBytes, totalBytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-27 09:19:09 -07:00
|
|
|
function getDownloadProgress(): OnProgressCallback {
|
2022-10-01 01:06:36 +02:00
|
|
|
if (process.stdout.isTTY)
|
2022-10-27 09:19:09 -07:00
|
|
|
return getAnimatedDownloadProgress();
|
|
|
|
return getBasicDownloadProgress();
|
2022-10-01 01:06:36 +02:00
|
|
|
}
|
|
|
|
|
2022-10-27 09:19:09 -07:00
|
|
|
function getAnimatedDownloadProgress(): OnProgressCallback {
|
2022-10-01 01:06:36 +02:00
|
|
|
let progressBar: ProgressBar;
|
|
|
|
let lastDownloadedBytes = 0;
|
|
|
|
|
|
|
|
return (downloadedBytes: number, totalBytes: number) => {
|
|
|
|
if (!progressBar) {
|
|
|
|
progressBar = new ProgressBar(
|
2022-10-27 09:19:09 -07:00
|
|
|
`${toMegabytes(
|
2022-10-01 01:06:36 +02:00
|
|
|
totalBytes
|
2022-10-27 09:19:09 -07:00
|
|
|
)} [:bar] :percent :etas`,
|
2022-10-01 01:06:36 +02:00
|
|
|
{
|
|
|
|
complete: '=',
|
|
|
|
incomplete: ' ',
|
|
|
|
width: 20,
|
|
|
|
total: totalBytes,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const delta = downloadedBytes - lastDownloadedBytes;
|
|
|
|
lastDownloadedBytes = downloadedBytes;
|
|
|
|
progressBar.tick(delta);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-27 09:19:09 -07:00
|
|
|
function getBasicDownloadProgress(): OnProgressCallback {
|
2022-10-01 01:06:36 +02:00
|
|
|
const totalRows = 10;
|
|
|
|
const stepWidth = 8;
|
|
|
|
let lastRow = -1;
|
|
|
|
return (downloadedBytes: number, totalBytes: number) => {
|
|
|
|
const percentage = downloadedBytes / totalBytes;
|
|
|
|
const row = Math.floor(totalRows * percentage);
|
|
|
|
if (row > lastRow) {
|
|
|
|
lastRow = row;
|
|
|
|
const percentageString = String(percentage * 100 | 0).padStart(3);
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log(`|${'■'.repeat(row * stepWidth)}${' '.repeat((totalRows - row) * stepWidth)}| ${percentageString}% of ${toMegabytes(totalBytes)}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function toMegabytes(bytes: number) {
|
|
|
|
const mb = bytes / 1024 / 1024;
|
|
|
|
return `${Math.round(mb * 10) / 10} Mb`;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
2023-06-16 20:40:15 +02:00
|
|
|
const log = (message: string) => process.send?.({ method: 'log', params: { message } });
|
|
|
|
const [title, browserDirectory, url, zipPath, executablePath, downloadConnectionTimeout] = process.argv.slice(2);
|
|
|
|
await downloadFile(url, zipPath, {
|
2022-10-27 09:19:09 -07:00
|
|
|
progressCallback: getDownloadProgress(),
|
2023-06-16 20:40:15 +02:00
|
|
|
userAgent: getUserAgent(),
|
|
|
|
log,
|
2022-10-19 13:06:35 -07:00
|
|
|
connectionTimeout: +downloadConnectionTimeout,
|
2022-10-01 01:06:36 +02:00
|
|
|
});
|
2023-06-16 20:40:15 +02:00
|
|
|
log(`SUCCESS downloading ${title}`);
|
|
|
|
log(`extracting archive`);
|
|
|
|
log(`-- zip: ${zipPath}`);
|
|
|
|
log(`-- location: ${browserDirectory}`);
|
|
|
|
await extract(zipPath, { dir: browserDirectory });
|
|
|
|
if (executablePath) {
|
|
|
|
log(`fixing permissions at ${executablePath}`);
|
|
|
|
await fs.promises.chmod(executablePath, 0o755);
|
|
|
|
}
|
|
|
|
await fs.promises.writeFile(browserDirectoryToMarkerFilePath(browserDirectory), '');
|
2022-10-01 01:06:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
main().catch(error => {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(error);
|
2023-07-24 08:29:29 -07:00
|
|
|
// eslint-disable-next-line no-restricted-properties
|
2022-10-01 01:06:36 +02:00
|
|
|
process.exit(1);
|
|
|
|
});
|