Skip to content

Commit 215e985

Browse files
committed
feat: implement download retry mechanism and prevent duplicate requests
1 parent f59b2ec commit 215e985

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

index.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const tags = {
2323
missing: '[MISSING]',
2424
};
2525

26+
const pendingDownloads = new Set();
27+
2628
const 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

125129
userInputReader.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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "resource-save-script",
3-
"version": "1.0.13",
3+
"version": "1.0.14",
44
"description": "HTTP server with auto-download capability",
55
"homepage": "https://github.com/luckfunc/resource-save-script#readme",
66
"bugs": {

0 commit comments

Comments
 (0)