mirror-url -implementation

This commit is contained in:
Aparna Jyothi 2025-02-19 19:10:42 +05:30
parent 802632921f
commit 1b0ef2d227
13 changed files with 1223 additions and 184 deletions

View file

@ -104,6 +104,31 @@ export default abstract class BaseDistribution {
return response.result || [];
}
protected async getMirrorUrlVersions(): Promise<INodeVersion[]> {
const initialUrl = this.getDistributionUrl();
const dataUrl = `${initialUrl}/index.json`;
try {
const response = await this.httpClient.getJson<INodeVersion[]>(dataUrl);
return response.result || [];
} catch (err) {
if (
err instanceof Error &&
err.message.includes('getaddrinfo EAI_AGAIN')
) {
core.error(`Network error: Failed to resolve the server at ${dataUrl}.
Please check your DNS settings or verify that the URL is correct.`);
} else if (err instanceof hc.HttpClientError && err.statusCode === 404) {
core.error(`404 Error: Unable to find versions at ${dataUrl}.
Please verify that the mirror URL is valid.`);
} else {
core.error(`Failed to fetch Node.js versions from ${dataUrl}.
Please check the URL and try again.}`);
}
throw err;
}
}
protected getNodejsDistInfo(version: string) {
const osArch: string = this.translateArchToDistUrl(this.nodeInfo.arch);
version = semver.clean(version) || '';
@ -128,6 +153,33 @@ export default abstract class BaseDistribution {
};
}
protected getNodejsMirrorURLInfo(version: string) {
const mirrorURL = this.nodeInfo.mirrorURL;
const osArch: string = this.translateArchToDistUrl(this.nodeInfo.arch);
version = semver.clean(version) || '';
const fileName: string =
this.osPlat == 'win32'
? `node-v${version}-win-${osArch}`
: `node-v${version}-${this.osPlat}-${osArch}`;
const urlFileName: string =
this.osPlat == 'win32'
? this.nodeInfo.arch === 'arm64'
? `${fileName}.zip`
: `${fileName}.7z`
: `${fileName}.tar.gz`;
const url = `${mirrorURL}/v${version}/${urlFileName}`;
return <INodeVersionInfo>{
downloadUrl: url,
resolvedVersion: version,
arch: osArch,
fileName: fileName
};
}
protected async downloadNodejs(info: INodeVersionInfo) {
let downloadPath = '';
core.info(
@ -143,9 +195,23 @@ export default abstract class BaseDistribution {
) {
return await this.acquireWindowsNodeFromFallbackLocation(
info.resolvedVersion,
info.arch
info.arch,
info.downloadUrl
);
}
// Handle network-related issues (e.g., DNS resolution failures)
if (
err instanceof Error &&
err.message.includes('getaddrinfo EAI_AGAIN')
) {
core.error(
`Network error: Failed to resolve the server at ${info.downloadUrl}.
This could be due to a DNS resolution issue. Please verify the URL or check your network connection.`
);
}
core.error(
`Download failed from ${info.downloadUrl}. Please check the URl and try again.`
);
throw err;
}
@ -166,9 +232,11 @@ export default abstract class BaseDistribution {
protected async acquireWindowsNodeFromFallbackLocation(
version: string,
arch: string = os.arch()
arch: string = os.arch(),
downloadUrl: string
): Promise<string> {
const initialUrl = this.getDistributionUrl();
core.info('url: ' + initialUrl);
const osArch: string = this.translateArchToDistUrl(arch);
// Create temporary folder to download to
@ -185,6 +253,12 @@ export default abstract class BaseDistribution {
core.info(`Downloading only node binary from ${exeUrl}`);
if (downloadUrl != exeUrl) {
core.error(
'unable to download node binary with the provided URL. Please check and try again'
);
}
const exePath = await tc.downloadTool(exeUrl);
await io.cp(exePath, path.join(tempDir, 'node.exe'));
const libPath = await tc.downloadTool(libUrl);

View file

@ -4,6 +4,7 @@ export interface NodeInputs {
auth?: string;
checkLatest: boolean;
stable: boolean;
mirrorURL?: string;
}
export interface INodeVersionInfo {

View file

@ -1,13 +1,29 @@
import BasePrereleaseNodejs from '../base-distribution-prerelease';
import {NodeInputs} from '../base-models';
import * as core from '@actions/core';
export default class NightlyNodejs extends BasePrereleaseNodejs {
protected distribution = 'nightly';
constructor(nodeInfo: NodeInputs) {
super(nodeInfo);
}
protected getDistributionUrl(): string {
return 'https://nodejs.org/download/nightly';
if (this.nodeInfo.mirrorURL) {
if (this.nodeInfo.mirrorURL != '') {
return this.nodeInfo.mirrorURL;
} else {
if (this.nodeInfo.mirrorURL === '') {
throw new Error(
'Mirror URL is empty. Please provide a valid mirror URL.'
);
} else {
throw new Error('Mirror URL is not a valid');
}
}
} else {
return 'https://nodejs.org/download/nightly';
}
}
}

View file

@ -15,115 +15,136 @@ export default class OfficialBuilds extends BaseDistribution {
}
public async setupNodeJs() {
let manifest: tc.IToolRelease[] | undefined;
let nodeJsVersions: INodeVersion[] | undefined;
const osArch = this.translateArchToDistUrl(this.nodeInfo.arch);
if (this.isLtsAlias(this.nodeInfo.versionSpec)) {
core.info('Attempt to resolve LTS alias from manifest...');
// No try-catch since it's not possible to resolve LTS alias without manifest
manifest = await this.getManifest();
this.nodeInfo.versionSpec = this.resolveLtsAliasFromManifest(
this.nodeInfo.versionSpec,
this.nodeInfo.stable,
manifest
);
}
if (this.isLatestSyntax(this.nodeInfo.versionSpec)) {
nodeJsVersions = await this.getNodeJsVersions();
const versions = this.filterVersions(nodeJsVersions);
this.nodeInfo.versionSpec = this.evaluateVersions(versions);
core.info('getting latest node version...');
}
if (this.nodeInfo.checkLatest) {
core.info('Attempt to resolve the latest version from manifest...');
const resolvedVersion = await this.resolveVersionFromManifest(
this.nodeInfo.versionSpec,
this.nodeInfo.stable,
osArch,
manifest
);
if (resolvedVersion) {
this.nodeInfo.versionSpec = resolvedVersion;
core.info(`Resolved as '${resolvedVersion}'`);
} else {
core.info(
`Failed to resolve version ${this.nodeInfo.versionSpec} from manifest`
if (this.nodeInfo.mirrorURL) {
if (this.nodeInfo.mirrorURL === '') {
throw new Error(
'Mirror URL is empty. Please provide a valid mirror URL.'
);
}
}
let toolPath = this.findVersionInHostedToolCacheDirectory();
if (toolPath) {
core.info(`Found in cache @ ${toolPath}`);
this.addToolPath(toolPath);
return;
}
let downloadPath = '';
try {
core.info(`Attempting to download ${this.nodeInfo.versionSpec}...`);
const versionInfo = await this.getInfoFromManifest(
this.nodeInfo.versionSpec,
this.nodeInfo.stable,
osArch,
manifest
);
if (versionInfo) {
core.info(
`Acquiring ${versionInfo.resolvedVersion} - ${versionInfo.arch} from ${versionInfo.downloadUrl}`
);
downloadPath = await tc.downloadTool(
versionInfo.downloadUrl,
undefined,
this.nodeInfo.auth
);
let downloadPath = '';
let toolPath = '';
try {
core.info(`Attempting to download using mirror URL...`);
downloadPath = await this.downloadFromMirrorURL(); // Attempt to download from the mirror
core.info('downloadPath from downloadFromMirrorURL() ' + downloadPath);
if (downloadPath) {
toolPath = await this.extractArchive(
downloadPath,
versionInfo,
false
toolPath = downloadPath;
}
} catch (err) {
core.info((err as Error).message);
core.debug((err as Error).stack ?? 'empty stack');
}
} else {
let manifest: tc.IToolRelease[] | undefined;
let nodeJsVersions: INodeVersion[] | undefined;
const osArch = this.translateArchToDistUrl(this.nodeInfo.arch);
if (this.isLtsAlias(this.nodeInfo.versionSpec)) {
core.info('Attempt to resolve LTS alias from manifest...');
// No try-catch since it's not possible to resolve LTS alias without manifest
manifest = await this.getManifest();
this.nodeInfo.versionSpec = this.resolveLtsAliasFromManifest(
this.nodeInfo.versionSpec,
this.nodeInfo.stable,
manifest
);
}
if (this.isLatestSyntax(this.nodeInfo.versionSpec)) {
nodeJsVersions = await this.getNodeJsVersions();
const versions = this.filterVersions(nodeJsVersions);
this.nodeInfo.versionSpec = this.evaluateVersions(versions);
core.info('getting latest node version...');
}
if (this.nodeInfo.checkLatest) {
core.info('Attempt to resolve the latest version from manifest...');
const resolvedVersion = await this.resolveVersionFromManifest(
this.nodeInfo.versionSpec,
this.nodeInfo.stable,
osArch,
manifest
);
if (resolvedVersion) {
this.nodeInfo.versionSpec = resolvedVersion;
core.info(`Resolved as '${resolvedVersion}'`);
} else {
core.info(
`Failed to resolve version ${this.nodeInfo.versionSpec} from manifest`
);
}
} else {
core.info(
'Not found in manifest. Falling back to download directly from Node'
);
}
} catch (err) {
// Rate limit?
if (
err instanceof tc.HTTPError &&
(err.httpStatusCode === 403 || err.httpStatusCode === 429)
) {
core.info(
`Received HTTP status code ${err.httpStatusCode}. This usually indicates the rate limit has been exceeded`
);
} else {
core.info((err as Error).message);
let toolPath = this.findVersionInHostedToolCacheDirectory();
if (toolPath) {
core.info(`Found in cache @ ${toolPath}`);
this.addToolPath(toolPath);
return;
}
core.debug((err as Error).stack ?? 'empty stack');
core.info('Falling back to download directly from Node');
}
if (!toolPath) {
toolPath = await this.downloadDirectlyFromNode();
}
let downloadPath = '';
try {
core.info(`Attempting to download ${this.nodeInfo.versionSpec}...`);
if (this.osPlat != 'win32') {
toolPath = path.join(toolPath, 'bin');
}
const versionInfo = await this.getInfoFromManifest(
this.nodeInfo.versionSpec,
this.nodeInfo.stable,
osArch,
manifest
);
core.addPath(toolPath);
if (versionInfo) {
core.info(
`Acquiring ${versionInfo.resolvedVersion} - ${versionInfo.arch} from ${versionInfo.downloadUrl}`
);
downloadPath = await tc.downloadTool(
versionInfo.downloadUrl,
undefined,
this.nodeInfo.auth
);
if (downloadPath) {
toolPath = await this.extractArchive(
downloadPath,
versionInfo,
false
);
}
} else {
core.info(
'Not found in manifest. Falling back to download directly from Node'
);
}
} catch (err) {
// Rate limit?
if (
err instanceof tc.HTTPError &&
(err.httpStatusCode === 403 || err.httpStatusCode === 429)
) {
core.info(
`Received HTTP status code ${err.httpStatusCode}. This usually indicates the rate limit has been exceeded`
);
} else {
core.info((err as Error).message);
}
core.debug((err as Error).stack ?? 'empty stack');
core.info('Falling back to download directly from Node');
}
if (!toolPath) {
toolPath = await this.downloadDirectlyFromNode();
}
if (this.osPlat != 'win32') {
toolPath = path.join(toolPath, 'bin');
}
core.addPath(toolPath);
}
}
protected addToolPath(toolPath: string) {
@ -177,6 +198,9 @@ export default class OfficialBuilds extends BaseDistribution {
}
protected getDistributionUrl(): string {
if (this.nodeInfo.mirrorURL) {
return this.nodeInfo.mirrorURL;
}
return `https://nodejs.org/dist`;
}
@ -291,4 +315,38 @@ export default class OfficialBuilds extends BaseDistribution {
private isLatestSyntax(versionSpec): boolean {
return ['current', 'latest', 'node'].includes(versionSpec);
}
protected async downloadFromMirrorURL() {
const nodeJsVersions = await this.getMirrorUrlVersions();
const versions = this.filterVersions(nodeJsVersions);
const evaluatedVersion = this.evaluateVersions(versions);
if (!evaluatedVersion) {
throw new Error(
`Unable to find Node version '${this.nodeInfo.versionSpec}' for platform ${this.osPlat} and architecture ${this.nodeInfo.arch} from the provided mirror-url ${this.nodeInfo.mirrorURL}. Please check the mirror-url`
);
}
const toolName = this.getNodejsMirrorURLInfo(evaluatedVersion);
try {
const toolPath = await this.downloadNodejs(toolName);
return toolPath;
} catch (error) {
if (error instanceof tc.HTTPError && error.httpStatusCode === 404) {
core.error(
`Node version ${this.nodeInfo.versionSpec} for platform ${this.osPlat} and architecture ${this.nodeInfo.arch} was found but failed to download. ` +
'This usually happens when downloadable binaries are not fully updated at https://nodejs.org/. ' +
'To resolve this issue you may either fall back to the older version or try again later.'
);
} else {
// For any other error type, you can log the error message.
core.error(`An unexpected error occurred like url might not correct`);
}
throw error;
}
}
}

View file

@ -1,12 +1,30 @@
import BaseDistribution from '../base-distribution';
import {NodeInputs} from '../base-models';
import * as core from '@actions/core';
export default class RcBuild extends BaseDistribution {
getDistributionMirrorUrl() {
throw new Error('Method not implemented.');
}
constructor(nodeInfo: NodeInputs) {
super(nodeInfo);
}
getDistributionUrl(): string {
return 'https://nodejs.org/download/rc';
protected getDistributionUrl(): string {
if (this.nodeInfo.mirrorURL) {
if (this.nodeInfo.mirrorURL != '') {
return this.nodeInfo.mirrorURL;
} else {
if (this.nodeInfo.mirrorURL === '') {
throw new Error(
'Mirror URL is empty. Please provide a valid mirror URL.'
);
} else {
throw new Error('Mirror URL is not a valid');
}
}
} else {
return 'https://nodejs.org/download/rc';
}
}
}

View file

@ -1,6 +1,5 @@
import BasePrereleaseNodejs from '../base-distribution-prerelease';
import {NodeInputs} from '../base-models';
export default class CanaryBuild extends BasePrereleaseNodejs {
protected distribution = 'v8-canary';
constructor(nodeInfo: NodeInputs) {
@ -8,6 +7,20 @@ export default class CanaryBuild extends BasePrereleaseNodejs {
}
protected getDistributionUrl(): string {
return 'https://nodejs.org/download/v8-canary';
if (this.nodeInfo.mirrorURL) {
if (this.nodeInfo.mirrorURL != '') {
return this.nodeInfo.mirrorURL;
} else {
if (this.nodeInfo.mirrorURL === '') {
throw new Error(
'Mirror URL is empty. Please provide a valid mirror URL.'
);
} else {
throw new Error('Mirror URL is not a valid');
}
}
} else {
return 'https://nodejs.org/download/v8-canary';
}
}
}

View file

@ -33,6 +33,8 @@ export async function run() {
arch = os.arch();
}
const mirrorURL = core.getInput('mirror-url').trim(); // .trim() to remove any accidental spaces
if (version) {
const token = core.getInput('token');
const auth = !token ? undefined : `token ${token}`;
@ -45,7 +47,8 @@ export async function run() {
checkLatest,
auth,
stable,
arch
arch,
mirrorURL
};
const nodeDistribution = getNodejsDistribution(nodejsInfo);
await nodeDistribution.setupNodeJs();