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/mime": "2.0.3",
"@types/mocha": "9.0.0", "@types/mocha": "9.0.0",
"@types/node": "16.10.9", "@types/node": "16.10.9",
"@types/progress": "2.0.5",
"@types/proxy-from-env": "1.0.1", "@types/proxy-from-env": "1.0.1",
"@types/rimraf": "3.0.2", "@types/rimraf": "3.0.2",
"@types/sinon": "10.0.4", "@types/sinon": "10.0.4",

View File

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

View File

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

View File

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

View File

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