feat: implement redirect for fetch-yarn-deps

This would fix NixOS/nixpkgs#245415
This commit is contained in:
MicroBlock
2023-07-26 15:13:37 +08:00
committed by GitHub
parent 49caf90f03
commit c2b8bf7f4e
@@ -22,7 +22,14 @@ const exec = async (...args) => {
const downloadFileHttps = (fileName, url, expectedHash, hashType = 'sha1') => {
return new Promise((resolve, reject) => {
https.get(url, (res) => {
const get = (url, redirects = 0) => https.get(url, (res) => {
if(redirects > 10) {
reject('Too many redirects!');
return;
}
if(res.statusCode === 301 || res.statusCode === 302) {
return get(res.headers.location, redirects + 1)
}
const file = fs.createWriteStream(fileName)
const hash = crypto.createHash(hashType)
res.pipe(file)
@@ -35,6 +42,7 @@ const downloadFileHttps = (fileName, url, expectedHash, hashType = 'sha1') => {
})
res.on('error', e => reject(e))
})
get(url)
})
}