@bitmilldev/warden 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/scripts/postinstall.js +17 -17
package/package.json
CHANGED
package/scripts/postinstall.js
CHANGED
|
@@ -86,24 +86,24 @@ async function main() {
|
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
-
function download(url, dest) {
|
|
89
|
+
function download(url, dest, redirects = 0) {
|
|
90
|
+
if (redirects > 10) return Promise.reject(new Error("Too many redirects"));
|
|
90
91
|
return new Promise((resolve, reject) => {
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
};
|
|
106
|
-
request(url);
|
|
92
|
+
const proto = url.startsWith("https") ? https : require("http");
|
|
93
|
+
proto.get(url, (response) => {
|
|
94
|
+
if (response.statusCode === 301 || response.statusCode === 302) {
|
|
95
|
+
response.resume(); // drain the response
|
|
96
|
+
return resolve(download(response.headers.location, dest, redirects + 1));
|
|
97
|
+
}
|
|
98
|
+
if (response.statusCode !== 200) {
|
|
99
|
+
response.resume();
|
|
100
|
+
return reject(new Error(`HTTP ${response.statusCode} for ${url}`));
|
|
101
|
+
}
|
|
102
|
+
const file = fs.createWriteStream(dest);
|
|
103
|
+
response.pipe(file);
|
|
104
|
+
file.on("finish", () => { file.close(); resolve(); });
|
|
105
|
+
file.on("error", (e) => { fs.unlinkSync(dest); reject(e); });
|
|
106
|
+
}).on("error", reject);
|
|
107
107
|
});
|
|
108
108
|
}
|
|
109
109
|
|