diff --git a/types/puppeteer/index.d.ts b/types/puppeteer/index.d.ts index 1b9428392a..e043587e16 100644 --- a/types/puppeteer/index.d.ts +++ b/types/puppeteer/index.d.ts @@ -31,6 +31,8 @@ export interface JSONObject { } export type SerializableOrJSHandle = Serializable | JSHandle; +export type Platform = "mac" | "win32" | "win64" | "linux"; + /** Defines `$eval` and `$$eval` for Page, Frame and ElementHandle. */ export interface Evalable { /** @@ -2187,6 +2189,40 @@ export interface CoverageEntry { ranges: Array<{start: number, end: number}>; } +/** BrowserFetcher can download and manage different versions of Chromium. */ +export interface BrowserFetcher { + /** The method initiates a HEAD request to check if the revision is available. */ + canDownload(revision: string): Promise; + /** The method initiates a GET request to download the revision from the host. */ + download(revision: string, progressCallback?: (downloadBytes: number, totalBytes: number) => void): Promise; + localRevisions(): Promise; + platform(): Platform; + remove(revision: string): Promise; + revisionInfo(revision: string): RevisionInfo; +} + +export interface RevisionInfo { + /** The revision the info was created from */ + revision: string; + /** Path to the extracted revision folder */ + folderPath: string; + /** Path to the revision executable */ + executablePath: string; + /** URL this revision can be downloaded from */ + url: string; + /** whether the revision is locally available on disk */ + local: boolean; +} + +export interface FetcherOptions { + /** A download host to be used. Defaults to `https://storage.googleapis.com`. */ + host?: string; + /** A path for the downloads folder. Defaults to `/.local-chromium`, where `` is puppeteer's package root. */ + path?: string; + /** Possible values are: `mac`, `win32`, `win64`, `linux`. Defaults to the current platform. */ + platform?: Platform; +} + /** Attaches Puppeteer to an existing Chromium instance */ export function connect(options?: ConnectOptions): Promise; /** The default flags that Chromium will be launched with */ @@ -2195,3 +2231,5 @@ export function defaultArgs(options?: ChromeArgOptions): string[]; export function executablePath(): string; /** The method launches a browser instance with given arguments. The browser will be closed when the parent node.js process is closed. */ export function launch(options?: LaunchOptions): Promise; +/** This methods attaches Puppeteer to an existing Chromium instance. */ +export function createBrowserFetcher(options?: FetcherOptions): BrowserFetcher; diff --git a/types/puppeteer/puppeteer-tests.ts b/types/puppeteer/puppeteer-tests.ts index e5c287b6ee..90006c77d4 100644 --- a/types/puppeteer/puppeteer-tests.ts +++ b/types/puppeteer/puppeteer-tests.ts @@ -618,3 +618,28 @@ puppeteer.launch().then(async browser => { ); console.log('there are', numMatchingEls, 'banana paragaphs'); }); + +(async () => { + const rev = '630727'; + const defaultFetcher = puppeteer.createBrowserFetcher(); + const options: puppeteer.FetcherOptions = { + host: 'https://storage.googleapis.com', + path: '/tmp/.local-chromium', + platform: 'linux', + }; + const browserFetcher = puppeteer.createBrowserFetcher(options); + const canDownload = await browserFetcher.canDownload(rev); + if (canDownload) { + const revisionInfo = await browserFetcher.download(rev); + const localRevisions = await browserFetcher.localRevisions(); + const browser = await puppeteer.launch({executablePath: revisionInfo.executablePath}); + browser.close(); + if (localRevisions.includes(rev)) { + await browserFetcher.remove(rev); + } + await browserFetcher.download(rev, (download, total) => { + console.log('downloadBytes:', download, 'totalBytes:', total); + }); + await browserFetcher.remove(rev); + } +});