From e3d852473856cc1de7d0c9e64375b89d6e7b0fc4 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Tue, 21 Feb 2023 15:19:06 +0100 Subject: [PATCH] chore: allow downloading the latest Chrome version (#9719) --- packages/browsers/src/CLI.ts | 6 ++++ packages/browsers/src/browsers/browsers.ts | 6 ++++ packages/browsers/src/browsers/chrome.ts | 39 ++++++++++++++++++++++ packages/browsers/test/src/cli.spec.ts | 11 ++++++ 4 files changed, 62 insertions(+) diff --git a/packages/browsers/src/CLI.ts b/packages/browsers/src/CLI.ts index fa02349fd52..bf1848f92bb 100644 --- a/packages/browsers/src/CLI.ts +++ b/packages/browsers/src/CLI.ts @@ -20,6 +20,7 @@ import {hideBin} from 'yargs/helpers'; import {resolveRevision} from './browsers/browsers.js'; import {Browser, BrowserPlatform} from './browsers/types.js'; +import {detectBrowserPlatform} from './detectPlatform.js'; import {fetch} from './fetch.js'; import {computeExecutablePath, launch} from './launcher.js'; @@ -68,8 +69,13 @@ export class CLI { }, async argv => { const args = argv as unknown as InstallArgs; + args.platform ??= detectBrowserPlatform(); + if (!args.platform) { + throw new Error(`Could not resolve the current platform`); + } args.browser.revision = await resolveRevision( args.browser.name, + args.platform, args.browser.revision ); await fetch({ diff --git a/packages/browsers/src/browsers/browsers.ts b/packages/browsers/src/browsers/browsers.ts index defcb74b436..77cb1d5cef5 100644 --- a/packages/browsers/src/browsers/browsers.ts +++ b/packages/browsers/src/browsers/browsers.ts @@ -32,6 +32,7 @@ export {Browser, BrowserPlatform}; export async function resolveRevision( browser: Browser, + platform: BrowserPlatform, tag: string ): Promise { switch (browser) { @@ -40,6 +41,11 @@ export async function resolveRevision( case BrowserTag.LATEST: return await firefox.resolveRevision('FIREFOX_NIGHTLY'); } + case Browser.CHROME: + switch (tag as BrowserTag) { + case BrowserTag.LATEST: + return await chrome.resolveRevision(platform, 'latest'); + } } // We assume the tag is the revision if it didn't match any keywords. return tag; diff --git a/packages/browsers/src/browsers/chrome.ts b/packages/browsers/src/browsers/chrome.ts index 6a4e26a4fc9..d9192d4d34b 100644 --- a/packages/browsers/src/browsers/chrome.ts +++ b/packages/browsers/src/browsers/chrome.ts @@ -16,6 +16,8 @@ import path from 'path'; +import {httpRequest} from '../httpUtil.js'; + import {BrowserPlatform} from './types.js'; function archive(platform: BrowserPlatform, revision: string): string { @@ -79,3 +81,40 @@ export function relativeExecutablePath( return path.join('chrome-win', 'chrome.exe'); } } + +export async function resolveRevision( + platform: BrowserPlatform, + // We will need it for other channels/keywords. + _channel: 'latest' = 'latest' +): Promise { + return new Promise((resolve, reject) => { + const request = httpRequest( + new URL( + `https://storage.googleapis.com/chromium-browser-snapshots/${folder( + platform + )}/LAST_CHANGE` + ), + 'GET', + response => { + let data = ''; + if (response.statusCode && response.statusCode >= 400) { + return reject(new Error(`Got status code ${response.statusCode}`)); + } + response.on('data', chunk => { + data += chunk; + }); + response.on('end', () => { + try { + return resolve(String(data)); + } catch { + return reject(new Error('Chrome version not found')); + } + }); + }, + false + ); + request.on('error', err => { + reject(err); + }); + }); +} diff --git a/packages/browsers/test/src/cli.spec.ts b/packages/browsers/test/src/cli.spec.ts index e317b15195a..ee7f8b31cf2 100644 --- a/packages/browsers/test/src/cli.spec.ts +++ b/packages/browsers/test/src/cli.spec.ts @@ -83,4 +83,15 @@ describe('CLI', function () { '--platform=linux', ]); }); + + it('should download latest Chrome binaries', async () => { + await new CLI(tmpDir).run([ + 'npx', + '@puppeteer/browsers', + 'install', + `chrome@latest`, + `--path=${tmpDir}`, + '--platform=linux', + ]); + }); });