promisify
This commit is contained in:
parent
b177157220
commit
8055eeb14d
|
|
@ -29,6 +29,8 @@ import { browserDirectoryToMarkerFilePath } from '.';
|
||||||
import { getUserAgent } from '../../utils/userAgent';
|
import { getUserAgent } from '../../utils/userAgent';
|
||||||
import type { DownloadParams } from './oopDownloadBrowserMain';
|
import type { DownloadParams } from './oopDownloadBrowserMain';
|
||||||
import { httpRequest } from '../../utils';
|
import { httpRequest } from '../../utils';
|
||||||
|
import type { IncomingMessage } from 'http';
|
||||||
|
import { pipeline, finished } from 'stream/promises';
|
||||||
|
|
||||||
export async function downloadBrowserWithProgressBar(title: string, browserDirectory: string, executablePath: string | undefined, downloadURLs: string[], downloadFileName: string, downloadConnectionTimeout: number): Promise<boolean> {
|
export async function downloadBrowserWithProgressBar(title: string, browserDirectory: string, executablePath: string | undefined, downloadURLs: string[], downloadFileName: string, downloadConnectionTimeout: number): Promise<boolean> {
|
||||||
if (await existsAsync(browserDirectoryToMarkerFilePath(browserDirectory))) {
|
if (await existsAsync(browserDirectoryToMarkerFilePath(browserDirectory))) {
|
||||||
|
|
@ -118,73 +120,64 @@ function downloadBrowserWithProgressBarOutOfProcess(title: string, browserDirect
|
||||||
|
|
||||||
|
|
||||||
async function downloadBrotli(title: string, browserDirectory: string, url: string, zipPath: string, executablePath: string | undefined, connectionTimeout: number): Promise<{ error: Error | null }> {
|
async function downloadBrotli(title: string, browserDirectory: string, url: string, zipPath: string, executablePath: string | undefined, connectionTimeout: number): Promise<{ error: Error | null }> {
|
||||||
|
try {
|
||||||
debugLogger.log('install', `running download:`);
|
debugLogger.log('install', `running download:`);
|
||||||
debugLogger.log('install', `-- from url: ${url}`);
|
debugLogger.log('install', `-- from url: ${url}`);
|
||||||
debugLogger.log('install', `-- to location: ${zipPath}`);
|
debugLogger.log('install', `-- to location: ${zipPath}`);
|
||||||
|
|
||||||
let downloadedBytes = 0;
|
const response = await new Promise<IncomingMessage>((resolve, reject) => {
|
||||||
let totalBytes = 0;
|
|
||||||
|
|
||||||
const promise = new ManualPromise<{ error: Error | null }>();
|
|
||||||
|
|
||||||
const progress = getDownloadProgress(title);
|
|
||||||
|
|
||||||
httpRequest({
|
httpRequest({
|
||||||
url,
|
url,
|
||||||
headers: { 'User-Agent': getUserAgent() },
|
headers: { 'User-Agent': getUserAgent() },
|
||||||
timeout: connectionTimeout,
|
timeout: connectionTimeout,
|
||||||
}, response => {
|
}, resolve, reject);
|
||||||
|
});
|
||||||
|
|
||||||
debugLogger.log('install', `-- response status code: ${response.statusCode}`);
|
debugLogger.log('install', `-- response status code: ${response.statusCode}`);
|
||||||
if (response.statusCode !== 200) {
|
if (response.statusCode !== 200) {
|
||||||
let content = '';
|
let content = '';
|
||||||
const handleError = () => {
|
response.on('data', chunk => content += chunk);
|
||||||
const error = new Error(`Download failed: server returned code ${response.statusCode} body '${content}'. URL: ${url}`);
|
try { await finished(response); } catch {}
|
||||||
// consume response data to free up memory
|
// consume response data to free up memory
|
||||||
response.resume();
|
response.resume();
|
||||||
promise.reject(error);
|
throw new Error(`Download failed: server returned code ${response.statusCode} body '${content}'. URL: ${url}`);
|
||||||
};
|
|
||||||
response
|
|
||||||
.on('data', chunk => content += chunk)
|
|
||||||
.on('end', handleError)
|
|
||||||
.on('error', handleError);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
totalBytes = parseInt(response.headers['content-length'] || '0', 10);
|
|
||||||
debugLogger.log('install', `-- total bytes: ${totalBytes}`);
|
|
||||||
|
|
||||||
const decompress = zlib.createBrotliDecompress();
|
const progress = getDownloadProgress(title);
|
||||||
const extract = tarFs.extract(browserDirectory);
|
const totalBytes = parseInt(response.headers['content-length'] || '0', 10);
|
||||||
|
let downloadedBytes = 0;
|
||||||
response
|
response.on('data', chunk => {
|
||||||
.pipe(decompress)
|
|
||||||
.pipe(extract)
|
|
||||||
.on('finish', () => {
|
|
||||||
if (downloadedBytes !== totalBytes) {
|
|
||||||
debugLogger.log('install', `-- download failed, size mismatch: ${downloadedBytes} != ${totalBytes}`);
|
|
||||||
promise.resolve({ error: new Error(`Download failed: size mismatch, file size: ${downloadedBytes}, expected size: ${totalBytes} URL: ${url}`) });
|
|
||||||
} else {
|
|
||||||
debugLogger.log('install', `-- download complete, size: ${downloadedBytes}`);
|
|
||||||
promise.resolve({ error: null });
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.on('error', error => promise.resolve({ error }));
|
|
||||||
|
|
||||||
response.on('data', onData);
|
|
||||||
response.on('error', (error: any) => {
|
|
||||||
if (error?.code === 'ECONNRESET') {
|
|
||||||
debugLogger.log('install', `-- download failed, server closed connection`);
|
|
||||||
promise.resolve({ error: new Error(`Download failed: server closed connection. URL: ${url}`) });
|
|
||||||
} else {
|
|
||||||
debugLogger.log('install', `-- download failed, unexpected error`);
|
|
||||||
promise.resolve({ error: new Error(`Download failed: ${error?.message ?? error}. URL: ${url}`) });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, (error: any) => promise.resolve({ error }));
|
|
||||||
return promise;
|
|
||||||
|
|
||||||
function onData(chunk: string) {
|
|
||||||
downloadedBytes += chunk.length;
|
downloadedBytes += chunk.length;
|
||||||
progress(downloadedBytes, totalBytes);
|
progress(downloadedBytes, totalBytes);
|
||||||
|
});
|
||||||
|
debugLogger.log('install', `-- total bytes: ${totalBytes}`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await pipeline(
|
||||||
|
response,
|
||||||
|
zlib.createBrotliDecompress(),
|
||||||
|
tarFs.extract(browserDirectory)
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
if (error?.code === 'ECONNRESET') {
|
||||||
|
debugLogger.log('install', `-- download failed, server closed connection`);
|
||||||
|
throw new Error(`Download failed: server closed connection. URL: ${url}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
debugLogger.log('install', `-- download failed, unexpected error`);
|
||||||
|
throw new Error(`Download failed: ${error?.message ?? error}. URL: ${url}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (downloadedBytes !== totalBytes) {
|
||||||
|
debugLogger.log('install', `-- download failed, size mismatch: ${downloadedBytes} != ${totalBytes}`);
|
||||||
|
throw new Error(`Download failed: size mismatch, file size: ${downloadedBytes}, expected size: ${totalBytes} URL: ${url}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
debugLogger.log('install', `-- download complete, size: ${downloadedBytes}`);
|
||||||
|
return { error: null };
|
||||||
|
} catch (error) {
|
||||||
|
return { error };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue