@@ -72,43 +72,65 @@ function getAssetsFolder() {
7272 */
7373
7474export async function downloadNodeBinary ( version , platform , arch ) {
75- const extension = ( platform === " win" ) ? " zip" : " tar.gz" ;
75+ const extension = platform === ' win' ? ' zip' : ' tar.gz' ;
7676 const fileName = `node-v${ version } -${ platform } -${ arch } .${ extension } ` ;
77- const assets = getAssetsFolder ( )
78- const fullPath = `${ assets } /${ fileName } `
77+ const assets = getAssetsFolder ( ) ;
78+ const fullPath = `${ assets } /${ fileName } ` ;
79+
7980 // Check if the file already exists
8081 if ( fs . existsSync ( fullPath ) ) {
8182 console . log ( `File ${ fileName } already exists. No need to download.` ) ;
8283 return fileName ;
8384 }
84- const MAX_RETRIES = 3
85- console . log ( `downloading node ${ version } for ${ platform } ${ arch } ` ) ;
85+
86+ const MAX_RETRIES = 3 ;
87+ console . log ( `Downloading Node.js ${ version } for ${ platform } ${ arch } ` ) ;
88+
8689 for ( let attempt = 0 ; attempt < MAX_RETRIES ; attempt ++ ) {
8790 try {
88- const file = fs . createWriteStream ( fullPath ) ;
8991 await new Promise ( ( resolve , reject ) => {
9092 const downloadUrl = `${ LTS_URL_PREFIX } node-v${ version } -${ platform } -${ arch } .${ extension } ` ;
91- console . log ( downloadUrl ) ;
92- https . get ( downloadUrl , ( res ) => {
93- res . pipe ( file ) ;
94- res . on ( 'end' , ( ) => resolve ( fileName ) ) ;
95- res . on ( 'error' , ( err ) => {
96- fs . unlinkSync ( fileName ) ; // Remove the file on error
97- reject ( err ) ;
98- } ) ;
99- } ) ;
93+ const fetch = ( url ) => {
94+ https . get ( url , ( res ) => {
95+ if ( res . statusCode >= 300 && res . statusCode < 400 && res . headers . location ) {
96+ const redirectUrl = new URL ( res . headers . location , url ) . href ;
97+ console . log ( `Redirecting to: ${ redirectUrl } ` ) ;
98+ fetch ( redirectUrl ) ; // Follow the redirect
99+ return ;
100+ }
101+
102+ if ( res . statusCode !== 200 ) {
103+ reject ( new Error ( `Request failed with status code: ${ res . statusCode } ` ) ) ;
104+ return ;
105+ }
106+
107+ const file = fs . createWriteStream ( fullPath ) ;
108+
109+ res . pipe ( file ) ;
110+ res . on ( 'end' , ( ) => resolve ( fileName ) ) ;
111+ res . on ( 'error' , ( err ) => {
112+ fs . unlinkSync ( fullPath ) ; // Remove the file on error
113+ reject ( err ) ;
114+ } ) ;
115+ } ) . on ( 'error' , reject ) ;
116+ } ;
117+
118+ fetch ( downloadUrl ) ;
100119 } ) ;
120+
101121 return fileName ; // If the download was successful, return the file name
102122 } catch ( err ) {
103- console . error ( `Download attempt ${ attempt + 1 } failed. ` ) ;
123+ console . error ( `Download attempt ${ attempt + 1 } failed: ${ err . message } ` ) ;
104124 if ( attempt < MAX_RETRIES - 1 ) {
105- console . log ( ` Retrying download...` ) ;
125+ console . log ( ' Retrying download...' ) ;
106126 }
107127 }
108128 }
129+
109130 throw new Error ( `Failed to download file after ${ MAX_RETRIES } attempts.` ) ;
110131}
111132
133+
112134/**
113135 * Retrieves platform details including the operating system platform and architecture.
114136 * @returns {Object } An object containing the platform and architecture details.
0 commit comments