fix: make more files work in strict-mode TypeScript (#7936)

This commit is contained in:
Alex Rudenko 2022-01-28 10:38:36 +01:00 committed by GitHub
parent 000c1f630d
commit 0636513e34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 24 deletions

View File

@ -80,6 +80,7 @@
"@types/mime": "2.0.3",
"@types/mocha": "9.0.0",
"@types/node": "16.10.9",
"@types/progress": "2.0.5",
"@types/proxy-from-env": "1.0.1",
"@types/rimraf": "3.0.2",
"@types/sinon": "10.0.4",

View File

@ -34,6 +34,10 @@ export const initializePuppeteerNode = (packageName: string): PuppeteerNode => {
if (!isPuppeteerCore && productName === 'firefox')
preferredRevision = PUPPETEER_REVISIONS.firefox;
if (!puppeteerRootDirectory) {
throw new Error('puppeteerRootDirectory is not found.');
}
return new PuppeteerNode({
projectRoot: puppeteerRootDirectory,
preferredRevision,

View File

@ -42,9 +42,9 @@ const tmpDir = () => process.env.PUPPETEER_TMP_DIR || os.tmpdir();
* @public
*/
export interface ProductLauncher {
launch(object: PuppeteerNodeLaunchOptions);
executablePath: (string?) => string;
defaultArgs(object: BrowserLaunchArgumentOptions);
launch(object: PuppeteerNodeLaunchOptions): Promise<Browser>;
executablePath: (path?: any) => string;
defaultArgs(object: BrowserLaunchArgumentOptions): string[];
product: Product;
}
@ -157,6 +157,10 @@ class ChromeLauncher implements ProductLauncher {
}
}
if (!chromeExecutable) {
throw new Error('chromeExecutable is not found.');
}
const usePipe = chromeArguments.includes('--remote-debugging-pipe');
const runner = new BrowserRunner(
this.product,
@ -187,7 +191,7 @@ class ChromeLauncher implements ProductLauncher {
[],
ignoreHTTPSErrors,
defaultViewport,
runner.proc,
runner.proc ?? undefined,
runner.close.bind(runner)
);
} catch (error) {
@ -364,6 +368,10 @@ class FirefoxLauncher implements ProductLauncher {
firefoxExecutable = executablePath;
}
if (!firefoxExecutable) {
throw new Error('firefoxExecutable is not found.');
}
const runner = new BrowserRunner(
this.product,
firefoxExecutable,
@ -786,7 +794,7 @@ function resolveExecutablePath(launcher: ChromeLauncher | FirefoxLauncher): {
executablePath: string;
missingText?: string;
} {
let downloadPath: string;
let downloadPath: string | undefined;
// puppeteer-core doesn't take into account PUPPETEER_* env variables.
if (!launcher._isPuppeteerCore) {
const executablePath =
@ -797,7 +805,7 @@ function resolveExecutablePath(launcher: ChromeLauncher | FirefoxLauncher): {
const missingText = !fs.existsSync(executablePath)
? 'Tried to use PUPPETEER_EXECUTABLE_PATH env variable to launch browser but did not find any executable at: ' +
executablePath
: null;
: undefined;
return { executablePath, missingText };
}
downloadPath =
@ -817,7 +825,7 @@ function resolveExecutablePath(launcher: ChromeLauncher | FirefoxLauncher): {
const missingText = !revisionInfo.local
? 'Tried to use PUPPETEER_CHROMIUM_REVISION env variable to launch browser but did not find executable at: ' +
revisionInfo.executablePath
: null;
: undefined;
return { executablePath: revisionInfo.executablePath, missingText };
}
}
@ -829,7 +837,7 @@ function resolveExecutablePath(launcher: ChromeLauncher | FirefoxLauncher): {
? `Could not find expected browser (${launcher.product}) locally. ${
launcher.product === 'chrome' ? chromeHelp : firefoxHelp
}`
: null;
: undefined;
return { executablePath: revisionInfo.executablePath, missingText };
}

View File

@ -66,7 +66,7 @@ import { Product } from '../common/Product.js';
* @public
*/
export class PuppeteerNode extends Puppeteer {
private _lazyLauncher: ProductLauncher;
private _lazyLauncher?: ProductLauncher;
private _projectRoot: string;
private __productName?: Product;
/**
@ -108,12 +108,12 @@ export class PuppeteerNode extends Puppeteer {
/**
* @internal
*/
get _productName(): Product {
get _productName(): Product | undefined {
return this.__productName;
}
// don't need any TSDoc here - because the getter is internal the setter is too.
set _productName(name: Product) {
set _productName(name: Product | undefined) {
if (this.__productName !== name) this._changedProduct = true;
this.__productName = name;
}

View File

@ -31,16 +31,24 @@ const supportedProducts = {
firefox: 'Firefox Nightly',
} as const;
function getProduct(input: string): 'chrome' | 'firefox' {
if (input !== 'chrome' && input !== 'firefox') {
throw new Error(`Unsupported product ${input}`);
}
return input;
}
export async function downloadBrowser(): Promise<void> {
const downloadHost =
process.env.PUPPETEER_DOWNLOAD_HOST ||
process.env.npm_config_puppeteer_download_host ||
process.env.npm_package_config_puppeteer_download_host;
const product =
const product = getProduct(
process.env.PUPPETEER_PRODUCT ||
process.env.npm_config_puppeteer_product ||
process.env.npm_package_config_puppeteer_product ||
'chrome';
process.env.npm_config_puppeteer_product ||
process.env.npm_package_config_puppeteer_product ||
'chrome'
);
const downloadPath =
process.env.PUPPETEER_DOWNLOAD_PATH ||
process.env.npm_config_puppeteer_download_path ||
@ -53,7 +61,7 @@ export async function downloadBrowser(): Promise<void> {
const revision = await getRevision();
await fetchBinary(revision);
function getRevision() {
async function getRevision(): Promise<string> {
if (product === 'chrome') {
return (
process.env.PUPPETEER_CHROMIUM_REVISION ||
@ -72,7 +80,7 @@ export async function downloadBrowser(): Promise<void> {
}
}
function fetchBinary(revision) {
function fetchBinary(revision: string) {
const revisionInfo = browserFetcher.revisionInfo(revision);
// Do nothing if the revision is already downloaded.
@ -119,9 +127,9 @@ export async function downloadBrowser(): Promise<void> {
process.exit(1);
}
let progressBar = null;
let progressBar: ProgressBar | null = null;
let lastDownloadedBytes = 0;
function onProgress(downloadedBytes, totalBytes) {
function onProgress(downloadedBytes: number, totalBytes: number) {
if (!progressBar) {
progressBar = new ProgressBar(
`Downloading ${
@ -147,12 +155,12 @@ export async function downloadBrowser(): Promise<void> {
.catch(onError);
}
function toMegabytes(bytes) {
function toMegabytes(bytes: number) {
const mb = bytes / 1024 / 1024;
return `${Math.round(mb * 10) / 10} Mb`;
}
function getFirefoxNightlyVersion() {
async function getFirefoxNightlyVersion(): Promise<string> {
const firefoxVersionsUrl =
'https://product-details.mozilla.org/1.0/firefox_versions.json';
@ -172,14 +180,14 @@ export async function downloadBrowser(): Promise<void> {
requestOptions.rejectUnauthorized = false;
}
const promise = new Promise((resolve, reject) => {
const promise = new Promise<string>((resolve, reject) => {
let data = '';
logPolitely(
`Requesting latest Firefox Nightly version from ${firefoxVersionsUrl}`
);
https
.get(firefoxVersionsUrl, requestOptions, (r) => {
if (r.statusCode >= 400)
if (r.statusCode && r.statusCode >= 400)
return reject(new Error(`Got status code ${r.statusCode}`));
r.on('data', (chunk) => {
data += chunk;
@ -200,7 +208,7 @@ export async function downloadBrowser(): Promise<void> {
}
export function logPolitely(toBeLogged: unknown): void {
const logLevel = process.env.npm_config_loglevel;
const logLevel = process.env.npm_config_loglevel || '';
const logLevelDisplay = ['silent', 'error', 'warn'].indexOf(logLevel) > -1;
// eslint-disable-next-line no-console