Node updated. Some todos.

This commit is contained in:
Norm Rasmussen
2024-09-23 20:52:09 -04:00
parent 8bfaca8375
commit f25622067f
2041 changed files with 124145 additions and 110445 deletions

View File

@ -5,21 +5,22 @@
*/
import assert from 'assert';
import {existsSync} from 'fs';
import {spawnSync} from 'child_process';
import {existsSync, readFileSync} from 'fs';
import {mkdir, unlink} from 'fs/promises';
import os from 'os';
import path from 'path';
import {
type Browser,
type BrowserPlatform,
Browser,
BrowserPlatform,
downloadUrls,
} from './browser-data/browser-data.js';
import {Cache, InstalledBrowser} from './Cache.js';
import {debug} from './debug.js';
import {detectBrowserPlatform} from './detectPlatform.js';
import {unpackArchive} from './fileUtil.js';
import {downloadFile, headHttpRequest} from './httpUtil.js';
import {downloadFile, getJSON, headHttpRequest} from './httpUtil.js';
const debugInstall = debug('puppeteer:browsers:install');
@ -62,6 +63,13 @@ export interface InstallOptions {
* binaries and they are used for caching.
*/
buildId: string;
/**
* An alias for the provided `buildId`. It will be used to maintain local
* metadata to support aliases in the `launch` command.
*
* @example 'canary'
*/
buildIdAlias?: string;
/**
* Provides information about the progress of the download.
*/
@ -74,7 +82,7 @@ export interface InstallOptions {
*
* @defaultValue Either
*
* - https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing or
* - https://storage.googleapis.com/chrome-for-testing-public or
* - https://archive.mozilla.org/pub/firefox/nightly/latest-mozilla-central
*
*/
@ -85,15 +93,41 @@ export interface InstallOptions {
* @defaultValue `true`
*/
unpack?: boolean;
/**
* @internal
* @defaultValue `false`
*/
forceFallbackForTesting?: boolean;
/**
* Whether to attempt to install system-level dependencies required
* for the browser.
*
* Only supported for Chrome on Debian or Ubuntu.
* Requires system-level privileges to run `apt-get`.
*
* @defaultValue `false`
*/
installDeps?: boolean;
}
/**
* Downloads and unpacks the browser archive according to the
* {@link InstallOptions}.
*
* @returns a {@link InstalledBrowser} instance.
*
* @public
*/
export function install(
options: InstallOptions & {unpack?: true}
): Promise<InstalledBrowser>;
/**
* Downloads the browser archive according to the {@link InstallOptions} without
* unpacking.
*
* @returns the absolute path to the archive.
*
* @public
*/
export function install(
@ -115,6 +149,113 @@ export async function install(
options.buildId,
options.baseUrl
);
try {
return await installUrl(url, options);
} catch (err) {
// If custom baseUrl is provided, do not fall back to CfT dashboard.
if (options.baseUrl && !options.forceFallbackForTesting) {
throw err;
}
debugInstall(`Error downloading from ${url}.`);
switch (options.browser) {
case Browser.CHROME:
case Browser.CHROMEDRIVER:
case Browser.CHROMEHEADLESSSHELL: {
debugInstall(
`Trying to find download URL via https://googlechromelabs.github.io/chrome-for-testing.`
);
interface Version {
downloads: Record<string, Array<{platform: string; url: string}>>;
}
const version = (await getJSON(
new URL(
`https://googlechromelabs.github.io/chrome-for-testing/${options.buildId}.json`
)
)) as Version;
let platform = '';
switch (options.platform) {
case BrowserPlatform.LINUX:
platform = 'linux64';
break;
case BrowserPlatform.MAC_ARM:
platform = 'mac-arm64';
break;
case BrowserPlatform.MAC:
platform = 'mac-x64';
break;
case BrowserPlatform.WIN32:
platform = 'win32';
break;
case BrowserPlatform.WIN64:
platform = 'win64';
break;
}
const url = version.downloads[options.browser]?.find(link => {
return link['platform'] === platform;
})?.url;
if (url) {
debugInstall(`Falling back to downloading from ${url}.`);
return await installUrl(new URL(url), options);
}
throw err;
}
default:
throw err;
}
}
}
async function installDeps(installedBrowser: InstalledBrowser) {
if (
process.platform !== 'linux' ||
installedBrowser.platform !== BrowserPlatform.LINUX
) {
return;
}
// Currently, only Debian-like deps are supported.
const depsPath = path.join(
path.dirname(installedBrowser.executablePath),
'deb.deps'
);
if (!existsSync(depsPath)) {
debugInstall(`deb.deps file was not found at ${depsPath}`);
return;
}
const data = readFileSync(depsPath, 'utf-8').split('\n').join(',');
if (process.getuid?.() !== 0) {
throw new Error('Installing system dependencies requires root privileges');
}
let result = spawnSync('apt-get', ['-v']);
if (result.status !== 0) {
throw new Error(
'Failed to install system dependencies: apt-get does not seem to be available'
);
}
debugInstall(`Trying to install dependencies: ${data}`);
result = spawnSync('apt-get', [
'satisfy',
'-y',
data,
'--no-install-recommends',
]);
if (result.status !== 0) {
throw new Error(
`Failed to install system dependencies: status=${result.status},error=${result.error},stdout=${result.stdout.toString('utf8')},stderr=${result.stderr.toString('utf8')}`
);
}
debugInstall(`Installed system dependencies ${data}`);
}
async function installUrl(
url: URL,
options: InstallOptions
): Promise<InstalledBrowser | string> {
options.platform ??= detectBrowserPlatform();
if (!options.platform) {
throw new Error(
`Cannot download a binary for the provided platform: ${os.platform()} (${os.arch()})`
);
}
const fileName = url.toString().split('/').pop();
assert(fileName, `A malformed download URL was found: ${url}.`);
const cache = new Cache(options.cacheDir);
@ -140,15 +281,26 @@ export async function install(
options.platform,
options.buildId
);
if (existsSync(outputPath)) {
return new InstalledBrowser(
cache,
options.browser,
options.buildId,
options.platform
);
}
try {
if (existsSync(outputPath)) {
const installedBrowser = new InstalledBrowser(
cache,
options.browser,
options.buildId,
options.platform
);
if (!existsSync(installedBrowser.executablePath)) {
throw new Error(
`The browser folder (${outputPath}) exists but the executable (${installedBrowser.executablePath}) is missing`
);
}
await runSetup(installedBrowser);
if (options.installDeps) {
await installDeps(installedBrowser);
}
return installedBrowser;
}
debugInstall(`Downloading binary from ${url}`);
try {
debugTime('download');
@ -164,17 +316,59 @@ export async function install(
} finally {
debugTimeEnd('extract');
}
const installedBrowser = new InstalledBrowser(
cache,
options.browser,
options.buildId,
options.platform
);
if (options.buildIdAlias) {
const metadata = installedBrowser.readMetadata();
metadata.aliases[options.buildIdAlias] = options.buildId;
installedBrowser.writeMetadata(metadata);
}
await runSetup(installedBrowser);
if (options.installDeps) {
await installDeps(installedBrowser);
}
return installedBrowser;
} finally {
if (existsSync(archivePath)) {
await unlink(archivePath);
}
}
return new InstalledBrowser(
cache,
options.browser,
options.buildId,
options.platform
);
}
async function runSetup(installedBrowser: InstalledBrowser): Promise<void> {
// On Windows for Chrome invoke setup.exe to configure sandboxes.
if (
(installedBrowser.platform === BrowserPlatform.WIN32 ||
installedBrowser.platform === BrowserPlatform.WIN64) &&
installedBrowser.browser === Browser.CHROME &&
installedBrowser.platform === detectBrowserPlatform()
) {
try {
debugTime('permissions');
const browserDir = path.dirname(installedBrowser.executablePath);
const setupExePath = path.join(browserDir, 'setup.exe');
if (!existsSync(setupExePath)) {
return;
}
spawnSync(
path.join(browserDir, 'setup.exe'),
[`--configure-browser-in-directory=` + browserDir],
{
shell: true,
}
);
// TODO: Handle error here. Currently the setup.exe sometimes
// errors although it sets the permissions correctly.
} finally {
debugTimeEnd('permissions');
}
}
}
/**