2019-11-15 20:44:11 +00:00
|
|
|
import * as plugins from './smartpuppeteer.plugins';
|
|
|
|
|
|
|
|
export interface IEnvAwareOptions {
|
|
|
|
forceNoSandbox?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getEnvAwareBrowserInstance = async (
|
|
|
|
optionsArg: IEnvAwareOptions = {}
|
|
|
|
): Promise<plugins.puppeteer.Browser> => {
|
|
|
|
const smartenv = new plugins.smartenv.Smartenv();
|
|
|
|
const options: IEnvAwareOptions = {
|
|
|
|
...{
|
|
|
|
forceNoSandbox: false
|
|
|
|
},
|
|
|
|
...optionsArg
|
|
|
|
};
|
|
|
|
|
|
|
|
let chromeArgs: string[] = [];
|
|
|
|
if ((process.env.CI || options.forceNoSandbox) && !smartenv.isWsl) {
|
|
|
|
chromeArgs = chromeArgs.concat([
|
|
|
|
'--no-sandbox',
|
|
|
|
'--disable-setuid-sandbox'
|
|
|
|
// '--disable-dev-shm-usage'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
let headlessBrowser: plugins.puppeteer.Browser;
|
|
|
|
if (!smartenv.isWsl) {
|
|
|
|
// lets get the actual instance
|
|
|
|
headlessBrowser = await plugins.puppeteer.launch({
|
|
|
|
args: chromeArgs
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log('Detected WSL. Using chromium.');
|
2019-11-15 22:41:39 +00:00
|
|
|
headlessBrowser = await plugins.puppeteer.launch({
|
|
|
|
executablePath: '/usr/bin/chromium-browser'
|
|
|
|
});
|
2019-11-15 20:44:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return headlessBrowser;
|
|
|
|
};
|