From b1771572206517c27b4f87d58320feb9c4f5d4b4 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Fri, 29 Nov 2024 12:11:25 +0100 Subject: [PATCH] decompress and extract --- .../src/server/registry/browserFetcher.ts | 33 +++++++++++-------- packages/playwright-core/src/zipBundle.ts | 1 + 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/packages/playwright-core/src/server/registry/browserFetcher.ts b/packages/playwright-core/src/server/registry/browserFetcher.ts index 415397179b..6d531e0644 100644 --- a/packages/playwright-core/src/server/registry/browserFetcher.ts +++ b/packages/playwright-core/src/server/registry/browserFetcher.ts @@ -19,10 +19,12 @@ import fs from 'fs'; import os from 'os'; import path from 'path'; import childProcess from 'child_process'; +import zlib from 'zlib'; import { existsAsync } from '../../utils/fileUtils'; import { debugLogger } from '../../utils/debugLogger'; import { ManualPromise } from '../../utils/manualPromise'; import { colors, multiProgress as MultiProgressBar } from '../../utilsBundle'; +import { tarFs } from '../../zipBundle'; import { browserDirectoryToMarkerFilePath } from '.'; import { getUserAgent } from '../../utils/userAgent'; 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); debugLogger.log('install', `-- total bytes: ${totalBytes}`); - const file = fs.createWriteStream(zipPath); - file.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 }); - } - }); - file.on('error', error => promise.resolve({ error })); - response.pipe(file); + + const decompress = zlib.createBrotliDecompress(); + const extract = tarFs.extract(browserDirectory); + + response + .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) => { - file.close(); if (error?.code === 'ECONNRESET') { debugLogger.log('install', `-- download failed, server closed connection`); promise.resolve({ error: new Error(`Download failed: server closed connection. URL: ${url}`) }); diff --git a/packages/playwright-core/src/zipBundle.ts b/packages/playwright-core/src/zipBundle.ts index 9c275873a8..58d142533c 100644 --- a/packages/playwright-core/src/zipBundle.ts +++ b/packages/playwright-core/src/zipBundle.ts @@ -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 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 tarFs: typeof import('../bundles/zip/node_modules/@types/tar-fs') = require('./zipBundleImpl').tarFs;