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,8 +151,14 @@ 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();
const extract = tarFs.extract(browserDirectory);
response
.pipe(decompress)
.pipe(extract)
.on('finish', () => {
if (downloadedBytes !== totalBytes) { if (downloadedBytes !== totalBytes) {
debugLogger.log('install', `-- download failed, size mismatch: ${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}`) }); promise.resolve({ error: new Error(`Download failed: size mismatch, file size: ${downloadedBytes}, expected size: ${totalBytes} URL: ${url}`) });
@ -158,12 +166,11 @@ async function downloadBrotli(title: string, browserDirectory: string, url: stri
debugLogger.log('install', `-- download complete, size: ${downloadedBytes}`); debugLogger.log('install', `-- download complete, size: ${downloadedBytes}`);
promise.resolve({ error: null }); promise.resolve({ error: null });
} }
}); })
file.on('error', error => promise.resolve({ error })); .on('error', error => promise.resolve({ error }));
response.pipe(file);
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;