Skip to content

🎥 Make videos programmatically with React

download browser

Jonny Burgerf3ab87759a3c
diff --git a/packages/cli/src/get-cli-options.ts b/packages/cli/src/get-cli-options.ts
index 28685db23e..81d2a26688 100644
--- a/packages/cli/src/get-cli-options.ts
+++ b/packages/cli/src/get-cli-options.ts
@@ -73,7 +73,7 @@ const getProResProfile = () => {
const getAndValidateBrowser = async (browserExecutable: BrowserExecutable) => {
const browser = getBrowser();
try {
- await RenderInternals.ensureLocalBrowser(browser, browserExecutable);
+ await RenderInternals.ensureLocalBrowser(browserExecutable);
} catch (err) {
Log.error('Could not download a browser for rendering frames.');
Log.error(err);
diff --git a/packages/renderer/src/browser/BrowserFetcher.ts b/packages/renderer/src/browser/BrowserFetcher.ts
index 9515fe1e7c..1ac6b11243 100644
--- a/packages/renderer/src/browser/BrowserFetcher.ts
+++ b/packages/renderer/src/browser/BrowserFetcher.ts
@@ -26,8 +26,8 @@ import extractZip from 'extract-zip';
import * as URL from 'node:url';
import {promisify} from 'node:util';
import {assert} from './assert';
-import type {Product} from './Product';
+import {Log} from '../logger';
import {getDownloadsCacheDir} from './get-download-destination';
const downloadURLs: Record<Platform, string> = {
@@ -68,20 +68,13 @@ interface BrowserFetcherRevisionInfo {
executablePath: string;
url: string;
local: boolean;
- product: string;
}
-export const getPlatform = (product: Product): Platform => {
+export const getPlatform = (): Platform => {
const platform = os.platform();
switch (platform) {
case 'darwin':
- switch (product) {
- case 'chrome':
- return os.arch() === 'arm64' ? 'mac_arm' : 'mac';
- default:
- throw new Error('unknown browser');
- }
-
+ return os.arch() === 'arm64' ? 'mac_arm' : 'mac';
case 'linux':
return 'linux';
case 'win32':
@@ -93,29 +86,22 @@ export const getPlatform = (product: Product): Platform => {
const destination = '.chromium';
-export const getDownloadsFolder = () => {
+const getDownloadsFolder = () => {
return path.join(getDownloadsCacheDir(), destination);
};
-export const download = async ({
- progressCallback,
- product,
- platform,
- downloadsFolder,
- downloadURL,
-}: {
- progressCallback: (x: number, y: number) => void;
- product: Product;
- platform: Platform;
- downloadsFolder: string;
- downloadURL: string;
-}): Promise<BrowserFetcherRevisionInfo | undefined> => {
+export const downloadBrowser = async (): Promise<
+ BrowserFetcherRevisionInfo | undefined
+> => {
+ const platform = getPlatform();
+ const downloadURL = getThoriumDownloadUrl(platform);
const fileName = downloadURL.split('/').pop();
assert(fileName, `A malformed download URL was found: ${downloadURL}.`);
+ const downloadsFolder = getDownloadsFolder();
const archivePath = path.join(downloadsFolder, fileName);
const outputPath = getFolderPath(downloadsFolder, platform);
if (await existsAsync(outputPath)) {
- return getRevisionInfo(product);
+ return getRevisionInfo();
}
if (!(await existsAsync(downloadsFolder))) {
@@ -126,12 +112,16 @@ export const download = async ({
// Use system Chromium builds on Linux ARM devices
if (os.platform() !== 'darwin' && os.arch() === 'arm64') {
- handleArm64();
- return;
+ throw new Error(
+ 'The chromium binary is not available for arm64.' +
+ '\nIf you are on Ubuntu, you can install with: ' +
+ '\n\n sudo apt install chromium\n' +
+ '\n\n sudo apt install chromium-browser\n'
+ );
}
try {
- await _downloadFile(downloadURL, archivePath, progressCallback);
+ await _downloadThorium(downloadURL, archivePath);
await install(archivePath, outputPath);
} finally {
if (await existsAsync(archivePath)) {
@@ -139,7 +129,7 @@ export const download = async ({
}
}
- const revisionInfo = getRevisionInfo(product);
+ const revisionInfo = getRevisionInfo();
if (revisionInfo) {
await chmodAsync(revisionInfo.executablePath, 0o755);
}
@@ -154,41 +144,37 @@ export const getFolderPath = (
return path.resolve(downloadsFolder, `${platform}`);
};
-const getExecutablePath = (product: Product) => {
+const getExecutablePath = () => {
const downloadsFolder = getDownloadsFolder();
- const platform = getPlatform(product);
+ const platform = getPlatform();
const folderPath = getFolderPath(downloadsFolder, platform);
- if (product === 'chrome') {
- if (platform === 'mac' || platform === 'mac_arm') {
- return path.join(
- folderPath,
- archiveName(platform),
- 'Chromium.app',
- 'Contents',
- 'MacOS',
- 'Chromium'
- );
- }
+ if (platform === 'mac' || platform === 'mac_arm') {
+ return path.join(
+ folderPath,
+ archiveName(platform),
+ 'Chromium.app',
+ 'Contents',
+ 'MacOS',
+ 'Chromium'
+ );
+ }
- if (platform === 'linux') {
- return path.join(folderPath, archiveName(platform), 'thorium');
- }
+ if (platform === 'linux') {
+ return path.join(folderPath, archiveName(platform), 'thorium');
+ }
- if (platform === 'win64') {
- return path.join(folderPath, archiveName(platform), 'thorium.exe');
- }
+ if (platform === 'win64') {
+ return path.join(folderPath, archiveName(platform), 'thorium.exe');
}
- throw new Error('Unsupported product: ' + product);
+ throw new Error('Can not download browser for platform: ' + platform);
};
-export const getRevisionInfo = (
- product: Product
-): BrowserFetcherRevisionInfo => {
- const executablePath = getExecutablePath(product);
+export const getRevisionInfo = (): BrowserFetcherRevisionInfo => {
+ const executablePath = getExecutablePath();
const downloadsFolder = getDownloadsFolder();
- const platform = getPlatform(product);
+ const platform = getPlatform();
const folderPath = getFolderPath(downloadsFolder, platform);
const url = getThoriumDownloadUrl(platform);
@@ -198,14 +184,12 @@ export const getRevisionInfo = (
folderPath,
local,
url,
- product,
};
};
-function _downloadFile(
+function _downloadThorium(
url: string,
- destinationPath: string,
- progressCallback: (x: number, y: number) => void
+ destinationPath: string
): Promise<number> {
let fulfill: (value: number | PromiseLike<number>) => void;
let reject: (err: Error) => void;
@@ -222,7 +206,10 @@ function _downloadFile(
function onData(chunk: string): void {
downloadedBytes += chunk.length;
if (Date.now() - lastProgress > 1000) {
- progressCallback(downloadedBytes, totalBytes);
+ Log.info(
+ 'Downloading Thorium',
+ toMegabytes(downloadedBytes) + '/' + toMegabytes(totalBytes)
+ );
lastProgress = Date.now();
}
}
@@ -372,22 +359,7 @@ function httpRequest(
return request;
}
-function handleArm64(): void {
- let exists = fs.existsSync('/usr/bin/chromium-browser');
- if (exists) {
- return;
- }
-
- exists = fs.existsSync('/usr/bin/chromium');
- if (exists) {
- return;
- }
-
- console.error(
- 'The chromium binary is not available for arm64.' +
- '\nIf you are on Ubuntu, you can install with: ' +
- '\n\n sudo apt install chromium\n' +
- '\n\n sudo apt install chromium-browser\n'
- );
- throw new Error();
+function toMegabytes(bytes: number) {
+ const mb = bytes / 1024 / 1024;
+ return `${Math.round(mb * 10) / 10} Mb`;
}
diff --git a/packages/renderer/src/browser/Launcher.ts b/packages/renderer/src/browser/Launcher.ts
index ceffdcbda6..4182226862 100644
--- a/packages/renderer/src/browser/Launcher.ts
+++ b/packages/renderer/src/browser/Launcher.ts
@@ -133,7 +133,7 @@ function resolveExecutablePath(launcher: ChromeLauncher): {
} {
const {product} = launcher;
- const revisionInfo = getRevisionInfo('chrome');
+ const revisionInfo = getRevisionInfo();
const firefoxHelp = `Run \`PUPPETEER_PRODUCT=firefox npm install\` to download a supported Firefox browser binary.`;
const chromeHelp = `Run \`npm install\` to download the correct Chromium revision.`;
diff --git a/packages/renderer/src/browser/PuppeteerNode.ts b/packages/renderer/src/browser/PuppeteerNode.ts
index 4add7c744e..65ef4e8f56 100644
--- a/packages/renderer/src/browser/PuppeteerNode.ts
+++ b/packages/renderer/src/browser/PuppeteerNode.ts
@@ -34,21 +34,13 @@ interface PuppeteerLaunchOptions
export class PuppeteerNode {
#lazyLauncher?: ProductLauncher;
- #productName?: Product;
-
- constructor(settings: {productName?: Product}) {
- const {productName} = settings;
- this.#productName = productName;
+ constructor() {
this.launch = this.launch.bind(this);
this.executablePath = this.executablePath.bind(this);
}
launch(options: PuppeteerLaunchOptions): Promise<HeadlessBrowser> {
- if (options.product) {
- this.#productName = options.product;
- }
-
return this._launcher.launch(options);
}
@@ -57,10 +49,7 @@ export class PuppeteerNode {
}
get _launcher(): ProductLauncher {
- if (
- !this.#lazyLauncher ||
- this.#lazyLauncher.product !== this.#productName
- ) {
+ if (!this.#lazyLauncher) {
this.#lazyLauncher = new ChromeLauncher();
}
diff --git a/packages/renderer/src/browser/create-browser-fetcher.ts b/packages/renderer/src/browser/create-browser-fetcher.ts
deleted file mode 100644
index ec08fe4aa6..0000000000
--- a/packages/renderer/src/browser/create-browser-fetcher.ts
+++ /dev/null
@@ -1,55 +0,0 @@
-/**
- * Copyright 2020 Google Inc. All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import {Log} from '../logger';
-import {
- download,
- getDownloadsFolder,
- getPlatform,
- getRevisionInfo,
- getThoriumDownloadUrl,
-} from './BrowserFetcher';
-import type {Product} from './Product';
-
-const supportedProducts = {
- chrome: 'Chromium',
-} as const;
-
-export async function downloadBrowser(product: Product): Promise<void> {
- const revisionInfo = getRevisionInfo(product);
-
- await download({
- progressCallback: (downloadedBytes, totalBytes) => {
- Log.info(
- 'Downloading Thorium',
- toMegabytes(downloadedBytes) + '/' + toMegabytes(totalBytes)
- );
- },
- product,
- platform: getPlatform(product),
- downloadsFolder: getDownloadsFolder(),
- downloadURL: getThoriumDownloadUrl(getPlatform(product)),
- });
-
- Log.info(
- `${supportedProducts[product]} (${revisionInfo.url}) downloaded to ${revisionInfo.folderPath}`
- );
-}
-
-function toMegabytes(bytes: number) {
- const mb = bytes / 1024 / 1024;
- return `${Math.round(mb * 10) / 10} Mb`;
-}
diff --git a/packages/renderer/src/browser/node.ts b/packages/renderer/src/browser/node.ts
index 5a12ad25bc..bf7795d9a4 100644
--- a/packages/renderer/src/browser/node.ts
+++ b/packages/renderer/src/browser/node.ts
@@ -1,5 +1,3 @@
import {PuppeteerNode} from './PuppeteerNode';
-export const puppeteer = new PuppeteerNode({
- productName: undefined,
-});
+export const puppeteer = new PuppeteerNode();
diff --git a/packages/renderer/src/get-local-browser-executable.ts b/packages/renderer/src/get-local-browser-executable.ts
index 835cc8a478..41ae0cfeb8 100644
--- a/packages/renderer/src/get-local-browser-executable.ts
+++ b/packages/renderer/src/get-local-browser-executable.ts
@@ -1,54 +1,45 @@
import fs from 'node:fs';
import os from 'node:os';
-import type {Browser} from './browser';
import type {BrowserExecutable} from './browser-executable';
-import {getRevisionInfo} from './browser/BrowserFetcher';
-import {downloadBrowser} from './browser/create-browser-fetcher';
-import type {Product} from './browser/Product';
+import {downloadBrowser, getRevisionInfo} from './browser/BrowserFetcher';
-const getSearchPathsForProduct = (product: Product) => {
- if (product === 'chrome') {
- return [
- process.env.PUPPETEER_EXECUTABLE_PATH ?? null,
- process.platform === 'darwin'
- ? '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
- : null,
- process.platform === 'linux' ? '/usr/bin/google-chrome' : null,
- process.platform === 'linux' ? '/usr/bin/chromium-browser' : null,
- process.platform === 'linux' ? '/usr/bin/chromium' : null, // Debian
- process.platform === 'linux'
- ? '/app/.apt/usr/bin/google-chrome-stable'
- : null,
- process.platform === 'win32'
- ? 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
- : null,
- process.platform === 'win32'
- ? 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
- : null,
- process.platform === 'win32'
- ? os.homedir() +
- '\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe'
- : null,
- process.platform === 'win32'
- ? 'C:\\Program Files\\Google\\Chrome SxS\\Application\\chrome.exe'
- : null,
- process.platform === 'win32'
- ? 'C:\\Program Files (x86)\\Google\\Chrome SxS\\Application\\chrome.exe'
- : null,
- process.platform === 'win32'
- ? os.homedir() +
- '\\AppData\\Local\\Google\\Chrome SxS\\Application\\chrome.exe'
- : null,
- ].filter(Boolean) as string[];
- }
-
- throw new TypeError(`Unknown browser product: ${product}`);
+const getSearchPathsForProduct = () => {
+ return [
+ process.env.PUPPETEER_EXECUTABLE_PATH ?? null,
+ process.platform === 'darwin'
+ ? '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
+ : null,
+ process.platform === 'linux' ? '/usr/bin/google-chrome' : null,
+ process.platform === 'linux' ? '/usr/bin/chromium-browser' : null,
+ process.platform === 'linux' ? '/usr/bin/chromium' : null, // Debian
+ process.platform === 'linux'
+ ? '/app/.apt/usr/bin/google-chrome-stable'
+ : null,
+ process.platform === 'win32'
+ ? 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
+ : null,
+ process.platform === 'win32'
+ ? 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
+ : null,
+ process.platform === 'win32'
+ ? os.homedir() +
+ '\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe'
+ : null,
+ process.platform === 'win32'
+ ? 'C:\\Program Files\\Google\\Chrome SxS\\Application\\chrome.exe'
+ : null,
+ process.platform === 'win32'
+ ? 'C:\\Program Files (x86)\\Google\\Chrome SxS\\Application\\chrome.exe'
+ : null,
+ process.platform === 'win32'
+ ? os.homedir() +
+ '\\AppData\\Local\\Google\\Chrome SxS\\Application\\chrome.exe'
+ : null,
+ ].filter(Boolean) as string[];
};
-const mapBrowserToProduct = (browser: Browser): Product => browser;
-
-const getLocalBrowser = (product: Product) => {
- for (const p of getSearchPathsForProduct(product)) {
+const getLocalBrowser = () => {
+ for (const p of getSearchPathsForProduct()) {
if (fs.existsSync(p)) {
return p;
}
@@ -75,7 +66,6 @@ type BrowserStatus =
};
const getBrowserStatus = (
- product: Product,
browserExecutablePath: BrowserExecutable
): BrowserStatus => {
if (browserExecutablePath) {
@@ -88,12 +78,12 @@ const getBrowserStatus = (
return {path: browserExecutablePath, type: 'user-defined-path'};
}
- const localBrowser = getLocalBrowser(product);
+ const localBrowser = getLocalBrowser();
if (localBrowser !== null) {
return {path: localBrowser, type: 'local-browser'};
}
- const revision = getRevisionInfo(product);
+ const revision = getRevisionInfo();
if (revision.local && fs.existsSync(revision.executablePath)) {
return {path: revision.executablePath, type: 'local-puppeteer-browser'};
}
@@ -102,29 +92,21 @@ const getBrowserStatus = (
};
export const ensureLocalBrowser = async (
- browser: Browser,
preferredBrowserExecutable: BrowserExecutable
) => {
- const status = getBrowserStatus(
- mapBrowserToProduct(browser),
- preferredBrowserExecutable
- );
+ const status = getBrowserStatus(preferredBrowserExecutable);
if (status.type === 'no-browser') {
console.log(
'No local browser could be found. Downloading one from the internet...'
);
- await downloadBrowser(browser);
+ await downloadBrowser();
}
};
export const getLocalBrowserExecutable = (
- browser: Browser,
preferredBrowserExecutable: BrowserExecutable
): string => {
- const status = getBrowserStatus(
- mapBrowserToProduct(browser),
- preferredBrowserExecutable
- );
+ const status = getBrowserStatus(preferredBrowserExecutable);
if (status.type === 'no-browser') {
throw new TypeError(
'No browser found for rendering frames! Please open a GitHub issue and describe ' +
diff --git a/packages/renderer/src/open-browser.ts b/packages/renderer/src/open-browser.ts
index 67f872fbd1..ebd1ca49e1 100644
--- a/packages/renderer/src/open-browser.ts
+++ b/packages/renderer/src/open-browser.ts
@@ -82,9 +82,9 @@ export const internalOpenBrowser = async ({
);
}
- await ensureLocalBrowser(browser, browserExecutable);
+ await ensureLocalBrowser(browserExecutable);
- const executablePath = getLocalBrowserExecutable(browser, browserExecutable);
+ const executablePath = getLocalBrowserExecutable(browserExecutable);
const customGlRenderer = getOpenGlRenderer(chromiumOptions.gl ?? null);
diff --git a/packages/renderer/src/test/download-browser.test.ts b/packages/renderer/src/test/download-browser.test.ts
index ca870a8ccb..48c66d8022 100644
--- a/packages/renderer/src/test/download-browser.test.ts
+++ b/packages/renderer/src/test/download-browser.test.ts
@@ -1,9 +1,9 @@
-import {downloadBrowser} from '../browser/create-browser-fetcher';
+import {downloadBrowser} from '../browser/BrowserFetcher';
-downloadBrowser('chrome')
+downloadBrowser()
.then((c) => {
console.log(c);
})
.catch((err) => {
- console.log(err);
+ console.log(err.stack);
});