Skip to content

Commit fb6d4c8

Browse files
committed
fix: exit process once execution is completed and handle url redirects
1 parent ff3e059 commit fb6d4c8

File tree

2 files changed

+57
-25
lines changed

2 files changed

+57
-25
lines changed

src/download.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
import {downloadNodeBinary, fetchLatestNodeVersion} from "./nodeDownloader.js";
2-
import platforms from "./platforms.json" assert {type: 'json'};
1+
import { downloadNodeBinary, fetchLatestNodeVersion } from "./nodeDownloader.js";
2+
import platforms from "./platforms.json" assert { type: 'json' };
3+
4+
(async () => {
5+
try {
6+
const supportedPlatforms = platforms.configurations;
7+
const version = await fetchLatestNodeVersion();
8+
9+
for (const platform of supportedPlatforms) {
10+
const fileName = await downloadNodeBinary(version, platform.platform, platform.arch);
11+
console.log(fileName);
12+
}
13+
} catch (error) {
14+
console.error('Error:', error.message);
15+
} finally {
16+
process.exit(0); // Ensure the process exits properly
17+
}
18+
})();
319

4-
const supportedPlatforms = platforms.configurations;
5-
const version = await fetchLatestNodeVersion();
6-
for (const platform of supportedPlatforms) {
7-
const fileName = await downloadNodeBinary(version, platform.platform, platform.arch);
8-
console.log(fileName);
9-
}

src/nodeDownloader.js

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,43 +72,65 @@ function getAssetsFolder() {
7272
*/
7373

7474
export async function downloadNodeBinary(version, platform, arch) {
75-
const extension = (platform === "win") ? "zip" : "tar.gz";
75+
const extension = platform === 'win' ? 'zip' : 'tar.gz';
7676
const fileName = `node-v${version}-${platform}-${arch}.${extension}`;
77-
const assets = getAssetsFolder()
78-
const fullPath = `${assets}/${fileName}`
77+
const assets = getAssetsFolder();
78+
const fullPath = `${assets}/${fileName}`;
79+
7980
// Check if the file already exists
8081
if (fs.existsSync(fullPath)) {
8182
console.log(`File ${fileName} already exists. No need to download.`);
8283
return fileName;
8384
}
84-
const MAX_RETRIES = 3
85-
console.log(`downloading node ${version} for ${platform} ${arch}`);
85+
86+
const MAX_RETRIES = 3;
87+
console.log(`Downloading Node.js ${version} for ${platform} ${arch}`);
88+
8689
for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
8790
try {
88-
const file = fs.createWriteStream(fullPath);
8991
await new Promise((resolve, reject) => {
9092
const downloadUrl = `${LTS_URL_PREFIX}node-v${version}-${platform}-${arch}.${extension}`;
91-
console.log(downloadUrl);
92-
https.get(downloadUrl, (res) => {
93-
res.pipe(file);
94-
res.on('end', () => resolve(fileName));
95-
res.on('error', (err) => {
96-
fs.unlinkSync(fileName); // Remove the file on error
97-
reject(err);
98-
});
99-
});
93+
const fetch = (url) => {
94+
https.get(url, (res) => {
95+
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
96+
const redirectUrl = new URL(res.headers.location, url).href;
97+
console.log(`Redirecting to: ${redirectUrl}`);
98+
fetch(redirectUrl); // Follow the redirect
99+
return;
100+
}
101+
102+
if (res.statusCode !== 200) {
103+
reject(new Error(`Request failed with status code: ${res.statusCode}`));
104+
return;
105+
}
106+
107+
const file = fs.createWriteStream(fullPath);
108+
109+
res.pipe(file);
110+
res.on('end', () => resolve(fileName));
111+
res.on('error', (err) => {
112+
fs.unlinkSync(fullPath); // Remove the file on error
113+
reject(err);
114+
});
115+
}).on('error', reject);
116+
};
117+
118+
fetch(downloadUrl);
100119
});
120+
101121
return fileName; // If the download was successful, return the file name
102122
} catch (err) {
103-
console.error(`Download attempt ${attempt + 1} failed.`);
123+
console.error(`Download attempt ${attempt + 1} failed: ${err.message}`);
104124
if (attempt < MAX_RETRIES - 1) {
105-
console.log(`Retrying download...`);
125+
console.log('Retrying download...');
106126
}
107127
}
108128
}
129+
109130
throw new Error(`Failed to download file after ${MAX_RETRIES} attempts.`);
110131
}
111132

133+
112134
/**
113135
* Retrieves platform details including the operating system platform and architecture.
114136
* @returns {Object} An object containing the platform and architecture details.

0 commit comments

Comments
 (0)