(url, maxRedirects = 10)
| 92 | } |
| 93 | |
| 94 | function download(url, maxRedirects = 10) { |
| 95 | if (!url.startsWith("https")) { |
| 96 | return Promise.reject(new Error(`Refusing non-HTTPS download: ${url}`)); |
| 97 | } |
| 98 | if (maxRedirects <= 0) { |
| 99 | return Promise.reject(new Error(`Too many redirects fetching ${url}`)); |
| 100 | } |
| 101 | return new Promise((resolve, reject) => { |
| 102 | https.get(url, (res) => { |
| 103 | if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) { |
| 104 | res.resume(); |
| 105 | download(res.headers.location, maxRedirects - 1).then(resolve).catch(reject); |
| 106 | return; |
| 107 | } |
| 108 | if (res.statusCode !== 200) { |
| 109 | res.resume(); |
| 110 | reject(new Error(`HTTP ${res.statusCode} fetching ${url}`)); |
| 111 | return; |
| 112 | } |
| 113 | resolve(res); |
| 114 | }).on("error", reject); |
| 115 | }); |
| 116 | } |
| 117 | |
| 118 | async function downloadText(url) { |
| 119 | const res = await download(url); |
no outgoing calls
no test coverage detected