mirror of
https://github.com/shivammathur/setup-php.git
synced 2025-09-09 06:14:10 +07:00
Improve error handling in utils.fetch and its uses
Minor fixes in tests Revert to ES2019
This commit is contained in:
25
src/tools.ts
25
src/tools.ts
@ -3,11 +3,16 @@ import * as utils from './utils';
|
||||
export async function getToolSemver(
|
||||
data: Record<string, string>
|
||||
): Promise<string> {
|
||||
const api_url = `https://api.github.com/repos/${data['repository']}/git/matching-refs/tags%2F${data['version_prefix']}${data['version']}`;
|
||||
return JSON.parse(await utils.fetch(api_url))
|
||||
.pop()
|
||||
['ref'].split('/')
|
||||
.pop();
|
||||
const ref: string = data['version_prefix'] + data['version'];
|
||||
const url = `https://api.github.com/repos/${data['repository']}/git/matching-refs/tags%2F${ref}.`;
|
||||
const token: string = await utils.readEnv('COMPOSER_TOKEN');
|
||||
const response: Record<string, string> = await utils.fetch(url, token);
|
||||
if (response.error || response.data === '[]') {
|
||||
data['error'] = response.error ?? `No version found with prefix ${ref}.`;
|
||||
return data['version'];
|
||||
} else {
|
||||
return JSON.parse(response['data']).pop()['ref'].split('/').pop();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -463,6 +468,14 @@ export async function addTools(
|
||||
);
|
||||
script += '\n';
|
||||
switch (true) {
|
||||
case data['error'] !== undefined:
|
||||
script += await utils.addLog(
|
||||
'$cross',
|
||||
data['tool'],
|
||||
data['error'],
|
||||
data['os_version']
|
||||
);
|
||||
break;
|
||||
case 'phar' === data['type']:
|
||||
data['url'] = await getUrl(data);
|
||||
script += await addArchive(data);
|
||||
@ -489,7 +502,7 @@ export async function addTools(
|
||||
'$cross',
|
||||
data['tool'],
|
||||
'Tool ' + data['tool'] + ' is not supported',
|
||||
os_version
|
||||
data['os_version']
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
65
src/utils.ts
65
src/utils.ts
@ -1,4 +1,4 @@
|
||||
import {IncomingMessage} from 'http';
|
||||
import {IncomingMessage, OutgoingHttpHeaders} from 'http';
|
||||
import * as fs from 'fs';
|
||||
import * as https from 'https';
|
||||
import * as path from 'path';
|
||||
@ -48,42 +48,59 @@ export async function getInput(
|
||||
* Function to fetch an URL
|
||||
*
|
||||
* @param input_url
|
||||
* @param auth_token
|
||||
*/
|
||||
export async function fetch(input_url: string): Promise<string> {
|
||||
const fetch_promise: Promise<string> = new Promise(resolve => {
|
||||
const url_object: url.UrlObject = new url.URL(input_url);
|
||||
const auth_token: string = process.env['COMPOSER_TOKEN'] || '';
|
||||
const auth_header: string = auth_token ? 'Bearer' + auth_token : '';
|
||||
const options: https.RequestOptions = {
|
||||
hostname: url_object.hostname,
|
||||
path: url_object.pathname,
|
||||
headers: {
|
||||
authorization: auth_header,
|
||||
'User-Agent': 'setup-php'
|
||||
export async function fetch(
|
||||
input_url: string,
|
||||
auth_token?: string
|
||||
): 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 req = https.get(options, (res: IncomingMessage) => {
|
||||
res.setEncoding('utf8');
|
||||
let body = '';
|
||||
res.on('data', chunk => (body += chunk));
|
||||
res.on('end', () => resolve(body));
|
||||
});
|
||||
req.end();
|
||||
});
|
||||
const options: https.RequestOptions = {
|
||||
hostname: url_object.hostname,
|
||||
path: url_object.pathname,
|
||||
headers: headers
|
||||
};
|
||||
const req = https.get(options, (res: IncomingMessage) => {
|
||||
if (res.statusCode != 200) {
|
||||
resolve({error: `${res.statusCode}: ${res.statusMessage}`});
|
||||
} else {
|
||||
let body = '';
|
||||
res.setEncoding('utf8');
|
||||
res.on('data', chunk => (body += chunk));
|
||||
res.on('end', () => resolve({data: `${body}`}));
|
||||
}
|
||||
});
|
||||
req.end();
|
||||
}
|
||||
);
|
||||
return await fetch_promise;
|
||||
}
|
||||
|
||||
/** Function to get manifest URL
|
||||
*
|
||||
*/
|
||||
export async function getManifestURL(): Promise<string> {
|
||||
return 'https://raw.githubusercontent.com/shivammathur/setup-php/develop/src/configs/php-versions.json';
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to parse PHP version.
|
||||
*
|
||||
* @param version
|
||||
*/
|
||||
export async function parseVersion(version: string): Promise<string> {
|
||||
const manifest =
|
||||
'https://raw.githubusercontent.com/shivammathur/setup-php/develop/src/configs/php-versions.json';
|
||||
const manifest = await getManifestURL();
|
||||
switch (true) {
|
||||
case /^(latest|\d+\.x)$/.test(version):
|
||||
return JSON.parse(await fetch(manifest))[version];
|
||||
return JSON.parse((await fetch(manifest))['data'])[version];
|
||||
default:
|
||||
switch (true) {
|
||||
case version.length > 1:
|
||||
|
Reference in New Issue
Block a user