From baa017db92b1fecf2e3584d5b3161371ae60f55b Mon Sep 17 00:00:00 2001 From: Valentin Semirulnik Date: Thu, 5 May 2022 10:33:19 +0300 Subject: [PATCH] feat: add support for Apple Silicon chromium builds (#7546) Google has published Chromium builds for Apple Silicon so we can fetch it now Related to #6622 --- CONTRIBUTING.md | 7 +++++++ docs/api.md | 1 + src/node/BrowserFetcher.ts | 33 +++++++++++++++++++++++---------- src/node/install.ts | 11 +++-------- utils/check_availability.js | 7 +++++-- 5 files changed, 39 insertions(+), 20 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index eda18d65..f4f18061 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -253,6 +253,13 @@ PUPPETEER_PRODUCT=firefox node install.js PUPPETEER_PRODUCT=firefox npm run unit ``` +- To run experimental Chromium MacOS ARM tests, firstly ensure you have correct Chromium version installed locally (you only need to do this once, not on every test run) and then you can run the tests: + +```bash +PUPPETEER_EXPERIMENTAL_CHROMIUM_MAC_ARM=1 node install.js +PUPPETEER_EXPERIMENTAL_CHROMIUM_MAC_ARM=1 npm run unit +``` + - To run tests with custom browser executable: ```bash diff --git a/docs/api.md b/docs/api.md index 4d396b28..cb4f79d3 100644 --- a/docs/api.md +++ b/docs/api.md @@ -487,6 +487,7 @@ If Puppeteer doesn't find them in the environment during the installation step, - `PUPPETEER_CHROMIUM_REVISION` - specify a certain version of Chromium you'd like Puppeteer to use. See [puppeteer.launch([options])](#puppeteerlaunchoptions) on how executable path is inferred. **BEWARE**: Puppeteer is only [guaranteed to work](https://github.com/puppeteer/puppeteer/#q-why-doesnt-puppeteer-vxxx-work-with-chromium-vyyy) with the bundled Chromium, use at your own risk. - `PUPPETEER_EXECUTABLE_PATH` - specify an executable path to be used in `puppeteer.launch`. See [puppeteer.launch([options])](#puppeteerlaunchoptions) on how the executable path is inferred. **BEWARE**: Puppeteer is only [guaranteed to work](https://github.com/puppeteer/puppeteer/#q-why-doesnt-puppeteer-vxxx-work-with-chromium-vyyy) with the bundled Chromium, use at your own risk. - `PUPPETEER_PRODUCT` - specify which browser you'd like Puppeteer to use. Must be one of `chrome` or `firefox`. This can also be used during installation to fetch the recommended browser binary. Setting `product` programmatically in [puppeteer.launch([options])](#puppeteerlaunchoptions) supersedes this environment variable. The product is exposed in [`puppeteer.product`](#puppeteerproduct) +- `PUPPETEER_EXPERIMENTAL_CHROMIUM_MAC_ARM` — specify Puppeteer download Chromium for Apple M1. On Apple M1 devices Puppeteer by default downloads the version for Intel's processor which runs via Rosetta. It works without any problems, however, with this option, you should get more efficient resource usage (CPU and RAM) that could lead to a faster execution time. **BEWARE**: it's an experimental option that makes sense only if you have an Apple M1 device, use at your own risk. > **NOTE** `PUPPETEER_*` env variables are not accounted for in the [`puppeteer-core`](https://www.npmjs.com/package/puppeteer-core) package. diff --git a/src/node/BrowserFetcher.ts b/src/node/BrowserFetcher.ts index d7a41f04..2323860e 100644 --- a/src/node/BrowserFetcher.ts +++ b/src/node/BrowserFetcher.ts @@ -35,12 +35,15 @@ import createHttpsProxyAgent, { import { getProxyForUrl } from 'proxy-from-env'; import { assert } from '../common/assert.js'; +const { PUPPETEER_EXPERIMENTAL_CHROMIUM_MAC_ARM } = process.env; + const debugFetcher = debug('puppeteer:fetcher'); const downloadURLs = { chrome: { linux: '%s/chromium-browser-snapshots/Linux_x64/%d/%s.zip', mac: '%s/chromium-browser-snapshots/Mac/%d/%s.zip', + mac_arm: '%s/chromium-browser-snapshots/Mac_Arm/%d/%s.zip', win32: '%s/chromium-browser-snapshots/Win/%d/%s.zip', win64: '%s/chromium-browser-snapshots/Win_x64/%d/%s.zip', }, @@ -67,7 +70,7 @@ const browserConfig = { * Supported platforms. * @public */ -export type Platform = 'linux' | 'mac' | 'win32' | 'win64'; +export type Platform = 'linux' | 'mac' | 'mac_arm' | 'win32' | 'win64'; function archiveName( product: Product, @@ -76,7 +79,7 @@ function archiveName( ): string { if (product === 'chrome') { if (platform === 'linux') return 'chrome-linux'; - if (platform === 'mac') return 'chrome-mac'; + if (platform === 'mac' || platform === 'mac_arm') return 'chrome-mac'; if (platform === 'win32' || platform === 'win64') { // Windows archive name changed at r591479. return parseInt(revision, 10) > 591479 ? 'chrome-win' : 'chrome-win32'; @@ -200,22 +203,33 @@ export class BrowserFetcher { options.path || path.join(projectRoot, browserConfig[this._product].destination); this._downloadHost = options.host || browserConfig[this._product].host; - this.setPlatform(options.platform); + this.setPlatform(options.platform, this._product); assert( downloadURLs[this._product][this._platform], 'Unsupported platform: ' + this._platform ); } - private setPlatform(platformFromOptions?: Platform): void { + private setPlatform( + platformFromOptions?: Platform, + productFromOptions?: Product + ): void { if (platformFromOptions) { this._platform = platformFromOptions; return; } const platform = os.platform(); - if (platform === 'darwin') this._platform = 'mac'; - else if (platform === 'linux') this._platform = 'linux'; + if (platform === 'darwin') { + if (productFromOptions === 'chrome') { + this._platform = + os.arch() === 'arm64' && PUPPETEER_EXPERIMENTAL_CHROMIUM_MAC_ARM + ? 'mac_arm' + : 'mac'; + } else if (productFromOptions === 'firefox') { + this._platform = 'mac'; + } + } else if (platform === 'linux') this._platform = 'linux'; else if (platform === 'win32') this._platform = os.arch() === 'x64' ? 'win64' : 'win32'; else assert(this._platform, 'Unsupported platform: ' + platform); @@ -297,8 +311,7 @@ export class BrowserFetcher { if (!(await existsAsync(this._downloadsFolder))) await mkdirAsync(this._downloadsFolder); - // Use Intel x86 builds on Apple M1 until native macOS arm64 - // Chromium builds are available. + // Use system Chromium builds on Linux ARM devices if (os.platform() !== 'darwin' && os.arch() === 'arm64') { handleArm64(); return; @@ -353,7 +366,7 @@ export class BrowserFetcher { const folderPath = this._getFolderPath(revision); let executablePath = ''; if (this._product === 'chrome') { - if (this._platform === 'mac') + if (this._platform === 'mac' || this._platform === 'mac_arm') executablePath = path.join( folderPath, archiveName(this._product, this._platform, revision), @@ -376,7 +389,7 @@ export class BrowserFetcher { ); else throw new Error('Unsupported platform: ' + this._platform); } else if (this._product === 'firefox') { - if (this._platform === 'mac') + if (this._platform === 'mac' || this._platform === 'mac_arm') executablePath = path.join( folderPath, 'Firefox Nightly.app', diff --git a/src/node/install.ts b/src/node/install.ts index 2c26ecae..913001bb 100644 --- a/src/node/install.ts +++ b/src/node/install.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -import os from 'os'; import https, { RequestOptions } from 'https'; import ProgressBar from 'progress'; import URL from 'url'; @@ -103,13 +102,9 @@ export async function downloadBrowser(): Promise { if (NPM_NO_PROXY) process.env.NO_PROXY = NPM_NO_PROXY; function onSuccess(localRevisions: string[]): void { - // Use Intel x86 builds on Apple M1 until native macOS arm64 - // Chromium builds are available. - if (os.platform() !== 'darwin' && os.arch() !== 'arm64') { - logPolitely( - `${supportedProducts[product]} (${revisionInfo.revision}) downloaded to ${revisionInfo.folderPath}` - ); - } + logPolitely( + `${supportedProducts[product]} (${revisionInfo.revision}) downloaded to ${revisionInfo.folderPath}` + ); localRevisions = localRevisions.filter( (revision) => revision !== revisionInfo.revision ); diff --git a/utils/check_availability.js b/utils/check_availability.js index a57378af..f9cf0226 100755 --- a/utils/check_availability.js +++ b/utils/check_availability.js @@ -21,7 +21,7 @@ const https = require('https'); const BrowserFetcher = require('../lib/cjs/puppeteer/node/BrowserFetcher.js').BrowserFetcher; -const SUPPORTED_PLATFORMS = ['linux', 'mac', 'win32', 'win64']; +const SUPPORTED_PLATFORMS = ['linux', 'mac', 'mac_arm', 'win32', 'win64']; const fetchers = SUPPORTED_PLATFORMS.map( (platform) => new BrowserFetcher('', { platform }) ); @@ -139,6 +139,9 @@ async function checkOmahaProxyAvailability() { fetch( 'https://storage.googleapis.com/chromium-browser-snapshots/Mac/LAST_CHANGE' ), + fetch( + 'https://storage.googleapis.com/chromium-browser-snapshots/Mac_Arm/LAST_CHANGE' + ), fetch( 'https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/LAST_CHANGE' ), @@ -191,7 +194,7 @@ async function checkRangeAvailability({ toRevision, stopWhenAllAvailable, }) { - const table = new Table([10, 7, 7, 7, 7]); + const table = new Table([10, 7, 7, 7, 7, 7]); table.drawRow([''].concat(SUPPORTED_PLATFORMS)); const inc = fromRevision < toRevision ? 1 : -1;