import { existsSync } from 'fs' ; import del from 'del' ; import type { Logger } from 'src/core/server' ; import type { ChromiumArchivePaths , PackageInfo } from '../chromium' ; import { md5 } from './checksum' ; import { fetch } from './fetch' ; param { BrowserSpec } browsers return { Promise < undefined > } export async function download ( paths , pkg , logger ? ) { const removedFiles = await del ( `${ paths . archivesPath }/**/*` , { force : true , onlyFiles : true , ignore : paths . getAllArchiveFilenames ( ) , } ) ; removedFiles . forEach ( ( path ) => logger ?. warn ( `Deleting unexpected file ${ path }` ) ) ; const invalidChecksums = [ ] ; const { archiveFilename , archiveChecksum } = pkg ; if ( ! archiveFilename || ! archiveChecksum ) { return ; } const resolvedPath = paths . resolvePath ( pkg ) ; const foundChecksum = await md5 ( resolvedPath ) . catch ( ( ) => 'MISSING' ) ; const pathExists = existsSync ( resolvedPath ) ; if ( pathExists && foundChecksum === archiveChecksum ) { logger ?. debug ( `Browser archive for ${ pkg . platform }/${ pkg . architecture } already found in ${ resolvedPath }.` ) ; return ; } if ( ! pathExists ) { logger ?. warn ( `Browser archive for ${ pkg . platform }/${ pkg . architecture } not found in ${ resolvedPath }.` ) ; } if ( foundChecksum !== archiveChecksum ) { logger ?. warn ( `Browser archive checksum for ${ pkg . platform }/${ pkg . architecture } ` + `is ${ foundChecksum } but ${ archiveChecksum } was expected.` ) ; } const url = paths . getDownloadUrl ( pkg ) ; try { const downloadedChecksum = await fetch ( url , resolvedPath , logger ) ; if ( downloadedChecksum !== archiveChecksum ) { logger ?. warn ( `Invalid checksum for ${ pkg . platform }/${ pkg . architecture }: ` + `expected ${ archiveChecksum } got ${ downloadedChecksum }` ) ; invalidChecksums . push ( `${ url } => ${ resolvedPath }` ) ; } } catch ( error ) { throw new Error ( `Failed to download ${ url }: ${ error }` ) ; } if ( invalidChecksums . length ) { const error = new Error ( `Error downloading browsers, checksums incorrect for:\n    - ${ invalidChecksums . join ( '\n    - ' ) }` ) ; logger ?. error ( error ) ; throw error ; } }