eval-kanban 0.1.0 → 0.1.1
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/bin/download.js +11 -7
- package/package.json +1 -1
package/bin/download.js
CHANGED
|
@@ -30,25 +30,29 @@ async function fetchJson(url) {
|
|
|
30
30
|
|
|
31
31
|
async function downloadFile(url, destPath, onProgress) {
|
|
32
32
|
const tempPath = destPath + '.tmp';
|
|
33
|
+
|
|
33
34
|
return new Promise((resolve, reject) => {
|
|
34
|
-
|
|
35
|
+
let file = null;
|
|
35
36
|
|
|
36
37
|
const cleanup = () => {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
38
|
+
if (file) {
|
|
39
|
+
try { file.close(); } catch {}
|
|
40
|
+
}
|
|
41
|
+
try { fs.unlinkSync(tempPath); } catch {}
|
|
40
42
|
};
|
|
41
43
|
|
|
42
44
|
const doRequest = (requestUrl) => {
|
|
45
|
+
// Create new WriteStream for each request (including after redirects)
|
|
46
|
+
file = fs.createWriteStream(tempPath);
|
|
47
|
+
|
|
43
48
|
https.get(requestUrl, { headers: { 'User-Agent': 'eval-kanban-cli' } }, (res) => {
|
|
44
49
|
if (res.statusCode === 301 || res.statusCode === 302) {
|
|
45
50
|
file.close();
|
|
46
|
-
cleanup
|
|
51
|
+
// Don't cleanup here - just close the stream and follow redirect
|
|
47
52
|
return doRequest(res.headers.location);
|
|
48
53
|
}
|
|
49
54
|
|
|
50
55
|
if (res.statusCode !== 200) {
|
|
51
|
-
file.close();
|
|
52
56
|
cleanup();
|
|
53
57
|
return reject(new Error(`HTTP ${res.statusCode} downloading ${requestUrl}`));
|
|
54
58
|
}
|
|
@@ -60,6 +64,7 @@ async function downloadFile(url, destPath, onProgress) {
|
|
|
60
64
|
downloadedSize += chunk.length;
|
|
61
65
|
if (onProgress) onProgress(downloadedSize, totalSize);
|
|
62
66
|
});
|
|
67
|
+
|
|
63
68
|
res.pipe(file);
|
|
64
69
|
|
|
65
70
|
file.on('finish', () => {
|
|
@@ -73,7 +78,6 @@ async function downloadFile(url, destPath, onProgress) {
|
|
|
73
78
|
}
|
|
74
79
|
});
|
|
75
80
|
}).on('error', (err) => {
|
|
76
|
-
file.close();
|
|
77
81
|
cleanup();
|
|
78
82
|
reject(err);
|
|
79
83
|
});
|