smartpuppeteer/ts/smartpuppeteer.classes.smartpuppeteer.ts

51 lines
1.3 KiB
TypeScript
Raw Normal View History

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',
2019-11-15 22:59:34 +00:00
'--disable-setuid-sandbox',
2019-11-15 23:01:05 +00:00
// '--disable-dev-shm-usage'
2019-11-15 20:44:11 +00:00
]);
}
let headlessBrowser: plugins.puppeteer.Browser;
if (!smartenv.isWsl) {
// lets get the actual instance
2019-11-15 22:59:34 +00:00
console.log('launching puppeteer bundled chrome');
2019-11-15 20:44:11 +00:00
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({
2019-11-15 22:59:34 +00:00
args: [
'--disable-gpu',
'--disable-dev-shm-usage',
'--disable-setuid-sandbox',
'--no-first-run',
'--no-sandbox',
'--no-zygote',
'--single-process'
]
2019-11-15 22:41:39 +00:00
});
2019-11-15 20:44:11 +00:00
}
return headlessBrowser;
};