From 5941e3481c703daf85efaa1e083a9d8e04af2913 Mon Sep 17 00:00:00 2001 From: Akihiro Uchida Date: Fri, 1 Mar 2019 15:05:09 +0900 Subject: [PATCH 1/5] Add definitions for puppeteer BrowserFetcher --- types/puppeteer/index.d.ts | 28 ++++++++++++++++++++++++++++ types/puppeteer/puppeteer-tests.ts | 8 ++++++++ 2 files changed, 36 insertions(+) diff --git a/types/puppeteer/index.d.ts b/types/puppeteer/index.d.ts index 1b9428392a..a4bf6e0d73 100644 --- a/types/puppeteer/index.d.ts +++ b/types/puppeteer/index.d.ts @@ -2195,3 +2195,31 @@ 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?: LaunchOptions): BrowserFetcher; + +/** 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) => any): Promise; + localRevisions(): Promise; + platform(): string; + 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; +} diff --git a/types/puppeteer/puppeteer-tests.ts b/types/puppeteer/puppeteer-tests.ts index e5c287b6ee..e62be5b7d5 100644 --- a/types/puppeteer/puppeteer-tests.ts +++ b/types/puppeteer/puppeteer-tests.ts @@ -618,3 +618,11 @@ puppeteer.launch().then(async browser => { ); console.log('there are', numMatchingEls, 'banana paragaphs'); }); + +(async () => { + const rev = '630727'; + const browserFetcher = puppeteer.createBrowserFetcher(); + await browserFetcher.canDownload(rev); + const revisionInfo = await browserFetcher.download(rev); + await browserFetcher.remove(rev); +}); From 558d043fd0f9b3e247839d2a09a16accf4e21e09 Mon Sep 17 00:00:00 2001 From: Akihiro Uchida Date: Sat, 2 Mar 2019 14:24:36 +0900 Subject: [PATCH 2/5] put options to createBrowserFetcher test --- types/puppeteer/puppeteer-tests.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/types/puppeteer/puppeteer-tests.ts b/types/puppeteer/puppeteer-tests.ts index e62be5b7d5..81c23e5b5e 100644 --- a/types/puppeteer/puppeteer-tests.ts +++ b/types/puppeteer/puppeteer-tests.ts @@ -621,7 +621,11 @@ puppeteer.launch().then(async browser => { (async () => { const rev = '630727'; - const browserFetcher = puppeteer.createBrowserFetcher(); + const browserFetcher = puppeteer.createBrowserFetcher({ + host: 'https://storage.googleapis.com', + path: '/tmp/.local-chromium', + platform: 'linux', + }); await browserFetcher.canDownload(rev); const revisionInfo = await browserFetcher.download(rev); await browserFetcher.remove(rev); From efaf421de15617c280593b8a45168874896c391f Mon Sep 17 00:00:00 2001 From: Akihiro Uchida Date: Sat, 2 Mar 2019 14:24:47 +0900 Subject: [PATCH 3/5] fixes due to the review comments --- types/puppeteer/index.d.ts | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/types/puppeteer/index.d.ts b/types/puppeteer/index.d.ts index a4bf6e0d73..aacd54a9d0 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,26 +2189,14 @@ export interface CoverageEntry { ranges: Array<{start: number, end: number}>; } -/** Attaches Puppeteer to an existing Chromium instance */ -export function connect(options?: ConnectOptions): Promise; -/** The default flags that Chromium will be launched with */ -export function defaultArgs(options?: ChromeArgOptions): string[]; -/** Path where Puppeteer expects to find bundled Chromium */ -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?: LaunchOptions): BrowserFetcher; - /** 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) => any): Promise; + download(revision: string, progressCallback?: (downloadBytes: number, totalBytes: number) => void): Promise; localRevisions(): Promise; - platform(): string; + platform(): Platform; remove(revision: string): Promise; revisionInfo(revision: string): RevisionInfo; } @@ -2223,3 +2213,23 @@ export interface RevisionInfo { /** 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 */ +export function defaultArgs(options?: ChromeArgOptions): string[]; +/** Path where Puppeteer expects to find bundled Chromium */ +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; From 8057cda1792524316a88ff73ca9054dd97b2888d Mon Sep 17 00:00:00 2001 From: Akihiro Uchida Date: Sat, 2 Mar 2019 17:07:47 +0900 Subject: [PATCH 4/5] make optional properties in FetcherOptions --- types/puppeteer/index.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/types/puppeteer/index.d.ts b/types/puppeteer/index.d.ts index aacd54a9d0..e043587e16 100644 --- a/types/puppeteer/index.d.ts +++ b/types/puppeteer/index.d.ts @@ -2216,11 +2216,11 @@ export interface RevisionInfo { export interface FetcherOptions { /** A download host to be used. Defaults to `https://storage.googleapis.com`. */ - host: string; + host?: string; /** A path for the downloads folder. Defaults to `/.local-chromium`, where `` is puppeteer's package root. */ - path: string; + path?: string; /** Possible values are: `mac`, `win32`, `win64`, `linux`. Defaults to the current platform. */ - platform: Platform; + platform?: Platform; } /** Attaches Puppeteer to an existing Chromium instance */ From 4b389b4092cb2a915f75767823fdbad1947469fc Mon Sep 17 00:00:00 2001 From: Akihiro Uchida Date: Sat, 2 Mar 2019 17:13:58 +0900 Subject: [PATCH 5/5] add some tests to cover methods and properties --- types/puppeteer/puppeteer-tests.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/types/puppeteer/puppeteer-tests.ts b/types/puppeteer/puppeteer-tests.ts index 81c23e5b5e..90006c77d4 100644 --- a/types/puppeteer/puppeteer-tests.ts +++ b/types/puppeteer/puppeteer-tests.ts @@ -621,12 +621,25 @@ puppeteer.launch().then(async browser => { (async () => { const rev = '630727'; - const browserFetcher = puppeteer.createBrowserFetcher({ + const defaultFetcher = puppeteer.createBrowserFetcher(); + const options: puppeteer.FetcherOptions = { host: 'https://storage.googleapis.com', path: '/tmp/.local-chromium', platform: 'linux', - }); - await browserFetcher.canDownload(rev); - const revisionInfo = await browserFetcher.download(rev); - await browserFetcher.remove(rev); + }; + 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); + } });