File size: 2,143 Bytes
4b26793
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
--- install-utils.js.backup	2025-11-21 07:38:06.154213581 +0000
+++ install-utils.js	2025-11-21 07:38:44.799438104 +0000
@@ -10,11 +10,27 @@
 const os = require('os');
 const AdmZip = require('adm-zip'); // Use adm-zip instead of spawn
 
-async function downloadFile(url, dest) {
+async function downloadFile(url, dest, maxRedirects = 5) {
   return new Promise((resolve, reject) => {
+    if (maxRedirects < 0) {
+      reject(new Error('Too many redirects'));
+      return;
+    }
+
     const file = fs.createWriteStream(dest);
     https
       .get(url, (res) => {
+        // Handle redirects (301, 302, 303, 307, 308)
+        if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
+          file.close();
+          fs.unlinkSync(dest);
+          // Follow redirect
+          const redirectUrl = new URL(res.headers.location, url).toString();
+          console.log(`Following redirect to ${redirectUrl}`);
+          downloadFile(redirectUrl, dest, maxRedirects - 1).then(resolve).catch(reject);
+          return;
+        }
+
         if (res.statusCode !== 200) {
           file.close();
           fs.unlinkSync(dest);
@@ -39,8 +55,13 @@
   });
 }
 
-async function downloadJson(url) {
+async function downloadJson(url, maxRedirects = 5) {
   return new Promise((resolve, reject) => {
+    if (maxRedirects < 0) {
+      reject(new Error('Too many redirects'));
+      return;
+    }
+
     https
       .get(url, (res) => {
         const { statusCode } = res;
@@ -50,6 +71,16 @@
           reject(new Error('No response statud code from server.'));
           return;
         }
+
+        // Handle redirects (301, 302, 303, 307, 308)
+        if (statusCode >= 300 && statusCode < 400 && res.headers.location) {
+          // Follow redirect
+          const redirectUrl = new URL(res.headers.location, url).toString();
+          console.log(`Following redirect to ${redirectUrl}`);
+          downloadJson(redirectUrl, maxRedirects - 1).then(resolve).catch(reject);
+          return;
+        }
+
         if (statusCode >= 400 && statusCode < 500) {
           resolve(null);
           return;