2017-08-15 01:08:06 +00:00
/ * *
* Copyright 2017 Google Inc . All rights reserved .
*
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* /
2017-08-22 03:12:16 +00:00
const os = require ( 'os' ) ;
2017-08-15 01:08:06 +00:00
const path = require ( 'path' ) ;
2017-10-11 07:55:48 +00:00
const removeFolder = require ( 'rimraf' ) ;
2017-08-15 01:08:06 +00:00
const childProcess = require ( 'child_process' ) ;
2017-12-08 21:39:13 +00:00
const Downloader = require ( './Downloader' ) ;
2017-10-10 05:31:40 +00:00
const { Connection } = require ( './Connection' ) ;
const { Browser } = require ( './Browser' ) ;
2017-08-15 01:08:06 +00:00
const readline = require ( 'readline' ) ;
2017-08-22 03:12:16 +00:00
const fs = require ( 'fs' ) ;
2017-09-11 23:21:51 +00:00
const { helper } = require ( './helper' ) ;
2017-12-08 21:39:13 +00:00
const ChromiumRevision = Downloader . defaultRevision ( ) ;
2017-08-15 01:08:06 +00:00
2017-10-11 07:55:48 +00:00
const mkdtempAsync = helper . promisify ( fs . mkdtemp ) ;
const removeFolderAsync = helper . promisify ( removeFolder ) ;
2017-08-22 03:12:16 +00:00
const CHROME _PROFILE _PATH = path . join ( os . tmpdir ( ) , 'puppeteer_dev_profile-' ) ;
2017-08-15 01:08:06 +00:00
const DEFAULT _ARGS = [
'--disable-background-networking' ,
'--disable-background-timer-throttling' ,
'--disable-client-side-phishing-detection' ,
'--disable-default-apps' ,
2017-09-15 02:07:22 +00:00
'--disable-extensions' ,
2017-08-15 01:08:06 +00:00
'--disable-hang-monitor' ,
'--disable-popup-blocking' ,
'--disable-prompt-on-repost' ,
'--disable-sync' ,
2017-09-15 02:07:22 +00:00
'--disable-translate' ,
2017-08-15 01:08:06 +00:00
'--metrics-recording-only' ,
'--no-first-run' ,
'--remote-debugging-port=0' ,
'--safebrowsing-disable-auto-update' ,
2017-09-29 22:16:32 +00:00
] ;
const AUTOMATION _ARGS = [
'--enable-automation' ,
'--password-store=basic' ,
2017-08-15 01:08:06 +00:00
'--use-mock-keychain' ,
] ;
class Launcher {
/ * *
2017-08-21 23:32:39 +00:00
* @ param { ! Object = } options
2017-08-15 01:08:06 +00:00
* @ return { ! Promise < ! Browser > }
* /
2017-08-22 03:12:16 +00:00
static async launch ( options ) {
2017-09-29 22:16:32 +00:00
options = Object . assign ( { } , options || { } ) ;
2017-08-28 19:14:21 +00:00
let temporaryUserDataDir = null ;
2017-12-20 01:51:21 +00:00
const chromeArguments = [ ] ;
if ( ! options . ignoreDefaultArgs )
chromeArguments . push ( ... DEFAULT _ARGS ) ;
2017-09-29 22:16:32 +00:00
if ( options . appMode )
options . headless = false ;
2017-12-20 01:51:21 +00:00
else if ( ! options . ignoreDefaultArgs )
2017-09-29 22:16:32 +00:00
chromeArguments . push ( ... AUTOMATION _ARGS ) ;
2017-08-28 21:59:41 +00:00
if ( ! options . args || ! options . args . some ( arg => arg . startsWith ( '--user-data-dir' ) ) ) {
2017-08-28 19:14:21 +00:00
if ( ! options . userDataDir )
2017-10-11 07:55:48 +00:00
temporaryUserDataDir = await mkdtempAsync ( CHROME _PROFILE _PATH ) ;
2017-08-18 06:18:08 +00:00
2017-08-28 19:14:21 +00:00
chromeArguments . push ( ` --user-data-dir= ${ options . userDataDir || temporaryUserDataDir } ` ) ;
}
2017-10-10 00:25:25 +00:00
if ( options . devtools === true ) {
chromeArguments . push ( '--auto-open-devtools-for-tabs' ) ;
options . headless = false ;
}
2017-08-15 01:08:06 +00:00
if ( typeof options . headless !== 'boolean' || options . headless ) {
chromeArguments . push (
2017-08-21 23:32:39 +00:00
'--headless' ,
'--disable-gpu' ,
'--hide-scrollbars' ,
2017-08-18 03:54:16 +00:00
'--mute-audio'
2017-08-15 01:08:06 +00:00
) ;
}
let chromeExecutable = options . executablePath ;
if ( typeof chromeExecutable !== 'string' ) {
2017-12-08 21:39:13 +00:00
const downloader = Downloader . createDefault ( ) ;
const revisionInfo = downloader . revisionInfo ( downloader . currentPlatform ( ) , ChromiumRevision ) ;
2018-01-10 05:56:04 +00:00
console . assert ( revisionInfo . downloaded , ` Chromium revision is not downloaded. Run "yarn install" or "npm install" ` ) ;
2017-08-15 01:08:06 +00:00
chromeExecutable = revisionInfo . executablePath ;
}
if ( Array . isArray ( options . args ) )
chromeArguments . push ( ... options . args ) ;
2017-08-21 22:43:36 +00:00
2017-10-05 21:34:35 +00:00
const chromeProcess = childProcess . spawn (
chromeExecutable ,
chromeArguments ,
{
detached : true ,
env : options . env || process . env
}
) ;
2017-08-15 01:08:06 +00:00
if ( options . dumpio ) {
chromeProcess . stdout . pipe ( process . stdout ) ;
chromeProcess . stderr . pipe ( process . stderr ) ;
}
2017-10-17 22:35:00 +00:00
let chromeClosed = false ;
2017-10-11 07:55:48 +00:00
const waitForChromeToClose = new Promise ( ( fulfill , reject ) => {
2017-09-14 04:27:14 +00:00
chromeProcess . once ( 'close' , ( ) => {
2017-10-17 22:35:00 +00:00
chromeClosed = true ;
2017-09-14 04:27:14 +00:00
// Cleanup as processes exit.
2017-10-11 07:55:48 +00:00
if ( temporaryUserDataDir ) {
removeFolderAsync ( temporaryUserDataDir )
. then ( ( ) => fulfill ( ) )
. catch ( err => console . error ( err ) ) ;
} else {
fulfill ( ) ;
}
2017-09-14 04:27:14 +00:00
} ) ;
} ) ;
2017-08-23 18:55:33 +00:00
2017-10-24 23:05:12 +00:00
const listeners = [ helper . addEventListener ( process , 'exit' , forceKillChrome ) ] ;
2017-08-21 22:43:36 +00:00
if ( options . handleSIGINT !== false )
2017-10-24 23:05:12 +00:00
listeners . push ( helper . addEventListener ( process , 'SIGINT' , forceKillChrome ) ) ;
2017-11-16 22:26:32 +00:00
if ( options . handleSIGTERM !== false )
listeners . push ( helper . addEventListener ( process , 'SIGTERM' , killChrome ) ) ;
2017-11-17 03:28:32 +00:00
if ( options . handleSIGHUP !== false )
listeners . push ( helper . addEventListener ( process , 'SIGHUP' , killChrome ) ) ;
2017-10-17 22:35:00 +00:00
/** @type {?Connection} */
let connection = null ;
2017-08-21 22:43:36 +00:00
try {
2017-08-21 23:32:39 +00:00
const connectionDelay = options . slowMo || 0 ;
const browserWSEndpoint = await waitForWSEndpoint ( chromeProcess , options . timeout || 30 * 1000 ) ;
2017-10-17 22:35:00 +00:00
connection = await Connection . create ( browserWSEndpoint , connectionDelay ) ;
2017-12-11 20:11:12 +00:00
return Browser . create ( connection , options , chromeProcess , killChrome ) ;
2017-08-21 22:43:36 +00:00
} catch ( e ) {
2017-11-16 22:26:32 +00:00
forceKillChrome ( ) ;
2017-08-21 22:43:36 +00:00
throw e ;
2017-08-15 01:08:06 +00:00
}
2017-09-14 04:27:14 +00:00
/ * *
* @ return { Promise }
* /
2017-08-23 18:55:33 +00:00
function killChrome ( ) {
2017-08-30 05:49:50 +00:00
helper . removeEventListeners ( listeners ) ;
2017-10-17 22:35:00 +00:00
if ( temporaryUserDataDir ) {
2017-10-24 23:05:12 +00:00
forceKillChrome ( ) ;
2017-10-17 22:35:00 +00:00
} else if ( connection ) {
// Attempt to close chrome gracefully
connection . send ( 'Browser.close' ) ;
2017-09-14 04:27:14 +00:00
}
return waitForChromeToClose ;
2017-08-21 22:43:36 +00:00
}
2017-10-24 23:05:12 +00:00
function forceKillChrome ( ) {
helper . removeEventListeners ( listeners ) ;
if ( chromeProcess . pid && ! chromeProcess . killed && ! chromeClosed ) {
// Force kill chrome.
if ( process . platform === 'win32' )
childProcess . execSync ( ` taskkill /pid ${ chromeProcess . pid } /T /F ` ) ;
else
process . kill ( - chromeProcess . pid , 'SIGKILL' ) ;
}
// Attempt to remove temporary profile directory to avoid littering.
try {
removeFolder . sync ( temporaryUserDataDir ) ;
} catch ( e ) { }
}
2017-08-15 01:08:06 +00:00
}
2017-08-15 21:29:42 +00:00
2017-12-20 01:51:21 +00:00
/ * *
* @ return { ! Array < string > }
* /
static defaultArgs ( ) {
return DEFAULT _ARGS . concat ( AUTOMATION _ARGS ) ;
}
2017-09-14 00:39:18 +00:00
/ * *
* @ return { string }
* /
static executablePath ( ) {
2017-12-08 21:39:13 +00:00
const downloader = Downloader . createDefault ( ) ;
const revisionInfo = downloader . revisionInfo ( downloader . currentPlatform ( ) , ChromiumRevision ) ;
2017-09-14 00:39:18 +00:00
return revisionInfo . executablePath ;
}
2017-08-15 21:29:42 +00:00
/ * *
2017-09-29 22:16:32 +00:00
* @ param { ! Object = } options
2017-08-15 21:29:42 +00:00
* @ return { ! Promise < ! Browser > }
* /
2017-09-29 22:16:32 +00:00
static async connect ( options = { } ) {
const connection = await Connection . create ( options . browserWSEndpoint ) ;
2017-12-11 20:11:12 +00:00
return Browser . create ( connection , options , null , ( ) => connection . send ( 'Browser.close' ) ) ;
2017-08-15 21:29:42 +00:00
}
2017-08-15 01:08:06 +00:00
}
/ * *
2017-10-10 05:31:40 +00:00
* @ param { ! Puppeteer . ChildProcess } chromeProcess
2017-08-21 22:43:36 +00:00
* @ param { number } timeout
2017-08-15 21:29:42 +00:00
* @ return { ! Promise < string > }
2017-08-15 01:08:06 +00:00
* /
2017-08-21 22:43:36 +00:00
function waitForWSEndpoint ( chromeProcess , timeout ) {
return new Promise ( ( resolve , reject ) => {
2017-08-15 01:08:06 +00:00
const rl = readline . createInterface ( { input : chromeProcess . stderr } ) ;
2017-08-21 22:43:36 +00:00
let stderr = '' ;
2017-08-21 23:39:04 +00:00
const listeners = [
2017-08-21 22:43:36 +00:00
helper . addEventListener ( rl , 'line' , onLine ) ,
2017-09-29 19:21:24 +00:00
helper . addEventListener ( rl , 'close' , ( ) => onClose ( ) ) ,
helper . addEventListener ( chromeProcess , 'exit' , ( ) => onClose ( ) ) ,
helper . addEventListener ( chromeProcess , 'error' , error => onClose ( error ) )
2017-08-21 22:43:36 +00:00
] ;
2017-08-21 23:39:04 +00:00
const timeoutId = timeout ? setTimeout ( onTimeout , timeout ) : 0 ;
2017-08-21 22:43:36 +00:00
2017-09-29 19:21:24 +00:00
/ * *
* @ param { ! Error = } error
* /
function onClose ( error ) {
2017-08-21 22:43:36 +00:00
cleanup ( ) ;
reject ( new Error ( [
2017-09-29 19:21:24 +00:00
'Failed to launch chrome!' + ( error ? ' ' + error . message : '' ) ,
2017-08-21 22:43:36 +00:00
stderr ,
'' ,
'TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md' ,
'' ,
] . join ( '\n' ) ) ) ;
}
function onTimeout ( ) {
cleanup ( ) ;
reject ( new Error ( ` Timed out after ${ timeout } ms while trying to connect to Chrome! The only Chrome revision guaranteed to work is r ${ ChromiumRevision } ` ) ) ;
}
2017-08-15 01:08:06 +00:00
/ * *
* @ param { string } line
* /
function onLine ( line ) {
2017-08-21 22:43:36 +00:00
stderr += line + '\n' ;
2017-08-15 21:29:42 +00:00
const match = line . match ( /^DevTools listening on (ws:\/\/.*)$/ ) ;
2017-08-15 01:08:06 +00:00
if ( ! match )
return ;
2017-08-21 22:43:36 +00:00
cleanup ( ) ;
resolve ( match [ 1 ] ) ;
}
function cleanup ( ) {
if ( timeoutId )
clearTimeout ( timeoutId ) ;
helper . removeEventListeners ( listeners ) ;
2017-08-15 01:08:06 +00:00
}
} ) ;
}
module . exports = Launcher ;