Skip to content

Instantly share code, notes, and snippets.

@prescience-data
Created December 8, 2021 00:20
Show Gist options
  • Save prescience-data/c7b2f1c6e6b463a732849bffc0e04428 to your computer and use it in GitHub Desktop.
Save prescience-data/c7b2f1c6e6b463a732849bffc0e04428 to your computer and use it in GitHub Desktop.
Download most recent Chrome build
import { createWriteStream, ensureDir } from "fs-extra"
import got from "got"
import { Stream } from "stream"
export type Platform =
| "Win_x64"
| "Win"
| "Mac"
| "Mac_Arm"
| "Linux_x64"
| "Android" // etc
export interface BuildParams {
platform: Platform
}
export interface SnapshotParams {
platform: Platform
build: string | number
filename: string
}
export interface DownloadParams {
dest: string
platform: Platform
build?: string | number
}
/**
* Define the base Google storage url.
* @type {string}
*/
const BASE_URL: string = `https://www.googleapis.com/download/storage/v1/b/chromium-browser-snapshots/o`
/**
* Prepares a valid snapshot filename from platform.
*
* @param {Platform} platform
* @return {string}
*/
const getSnapshotFilename = (platform: Platform): string =>
`chrome-${platform.split(`_`)[0].toLowerCase()}.zip`
/**
* Returns the most current build version for a specific platform.
*
* @param {"Win_x64" | "Win" | "Mac" | "Mac_Arm" | "Linux_x64" | "Android"} platform
* @return {Promise<string>}
*/
export const fetchBuild = async ({ platform }: BuildParams): Promise<string> =>
got(`${BASE_URL}/${platform}%2FLAST_CHANGE?generation=0&alt=media`).text()
/**
* Begins streaming the snapshot archive file.
*
* @param {"Win_x64" | "Win" | "Mac" | "Mac_Arm" | "Linux_x64" | "Android"} platform
* @param {string | number} build
* @param {string} filename
* @return {Stream}
*/
export const fetchSnapshot = ({
platform,
build,
filename
}: SnapshotParams): Stream =>
got.stream(
`${BASE_URL}/${platform}%2F${build}%2F${filename}?generation=0&alt=media`
)
/**
* Primary entrypoint to begin a browser download.
* Must define a target destination and platform.
* Browser build version can optionally be provided, or falls back to most current.
* Returns the path to the downloaded file, or rejects with an error.
*
* @param {string} dest
* @param {"Win_x64" | "Win" | "Mac" | "Mac_Arm" | "Linux_x64" | "Android"} platform
* @param {string | number | undefined} build
* @return {Promise<string>}
*/
export const downloadBrowser = async ({
dest,
platform,
build
}: DownloadParams): Promise<string> => {
const params: SnapshotParams = {
platform,
build: build ?? (await fetchBuild({ platform })),
filename: getSnapshotFilename(platform)
}
const pathToSnapshot: string = `${dest}/${params.filename}`
await ensureDir(dest)
await new Promise((resolve, reject) => {
fetchSnapshot(params)
.on("error", reject)
.pipe(
createWriteStream(pathToSnapshot)
.on("error", reject)
.on("finish", resolve)
)
})
return pathToSnapshot
}
@clouedoc
Copy link

clouedoc commented Dec 8, 2021

😍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment