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'
})
.get('/pong')
.reply(200, 'pong');
.reply(200, 'pong')
.get('/error')
.replyWithError('Network failure');
let response: Record<string, string> = await fetch.fetch(manifest_url);
expect(response.error).toBe(undefined);
@@ -36,4 +38,8 @@ it('checking fetch', async () => {
response = await fetch.fetch(manifest_url, 'invalid_token');
expect(response.error).not.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:
description: 'PHP version in semver format'
runs:
using: 'node20'
using: 'node24'
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 auth_token
@@ -14,43 +10,31 @@ export async function fetch(
auth_token?: string,
redirect_count = 5
): Promise<Record<string, string>> {
const fetch_promise: Promise<Record<string, string>> = new Promise(
resolve => {
const url_object: url.UrlObject = new url.URL(input_url);
const headers: OutgoingHttpHeaders = {
const headers: Record<string, string> = {
'User-Agent': `Mozilla/5.0 (${process.platform} ${process.arch}) setup-php`
};
if (auth_token) {
headers.authorization = 'Bearer ' + auth_token;
}
const options: https.RequestOptions = {
hostname: url_object.hostname,
path: url_object.pathname,
headers: headers,
agent: new https.Agent({keepAlive: false})
};
const req = https.get(options, (res: IncomingMessage) => {
if (res.statusCode === 200) {
let body = '';
res.setEncoding('utf8');
res.on('data', chunk => (body += chunk));
res.on('end', () => resolve({data: `${body}`}));
} else if (
[301, 302, 303, 307, 308].includes(res.statusCode as number)
) {
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}`});
headers['Authorization'] = 'Bearer ' + auth_token;
}
try {
const response = await globalThis.fetch(input_url, {
headers,
redirect: redirect_count > 0 ? 'follow' : 'manual'
});
req.end();
if (response.ok) {
const data = await response.text();
return {data};
} else if (
[301, 302, 303, 307, 308].includes(response.status) &&
redirect_count <= 0
) {
return {error: `${response.status}: Redirect error`};
} else {
return {error: `${response.status}: ${response.statusText}`};
}
} catch (error) {
return {error: `Fetch error: ${(error as Error).message}`};
}
);
return await fetch_promise;
}

View File

@@ -46,14 +46,14 @@ export async function getSemverVersion(data: RS): Promise<string> {
fixedToOriginal.set(f, t);
return f;
});
fixed.sort((a, b) => {
const sorted = fixed.toSorted((a, b) => {
try {
return cv.compareVersions(b, a);
} catch {
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)
].map(match => match[2]);
return (
releases
.sort((a: string, b: string) =>
const sorted = releases.toSorted((a: string, b: string) =>
a.localeCompare(b, undefined, {numeric: true})
)
.pop() || 'latest'
);
return sorted.at(-1) || 'latest';
}
return 'latest';
}

View File

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