Skip to content

🎥 Make videos programmatically with React

refactor browser fetcher

Jonny Burgerc52a5ecb1690
diff --git a/packages/renderer/src/browser/BrowserFetcher.ts b/packages/renderer/src/browser/BrowserFetcher.ts
index a747051f09..8da0c97a4c 100644
--- a/packages/renderer/src/browser/BrowserFetcher.ts
+++ b/packages/renderer/src/browser/BrowserFetcher.ts
@@ -145,7 +145,6 @@ function existsAsync(filePath: string): Promise<boolean> {
}
export interface BrowserFetcherOptions {
- platform: Platform | null;
product: Product;
path: string | null;
}
@@ -159,293 +158,231 @@ interface BrowserFetcherRevisionInfo {
product: string;
}
-export class BrowserFetcher {
- #product: Product;
- #downloadsFolder: string;
- #downloadHost: string;
- #platform: Platform;
-
- constructor(options: BrowserFetcherOptions) {
- this.#product = options.product.toLowerCase() as Product;
- assert(
- this.#product === 'chrome' || this.#product === 'firefox',
- `Unknown product: "${options.product}"`
- );
-
- this.#downloadsFolder =
- options.path ||
- path.join(
- getDownloadsCacheDir(),
- browserConfig[this.#product].destination
- );
- this.#downloadHost = browserConfig[this.#product].host;
-
- if (options.platform) {
- this.#platform = options.platform;
- } else {
- const platform = os.platform();
- switch (platform) {
- case 'darwin':
- switch (this.#product) {
- case 'chrome':
- this.#platform =
- os.arch() === 'arm64' && PUPPETEER_EXPERIMENTAL_CHROMIUM_MAC_ARM
- ? 'mac_arm'
- : 'mac';
- break;
- case 'firefox':
- this.#platform = 'mac';
- break;
- default:
- throw new Error('unknown browser');
- }
-
- break;
- case 'linux':
- this.#platform = 'linux';
- break;
- case 'win32':
- this.#platform = os.arch() === 'x64' ? 'win64' : 'win32';
- return;
+export const getPlatform = (product: Product): Platform => {
+ const platform = os.platform();
+ switch (platform) {
+ case 'darwin':
+ switch (product) {
+ case 'chrome':
+ return os.arch() === 'arm64' &&
+ PUPPETEER_EXPERIMENTAL_CHROMIUM_MAC_ARM
+ ? 'mac_arm'
+ : 'mac';
+ case 'firefox':
+ return 'mac';
default:
- assert(false, 'Unsupported platform: ' + platform);
+ throw new Error('unknown browser');
}
- }
-
- assert(
- downloadURLs[this.#product][this.#platform],
- 'Unsupported platform: ' + this.#platform
- );
- }
- /**
- * @returns Returns the current `Platform`, which is one of `mac`, `linux`,
- * `win32` or `win64`.
- */
- platform(): Platform {
- return this.#platform;
+ case 'linux':
+ return 'linux';
+ case 'win32':
+ return os.arch() === 'x64' ? 'win64' : 'win32';
+ default:
+ assert(false, 'Unsupported platform: ' + platform);
}
+};
- /**
- * @returns Returns the current `Product`, which is one of `chrome` or
- * `firefox`.
- */
- product(): Product {
- return this.#product;
- }
+export const getDownloadsFolder = (product: Product) => {
+ return path.join(getDownloadsCacheDir(), browserConfig[product].destination);
+};
- /**
- * @returns The download host being used.
- */
- host(): string {
- return this.#downloadHost;
- }
+export const getDownloadHost = (product: Product) => {
+ return browserConfig[product].host;
+};
- /**
- * Initiates a HEAD request to check if the revision is available.
- * @remarks
- * This method is affected by the current `product`.
- * @param revision - The revision to check availability for.
- * @returns A promise that resolves to `true` if the revision could be downloaded
- * from the host.
- */
- canDownload(revision: string): Promise<boolean> {
- const url = _downloadURL(
- this.#product,
- this.#platform,
- this.#downloadHost,
- revision
+export const canDownload = (
+ revision: string,
+ product: Product,
+ platform: Platform,
+ downloadHost: string
+): Promise<boolean> => {
+ const url = _downloadURL(product, platform, downloadHost, revision);
+ return new Promise((resolve) => {
+ const request = httpRequest(
+ url,
+ 'HEAD',
+ (response) => {
+ resolve(response.statusCode === 200);
+ },
+ false
);
- return new Promise((resolve) => {
- const request = httpRequest(
- url,
- 'HEAD',
- (response) => {
- resolve(response.statusCode === 200);
- },
- false
- );
- request.on('error', (error) => {
- console.error(error);
- resolve(false);
- });
+ request.on('error', (error) => {
+ console.error(error);
+ resolve(false);
});
- }
+ });
+};
- /**
- * Initiates a GET request to download the revision from the host.
- * @remarks
- * This method is affected by the current `product`.
- * @param revision - The revision to download.
- * @param progressCallback - A function that will be called with two arguments:
- * How many bytes have been downloaded and the total number of bytes of the download.
- * @returns A promise with revision information when the revision is downloaded
- * and extracted.
- */
- async download(
- revision: string,
- progressCallback: (x: number, y: number) => void = (): void => undefined
- ): Promise<BrowserFetcherRevisionInfo | undefined> {
- const url = _downloadURL(
- this.#product,
- this.#platform,
- this.#downloadHost,
- revision
+export const download = async (
+ revision: string,
+ progressCallback: (x: number, y: number) => void,
+ product: Product,
+ platform: Platform,
+ downloadHost: string,
+ downloadsFolder: string
+): Promise<BrowserFetcherRevisionInfo | undefined> => {
+ const url = _downloadURL(product, platform, downloadHost, revision);
+ const fileName = url.split('/').pop();
+ assert(fileName, `A malformed download URL was found: ${url}.`);
+ const archivePath = path.join(downloadsFolder, fileName);
+ const outputPath = getFolderPath(revision, downloadsFolder, platform);
+ if (await existsAsync(outputPath)) {
+ return getRevisionInfo(
+ revision,
+ product,
+ getFolderPath(revision, downloadsFolder, platform),
+ platform
);
- const fileName = url.split('/').pop();
- assert(fileName, `A malformed download URL was found: ${url}.`);
- const archivePath = path.join(this.#downloadsFolder, fileName);
- const outputPath = this.#getFolderPath(revision);
- if (await existsAsync(outputPath)) {
- return this.revisionInfo(revision);
- }
-
- if (!(await existsAsync(this.#downloadsFolder))) {
- await mkdirAsync(this.#downloadsFolder, {
- recursive: true,
- });
- }
-
- // Use system Chromium builds on Linux ARM devices
- if (os.platform() !== 'darwin' && os.arch() === 'arm64') {
- handleArm64();
- return;
- }
-
- try {
- await _downloadFile(url, archivePath, progressCallback);
- await install(archivePath, outputPath);
- } finally {
- if (await existsAsync(archivePath)) {
- await unlinkAsync(archivePath);
- }
- }
+ }
- const revisionInfo = this.revisionInfo(revision);
- if (revisionInfo) {
- await chmodAsync(revisionInfo.executablePath, 0o755);
- }
+ if (!(await existsAsync(downloadsFolder))) {
+ await mkdirAsync(downloadsFolder, {
+ recursive: true,
+ });
+ }
- return revisionInfo;
+ // Use system Chromium builds on Linux ARM devices
+ if (os.platform() !== 'darwin' && os.arch() === 'arm64') {
+ handleArm64();
+ return;
}
- /**
- * @remarks
- * This method is affected by the current `product`.
- * @returns A promise with a list of all revision strings (for the current `product`)
- * available locally on disk.
- */
- async localRevisions(): Promise<string[]> {
- if (!(await existsAsync(this.#downloadsFolder))) {
- return [];
+ try {
+ await _downloadFile(url, archivePath, progressCallback);
+ await install(archivePath, outputPath);
+ } finally {
+ if (await existsAsync(archivePath)) {
+ await unlinkAsync(archivePath);
}
+ }
- const fileNames = await readdirAsync(this.#downloadsFolder);
- return fileNames
- .map((fileName) => {
- return parseFolderPath(this.#product, fileName);
- })
- .filter(
- (
- entry
- ): entry is {product: string; platform: string; revision: string} => {
- return (entry && entry.platform === this.#platform) ?? false;
- }
- )
- .map((entry) => {
- return entry.revision;
- });
+ const revisionInfo = getRevisionInfo(
+ revision,
+ product,
+ getFolderPath(revision, downloadsFolder, platform),
+ platform
+ );
+ if (revisionInfo) {
+ await chmodAsync(revisionInfo.executablePath, 0o755);
}
- /**
- * @remarks
- * This method is affected by the current `product`.
- * @param revision - A revision to remove for the current `product`.
- * @returns A promise that resolves when the revision has been removes or
- * throws if the revision has not been downloaded.
- */
- async remove(revision: string): Promise<void> {
- const folderPath = this.#getFolderPath(revision);
- assert(
- await existsAsync(folderPath),
- `Failed to remove: revision ${revision} is not downloaded`
- );
- deleteDirectory(folderPath);
+ return revisionInfo;
+};
+
+export const localRevisions = async (
+ downloadsFolder: string,
+ product: Product,
+ platform: Platform
+): Promise<string[]> => {
+ if (!(await existsAsync(downloadsFolder))) {
+ return [];
}
- /**
- * @param revision - The revision to get info for.
- * @returns The revision info for the given revision.
- */
- revisionInfo(revision: string): BrowserFetcherRevisionInfo {
- const folderPath = this.#getFolderPath(revision);
- let executablePath = '';
- if (this.#product === 'chrome') {
- if (this.#platform === 'mac' || this.#platform === 'mac_arm') {
- executablePath = path.join(
- folderPath,
- archiveName(this.#product, this.#platform, revision),
- 'Chromium.app',
- 'Contents',
- 'MacOS',
- 'Chromium'
- );
- } else if (this.#platform === 'linux') {
- executablePath = path.join(
- folderPath,
- archiveName(this.#product, this.#platform, revision),
- 'chrome'
- );
- } else if (this.#platform === 'win32' || this.#platform === 'win64') {
- executablePath = path.join(
- folderPath,
- archiveName(this.#product, this.#platform, revision),
- 'thorium.exe'
- );
- } else {
- throw new Error('Unsupported platform: ' + this.#platform);
- }
- } else if (this.#product === 'firefox') {
- if (this.#platform === 'mac' || this.#platform === 'mac_arm') {
- executablePath = path.join(
- folderPath,
- 'Firefox Nightly.app',
- 'Contents',
- 'MacOS',
- 'firefox'
- );
- } else if (this.#platform === 'linux') {
- executablePath = path.join(folderPath, 'firefox', 'firefox');
- } else if (this.#platform === 'win32' || this.#platform === 'win64') {
- executablePath = path.join(folderPath, 'firefox', 'firefox.exe');
- } else {
- throw new Error('Unsupported platform: ' + this.#platform);
+ const fileNames = await readdirAsync(downloadsFolder);
+ return fileNames
+ .map((fileName) => {
+ return parseFolderPath(product, fileName);
+ })
+ .filter(
+ (
+ entry
+ ): entry is {product: string; platform: string; revision: string} => {
+ return (entry && entry.platform === platform) ?? false;
}
+ )
+ .map((entry) => {
+ return entry.revision;
+ });
+};
+
+export const removeBrowser = async (
+ revision: string,
+ folderPath: string
+): Promise<void> => {
+ assert(
+ await existsAsync(folderPath),
+ `Failed to remove: revision ${revision} is not downloaded`
+ );
+ deleteDirectory(folderPath);
+};
+
+export const getFolderPath = (
+ revision: string,
+ downloadsFolder: string,
+ platform: Platform
+): string => {
+ return path.resolve(downloadsFolder, `${platform}-${revision}`);
+};
+
+export const getRevisionInfo = (
+ revision: string,
+ product: Product,
+ folderPath: string,
+ platform: Platform
+): BrowserFetcherRevisionInfo => {
+ let executablePath = '';
+ if (product === 'chrome') {
+ if (platform === 'mac' || platform === 'mac_arm') {
+ executablePath = path.join(
+ folderPath,
+ archiveName(product, platform, revision),
+ 'Chromium.app',
+ 'Contents',
+ 'MacOS',
+ 'Chromium'
+ );
+ } else if (platform === 'linux') {
+ executablePath = path.join(
+ folderPath,
+ archiveName(product, platform, revision),
+ 'chrome'
+ );
+ } else if (platform === 'win32' || platform === 'win64') {
+ executablePath = path.join(
+ folderPath,
+ archiveName(product, platform, revision),
+ 'thorium.exe'
+ );
} else {
- throw new Error('Unsupported product: ' + this.#product);
+ throw new Error('Unsupported platform: ' + platform);
}
-
- const url = _downloadURL(
- this.#product,
- this.#platform,
- this.#downloadHost,
- revision
- );
- const local = fs.existsSync(folderPath);
- return {
- revision,
- executablePath,
- folderPath,
- local,
- url,
- product: this.#product,
- };
+ } else if (product === 'firefox') {
+ if (platform === 'mac' || platform === 'mac_arm') {
+ executablePath = path.join(
+ folderPath,
+ 'Firefox Nightly.app',
+ 'Contents',
+ 'MacOS',
+ 'firefox'
+ );
+ } else if (platform === 'linux') {
+ executablePath = path.join(folderPath, 'firefox', 'firefox');
+ } else if (platform === 'win32' || platform === 'win64') {
+ executablePath = path.join(folderPath, 'firefox', 'firefox.exe');
+ } else {
+ throw new Error('Unsupported platform: ' + platform);
+ }
+ } else {
+ throw new Error('Unsupported product: ' + product);
}
- #getFolderPath(revision: string): string {
- return path.resolve(this.#downloadsFolder, `${this.#platform}-${revision}`);
- }
-}
+ const url = _downloadURL(
+ product,
+ platform,
+ getDownloadHost(product),
+ revision
+ );
+ const local = fs.existsSync(folderPath);
+ return {
+ revision,
+ executablePath,
+ folderPath,
+ local,
+ url,
+ product,
+ };
+};
function parseFolderPath(
product: Product,
diff --git a/packages/renderer/src/browser/Launcher.ts b/packages/renderer/src/browser/Launcher.ts
index 736da7ccea..cb51fcc1fc 100644
--- a/packages/renderer/src/browser/Launcher.ts
+++ b/packages/renderer/src/browser/Launcher.ts
@@ -19,11 +19,16 @@ import * as path from 'path';
import {assert} from './assert';
import {Browser} from './Browser';
-import {BrowserFetcher} from './BrowserFetcher';
import {BrowserRunner} from './BrowserRunner';
import type {PuppeteerNodeLaunchOptions} from './LaunchOptions';
+import {
+ getDownloadsFolder,
+ getFolderPath,
+ getPlatform,
+ getRevisionInfo,
+} from './BrowserFetcher';
import type {Product} from './Product';
const tmpDir = () => {
@@ -149,13 +154,16 @@ function resolveExecutablePath(launcher: ChromeLauncher): {
} {
const {product, _preferredRevision} = launcher;
- const browserFetcher = new BrowserFetcher({
- product,
- path: null,
- platform: null,
- });
-
- const revisionInfo = browserFetcher.revisionInfo(_preferredRevision);
+ const revisionInfo = getRevisionInfo(
+ _preferredRevision,
+ 'chrome',
+ getFolderPath(
+ _preferredRevision,
+ getDownloadsFolder(product),
+ getPlatform(product)
+ ),
+ getPlatform(product)
+ );
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 (${launcher._preferredRevision}).`;
diff --git a/packages/renderer/src/browser/PuppeteerNode.ts b/packages/renderer/src/browser/PuppeteerNode.ts
index c978cf573f..992274ec54 100644
--- a/packages/renderer/src/browser/PuppeteerNode.ts
+++ b/packages/renderer/src/browser/PuppeteerNode.ts
@@ -16,8 +16,6 @@
import type {Browser} from './Browser';
import type {BrowserConnectOptions} from './BrowserConnector';
-import type {BrowserFetcherOptions} from './BrowserFetcher';
-import {BrowserFetcher} from './BrowserFetcher';
import type {ProductLauncher} from './Launcher';
import {ChromeLauncher} from './Launcher';
import type {
@@ -48,7 +46,6 @@ export class PuppeteerNode {
this.launch = this.launch.bind(this);
this.executablePath = this.executablePath.bind(this);
- this.createBrowserFetcher = this.createBrowserFetcher.bind(this);
}
launch(options: PuppeteerLaunchOptions): Promise<Browser> {
@@ -83,8 +80,4 @@ export class PuppeteerNode {
get product(): string {
return this._launcher.product;
}
-
- createBrowserFetcher(options: BrowserFetcherOptions): BrowserFetcher {
- return new BrowserFetcher(options);
- }
}
diff --git a/packages/renderer/src/browser/create-browser-fetcher.ts b/packages/renderer/src/browser/create-browser-fetcher.ts
index 03bf63011d..5646fa76a1 100644
--- a/packages/renderer/src/browser/create-browser-fetcher.ts
+++ b/packages/renderer/src/browser/create-browser-fetcher.ts
@@ -14,7 +14,16 @@
* limitations under the License.
*/
-import {puppeteer} from './node';
+import {
+ download,
+ getDownloadHost,
+ getDownloadsFolder,
+ getFolderPath,
+ getPlatform,
+ getRevisionInfo,
+ localRevisions,
+ removeBrowser,
+} from './BrowserFetcher';
import type {Product} from './Product';
import {PUPPETEER_REVISIONS} from './revisions';
@@ -23,25 +32,29 @@ const supportedProducts = {
firefox: 'Firefox Nightly',
} as const;
-export async function downloadBrowser(product: Product): Promise<void> {
- const browserFetcher = puppeteer.createBrowserFetcher({
- product,
- path: null,
- platform: null,
- });
- const revision = await getRevision();
- await fetchBinary(revision);
+export function getRevision(product: Product): string {
+ if (product === 'chrome') {
+ return PUPPETEER_REVISIONS.chromium;
+ }
- function getRevision(): string {
- if (product === 'chrome') {
- return PUPPETEER_REVISIONS.chromium;
- }
+ throw new Error(`Unsupported product ${product}`);
+}
- throw new Error(`Unsupported product ${product}`);
- }
+export async function downloadBrowser(product: Product): Promise<void> {
+ const revision = getRevision(product);
+ await fetchBinary(revision);
function fetchBinary(_revision: string) {
- const revisionInfo = browserFetcher.revisionInfo(_revision);
+ const revisionInfo = getRevisionInfo(
+ _revision,
+ product,
+ getFolderPath(
+ _revision,
+ getDownloadsFolder(product),
+ getPlatform(product)
+ ),
+ getPlatform(product)
+ );
// Do nothing if the revision is already downloaded.
if (revisionInfo.local) {
@@ -59,7 +72,14 @@ export async function downloadBrowser(product: Product): Promise<void> {
return __revision !== revisionInfo.revision;
});
const cleanupOldVersions = localRevisions.map((__revision) => {
- return browserFetcher.remove(__revision);
+ return removeBrowser(
+ __revision,
+ getFolderPath(
+ _revision,
+ getDownloadsFolder(product),
+ getPlatform(product)
+ )
+ );
});
Promise.all([...cleanupOldVersions]);
}
@@ -80,10 +100,20 @@ export async function downloadBrowser(product: Product): Promise<void> {
);
}
- return browserFetcher
- .download(revisionInfo.revision, onProgress)
+ return download(
+ revisionInfo.revision,
+ onProgress,
+ product,
+ getPlatform(product),
+ getDownloadHost(product),
+ getDownloadsFolder(product)
+ )
.then(() => {
- return browserFetcher.localRevisions();
+ return localRevisions(
+ getDownloadsFolder(product),
+ product,
+ getPlatform(product)
+ );
})
.then(onSuccess)
.catch(onError);
diff --git a/packages/renderer/src/get-local-browser-executable.ts b/packages/renderer/src/get-local-browser-executable.ts
index 78a099b1fe..6aa3edaf9d 100644
--- a/packages/renderer/src/get-local-browser-executable.ts
+++ b/packages/renderer/src/get-local-browser-executable.ts
@@ -1,8 +1,13 @@
import fs from 'fs';
import type {Browser} from './browser';
import type {BrowserExecutable} from './browser-executable';
+import {
+ getDownloadsFolder,
+ getFolderPath,
+ getPlatform,
+ getRevisionInfo,
+} from './browser/BrowserFetcher';
import {downloadBrowser} from './browser/create-browser-fetcher';
-import {puppeteer} from './browser/node';
import type {Product} from './browser/Product';
import {PUPPETEER_REVISIONS} from './browser/revisions';
@@ -48,13 +53,17 @@ const getLocalBrowser = (product: Product) => {
};
const getBrowserRevision = (product: Product) => {
- const browserFetcher = puppeteer.createBrowserFetcher({
+ const platform = getPlatform(product);
+
+ const revisionInfo = getRevisionInfo(
+ PUPPETEER_REVISIONS.chromium,
product,
- path: null,
- platform: null,
- });
- const revisionInfo = browserFetcher.revisionInfo(
- PUPPETEER_REVISIONS.chromium
+ getFolderPath(
+ PUPPETEER_REVISIONS.chromium,
+ getDownloadsFolder(product),
+ platform
+ ),
+ platform
);
return revisionInfo;