Repository navigation
fix: support geckodriver download mirrors #1341
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,104 @@ | ||
| import { download as downloadGeckoDriver } from "geckodriver"; | ||
| import fs from "fs-extra"; | ||
| import path from "path"; | ||
| import { execFile } from "child_process"; | ||
| import { promisify } from "util"; | ||
| import { Readable } from "stream"; | ||
| import { pipeline } from "stream/promises"; | ||
| import { BrowserPlatform } from "@puppeteer/browsers"; | ||
| import { GECKODRIVER_CARGO_TOML } from "../constants"; | ||
| import registry from "../registry"; | ||
| import { DriverName, browserInstallerDebug, getBrowserPlatform, getGeckoDriverDir, retryFetch } from "../utils"; | ||
| import { | ||
| DriverName, | ||
| browserInstallerDebug, | ||
| getBrowserPlatform, | ||
| getGeckoDriverDir, | ||
| retryFetch, | ||
| unzipFile, | ||
| } from "../utils"; | ||
| import type { BrowserDownloadMirrors } from "../../config/types"; | ||
| import { getBrowserDownloadMirror, getBrowserDownloadMirrorFileUrl } from "../mirrors"; | ||
|
|
||
| const getLatestGeckoDriverVersion = async (): Promise<string> => { | ||
| const cargoVersionsToml = await retryFetch(GECKODRIVER_CARGO_TOML).then(res => res.text()); | ||
| const version = cargoVersionsToml.split("\n").find(line => line.startsWith("version = ")); | ||
| const execFileAsync = promisify(execFile); | ||
| const GECKODRIVER_PLATFORMS = { | ||
| [BrowserPlatform.LINUX]: "linux64", | ||
| [BrowserPlatform.LINUX_ARM]: "linux-aarch64", | ||
| [BrowserPlatform.MAC]: "macos", | ||
| [BrowserPlatform.MAC_ARM]: "macos-aarch64", | ||
| [BrowserPlatform.WIN32]: "win32", | ||
| [BrowserPlatform.WIN64]: "win64", | ||
| }; | ||
|
|
||
| const getLatestGeckoDriverVersion = async (mirror?: string): Promise<string> => { | ||
| const url = mirror ? getBrowserDownloadMirrorFileUrl(mirror, "Cargo.toml") : GECKODRIVER_CARGO_TOML; | ||
| const response = await retryFetch(url); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`Couldn't resolve latest geckodriver version from ${url}: ${response.status}`); | ||
| } | ||
|
|
||
| const cargoVersionsToml = await response.text(); | ||
| const versionLine = cargoVersionsToml.split("\n").find(line => /^version\s*=/.test(line)); | ||
| const version = versionLine?.match(/^version\s*=\s*["'](\d+\.\d+\.\d+)["']/); | ||
|
Comment on lines
+41
to
+42
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switched to using explicit regexp matching |
||
|
|
||
| if (!version) { | ||
| throw new Error("Couldn't resolve latest geckodriver version while downloading geckodriver"); | ||
| } | ||
|
|
||
| const latestGeckoVersion = version.split(" = ").pop()!.slice(1, -1); | ||
| const latestGeckoVersion = version[1]; | ||
|
|
||
| browserInstallerDebug(`resolved latest geckodriver version: ${latestGeckoVersion}`); | ||
|
|
||
| return latestGeckoVersion; | ||
| }; | ||
|
|
||
| export const installLatestGeckoDriver = async (firefoxVersion: string, { force = false } = {}): Promise<string> => { | ||
| const downloadMirroredGeckoDriver = async ( | ||
| version: string, | ||
| platform: BrowserPlatform, | ||
| mirror: string, | ||
| ): Promise<string> => { | ||
| const isWindows = platform === BrowserPlatform.WIN32 || platform === BrowserPlatform.WIN64; | ||
| const filename = `geckodriver-v${version}-${GECKODRIVER_PLATFORMS[platform]}${isWindows ? ".zip" : ".tar.gz"}`; | ||
|
Comment on lines
+60
to
+61
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like every other mirror packs binaries into ".zip" for windows and ".tar.gz" for other platforms |
||
| const url = getBrowserDownloadMirrorFileUrl(mirror, `v${version}/${filename}`); | ||
|
|
||
| browserInstallerDebug(`downloading ${DriverName.GECKODRIVER}@${version} from mirror ${mirror}`); | ||
|
|
||
| const response = await retryFetch(url); | ||
|
|
||
| if (!response.ok || !response.body) { | ||
| throw new Error(`Unable to download geckodriver from ${url}: ${response.status}`); | ||
| } | ||
|
|
||
| const driverDir = getGeckoDriverDir(version); | ||
| await fs.ensureDir(driverDir); | ||
| const downloadDir = await fs.mkdtemp(path.join(driverDir, "download-")); | ||
|
|
||
| try { | ||
| const archivePath = path.join(downloadDir, filename); | ||
| await pipeline(Readable.fromWeb(response.body as never), fs.createWriteStream(archivePath)); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For chromedriver and chrome, we can use puppeteer's mirror feature |
||
|
|
||
| if (isWindows) { | ||
| await unzipFile(archivePath, driverDir); | ||
| } else { | ||
| await execFileAsync("tar", ["-xzf", archivePath, "-C", driverDir]); | ||
| } | ||
|
|
||
| const binaryPath = path.join(driverDir, isWindows ? "geckodriver.exe" : "geckodriver"); | ||
| await fs.chmod(binaryPath, 0o755); | ||
|
|
||
| return binaryPath; | ||
| } finally { | ||
| await fs.remove(downloadDir); | ||
| } | ||
| }; | ||
|
|
||
| export const installLatestGeckoDriver = async ( | ||
| firefoxVersion: string, | ||
| { | ||
| force = false, | ||
| browserDownloadMirrors, | ||
| }: { force?: boolean; browserDownloadMirrors?: BrowserDownloadMirrors } = {}, | ||
| ): Promise<string> => { | ||
| const platform = getBrowserPlatform(); | ||
| const existingLocallyDriverVersion = registry.getMatchedDriverVersion( | ||
| DriverName.GECKODRIVER, | ||
|
|
@@ -34,9 +114,13 @@ export const installLatestGeckoDriver = async (firefoxVersion: string, { force = | |
| return registry.getBinaryPath(DriverName.GECKODRIVER, platform, existingLocallyDriverVersion); | ||
| } | ||
|
|
||
| const latestVersion = await getLatestGeckoDriverVersion(); | ||
| const mirror = getBrowserDownloadMirror(DriverName.GECKODRIVER, browserDownloadMirrors); | ||
| const latestVersion = await getLatestGeckoDriverVersion(mirror); | ||
|
|
||
| const installFn = (): Promise<string> => downloadGeckoDriver(latestVersion, getGeckoDriverDir(latestVersion)); | ||
| const installFn = (): Promise<string> => | ||
| mirror | ||
| ? downloadMirroredGeckoDriver(latestVersion, platform, mirror) | ||
| : downloadGeckoDriver(latestVersion, getGeckoDriverDir(latestVersion)); | ||
|
|
||
| return registry.installBinary(DriverName.GECKODRIVER, platform, latestVersion, installFn); | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Despite our mirror only has linux64 geckodriver, it sounds right to implement other platforms support for the future, in case there would be a mirror with other platforms support