Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: download browsers as TAR #34033

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/playwright-core/browsers.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
"revision": "1011",
"installByDefault": true,
"revisionOverrides": {
"mac12": "1010",
"mac12-arm64": "1010"
"mac12": "1011",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the motivation for changing this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1010 doesn't have .tar.br, and 1010 is identical to 1011 in functionality

"mac12-arm64": "1011"
}
},
{
Expand Down
237 changes: 120 additions & 117 deletions packages/playwright-core/src/server/registry/index.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import { httpRequest } from '../../utils/network';
import { ManualPromise } from '../../utils/manualPromise';
import { extract } from '../../zipBundle';
import tar from '../../utils/tar';

Check failure on line 22 in packages/playwright-core/src/server/registry/oopDownloadBrowserMain.ts

View workflow job for this annotation

GitHub Actions / docs & lint

Could not find a declaration file for module '../../utils/tar'. '/home/runner/work/playwright/playwright/packages/playwright-core/src/utils/tar/index.js' implicitly has an 'any' type.
import type http from 'http';
import { pipeline } from 'stream/promises';
import { createBrotliDecompress } from 'zlib';

export type DownloadParams = {
title: string;
Expand Down Expand Up @@ -104,11 +108,72 @@
}
}

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) {
await downloadFile(options);
log(`SUCCESS downloading ${options.title}`);
log(`extracting archive`);
await extract(options.zipPath, { dir: options.browserDirectory });
if (options.url.endsWith('.tar.br')) {
try {
await downloadAndExtractBrotli(options);
} catch (error) {
throw new Error(`Download failed. URL: ${options.url}`, { cause: error });
}
log(`SUCCESS downloading and extracting ${options.title}`);
} else {
await downloadFile(options);
log(`SUCCESS downloading ${options.title}`);
log(`extracting archive`);
await extract(options.zipPath, { dir: options.browserDirectory });
}
if (options.executablePath) {
log(`fixing permissions at ${options.executablePath}`);
await fs.promises.chmod(options.executablePath, 0o755);
Expand Down
1 change: 1 addition & 0 deletions packages/playwright-core/src/utils/tar/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This directory contains a modified copy of the `tar-stream` library that's used exclusively to extract TAR files.
Loading
Loading