reuse existing code better
This commit is contained in:
parent
e2b1500ba6
commit
25494cb53e
|
|
@ -20,9 +20,9 @@ import { httpRequest } from '../../utils/network';
|
||||||
import { ManualPromise } from '../../utils/manualPromise';
|
import { ManualPromise } from '../../utils/manualPromise';
|
||||||
import { extract } from '../../zipBundle';
|
import { extract } from '../../zipBundle';
|
||||||
import tar from '../../utils/tar';
|
import tar from '../../utils/tar';
|
||||||
import type http from 'http';
|
|
||||||
import { pipeline } from 'stream/promises';
|
import { pipeline } from 'stream/promises';
|
||||||
import { createBrotliDecompress } from 'zlib';
|
import { createBrotliDecompress } from 'zlib';
|
||||||
|
import type { Writable } from 'stream';
|
||||||
|
|
||||||
export type DownloadParams = {
|
export type DownloadParams = {
|
||||||
title: string;
|
title: string;
|
||||||
|
|
@ -46,7 +46,7 @@ function browserDirectoryToMarkerFilePath(browserDirectory: string): string {
|
||||||
return path.join(browserDirectory, 'INSTALLATION_COMPLETE');
|
return path.join(browserDirectory, 'INSTALLATION_COMPLETE');
|
||||||
}
|
}
|
||||||
|
|
||||||
function downloadFile(options: DownloadParams): Promise<void> {
|
function downloadFile(options: DownloadParams, file: Writable): Promise<void> {
|
||||||
let downloadedBytes = 0;
|
let downloadedBytes = 0;
|
||||||
let totalBytes = 0;
|
let totalBytes = 0;
|
||||||
|
|
||||||
|
|
@ -76,7 +76,6 @@ function downloadFile(options: DownloadParams): Promise<void> {
|
||||||
}
|
}
|
||||||
totalBytes = parseInt(response.headers['content-length'] || '0', 10);
|
totalBytes = parseInt(response.headers['content-length'] || '0', 10);
|
||||||
log(`-- total bytes: ${totalBytes}`);
|
log(`-- total bytes: ${totalBytes}`);
|
||||||
const file = fs.createWriteStream(options.zipPath);
|
|
||||||
file.on('finish', () => {
|
file.on('finish', () => {
|
||||||
if (downloadedBytes !== totalBytes) {
|
if (downloadedBytes !== totalBytes) {
|
||||||
log(`-- download failed, size mismatch: ${downloadedBytes} != ${totalBytes}`);
|
log(`-- download failed, size mismatch: ${downloadedBytes} != ${totalBytes}`);
|
||||||
|
|
@ -90,7 +89,7 @@ function downloadFile(options: DownloadParams): Promise<void> {
|
||||||
response.pipe(file);
|
response.pipe(file);
|
||||||
response.on('data', onData);
|
response.on('data', onData);
|
||||||
response.on('error', (error: any) => {
|
response.on('error', (error: any) => {
|
||||||
file.close();
|
file.destroy();
|
||||||
if (error?.code === 'ECONNRESET') {
|
if (error?.code === 'ECONNRESET') {
|
||||||
log(`-- download failed, server closed connection`);
|
log(`-- download failed, server closed connection`);
|
||||||
promise.reject(new Error(`Download failed: server closed connection. URL: ${options.url}`));
|
promise.reject(new Error(`Download failed: server closed connection. URL: ${options.url}`));
|
||||||
|
|
@ -108,68 +107,21 @@ function downloadFile(options: DownloadParams): Promise<void> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function throwUnexpectedResponseError(response: http.IncomingMessage) {
|
|
||||||
let body = '';
|
|
||||||
try {
|
|
||||||
await new Promise<void>((resolve, reject) => {
|
|
||||||
response
|
|
||||||
.on('data', chunk => body += chunk)
|
|
||||||
.on('end', resolve)
|
|
||||||
.on('error', reject);
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
body += error;
|
|
||||||
}
|
|
||||||
|
|
||||||
response.resume(); // consume response data to free up memory
|
|
||||||
|
|
||||||
throw new Error(`server returned code ${response.statusCode} body '${body}'`);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function downloadAndExtractBrotli(options: DownloadParams) {
|
|
||||||
const response = await new Promise<http.IncomingMessage>((resolve, reject) => httpRequest({
|
|
||||||
url: options.url,
|
|
||||||
headers: {
|
|
||||||
'User-Agent': options.userAgent,
|
|
||||||
},
|
|
||||||
timeout: options.connectionTimeout,
|
|
||||||
}, resolve, reject));
|
|
||||||
|
|
||||||
log(`-- response status code: ${response.statusCode}`);
|
|
||||||
if (response.statusCode !== 200)
|
|
||||||
await throwUnexpectedResponseError(response);
|
|
||||||
|
|
||||||
const totalBytes = parseInt(response.headers['content-length'] || '0', 10);
|
|
||||||
log(`-- total bytes: ${totalBytes}`);
|
|
||||||
|
|
||||||
let downloadedBytes = 0;
|
|
||||||
response.on('data', chunk => {
|
|
||||||
downloadedBytes += chunk.length;
|
|
||||||
progress(downloadedBytes, totalBytes);
|
|
||||||
});
|
|
||||||
|
|
||||||
await pipeline(
|
|
||||||
response,
|
|
||||||
createBrotliDecompress(),
|
|
||||||
tar.extract(options.browserDirectory)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (downloadedBytes !== totalBytes)
|
|
||||||
throw new Error(`size mismatch, file size: ${downloadedBytes}, expected size: ${totalBytes}`);
|
|
||||||
|
|
||||||
log(`-- download complete, size: ${downloadedBytes}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function main(options: DownloadParams) {
|
async function main(options: DownloadParams) {
|
||||||
if (options.url.endsWith('.tar.br')) {
|
if (options.url.endsWith('.tar.br')) {
|
||||||
try {
|
const decompress = createBrotliDecompress();
|
||||||
await downloadAndExtractBrotli(options);
|
const extraction = pipeline(
|
||||||
} catch (error) {
|
decompress,
|
||||||
throw new Error(`Download failed. URL: ${options.url}`, { cause: error });
|
tar.extract(options.browserDirectory),
|
||||||
}
|
);
|
||||||
|
await Promise.all([
|
||||||
|
extraction,
|
||||||
|
downloadFile(options, decompress)
|
||||||
|
]);
|
||||||
log(`SUCCESS downloading and extracting ${options.title}`);
|
log(`SUCCESS downloading and extracting ${options.title}`);
|
||||||
} else {
|
} else {
|
||||||
await downloadFile(options);
|
const file = fs.createWriteStream(options.zipPath);
|
||||||
|
await downloadFile(options, file);
|
||||||
log(`SUCCESS downloading ${options.title}`);
|
log(`SUCCESS downloading ${options.title}`);
|
||||||
log(`extracting archive`);
|
log(`extracting archive`);
|
||||||
await extract(options.zipPath, { dir: options.browserDirectory });
|
await extract(options.zipPath, { dir: options.browserDirectory });
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue