refactor: rename fetch->install, launcher->launch (#9985)

This commit is contained in:
Alex Rudenko 2023-04-06 13:23:28 +02:00 committed by GitHub
parent e7265c9aa9
commit 24bd05877d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 47 additions and 47 deletions

View File

@ -30,12 +30,12 @@ import {
} from './browser-data/browser-data.js'; } from './browser-data/browser-data.js';
import {Cache} from './Cache.js'; import {Cache} from './Cache.js';
import {detectBrowserPlatform} from './detectPlatform.js'; import {detectBrowserPlatform} from './detectPlatform.js';
import {fetch} from './fetch.js'; import {install} from './install.js';
import { import {
computeExecutablePath, computeExecutablePath,
computeSystemExecutablePath, computeSystemExecutablePath,
launch, launch,
} from './launcher.js'; } from './launch.js';
type InstallArgs = { type InstallArgs = {
browser: { browser: {
@ -157,7 +157,7 @@ export class CLI {
args.platform, args.platform,
args.browser.buildId args.browser.buildId
); );
await fetch({ await install({
browser: args.browser.name, browser: args.browser.name,
buildId: args.browser.buildId, buildId: args.browser.buildId,
platform: args.platform, platform: args.platform,

View File

@ -31,7 +31,7 @@ import {detectBrowserPlatform} from './detectPlatform.js';
import {unpackArchive} from './fileUtil.js'; import {unpackArchive} from './fileUtil.js';
import {downloadFile, headHttpRequest} from './httpUtil.js'; import {downloadFile, headHttpRequest} from './httpUtil.js';
const debugFetch = debug('puppeteer:browsers:fetcher'); const debugInstall = debug('puppeteer:browsers:install');
const times = new Map<string, [number, number]>(); const times = new Map<string, [number, number]>();
function debugTime(label: string) { function debugTime(label: string) {
@ -46,7 +46,7 @@ function debugTimeEnd(label: string) {
} }
const duration = const duration =
end[0] * 1000 + end[1] / 1e6 - (start[0] * 1000 + start[1] / 1e6); // calculate duration in milliseconds end[0] * 1000 + end[1] / 1e6 - (start[0] * 1000 + start[1] / 1e6); // calculate duration in milliseconds
debugFetch(`Duration for ${label}: ${duration}ms`); debugInstall(`Duration for ${label}: ${duration}ms`);
} }
/** /**
@ -64,7 +64,7 @@ export interface Options {
*/ */
platform?: BrowserPlatform; platform?: BrowserPlatform;
/** /**
* Determines which browser to fetch. * Determines which browser to install.
*/ */
browser: Browser; browser: Browser;
/** /**
@ -94,7 +94,7 @@ export interface Options {
* *
* @defaultValue `true` * @defaultValue `true`
*/ */
install?: boolean; unpack?: boolean;
} }
export type InstalledBrowser = { export type InstalledBrowser = {
@ -104,9 +104,9 @@ export type InstalledBrowser = {
platform: BrowserPlatform; platform: BrowserPlatform;
}; };
export async function fetch(options: Options): Promise<InstalledBrowser> { export async function install(options: Options): Promise<InstalledBrowser> {
options.platform ??= detectBrowserPlatform(); options.platform ??= detectBrowserPlatform();
options.install ??= true; options.unpack ??= true;
if (!options.platform) { if (!options.platform) {
throw new Error( throw new Error(
`Cannot download a binary for the provided platform: ${os.platform()} (${os.arch()})` `Cannot download a binary for the provided platform: ${os.platform()} (${os.arch()})`
@ -127,7 +127,7 @@ export async function fetch(options: Options): Promise<InstalledBrowser> {
await mkdir(browserRoot, {recursive: true}); await mkdir(browserRoot, {recursive: true});
} }
if (!options.install) { if (!options.unpack) {
if (existsSync(archivePath)) { if (existsSync(archivePath)) {
return { return {
path: archivePath, path: archivePath,
@ -136,7 +136,7 @@ export async function fetch(options: Options): Promise<InstalledBrowser> {
buildId: options.buildId, buildId: options.buildId,
}; };
} }
debugFetch(`Downloading binary from ${url}`); debugInstall(`Downloading binary from ${url}`);
debugTime('download'); debugTime('download');
await downloadFile(url, archivePath, options.downloadProgressCallback); await downloadFile(url, archivePath, options.downloadProgressCallback);
debugTimeEnd('download'); debugTimeEnd('download');
@ -162,7 +162,7 @@ export async function fetch(options: Options): Promise<InstalledBrowser> {
}; };
} }
try { try {
debugFetch(`Downloading binary from ${url}`); debugInstall(`Downloading binary from ${url}`);
try { try {
debugTime('download'); debugTime('download');
await downloadFile(url, archivePath, options.downloadProgressCallback); await downloadFile(url, archivePath, options.downloadProgressCallback);
@ -170,7 +170,7 @@ export async function fetch(options: Options): Promise<InstalledBrowser> {
debugTimeEnd('download'); debugTimeEnd('download');
} }
debugFetch(`Installing ${archivePath} to ${outputPath}`); debugInstall(`Installing ${archivePath} to ${outputPath}`);
try { try {
debugTime('extract'); debugTime('extract');
await unpackArchive(archivePath, outputPath); await unpackArchive(archivePath, outputPath);
@ -178,7 +178,7 @@ export async function fetch(options: Options): Promise<InstalledBrowser> {
debugTimeEnd('extract'); debugTimeEnd('extract');
} }
} catch (err) { } catch (err) {
debugFetch(`Error during installation`, err); debugInstall(`Error during installation`, err);
} finally { } finally {
if (existsSync(archivePath)) { if (existsSync(archivePath)) {
await unlink(archivePath); await unlink(archivePath);
@ -192,7 +192,7 @@ export async function fetch(options: Options): Promise<InstalledBrowser> {
}; };
} }
export async function canFetch(options: Options): Promise<boolean> { export async function canDownload(options: Options): Promise<boolean> {
options.platform ??= detectBrowserPlatform(); options.platform ??= detectBrowserPlatform();
if (!options.platform) { if (!options.platform) {
throw new Error( throw new Error(

View File

@ -48,7 +48,7 @@ export interface Options {
*/ */
platform?: BrowserPlatform; platform?: BrowserPlatform;
/** /**
* Determines which browser to fetch. * Determines which browser to launch.
*/ */
browser: Browser; browser: Browser;
/** /**
@ -87,7 +87,7 @@ export interface SystemOptions {
*/ */
platform?: BrowserPlatform; platform?: BrowserPlatform;
/** /**
* Determines which browser to fetch. * Determines which browser to launch.
*/ */
browser: Browser; browser: Browser;
/** /**

View File

@ -21,8 +21,8 @@ export {
TimeoutError, TimeoutError,
CDP_WEBSOCKET_ENDPOINT_REGEX, CDP_WEBSOCKET_ENDPOINT_REGEX,
WEBDRIVER_BIDI_WEBSOCKET_ENDPOINT_REGEX, WEBDRIVER_BIDI_WEBSOCKET_ENDPOINT_REGEX,
} from './launcher.js'; } from './launch.js';
export {fetch, canFetch} from './fetch.js'; export {install, canDownload} from './install.js';
export {detectBrowserPlatform} from './detectPlatform.js'; export {detectBrowserPlatform} from './detectPlatform.js';
export { export {
resolveBuildId, resolveBuildId,

View File

@ -22,8 +22,8 @@ import os from 'os';
import path from 'path'; import path from 'path';
import { import {
fetch, install,
canFetch, canDownload,
Browser, Browser,
BrowserPlatform, BrowserPlatform,
Cache, Cache,
@ -35,7 +35,7 @@ import {testChromeBuildId} from '../versions.js';
* Tests in this spec use real download URLs and unpack live browser archives * Tests in this spec use real download URLs and unpack live browser archives
* so it requires the network access. * so it requires the network access.
*/ */
describe('Chrome fetch', () => { describe('Chrome install', () => {
setupTestServer(); setupTestServer();
let tmpDir = '/tmp/puppeteer-browsers-test'; let tmpDir = '/tmp/puppeteer-browsers-test';
@ -50,7 +50,7 @@ describe('Chrome fetch', () => {
it('should check if a buildId can be downloaded', async () => { it('should check if a buildId can be downloaded', async () => {
assert.ok( assert.ok(
await canFetch({ await canDownload({
cacheDir: tmpDir, cacheDir: tmpDir,
browser: Browser.CHROME, browser: Browser.CHROME,
platform: BrowserPlatform.LINUX, platform: BrowserPlatform.LINUX,
@ -62,7 +62,7 @@ describe('Chrome fetch', () => {
it('should report if a buildId is not downloadable', async () => { it('should report if a buildId is not downloadable', async () => {
assert.strictEqual( assert.strictEqual(
await canFetch({ await canDownload({
cacheDir: tmpDir, cacheDir: tmpDir,
browser: Browser.CHROME, browser: Browser.CHROME,
platform: BrowserPlatform.LINUX, platform: BrowserPlatform.LINUX,
@ -81,7 +81,7 @@ describe('Chrome fetch', () => {
`${BrowserPlatform.LINUX}-${testChromeBuildId}` `${BrowserPlatform.LINUX}-${testChromeBuildId}`
); );
assert.strictEqual(fs.existsSync(expectedOutputPath), false); assert.strictEqual(fs.existsSync(expectedOutputPath), false);
let browser = await fetch({ let browser = await install({
cacheDir: tmpDir, cacheDir: tmpDir,
browser: Browser.CHROME, browser: Browser.CHROME,
platform: BrowserPlatform.LINUX, platform: BrowserPlatform.LINUX,
@ -91,7 +91,7 @@ describe('Chrome fetch', () => {
assert.strictEqual(browser.path, expectedOutputPath); assert.strictEqual(browser.path, expectedOutputPath);
assert.ok(fs.existsSync(expectedOutputPath)); assert.ok(fs.existsSync(expectedOutputPath));
// Second iteration should be no-op. // Second iteration should be no-op.
browser = await fetch({ browser = await install({
cacheDir: tmpDir, cacheDir: tmpDir,
browser: Browser.CHROME, browser: Browser.CHROME,
platform: BrowserPlatform.LINUX, platform: BrowserPlatform.LINUX,
@ -159,9 +159,9 @@ describe('Chrome fetch', () => {
delete process.env['HTTPS_PROXY']; delete process.env['HTTPS_PROXY'];
}); });
it('can send canFetch requests via a proxy', async () => { it('can send canDownload requests via a proxy', async () => {
assert.strictEqual( assert.strictEqual(
await canFetch({ await canDownload({
cacheDir: tmpDir, cacheDir: tmpDir,
browser: Browser.CHROME, browser: Browser.CHROME,
platform: BrowserPlatform.LINUX, platform: BrowserPlatform.LINUX,
@ -175,7 +175,7 @@ describe('Chrome fetch', () => {
]); ]);
}); });
it('can fetch via a proxy', async function () { it('can download via a proxy', async function () {
this.timeout(120000); this.timeout(120000);
const expectedOutputPath = path.join( const expectedOutputPath = path.join(
tmpDir, tmpDir,
@ -183,7 +183,7 @@ describe('Chrome fetch', () => {
`${BrowserPlatform.LINUX}-${testChromeBuildId}` `${BrowserPlatform.LINUX}-${testChromeBuildId}`
); );
assert.strictEqual(fs.existsSync(expectedOutputPath), false); assert.strictEqual(fs.existsSync(expectedOutputPath), false);
const browser = await fetch({ const browser = await install({
cacheDir: tmpDir, cacheDir: tmpDir,
browser: Browser.CHROME, browser: Browser.CHROME,
platform: BrowserPlatform.LINUX, platform: BrowserPlatform.LINUX,

View File

@ -23,7 +23,7 @@ import {
CDP_WEBSOCKET_ENDPOINT_REGEX, CDP_WEBSOCKET_ENDPOINT_REGEX,
computeExecutablePath, computeExecutablePath,
launch, launch,
fetch, install,
Browser, Browser,
BrowserPlatform, BrowserPlatform,
} from '../../../lib/cjs/main.js'; } from '../../../lib/cjs/main.js';
@ -54,7 +54,7 @@ describe('Chrome', () => {
tmpDir = fs.mkdtempSync( tmpDir = fs.mkdtempSync(
path.join(os.tmpdir(), 'puppeteer-browsers-test') path.join(os.tmpdir(), 'puppeteer-browsers-test')
); );
await fetch({ await install({
cacheDir: tmpDir, cacheDir: tmpDir,
browser: Browser.CHROME, browser: Browser.CHROME,
buildId: testChromeBuildId, buildId: testChromeBuildId,

View File

@ -23,7 +23,7 @@ import {
CDP_WEBSOCKET_ENDPOINT_REGEX, CDP_WEBSOCKET_ENDPOINT_REGEX,
computeExecutablePath, computeExecutablePath,
launch, launch,
fetch, install,
Browser, Browser,
BrowserPlatform, BrowserPlatform,
} from '../../../lib/cjs/main.js'; } from '../../../lib/cjs/main.js';
@ -54,7 +54,7 @@ describe('Chromium', () => {
tmpDir = fs.mkdtempSync( tmpDir = fs.mkdtempSync(
path.join(os.tmpdir(), 'puppeteer-browsers-test') path.join(os.tmpdir(), 'puppeteer-browsers-test')
); );
await fetch({ await install({
cacheDir: tmpDir, cacheDir: tmpDir,
browser: Browser.CHROMIUM, browser: Browser.CHROMIUM,
buildId: testChromiumBuildId, buildId: testChromiumBuildId,

View File

@ -19,7 +19,7 @@ import fs from 'fs';
import os from 'os'; import os from 'os';
import path from 'path'; import path from 'path';
import {fetch, Browser, BrowserPlatform} from '../../../lib/cjs/main.js'; import {install, Browser, BrowserPlatform} from '../../../lib/cjs/main.js';
import {setupTestServer, getServerUrl, clearCache} from '../utils.js'; import {setupTestServer, getServerUrl, clearCache} from '../utils.js';
import {testFirefoxBuildId} from '../versions.js'; import {testFirefoxBuildId} from '../versions.js';
@ -27,7 +27,7 @@ import {testFirefoxBuildId} from '../versions.js';
* Tests in this spec use real download URLs and unpack live browser archives * Tests in this spec use real download URLs and unpack live browser archives
* so it requires the network access. * so it requires the network access.
*/ */
describe('Firefox fetch', () => { describe('Firefox install', () => {
setupTestServer(); setupTestServer();
let tmpDir = '/tmp/puppeteer-browsers-test'; let tmpDir = '/tmp/puppeteer-browsers-test';
@ -48,7 +48,7 @@ describe('Firefox fetch', () => {
`${BrowserPlatform.LINUX}-${testFirefoxBuildId}` `${BrowserPlatform.LINUX}-${testFirefoxBuildId}`
); );
assert.strictEqual(fs.existsSync(expectedOutputPath), false); assert.strictEqual(fs.existsSync(expectedOutputPath), false);
const browser = await fetch({ const browser = await install({
cacheDir: tmpDir, cacheDir: tmpDir,
browser: Browser.FIREFOX, browser: Browser.FIREFOX,
platform: BrowserPlatform.LINUX, platform: BrowserPlatform.LINUX,
@ -59,7 +59,7 @@ describe('Firefox fetch', () => {
assert.ok(fs.existsSync(expectedOutputPath)); assert.ok(fs.existsSync(expectedOutputPath));
}); });
// Fetch relies on the `hdiutil` utility on MacOS. // install relies on the `hdiutil` utility on MacOS.
// The utility is not available on other platforms. // The utility is not available on other platforms.
(os.platform() === 'darwin' ? it : it.skip)( (os.platform() === 'darwin' ? it : it.skip)(
'should download a buildId that is a dmg archive', 'should download a buildId that is a dmg archive',
@ -71,7 +71,7 @@ describe('Firefox fetch', () => {
`${BrowserPlatform.MAC}-${testFirefoxBuildId}` `${BrowserPlatform.MAC}-${testFirefoxBuildId}`
); );
assert.strictEqual(fs.existsSync(expectedOutputPath), false); assert.strictEqual(fs.existsSync(expectedOutputPath), false);
const browser = await fetch({ const browser = await install({
cacheDir: tmpDir, cacheDir: tmpDir,
browser: Browser.FIREFOX, browser: Browser.FIREFOX,
platform: BrowserPlatform.MAC, platform: BrowserPlatform.MAC,

View File

@ -22,7 +22,7 @@ import path from 'path';
import { import {
computeExecutablePath, computeExecutablePath,
launch, launch,
fetch, install,
Browser, Browser,
BrowserPlatform, BrowserPlatform,
createProfile, createProfile,
@ -54,7 +54,7 @@ describe('Firefox', () => {
tmpDir = fs.mkdtempSync( tmpDir = fs.mkdtempSync(
path.join(os.tmpdir(), 'puppeteer-browsers-test') path.join(os.tmpdir(), 'puppeteer-browsers-test')
); );
await fetch({ await install({
cacheDir: tmpDir, cacheDir: tmpDir,
browser: Browser.FIREFOX, browser: Browser.FIREFOX,
buildId: testFirefoxBuildId, buildId: testFirefoxBuildId,

View File

@ -22,7 +22,7 @@ import {Writable, Readable} from 'stream';
import {TestServer} from '@pptr/testserver'; import {TestServer} from '@pptr/testserver';
import {isErrorLike} from '../../lib/cjs/launcher.js'; import {isErrorLike} from '../../lib/cjs/launch.js';
import {Cache} from '../../lib/cjs/main.js'; import {Cache} from '../../lib/cjs/main.js';
export function createMockedReadlineInterface( export function createMockedReadlineInterface(

View File

@ -19,7 +19,7 @@
* mirrors the structure of the download server. * mirrors the structure of the download server.
*/ */
import {BrowserPlatform, fetch} from '@puppeteer/browsers'; import {BrowserPlatform, install} from '@puppeteer/browsers';
import path from 'path'; import path from 'path';
import fs from 'fs'; import fs from 'fs';
@ -59,12 +59,12 @@ for (const version of Object.keys(versions)) {
continue; continue;
} }
const result = await fetch({ const result = await install({
browser, browser,
buildId, buildId,
platform, platform,
cacheDir: path.join(cacheDir, 'tmp'), cacheDir: path.join(cacheDir, 'tmp'),
install: false, unpack: false,
}); });
fs.mkdirSync(path.dirname(targetPath), { fs.mkdirSync(path.dirname(targetPath), {

View File

@ -15,7 +15,7 @@
*/ */
import { import {
fetch, install,
Browser, Browser,
resolveBuildId, resolveBuildId,
makeProgressCallback, makeProgressCallback,
@ -88,7 +88,7 @@ export async function downloadBrowser(): Promise<void> {
const buildId = await resolveBuildId(browser, platform, unresolvedBuildId); const buildId = await resolveBuildId(browser, platform, unresolvedBuildId);
try { try {
const result = await fetch({ const result = await install({
browser, browser,
cacheDir: configuration.cacheDirectory!, cacheDir: configuration.cacheDirectory!,
platform, platform,