Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mirror-url parameter to allow downloading Node.js from a custom URL #1232

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
added check-latest
  • Loading branch information
aparnajyothi-y committed Mar 4, 2025
commit 8495358ea6b7d3f10cc811c91d001b210350aaf8
38 changes: 33 additions & 5 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
@@ -100124,6 +100124,19 @@ class BaseDistribution {
return evaluatedVersion;
});
}
findMirrorVersionInDist(nodeJsVersions) {
return __awaiter(this, void 0, void 0, function* () {
if (!nodeJsVersions) {
nodeJsVersions = yield this.getNodeJsVersions();
}
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}.`);
}
return evaluatedVersion;
});
}
evaluateVersions(versions) {
let version = '';
const { range, options } = this.validRange(this.nodeInfo.versionSpec);
@@ -100705,28 +100718,43 @@ class OfficialBuilds extends base_distribution_1.default {
}
downloadFromMirrorURL() {
return __awaiter(this, void 0, void 0, function* () {
// Fetch the available Node.js versions from the mirror
const nodeJsVersions = yield this.getMirrorUrlVersions();
// Filter the available versions based on your criteria
const versions = this.filterVersions(nodeJsVersions);
const evaluatedVersion = this.evaluateVersions(versions);
let evaluatedVersion;
// If `checkLatest` is set, use the latest version from the mirror
if (this.nodeInfo.checkLatest) {
evaluatedVersion = yield this.findMirrorVersionInDist(nodeJsVersions);
this.nodeInfo.versionSpec = evaluatedVersion; // Update versionSpec to the latest version
}
else {
// Otherwise, evaluate the version from the filtered list
evaluatedVersion = this.evaluateVersions(versions);
}
// If no version is found, throw an error
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`);
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.`);
}
// Get the tool name for downloading
const toolName = this.getNodejsMirrorURLInfo(evaluatedVersion);
try {
// Try to download the Node.js binaries
const toolPath = yield this.downloadNodejs(toolName);
return toolPath;
}
catch (error) {
// Handle specific HTTP error (404 - Not Found)
if (error instanceof tc.HTTPError && error.httpStatusCode === 404) {
core.setFailed(`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.setFailed(`An unexpected error occurred like url might not correct`);
// For any other error, log the actual error message
core.setFailed(`An unexpected error occurred:'url might not be correct'}`);
}
throw error;
throw error; // Re-throw the error after logging
}
});
}
14 changes: 14 additions & 0 deletions src/distributions/base-distribution.ts
Original file line number Diff line number Diff line change
@@ -63,6 +63,20 @@ export default abstract class BaseDistribution {

return evaluatedVersion;
}
protected async findMirrorVersionInDist(nodeJsVersions?: INodeVersion[]) {
if (!nodeJsVersions) {
nodeJsVersions = await this.getNodeJsVersions();
}
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}.`
);
}

return evaluatedVersion;
}

protected evaluateVersions(versions: string[]): string {
let version = '';
27 changes: 21 additions & 6 deletions src/distributions/official_builds/official_builds.ts
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@

try {
core.info(`Attempting to download using mirror URL...`);
downloadPath = await this.downloadFromMirrorURL(); // Attempt to download from the mirror

Check warning on line 23 in src/distributions/official_builds/official_builds.ts

GitHub Actions / Basic validation / build (macos-latest)

'downloadPath' is assigned a value but never used

Check warning on line 23 in src/distributions/official_builds/official_builds.ts

GitHub Actions / Basic validation / build (ubuntu-latest)

'downloadPath' is assigned a value but never used

Check warning on line 23 in src/distributions/official_builds/official_builds.ts

GitHub Actions / Basic validation / build (windows-latest)

'downloadPath' is assigned a value but never used
} catch (err) {
core.setFailed((err as Error).message);
core.setFailed('Download failed');
@@ -310,38 +310,53 @@
}

protected async downloadFromMirrorURL() {
// Fetch the available Node.js versions from the mirror
const nodeJsVersions = await this.getMirrorUrlVersions();

// Filter the available versions based on your criteria
const versions = this.filterVersions(nodeJsVersions);

const evaluatedVersion = this.evaluateVersions(versions);
let evaluatedVersion;

// If `checkLatest` is set, use the latest version from the mirror
if (this.nodeInfo.checkLatest) {
evaluatedVersion = await this.findMirrorVersionInDist(nodeJsVersions);
this.nodeInfo.versionSpec = evaluatedVersion; // Update versionSpec to the latest version
} else {
// Otherwise, evaluate the version from the filtered list
evaluatedVersion = this.evaluateVersions(versions);
}

// If no version is found, throw an error
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`
`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.`
);
}

// Get the tool name for downloading
const toolName = this.getNodejsMirrorURLInfo(evaluatedVersion);

try {
// Try to download the Node.js binaries
const toolPath = await this.downloadNodejs(toolName);

return toolPath;
} catch (error) {
// Handle specific HTTP error (404 - Not Found)
if (error instanceof tc.HTTPError && error.httpStatusCode === 404) {
core.setFailed(
`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.
// For any other error, log the actual error message
core.setFailed(
`An unexpected error occurred like url might not correct`
`An unexpected error occurred:'url might not be correct'}`
);
}

throw error;
throw error; // Re-throw the error after logging
}
}
}
Loading
Oops, something went wrong.