@@ -54,17 +54,23 @@ if (skipDownload) {
54
54
const majorVersion = parseInt ( chromedriverVersion . split ( '.' ) [ 0 ] ) ;
55
55
const useLegacyMethod = majorVersion <= 114 ;
56
56
const platform = getPlatform ( chromedriverVersion ) ;
57
- let downloadedFile = getDownloadFilePath ( useLegacyMethod , tmpPath , platform ) ;
58
- if ( ! useLegacyMethod ) {
57
+ const downloadedFile = getDownloadFilePath ( useLegacyMethod , tmpPath , platform ) ;
58
+ if ( ! useLegacyMethod )
59
59
tmpPath = path . join ( tmpPath , path . basename ( downloadedFile , path . extname ( downloadedFile ) ) ) ;
60
- }
61
60
const chromedriverBinaryFileName = process . platform === 'win32' ? 'chromedriver.exe' : 'chromedriver' ;
62
61
const chromedriverBinaryFilePath = path . resolve ( tmpPath , chromedriverBinaryFileName ) ;
63
62
const chromedriverIsAvailable = await verifyIfChromedriverIsAvailableAndHasCorrectVersion ( chromedriverVersion , chromedriverBinaryFilePath ) ;
64
63
if ( ! chromedriverIsAvailable ) {
65
64
console . log ( 'Current existing ChromeDriver binary is unavailable, proceeding with download and extraction.' ) ;
66
- const cdnBinariesUrl = ( process . env . npm_config_chromedriver_cdnbinariesurl || process . env . CHROMEDRIVER_CDNBINARIESURL || 'https://storage.googleapis.com/chrome-for-testing-public' ) . replace ( / \/ + $ / , '' ) ;
67
- downloadedFile = await downloadFile ( useLegacyMethod ? legacyCdnUrl : cdnBinariesUrl , useLegacyMethod , downloadedFile , chromedriverVersion , platform ) ;
65
+ const configuredfilePath = process . env . npm_config_chromedriver_filepath || process . env . CHROMEDRIVER_FILEPATH ;
66
+ if ( configuredfilePath ) {
67
+ console . log ( 'Using file: ' , configuredfilePath ) ;
68
+ return configuredfilePath ;
69
+ }
70
+ if ( useLegacyMethod )
71
+ await downloadFileLegacy ( legacyCdnUrl , downloadedFile , chromedriverVersion ) ;
72
+ else
73
+ await downloadFile ( cdnUrl , downloadedFile , chromedriverVersion , platform ) ;
68
74
await extractDownload ( extractDirectory , chromedriverBinaryFilePath , downloadedFile ) ;
69
75
}
70
76
const libPath = path . join ( __dirname , 'lib' , 'chromedriver' ) ;
@@ -86,14 +92,14 @@ function getPlatform(chromedriverVersion) {
86
92
if ( process . arch === 'arm64' || process . arch === 's390x' || process . arch === 'x64' ) {
87
93
return 'linux64' ;
88
94
} else {
89
- console . log ( 'Only Linux 64 bits supported.' ) ;
95
+ console . error ( 'Only Linux 64 bits supported.' ) ;
90
96
process . exit ( 1 ) ;
91
97
}
92
98
} else if ( thePlatform === 'darwin' || thePlatform === 'freebsd' ) {
93
99
const osxPlatform = getMacOsRealArch ( chromedriverVersion ) ;
94
100
95
101
if ( ! osxPlatform ) {
96
- console . log ( 'Only Mac 64 bits supported.' ) ;
102
+ console . error ( 'Only Mac 64 bits supported.' ) ;
97
103
process . exit ( 1 ) ;
98
104
}
99
105
@@ -104,36 +110,38 @@ function getPlatform(chromedriverVersion) {
104
110
}
105
111
return ( process . arch === 'x64' ) ? 'win64' : 'win32' ;
106
112
}
107
-
108
- console . log ( 'Unexpected platform or architecture:' , process . platform , process . arch ) ;
113
+ console . error ( 'Unexpected platform or architecture:' , process . platform , process . arch ) ;
109
114
process . exit ( 1 ) ;
110
115
}
111
116
112
117
/**
113
118
* @param {string } cdnUrl
114
- * @param {boolean } useLegacyDownloadMethod
115
119
* @param {string } downloadedFile
116
120
* @param {string } chromedriverVersion
117
121
* @param {string } platform
118
122
*/
119
- async function downloadFile ( cdnUrl , useLegacyDownloadMethod , downloadedFile , chromedriverVersion , platform ) {
120
- const configuredfilePath = process . env . npm_config_chromedriver_filepath || process . env . CHROMEDRIVER_FILEPATH ;
121
- if ( configuredfilePath ) {
122
- console . log ( 'Using file: ' , configuredfilePath ) ;
123
- return configuredfilePath ;
124
- } else {
125
- const fileName = path . basename ( downloadedFile ) ;
126
- if ( useLegacyDownloadMethod ) {
127
- const formattedDownloadUrl = `${ cdnUrl } /${ chromedriverVersion } /${ fileName } ` ;
128
- console . log ( 'Downloading from file: ' , formattedDownloadUrl ) ;
129
- await requestBinary ( getRequestOptions ( formattedDownloadUrl ) , downloadedFile ) ;
130
- } else {
131
- const formattedDownloadUrl = `${ cdnUrl } /${ chromedriverVersion } /${ platform } /${ fileName } ` ;
132
- console . log ( 'Downloading from file: ' , formattedDownloadUrl ) ;
133
- await requestBinary ( getRequestOptions ( formattedDownloadUrl ) , downloadedFile ) ;
134
- }
135
- return downloadedFile ;
123
+ async function downloadFile ( cdnUrl , downloadedFile , chromedriverVersion , platform ) {
124
+ const cdnBinariesUrl = ( process . env . npm_config_chromedriver_cdnbinariesurl || process . env . CHROMEDRIVER_CDNBINARIESURL ) ?. replace ( / \/ + $ / , '' ) ;
125
+ const url = cdnBinariesUrl
126
+ ? `${ cdnBinariesUrl } /${ chromedriverVersion } /${ platform } /${ path . basename ( downloadedFile ) } `
127
+ : await getDownloadUrl ( cdnUrl , chromedriverVersion , platform ) ;
128
+ if ( ! url ) {
129
+ console . error ( `Download url could not be found for version ${ chromedriverVersion } and platform '${ platform } '` ) ;
130
+ process . exit ( 1 ) ;
136
131
}
132
+ console . log ( 'Downloading from file: ' , url ) ;
133
+ await requestBinary ( getRequestOptions ( url ) , downloadedFile ) ;
134
+ }
135
+
136
+ /**
137
+ * @param {string } cdnUrl
138
+ * @param {string } downloadedFile
139
+ * @param {string } chromedriverVersion
140
+ */
141
+ async function downloadFileLegacy ( cdnUrl , downloadedFile , chromedriverVersion ) {
142
+ const url = `${ cdnUrl } /${ chromedriverVersion } /${ path . basename ( downloadedFile ) } ` ;
143
+ console . log ( 'Downloading from file: ' , url ) ;
144
+ await requestBinary ( getRequestOptions ( url ) , downloadedFile ) ;
137
145
}
138
146
139
147
/**
@@ -309,6 +317,21 @@ async function getChromeDriverVersion(cdnUrl, legacyCdnUrl, majorVersion) {
309
317
}
310
318
}
311
319
320
+ /**
321
+ * @param {string } cdnUrl
322
+ * @param {string } version
323
+ * @param {string } platform
324
+ * @returns {Promise<[string]> }
325
+ */
326
+ async function getDownloadUrl ( cdnUrl , version , platform ) {
327
+ const getUrlUrl = `${ cdnUrl } /chrome-for-testing/${ version } .json` ;
328
+ const requestOptions = getRequestOptions ( getUrlUrl ) ;
329
+ // @ts -expect-error
330
+ const response = await axios . request ( requestOptions ) ;
331
+ const url = response . data ?. downloads ?. chromedriver ?. find ( ( /** @type {{ platform: string; } } */ c ) => c . platform == platform ) ?. url ;
332
+ return url ;
333
+ }
334
+
312
335
/**
313
336
*
314
337
* @param {import('axios').AxiosRequestConfig } requestOptions
@@ -321,19 +344,21 @@ async function requestBinary(requestOptions, filePath) {
321
344
// @ts -expect-error
322
345
response = await axios . request ( { responseType : 'stream' , ...requestOptions } ) ;
323
346
} catch ( error ) {
347
+ let errorData = '' ;
324
348
if ( error && error . response ) {
325
349
if ( error . response . status )
326
350
console . error ( 'Error status code:' , error . response . status ) ;
327
351
if ( error . response . data ) {
328
- error . response . data . on ( 'data' , data => console . error ( data . toString ( 'utf8' ) ) ) ;
352
+ error . response . data . on ( 'data' , data => errorData += data . toString ( 'utf8' ) ) ;
329
353
try {
330
354
await finishedAsync ( error . response . data ) ;
331
355
} catch ( error ) {
332
356
console . error ( 'Error downloading entire response:' , error ) ;
333
357
}
334
358
}
335
359
}
336
- throw new Error ( 'Error with http(s) request: ' + error ) ;
360
+ console . error ( `Error with http(s) request:\n${ error } \nError data:\n${ errorData } ` ) ;
361
+ process . exit ( 1 ) ;
337
362
}
338
363
let count = 0 ;
339
364
let notifiedCount = 0 ;
@@ -359,7 +384,7 @@ async function requestBinary(requestOptions, filePath) {
359
384
*/
360
385
async function extractDownload ( dirToExtractTo , chromedriverBinaryFilePath , downloadedFile ) {
361
386
if ( path . extname ( downloadedFile ) !== '.zip' ) {
362
- fs . mkdirSync ( path . dirname ( chromedriverBinaryFilePath ) , { recursive : true } ) ;
387
+ fs . mkdirSync ( path . dirname ( chromedriverBinaryFilePath ) , { recursive : true } ) ;
363
388
fs . copyFileSync ( downloadedFile , chromedriverBinaryFilePath ) ;
364
389
console . log ( 'Skipping zip extraction - binary file found.' ) ;
365
390
return ;
0 commit comments