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

@@ -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 = {
'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}`});
}
});
req.end();
const headers: Record<string, string> = {
'User-Agent': `Mozilla/5.0 (${process.platform} ${process.arch}) setup-php`
};
if (auth_token) {
headers['Authorization'] = 'Bearer ' + auth_token;
}
try {
const response = await globalThis.fetch(input_url, {
headers,
redirect: redirect_count > 0 ? 'follow' : 'manual'
});
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}`};
}
);
return await fetch_promise;
} catch (error) {
return {error: `Fetch error: ${(error as Error).message}`};
}
}