Update to Node 24

Update fetch.ts to use native fetch() API

Use immutable sort in tools.ts
This commit is contained in:
Shivam Mathur
2026-01-20 06:29:00 +05:30
parent f89a301251
commit 46ae35f333
6 changed files with 44 additions and 57 deletions

View File

@@ -19,7 +19,9 @@ it('checking fetch', async () => {
Location: host_url + '/pong' Location: host_url + '/pong'
}) })
.get('/pong') .get('/pong')
.reply(200, 'pong'); .reply(200, 'pong')
.get('/error')
.replyWithError('Network failure');
let response: Record<string, string> = await fetch.fetch(manifest_url); let response: Record<string, string> = await fetch.fetch(manifest_url);
expect(response.error).toBe(undefined); expect(response.error).toBe(undefined);
@@ -36,4 +38,8 @@ it('checking fetch', async () => {
response = await fetch.fetch(manifest_url, 'invalid_token'); response = await fetch.fetch(manifest_url, 'invalid_token');
expect(response.error).not.toBe(undefined); expect(response.error).not.toBe(undefined);
expect(response.data).toBe(undefined); expect(response.data).toBe(undefined);
response = await fetch.fetch(host_url + '/error');
expect(response.error).toContain('Fetch error:');
expect(response.data).toBe(undefined);
}); });

View File

@@ -34,5 +34,5 @@ outputs:
php-version: php-version:
description: 'PHP version in semver format' description: 'PHP version in semver format'
runs: runs:
using: 'node20' using: 'node24'
main: 'dist/index.js' main: 'dist/index.js'

4
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -1,9 +1,5 @@
import {IncomingMessage, OutgoingHttpHeaders} from 'http';
import * as https from 'https';
import * as url from 'url';
/** /**
* Function to fetch a URL * Function to fetch a URL using native fetch API (Node 24+)
* *
* @param input_url * @param input_url
* @param auth_token * @param auth_token
@@ -14,43 +10,31 @@ export async function fetch(
auth_token?: string, auth_token?: string,
redirect_count = 5 redirect_count = 5
): Promise<Record<string, string>> { ): Promise<Record<string, string>> {
const fetch_promise: Promise<Record<string, string>> = new Promise( const headers: Record<string, string> = {
resolve => { 'User-Agent': `Mozilla/5.0 (${process.platform} ${process.arch}) setup-php`
const url_object: url.UrlObject = new url.URL(input_url); };
const headers: OutgoingHttpHeaders = { if (auth_token) {
'User-Agent': `Mozilla/5.0 (${process.platform} ${process.arch}) setup-php` headers['Authorization'] = 'Bearer ' + auth_token;
}; }
if (auth_token) {
headers.authorization = 'Bearer ' + auth_token; try {
} const response = await globalThis.fetch(input_url, {
const options: https.RequestOptions = { headers,
hostname: url_object.hostname, redirect: redirect_count > 0 ? 'follow' : 'manual'
path: url_object.pathname, });
headers: headers,
agent: new https.Agent({keepAlive: false}) if (response.ok) {
}; const data = await response.text();
const req = https.get(options, (res: IncomingMessage) => { return {data};
if (res.statusCode === 200) { } else if (
let body = ''; [301, 302, 303, 307, 308].includes(response.status) &&
res.setEncoding('utf8'); redirect_count <= 0
res.on('data', chunk => (body += chunk)); ) {
res.on('end', () => resolve({data: `${body}`})); return {error: `${response.status}: Redirect error`};
} else if ( } else {
[301, 302, 303, 307, 308].includes(res.statusCode as number) return {error: `${response.status}: ${response.statusText}`};
) {
if (redirect_count > 0 && res.headers.location) {
fetch(res.headers.location, auth_token, redirect_count--).then(
resolve
);
} else {
resolve({error: `${res.statusCode}: Redirect error`});
}
} else {
resolve({error: `${res.statusCode}: ${res.statusMessage}`});
}
});
req.end();
} }
); } catch (error) {
return await fetch_promise; return {error: `Fetch error: ${(error as Error).message}`};
}
} }

View File

@@ -46,14 +46,14 @@ export async function getSemverVersion(data: RS): Promise<string> {
fixedToOriginal.set(f, t); fixedToOriginal.set(f, t);
return f; return f;
}); });
fixed.sort((a, b) => { const sorted = fixed.toSorted((a, b) => {
try { try {
return cv.compareVersions(b, a); return cv.compareVersions(b, a);
} catch { } catch {
return b.localeCompare(a, 'en', {numeric: true, sensitivity: 'base'}); return b.localeCompare(a, 'en', {numeric: true, sensitivity: 'base'});
} }
}); });
return fixedToOriginal.get(fixed[0]) ?? fixed[0]; return fixedToOriginal.get(sorted[0]) ?? sorted[0];
} }
} }
@@ -74,13 +74,10 @@ export async function getLatestVersion(data: RS): Promise<string> {
...resp['data'].matchAll(/releases\/tag\/([a-zA-Z]*)?(\d+.\d+.\d+)"/g) ...resp['data'].matchAll(/releases\/tag\/([a-zA-Z]*)?(\d+.\d+.\d+)"/g)
].map(match => match[2]); ].map(match => match[2]);
return ( const sorted = releases.toSorted((a: string, b: string) =>
releases a.localeCompare(b, undefined, {numeric: true})
.sort((a: string, b: string) =>
a.localeCompare(b, undefined, {numeric: true})
)
.pop() || 'latest'
); );
return sorted.at(-1) || 'latest';
} }
return 'latest'; return 'latest';
} }

View File

@@ -3,7 +3,7 @@
"declaration": true, "declaration": true,
"esModuleInterop": true, "esModuleInterop": true,
"lib": [ "lib": [
"ES2021" "ES2024"
], ],
"module": "commonjs", "module": "commonjs",
"moduleResolution": "node", "moduleResolution": "node",
@@ -13,7 +13,7 @@
"rootDir": "./src", "rootDir": "./src",
"sourceMap": true, "sourceMap": true,
"strict": true, "strict": true,
"target": "ES2021" "target": "ES2024"
}, },
"exclude": ["__tests__", "lib", "node_modules"] "exclude": ["__tests__", "lib", "node_modules"]
} }