@@ -7,10 +7,16 @@ import {dirname} from 'path';
77import * as fsExtra from "fs-extra" ;
88import { getPlatformDetails , getSideCarBinName , removeDir } from "./utils.js" ;
99import axios from 'axios' ;
10-
10+ import axiosRetry from 'axios-retry' ;
1111const __filename = fileURLToPath ( import . meta. url ) ;
1212const __dirname = dirname ( __filename ) ;
1313
14+ // Setup axios-retry
15+ axiosRetry ( axios , {
16+ retries : 3 ,
17+ retryDelay : axiosRetry . exponentialDelay ,
18+ retryCondition : ( error ) => error . code === 'ECONNABORTED' || / 5 \d { 2 } / . test ( error . response ?. status )
19+ } ) ;
1420/**
1521 * Downloads the latest Node.js binary for the specified platform and architecture.
1622 * If the file already exists, it will not be downloaded again. If the download fails,
@@ -46,7 +52,7 @@ async function downloadNodeBinary(platform, arch, maxRetries = 3) {
4652
4753 const outputPath = path . resolve ( __dirname , asset . name ) ;
4854 if ( fs . existsSync ( outputPath ) ) {
49- console . log ( ' File already downloaded:' , asset . name ) ;
55+ console . log ( ` File already downloaded: ${ asset . name } ` ) ;
5056 return asset . name ;
5157 }
5258
@@ -62,19 +68,22 @@ async function downloadNodeBinary(platform, arch, maxRetries = 3) {
6268
6369 return new Promise ( ( resolve , reject ) => {
6470 writer . on ( 'finish' , ( ) => {
65- console . log ( ' Download completed:' , asset . name ) ;
71+ console . log ( ` Download completed: ${ asset . name } ` ) ;
6672 resolve ( asset . name ) ;
6773 } ) ;
68- writer . on ( 'error' , err => {
69- fs . unlinkSync ( outputPath ) ; // remove the partially downloaded file
74+ writer . on ( 'error' , ( err ) => {
75+ fs . unlink ( outputPath , ( unlinkErr ) => {
76+ if ( unlinkErr ) console . error ( `Error removing incomplete download: ${ unlinkErr } ` ) ;
77+ } ) ;
7078 reject ( err ) ;
7179 } ) ;
7280 } ) ;
7381
7482 } catch ( err ) {
75- console . error ( ' Error:' , err . message ) ;
83+ console . error ( ` Error: ${ err . message } ` ) ;
7684 if ( maxRetries > 0 ) {
7785 console . log ( 'Retrying download...' ) ;
86+ await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ; // Wait before retrying
7887 return downloadNodeBinary ( platform , arch , maxRetries - 1 ) ;
7988 } else {
8089 throw new Error ( 'Max retries reached, download failed.' ) ;
@@ -230,4 +239,4 @@ console.log(args);
230239const platformDetails = ( args . length === 1 ) ? JSON . parse ( args [ 0 ] ) : getPlatformDetails ( ) ;
231240console . log ( platformDetails ) ;
232241
233- await copyLatestNodeForBuild ( platformDetails . platform , platformDetails . arch ) ;
242+ await copyLatestNodeForBuild ( platformDetails . platform , platformDetails . arch ) ;
0 commit comments