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) {
|
2019-11-15 23:35:03 +00:00
|
|
|
chromeArgs = chromeArgs.concat(['--no-sandbox', '--disable-setuid-sandbox']);
|
|
|
|
} else if (smartenv.isWsl) {
|
2019-11-16 16:51:11 +00:00
|
|
|
console.log('Detected WSL.');
|
2019-11-16 00:15:08 +00:00
|
|
|
chromeArgs = chromeArgs.concat(['--no-sandbox', '--single-process']);
|
2019-11-15 20:44:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let headlessBrowser: plugins.puppeteer.Browser;
|
2019-11-15 23:35:03 +00:00
|
|
|
console.log('launching puppeteer bundled chrome with arguments:');
|
|
|
|
console.log(chromeArgs);
|
|
|
|
headlessBrowser = await plugins.puppeteer.launch({
|
|
|
|
args: chromeArgs
|
|
|
|
});
|
2019-11-15 20:44:11 +00:00
|
|
|
|
|
|
|
return headlessBrowser;
|
|
|
|
};
|