diff --git a/lib/cache.js b/lib/cache.js index aa31ef6..f0c30c2 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -1,5 +1,6 @@ // Packages const fetch = require('node-fetch') +const retry = require('async-retry') const convertStream = require('stream-to-string') const ms = require('ms') @@ -40,13 +41,17 @@ module.exports = class Cache { headers.Authorization = `token ${token}` } - const { status, body } = await fetch(url, { headers }) + const { status, body } = await retry(async () => { + const response = await fetch(url, { headers }) - if (status !== 200) { - throw new Error( - `Tried to cache RELEASES, but failed fetching ${url}, status ${status}` - ) - } + if (response.status !== 200) { + throw new Error( + `Tried to cache RELEASES, but failed fetching ${url}, status ${status}` + ) + } + + return response; + }, { retries: 3 }); const content = await convertStream(body) const matches = content.match(/[^ ]*\.nupkg/gim) @@ -71,13 +76,17 @@ module.exports = class Cache { headers.Authorization = `token ${token}` } - const response = await fetch(url, { headers }) + const response = await retry(async () => { + const response = await fetch(url, { headers }) - if (response.status !== 200) { - throw new Error( - `GitHub API responded with ${response.status} for url ${url}` - ) - } + if (response.status !== 200) { + throw new Error( + `GitHub API responded with ${response.status} for url ${url}` + ) + } + + return response; + }, { retries: 3 }); const data = await response.json() diff --git a/package.json b/package.json index bb1282f..e979ad0 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ ] }, "dependencies": { + "async-retry": "1.2.3", "date-fns": "1.29.0", "express-useragent": "1.0.12", "fetch": "1.1.0", diff --git a/yarn.lock b/yarn.lock index 0209202..6f05c09 100644 --- a/yarn.lock +++ b/yarn.lock @@ -433,6 +433,13 @@ async-limiter@~1.0.0: resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.0.tgz#78faed8c3d074ab81f22b4e985d79e8738f720f8" integrity sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg== +async-retry@1.2.3: + version "1.2.3" + resolved "https://registry.yarnpkg.com/async-retry/-/async-retry-1.2.3.tgz#a6521f338358d322b1a0012b79030c6f411d1ce0" + integrity sha512-tfDb02Th6CE6pJUF2gjW5ZVjsgwlucVXOEQMvEX9JgSJMs9gAX+Nz3xRuJBKuUYjTSYORqvDBORdAQ3LU59g7Q== + dependencies: + retry "0.12.0" + async@^1.4.0: version "1.5.2" resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a" @@ -5269,6 +5276,11 @@ ret@~0.1.10: resolved "https://registry.yarnpkg.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc" integrity sha512-TTlYpa+OL+vMMNG24xSlQGEJ3B/RzEfUlLct7b5G/ytav+wPrplCpVMFuwzXbkecJrb6IYo1iFb0S9v37754mg== +retry@0.12.0: + version "0.12.0" + resolved "https://registry.yarnpkg.com/retry/-/retry-0.12.0.tgz#1b42a6266a21f07421d1b0b54b7dc167b01c013b" + integrity sha1-G0KmJmoh8HQh0bC1S33BZ7AcATs= + right-align@^0.1.1: version "0.1.3" resolved "https://registry.yarnpkg.com/right-align/-/right-align-0.1.3.tgz#61339b722fe6a3515689210d24e14c96148613ef"