Skip to content

Commit 0c8ae0b

Browse files
committed
fix: download was failing
1 parent 125b290 commit 0c8ae0b

File tree

2 files changed

+51
-20
lines changed

2 files changed

+51
-20
lines changed

src/getLatestNodeVersion.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
import {fetchLatestNodeVersion} from "./nodeDownloader.js";
1+
import { fetchLatestNodeVersion } from "./nodeDownloader.js";
2+
3+
(async () => {
4+
try {
5+
const latestVersion = await fetchLatestNodeVersion();
6+
console.log(latestVersion);
7+
} catch (error) {
8+
console.error('Error fetching the latest Node.js version:', error.message);
9+
} finally {
10+
process.exit(0); // Ensure the process exits after execution
11+
}
12+
})();
213

3-
console.log(await fetchLatestNodeVersion());

src/nodeDownloader.js

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,56 @@ import * as fs from "fs";
44
import {fileURLToPath} from 'url';
55
import {dirname} from 'path';
66

7+
import { URL } from 'url';
78

89
const LTS_URL_PREFIX = 'https://nodejs.org/dist/latest-v20.x/';
910

1011
/**
11-
Fetches the latest Node.js version by making a request to a specified URL.
12-
@returns {Promise<string>} A promise that resolves with the latest Node.js version string on success,
13-
or rejects with an error if the latest version cannot be found.
12+
* Fetches the latest Node.js version by making a request to a specified URL.
13+
* @returns {Promise<string>} A promise that resolves with the latest Node.js version string on success,
14+
* or rejects with an error if the latest version cannot be found.
1415
*/
15-
export async function fetchLatestNodeVersion() {
16+
export async function fetchLatestNodeVersion() {
1617
return new Promise((resolve, reject) => {
17-
https.get(LTS_URL_PREFIX, (res) => {
18-
let data = '';
19-
20-
res.on('data', (chunk) => {
21-
data += chunk;
22-
});
23-
res.on('end', () => {
18+
const fetch = (url) => {
19+
https.get(url, (res) => {
20+
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
21+
const redirectUrl = new URL(res.headers.location, url).href;
22+
fetch(redirectUrl);
23+
return;
24+
}
2425

25-
const versionMatch = /node-v(\d+\.\d+\.\d+)/.exec(data);
26-
if (versionMatch) {
27-
resolve(versionMatch[1]);
28-
} else {
29-
reject(new Error('Could not find latest Node.js version'));
26+
if (res.statusCode !== 200) {
27+
reject(new Error(`Request failed with status code: ${res.statusCode}`));
28+
return;
3029
}
31-
});
32-
}).on('error', reject);
30+
31+
let data = '';
32+
33+
res.on('data', (chunk) => {
34+
data += chunk;
35+
});
36+
37+
res.on('end', () => {
38+
try {
39+
const versionMatch = /node-v(\d+\.\d+\.\d+)/.exec(data);
40+
if (versionMatch) {
41+
resolve(versionMatch[1]);
42+
} else {
43+
reject(new Error('Could not find the latest Node.js version in the data.'));
44+
}
45+
} catch (error) {
46+
reject(error);
47+
}
48+
});
49+
}).on('error', reject);
50+
};
51+
52+
fetch(LTS_URL_PREFIX);
3353
});
3454
}
3555

56+
3657
function getAssetsFolder() {
3758
const fullyQualifiedFileName = fileURLToPath(import.meta.url);
3859
const assets = `${dirname(fullyQualifiedFileName)}/assets`;

0 commit comments

Comments
 (0)