@@ -23,6 +23,8 @@ const tags = {
2323 missing : '[MISSING]' ,
2424} ;
2525
26+ const pendingDownloads = new Set ( ) ;
27+
2628const logger = {
2729 info ( message ) {
2830 console . log ( `${ colors . info } ${ tags . info } ${ message } ${ colors . reset } ` ) ;
@@ -104,14 +106,14 @@ async function downloadFileWithRetries(url, filePath, maxRetries = 3) {
104106 fs . mkdirSync ( path . dirname ( filePath ) , { recursive : true } ) ;
105107 if ( fs . existsSync ( filePath ) ) {
106108 logger . info ( `Skip download, file already exists → ${ filePath } ` ) ;
107- return ;
109+ return true ;
108110 }
109111
110112 for ( let attempt = 1 ; attempt <= maxRetries ; attempt ++ ) {
111113 try {
112114 const fileSize = await streamDownload ( url , filePath ) ;
113115 logger . success ( `Downloaded ${ filePath } (${ fileSize } bytes)` ) ;
114- return ;
116+ return true ;
115117 } catch ( error ) {
116118 if ( attempt < maxRetries ) {
117119 logger . warn ( `Retry ${ attempt } /${ maxRetries } → ${ filePath } ` ) ;
@@ -120,6 +122,8 @@ async function downloadFileWithRetries(url, filePath, maxRetries = 3) {
120122 }
121123 }
122124 }
125+
126+ return false ;
123127}
124128
125129userInputReader . question (
@@ -164,8 +168,18 @@ userInputReader.question(
164168 const localFilePath = path . join ( process . cwd ( ) , decodedPath ) ;
165169 const remoteFileUrl = `${ baseUrl } ${ encodedPath } ` ;
166170
171+ if ( pendingDownloads . has ( localFilePath ) ) {
172+ logger . warn ( `Skip duplicate request → ${ decodedPath } ` ) ;
173+ continue ;
174+ }
175+
176+ pendingDownloads . add ( localFilePath ) ;
167177 logger . missing ( decodedPath , remoteFileUrl , localFilePath ) ;
168- await downloadFileWithRetries ( remoteFileUrl , localFilePath ) ;
178+ try {
179+ await downloadFileWithRetries ( remoteFileUrl , localFilePath ) ;
180+ } finally {
181+ pendingDownloads . delete ( localFilePath ) ;
182+ }
169183 }
170184 } ) ;
171185
0 commit comments