decompress and extract

This commit is contained in:
Simon Knott 2024-11-29 12:11:25 +01:00
parent e8ce8ca2cd
commit b177157220
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
2 changed files with 21 additions and 13 deletions

View file

@ -19,10 +19,12 @@ import fs from 'fs';
import os from 'os'; import os from 'os';
import path from 'path'; import path from 'path';
import childProcess from 'child_process'; import childProcess from 'child_process';
import zlib from 'zlib';
import { existsAsync } from '../../utils/fileUtils'; import { existsAsync } from '../../utils/fileUtils';
import { debugLogger } from '../../utils/debugLogger'; import { debugLogger } from '../../utils/debugLogger';
import { ManualPromise } from '../../utils/manualPromise'; import { ManualPromise } from '../../utils/manualPromise';
import { colors, multiProgress as MultiProgressBar } from '../../utilsBundle'; import { colors, multiProgress as MultiProgressBar } from '../../utilsBundle';
import { tarFs } from '../../zipBundle';
import { browserDirectoryToMarkerFilePath } from '.'; import { browserDirectoryToMarkerFilePath } from '.';
import { getUserAgent } from '../../utils/userAgent'; import { getUserAgent } from '../../utils/userAgent';
import type { DownloadParams } from './oopDownloadBrowserMain'; import type { DownloadParams } from './oopDownloadBrowserMain';
@ -149,21 +151,26 @@ async function downloadBrotli(title: string, browserDirectory: string, url: stri
} }
totalBytes = parseInt(response.headers['content-length'] || '0', 10); totalBytes = parseInt(response.headers['content-length'] || '0', 10);
debugLogger.log('install', `-- total bytes: ${totalBytes}`); debugLogger.log('install', `-- total bytes: ${totalBytes}`);
const file = fs.createWriteStream(zipPath);
file.on('finish', () => { const decompress = zlib.createBrotliDecompress();
if (downloadedBytes !== totalBytes) { const extract = tarFs.extract(browserDirectory);
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}`) }); response
} else { .pipe(decompress)
debugLogger.log('install', `-- download complete, size: ${downloadedBytes}`); .pipe(extract)
promise.resolve({ error: null }); .on('finish', () => {
} if (downloadedBytes !== totalBytes) {
}); debugLogger.log('install', `-- download failed, size mismatch: ${downloadedBytes} != ${totalBytes}`);
file.on('error', error => promise.resolve({ error })); promise.resolve({ error: new Error(`Download failed: size mismatch, file size: ${downloadedBytes}, expected size: ${totalBytes} URL: ${url}`) });
response.pipe(file); } else {
debugLogger.log('install', `-- download complete, size: ${downloadedBytes}`);
promise.resolve({ error: null });
}
})
.on('error', error => promise.resolve({ error }));
response.on('data', onData); response.on('data', onData);
response.on('error', (error: any) => { response.on('error', (error: any) => {
file.close();
if (error?.code === 'ECONNRESET') { if (error?.code === 'ECONNRESET') {
debugLogger.log('install', `-- download failed, server closed connection`); debugLogger.log('install', `-- download failed, server closed connection`);
promise.resolve({ error: new Error(`Download failed: server closed connection. URL: ${url}`) }); promise.resolve({ error: new Error(`Download failed: server closed connection. URL: ${url}`) });

View file

@ -19,3 +19,4 @@ export type { ZipFile } from '../bundles/zip/node_modules/@types/yazl';
export const yauzl: typeof import('../bundles/zip/node_modules/@types/yauzl') = require('./zipBundleImpl').yauzl; export const yauzl: typeof import('../bundles/zip/node_modules/@types/yauzl') = require('./zipBundleImpl').yauzl;
export type { ZipFile as UnzipFile, Entry } from '../bundles/zip/node_modules/@types/yauzl'; export type { ZipFile as UnzipFile, Entry } from '../bundles/zip/node_modules/@types/yauzl';
export const extract: typeof import('../bundles/zip/node_modules/extract-zip') = require('./zipBundleImpl').extract; export const extract: typeof import('../bundles/zip/node_modules/extract-zip') = require('./zipBundleImpl').extract;
export const tarFs: typeof import('../bundles/zip/node_modules/@types/tar-fs') = require('./zipBundleImpl').tarFs;