Skip to content

Commit

Permalink
add retries properly
Browse files Browse the repository at this point in the history
  • Loading branch information
ayys committed Feb 7, 2024
1 parent 7cf5491 commit 327f128
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

44 changes: 23 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,32 @@ const main = async () =>
const percent = Math.round(data.percent * 100);

//Log
info(`Downloaded ${transferred}K out of ${total}K (${percent}%)`);
if (!Number.isNaN(transferred) && !Number.isNaN(total) && !Number.isNaN(percent)) {
info(`Downloaded ${transferred}K out of ${total}K (${percent}%)`);
}
};
const url = isWindows ? 'https://win.wasmer.io' : 'https://get.wasmer.io';
await pipeline(
got.stream(url, {
retry: {
limit: 10,
errorCodes: [
'ETIMEDOUT',
'ECONNRESET',
'EADDRINUSE',
'ECONNREFUSED',
'EPIPE',
'ENOTFOUND',
'ENETUNREACH',
'EAI_AGAIN',
'ERR_NON_2XX_3XX_RESPONSE',
],
let retryAttempts = 0;
const maxRetryAttempts = 10;

while (retryAttempts < maxRetryAttempts) {
try {
await pipeline(
got.stream(url).on('downloadProgress', progressHandler),
createWriteStream(tmp, {
mode: 0o655
})
);
console.log('Downloaded installer.');
break;
}
catch (error) {
retryAttempts++;
if (retryAttempts >= maxRetryAttempts) {
throw new Error(`Failed to download installer after ${retryAttempts} attempts.\nLast error: ${error}`);
}
}).on('downloadProgress', progressHandler),
createWriteStream(tmp, {
mode: 0o655
})
);
}
}
endGroup();

info('Downloaded installer.');
Expand Down

0 comments on commit 327f128

Please sign in to comment.