| 
									
										
										
										
											2022-03-24 13:51:12 +01:00
										 |  |  | import * as plugins from './smartpuppeteer.plugins.js'; | 
					
						
							| 
									
										
										
										
											2019-11-15 21:44:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | export interface IEnvAwareOptions { | 
					
						
							|  |  |  |   forceNoSandbox?: boolean; | 
					
						
							| 
									
										
										
										
											2022-07-18 03:08:32 +02:00
										 |  |  |   usePipe?: boolean; | 
					
						
							| 
									
										
										
										
											2019-11-15 21:44:11 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const getEnvAwareBrowserInstance = async ( | 
					
						
							|  |  |  |   optionsArg: IEnvAwareOptions = {} | 
					
						
							|  |  |  | ): Promise<plugins.puppeteer.Browser> => { | 
					
						
							|  |  |  |   const options: IEnvAwareOptions = { | 
					
						
							| 
									
										
										
										
											2025-02-25 17:33:48 +00:00
										 |  |  |     forceNoSandbox: false, | 
					
						
							| 
									
										
										
										
											2021-01-08 21:16:25 +00:00
										 |  |  |     ...optionsArg, | 
					
						
							| 
									
										
										
										
											2019-11-15 21:44:11 +01:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let chromeArgs: string[] = []; | 
					
						
							| 
									
										
										
										
											2025-02-25 17:33:48 +00:00
										 |  |  |   if ( | 
					
						
							|  |  |  |     process.env.CI || | 
					
						
							|  |  |  |     options.forceNoSandbox || | 
					
						
							|  |  |  |     plugins.os.userInfo().username === 'root' | 
					
						
							|  |  |  |   ) { | 
					
						
							| 
									
										
										
										
											2019-11-16 00:35:03 +01:00
										 |  |  |     chromeArgs = chromeArgs.concat(['--no-sandbox', '--disable-setuid-sandbox']); | 
					
						
							| 
									
										
										
										
											2025-02-25 17:33:48 +00:00
										 |  |  |     console.warn('********************************************************'); | 
					
						
							|  |  |  |     console.warn('WARNING: Launching browser without sandbox. This can be insecure!'); | 
					
						
							|  |  |  |     console.warn('********************************************************'); | 
					
						
							| 
									
										
										
										
											2019-11-15 21:44:11 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-25 17:33:48 +00:00
										 |  |  |   // Automatically choose an executable if available: prefer google-chrome, then chromium, then chromium-browser.
 | 
					
						
							|  |  |  |   const execPath = | 
					
						
							|  |  |  |     plugins.smartshell.which.sync('google-chrome') || | 
					
						
							|  |  |  |     plugins.smartshell.which.sync('chromium') || | 
					
						
							|  |  |  |     plugins.smartshell.which.sync('chromium-browser'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const executablePathOptions = execPath ? { executablePath: execPath } : {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   console.log('Launching puppeteer browser with arguments:'); | 
					
						
							| 
									
										
										
										
											2019-11-16 00:35:03 +01:00
										 |  |  |   console.log(chromeArgs); | 
					
						
							| 
									
										
										
										
											2025-02-25 17:33:48 +00:00
										 |  |  |   if (execPath) { | 
					
						
							|  |  |  |     console.log(`Using executable: ${execPath}`); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     console.log('No specific browser executable found; falling back to Puppeteer default.'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const headlessBrowser = await plugins.puppeteer.launch({ | 
					
						
							| 
									
										
										
										
											2021-01-08 21:16:25 +00:00
										 |  |  |     args: chromeArgs, | 
					
						
							| 
									
										
										
										
											2022-07-18 03:08:32 +02:00
										 |  |  |     pipe: options.usePipe !== undefined ? options.usePipe : true, | 
					
						
							| 
									
										
										
										
											2021-11-07 18:30:08 +01:00
										 |  |  |     headless: true, | 
					
						
							| 
									
										
										
										
											2025-02-25 17:33:48 +00:00
										 |  |  |     ...executablePathOptions, | 
					
						
							| 
									
										
										
										
											2019-11-16 00:35:03 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-11-15 21:44:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return headlessBrowser; | 
					
						
							| 
									
										
										
										
											2025-02-25 17:33:48 +00:00
										 |  |  | }; |