2024-01-05 17:07:59 -05:00
"use strict" ;
/ * *
* @ license
* Copyright 2017 Google Inc .
* SPDX - License - Identifier : Apache - 2.0
* /
var _ _importDefault = ( this && this . _ _importDefault ) || function ( mod ) {
return ( mod && mod . _ _esModule ) ? mod : { "default" : mod } ;
} ;
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
exports . canDownload = exports . getInstalledBrowsers = exports . uninstall = exports . install = void 0 ;
const assert _1 = _ _importDefault ( require ( "assert" ) ) ;
2024-09-23 20:52:09 -04:00
const child _process _1 = require ( "child_process" ) ;
2024-01-05 17:07:59 -05:00
const fs _1 = require ( "fs" ) ;
const promises _1 = require ( "fs/promises" ) ;
const os _1 = _ _importDefault ( require ( "os" ) ) ;
const path _1 = _ _importDefault ( require ( "path" ) ) ;
const browser _data _js _1 = require ( "./browser-data/browser-data.js" ) ;
const Cache _js _1 = require ( "./Cache.js" ) ;
const debug _js _1 = require ( "./debug.js" ) ;
const detectPlatform _js _1 = require ( "./detectPlatform.js" ) ;
const fileUtil _js _1 = require ( "./fileUtil.js" ) ;
const httpUtil _js _1 = require ( "./httpUtil.js" ) ;
const debugInstall = ( 0 , debug _js _1 . debug ) ( 'puppeteer:browsers:install' ) ;
const times = new Map ( ) ;
function debugTime ( label ) {
times . set ( label , process . hrtime ( ) ) ;
}
function debugTimeEnd ( label ) {
const end = process . hrtime ( ) ;
const start = times . get ( label ) ;
if ( ! start ) {
return ;
}
const duration = end [ 0 ] * 1000 + end [ 1 ] / 1e6 - ( start [ 0 ] * 1000 + start [ 1 ] / 1e6 ) ; // calculate duration in milliseconds
debugInstall ( ` Duration for ${ label } : ${ duration } ms ` ) ;
}
async function install ( options ) {
options . platform ? ? = ( 0 , detectPlatform _js _1 . detectBrowserPlatform ) ( ) ;
options . unpack ? ? = true ;
if ( ! options . platform ) {
throw new Error ( ` Cannot download a binary for the provided platform: ${ os _1 . default . platform ( ) } ( ${ os _1 . default . arch ( ) } ) ` ) ;
}
const url = getDownloadUrl ( options . browser , options . platform , options . buildId , options . baseUrl ) ;
2024-09-23 20:52:09 -04:00
try {
return await installUrl ( url , options ) ;
}
catch ( err ) {
// If custom baseUrl is provided, do not fall back to CfT dashboard.
if ( options . baseUrl && ! options . forceFallbackForTesting ) {
throw err ;
}
debugInstall ( ` Error downloading from ${ url } . ` ) ;
switch ( options . browser ) {
case browser _data _js _1 . Browser . CHROME :
case browser _data _js _1 . Browser . CHROMEDRIVER :
case browser _data _js _1 . Browser . CHROMEHEADLESSSHELL : {
debugInstall ( ` Trying to find download URL via https://googlechromelabs.github.io/chrome-for-testing. ` ) ;
const version = ( await ( 0 , httpUtil _js _1 . getJSON ) ( new URL ( ` https://googlechromelabs.github.io/chrome-for-testing/ ${ options . buildId } .json ` ) ) ) ;
let platform = '' ;
switch ( options . platform ) {
case browser _data _js _1 . BrowserPlatform . LINUX :
platform = 'linux64' ;
break ;
case browser _data _js _1 . BrowserPlatform . MAC _ARM :
platform = 'mac-arm64' ;
break ;
case browser _data _js _1 . BrowserPlatform . MAC :
platform = 'mac-x64' ;
break ;
case browser _data _js _1 . BrowserPlatform . WIN32 :
platform = 'win32' ;
break ;
case browser _data _js _1 . BrowserPlatform . WIN64 :
platform = 'win64' ;
break ;
}
const url = version . downloads [ options . browser ] ? . find ( link => {
return link [ 'platform' ] === platform ;
} ) ? . url ;
if ( url ) {
debugInstall ( ` Falling back to downloading from ${ url } . ` ) ;
return await installUrl ( new URL ( url ) , options ) ;
}
throw err ;
}
default :
throw err ;
}
}
}
exports . install = install ;
async function installDeps ( installedBrowser ) {
if ( process . platform !== 'linux' ||
installedBrowser . platform !== browser _data _js _1 . BrowserPlatform . LINUX ) {
return ;
}
// Currently, only Debian-like deps are supported.
const depsPath = path _1 . default . join ( path _1 . default . dirname ( installedBrowser . executablePath ) , 'deb.deps' ) ;
if ( ! ( 0 , fs _1 . existsSync ) ( depsPath ) ) {
debugInstall ( ` deb.deps file was not found at ${ depsPath } ` ) ;
return ;
}
const data = ( 0 , fs _1 . readFileSync ) ( depsPath , 'utf-8' ) . split ( '\n' ) . join ( ',' ) ;
if ( process . getuid ? . ( ) !== 0 ) {
throw new Error ( 'Installing system dependencies requires root privileges' ) ;
}
let result = ( 0 , child _process _1 . spawnSync ) ( 'apt-get' , [ '-v' ] ) ;
if ( result . status !== 0 ) {
throw new Error ( 'Failed to install system dependencies: apt-get does not seem to be available' ) ;
}
debugInstall ( ` Trying to install dependencies: ${ data } ` ) ;
result = ( 0 , child _process _1 . spawnSync ) ( 'apt-get' , [
'satisfy' ,
'-y' ,
data ,
'--no-install-recommends' ,
] ) ;
if ( result . status !== 0 ) {
throw new Error ( ` Failed to install system dependencies: status= ${ result . status } ,error= ${ result . error } ,stdout= ${ result . stdout . toString ( 'utf8' ) } ,stderr= ${ result . stderr . toString ( 'utf8' ) } ` ) ;
}
debugInstall ( ` Installed system dependencies ${ data } ` ) ;
}
async function installUrl ( url , options ) {
options . platform ? ? = ( 0 , detectPlatform _js _1 . detectBrowserPlatform ) ( ) ;
if ( ! options . platform ) {
throw new Error ( ` Cannot download a binary for the provided platform: ${ os _1 . default . platform ( ) } ( ${ os _1 . default . arch ( ) } ) ` ) ;
}
2024-01-05 17:07:59 -05:00
const fileName = url . toString ( ) . split ( '/' ) . pop ( ) ;
( 0 , assert _1 . default ) ( fileName , ` A malformed download URL was found: ${ url } . ` ) ;
const cache = new Cache _js _1 . Cache ( options . cacheDir ) ;
const browserRoot = cache . browserRoot ( options . browser ) ;
const archivePath = path _1 . default . join ( browserRoot , ` ${ options . buildId } - ${ fileName } ` ) ;
if ( ! ( 0 , fs _1 . existsSync ) ( browserRoot ) ) {
await ( 0 , promises _1 . mkdir ) ( browserRoot , { recursive : true } ) ;
}
if ( ! options . unpack ) {
if ( ( 0 , fs _1 . existsSync ) ( archivePath ) ) {
return archivePath ;
}
debugInstall ( ` Downloading binary from ${ url } ` ) ;
debugTime ( 'download' ) ;
await ( 0 , httpUtil _js _1 . downloadFile ) ( url , archivePath , options . downloadProgressCallback ) ;
debugTimeEnd ( 'download' ) ;
return archivePath ;
}
const outputPath = cache . installationDir ( options . browser , options . platform , options . buildId ) ;
try {
2024-09-23 20:52:09 -04:00
if ( ( 0 , fs _1 . existsSync ) ( outputPath ) ) {
const installedBrowser = new Cache _js _1 . InstalledBrowser ( cache , options . browser , options . buildId , options . platform ) ;
if ( ! ( 0 , fs _1 . existsSync ) ( installedBrowser . executablePath ) ) {
throw new Error ( ` The browser folder ( ${ outputPath } ) exists but the executable ( ${ installedBrowser . executablePath } ) is missing ` ) ;
}
await runSetup ( installedBrowser ) ;
if ( options . installDeps ) {
await installDeps ( installedBrowser ) ;
}
return installedBrowser ;
}
2024-01-05 17:07:59 -05:00
debugInstall ( ` Downloading binary from ${ url } ` ) ;
try {
debugTime ( 'download' ) ;
await ( 0 , httpUtil _js _1 . downloadFile ) ( url , archivePath , options . downloadProgressCallback ) ;
}
finally {
debugTimeEnd ( 'download' ) ;
}
debugInstall ( ` Installing ${ archivePath } to ${ outputPath } ` ) ;
try {
debugTime ( 'extract' ) ;
await ( 0 , fileUtil _js _1 . unpackArchive ) ( archivePath , outputPath ) ;
}
finally {
debugTimeEnd ( 'extract' ) ;
}
2024-09-23 20:52:09 -04:00
const installedBrowser = new Cache _js _1 . InstalledBrowser ( cache , options . browser , options . buildId , options . platform ) ;
if ( options . buildIdAlias ) {
const metadata = installedBrowser . readMetadata ( ) ;
metadata . aliases [ options . buildIdAlias ] = options . buildId ;
installedBrowser . writeMetadata ( metadata ) ;
}
await runSetup ( installedBrowser ) ;
if ( options . installDeps ) {
await installDeps ( installedBrowser ) ;
}
return installedBrowser ;
2024-01-05 17:07:59 -05:00
}
finally {
if ( ( 0 , fs _1 . existsSync ) ( archivePath ) ) {
await ( 0 , promises _1 . unlink ) ( archivePath ) ;
}
}
}
2024-09-23 20:52:09 -04:00
async function runSetup ( installedBrowser ) {
// On Windows for Chrome invoke setup.exe to configure sandboxes.
if ( ( installedBrowser . platform === browser _data _js _1 . BrowserPlatform . WIN32 ||
installedBrowser . platform === browser _data _js _1 . BrowserPlatform . WIN64 ) &&
installedBrowser . browser === browser _data _js _1 . Browser . CHROME &&
installedBrowser . platform === ( 0 , detectPlatform _js _1 . detectBrowserPlatform ) ( ) ) {
try {
debugTime ( 'permissions' ) ;
const browserDir = path _1 . default . dirname ( installedBrowser . executablePath ) ;
const setupExePath = path _1 . default . join ( browserDir , 'setup.exe' ) ;
if ( ! ( 0 , fs _1 . existsSync ) ( setupExePath ) ) {
return ;
}
( 0 , child _process _1 . spawnSync ) ( path _1 . default . join ( browserDir , 'setup.exe' ) , [ ` --configure-browser-in-directory= ` + browserDir ] , {
shell : true ,
} ) ;
// TODO: Handle error here. Currently the setup.exe sometimes
// errors although it sets the permissions correctly.
}
finally {
debugTimeEnd ( 'permissions' ) ;
}
}
}
2024-01-05 17:07:59 -05:00
/ * *
*
* @ public
* /
async function uninstall ( options ) {
options . platform ? ? = ( 0 , detectPlatform _js _1 . detectBrowserPlatform ) ( ) ;
if ( ! options . platform ) {
throw new Error ( ` Cannot detect the browser platform for: ${ os _1 . default . platform ( ) } ( ${ os _1 . default . arch ( ) } ) ` ) ;
}
new Cache _js _1 . Cache ( options . cacheDir ) . uninstall ( options . browser , options . platform , options . buildId ) ;
}
exports . uninstall = uninstall ;
/ * *
* Returns metadata about browsers installed in the cache directory .
*
* @ public
* /
async function getInstalledBrowsers ( options ) {
return new Cache _js _1 . Cache ( options . cacheDir ) . getInstalledBrowsers ( ) ;
}
exports . getInstalledBrowsers = getInstalledBrowsers ;
/ * *
* @ public
* /
async function canDownload ( options ) {
options . platform ? ? = ( 0 , detectPlatform _js _1 . detectBrowserPlatform ) ( ) ;
if ( ! options . platform ) {
throw new Error ( ` Cannot download a binary for the provided platform: ${ os _1 . default . platform ( ) } ( ${ os _1 . default . arch ( ) } ) ` ) ;
}
return await ( 0 , httpUtil _js _1 . headHttpRequest ) ( getDownloadUrl ( options . browser , options . platform , options . buildId , options . baseUrl ) ) ;
}
exports . canDownload = canDownload ;
function getDownloadUrl ( browser , platform , buildId , baseUrl ) {
return new URL ( browser _data _js _1 . downloadUrls [ browser ] ( platform , buildId , baseUrl ) ) ;
}
//# sourceMappingURL=install.js.map