Skip to content

Commit

Permalink
fix(browser): avoid SPOF for chrome launcher
Browse files Browse the repository at this point in the history
Chrome launcher might fail to connect to system Chromium instance on some systems. This introduced
SPOF for the lib.

fix #56
  • Loading branch information
onderceylan committed Oct 27, 2019
1 parent 13d8f90 commit f5ca991
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ export default {
},
},

CHROME_LAUNCHER_MAX_CONN_RETRIES: 5,

PUPPETEER_LAUNCH_ARGS: [
'--log-level=3', // Fatal only
'--no-default-browser-check',
Expand All @@ -135,6 +137,7 @@ export default {
'--mute-audio',
'--no-first-run',
'--headless',
'--no-sandbox',
],
EMULATED_USER_AGENT:
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3641.0 Safari/537.36',
Expand Down
17 changes: 11 additions & 6 deletions src/helpers/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ interface BrowserVersionInfo {
'Webkit-Version': string;
}

let isSystemBrowserAvailable: boolean;

const isPreferredBrowserRevisionInstalled = (): boolean => {
const revisionInfo = installer.getPreferredBrowserRevisionInfo();
return revisionInfo.local;
Expand Down Expand Up @@ -66,6 +68,7 @@ const getLocalBrowserInstance = async (
const launchSystemBrowser = (): Promise<LaunchedChrome> => {
const launchOptions: ChromeLauncherOptions = {
chromeFlags: constants.PUPPETEER_LAUNCH_ARGS,
maxConnectionRetries: constants.CHROME_LAUNCHER_MAX_CONN_RETRIES,
logLevel: 'silent',
};

Expand Down Expand Up @@ -108,17 +111,19 @@ const getBrowserInstance = async (
): Promise<{ chrome: LaunchedChrome | undefined; browser: Browser }> => {
let browser: Browser;
let chrome: LaunchedChrome | undefined;
const LAUNCHER_NOT_INSTALLED_ERROR_CODE = 'ERR_LAUNCHER_NOT_INSTALLED';

if (isSystemBrowserAvailable !== undefined && !isSystemBrowserAvailable) {
browser = await getLocalBrowserInstance(launchArgs);
return { browser, chrome };
}

try {
chrome = await launchSystemBrowser();
browser = await getSystemBrowserInstance(chrome, launchArgs);
isSystemBrowserAvailable = true;
} catch (e) {
if (e.code === LAUNCHER_NOT_INSTALLED_ERROR_CODE) {
browser = await getLocalBrowserInstance(launchArgs);
} else {
throw e;
}
browser = await getLocalBrowserInstance(launchArgs);
isSystemBrowserAvailable = false;
}

return { browser, chrome };
Expand Down

0 comments on commit f5ca991

Please sign in to comment.