@@ -4,35 +4,56 @@ import * as fs from "fs";
44import { fileURLToPath } from 'url' ;
55import { dirname } from 'path' ;
66
7+ import { URL } from 'url' ;
78
89const 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 = / n o d e - 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 = / n o d e - 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+
3657function getAssetsFolder ( ) {
3758 const fullyQualifiedFileName = fileURLToPath ( import . meta. url ) ;
3859 const assets = `${ dirname ( fullyQualifiedFileName ) } /assets` ;
0 commit comments