pull/1211/merge
aparnajyothi-y 2025-01-31 14:52:25 +00:00 committed by GitHub
commit eefc13894f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 1056 additions and 174 deletions

View File

@ -10,13 +10,14 @@ import osm from 'os';
import path from 'path';
import * as main from '../src/main';
import * as auth from '../src/authutil';
import {INodeVersion} from '../src/distributions/base-models';
import {INodeVersion, NodeInputs} from '../src/distributions/base-models';
import nodeTestManifest from './data/versions-manifest.json';
import nodeTestDist from './data/node-dist-index.json';
import nodeTestDistNightly from './data/node-nightly-index.json';
import nodeTestDistRc from './data/node-rc-index.json';
import nodeV8CanaryTestDist from './data/v8-canary-dist-index.json';
import canaryBuild from '../src/distributions/v8-canary/canary_builds';
describe('setup-node', () => {
let inputs = {} as any;
@ -529,3 +530,84 @@ describe('setup-node', () => {
});
});
});
describe('CanaryBuild - Mirror URL functionality', () => {
const nodeInfo: NodeInputs = {
versionSpec: '18.0.0-rc', arch: 'x64', mirrorURL: '',
checkLatest: false,
stable: false
};
it('should return the default distribution URL if no mirror URL is provided', () => {
const canaryBuild = new CanaryBuild(nodeInfo);
// @ts-ignore: Accessing protected method for testing purposes
const distributionMirrorUrl = canaryBuild.getDistributionMirrorUrl();
expect(distributionMirrorUrl).toBe('https://nodejs.org/download/v8-canary');
});
it('should use the mirror URL from nodeInfo if provided', () => {
const mirrorURL = 'https://custom.mirror.url/v8-canary';
nodeInfo.mirrorURL = mirrorURL;
const canaryBuild = new CanaryBuild(nodeInfo);
// @ts-ignore: Accessing protected method for testing purposes
const distributionMirrorUrl = canaryBuild.getDistributionMirrorUrl();
expect(core.info).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`);
expect(distributionMirrorUrl).toBe(mirrorURL);
});
it('should fall back to the default distribution URL if mirror URL is not provided', () => {
nodeInfo.mirrorURL = ''; // No mirror URL
const canaryBuild = new CanaryBuild(nodeInfo);
// @ts-ignore: Accessing protected method for testing purposes
const distributionMirrorUrl = canaryBuild.getDistributionMirrorUrl();
expect(core.info).toHaveBeenCalledWith('Using mirror URL: https://nodejs.org/download/v8-canary');
expect(distributionMirrorUrl).toBe('https://nodejs.org/download/v8-canary');
});
it('should log the correct info when mirror URL is not provided', () => {
nodeInfo.mirrorURL = ''; // No mirror URL
const canaryBuild = new CanaryBuild(nodeInfo);
// @ts-ignore: Accessing protected method for testing purposes
canaryBuild.getDistributionMirrorUrl();
expect(core.info).toHaveBeenCalledWith('Using mirror URL: https://nodejs.org/download/v8-canary');
});
it('should return mirror URL if provided in nodeInfo', () => {
const mirrorURL = 'https://custom.mirror.url/v8-canary';
nodeInfo.mirrorURL = mirrorURL;
const canaryBuild = new CanaryBuild(nodeInfo);
// @ts-ignore: Accessing protected method for testing purposes
const distributionMirrorUrl = canaryBuild.getDistributionMirrorUrl();
expect(core.info).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`);
expect(distributionMirrorUrl).toBe(mirrorURL);
});
it('should use the default URL if mirror URL is empty string', () => {
nodeInfo.mirrorURL = ''; // Empty string for mirror URL
const canaryBuild = new CanaryBuild(nodeInfo);
// @ts-ignore: Accessing protected method for testing purposes
const distributionMirrorUrl = canaryBuild.getDistributionMirrorUrl();
expect(core.info).toHaveBeenCalledWith('Using mirror URL: https://nodejs.org/download/v8-canary');
expect(distributionMirrorUrl).toBe('https://nodejs.org/download/v8-canary');
});
it('should return the default URL if mirror URL is undefined', async () => {
// Create a spy on core.info
const infoSpy = jest.spyOn(core, 'info').mockImplementation(() => {}); // Mocking with empty implementation
// @ts-ignore: Accessing protected method for testing purposes
const distributionMirrorUrl = await canaryBuild.getDistributionMirrorUrl(); // Use await here
// Assert that core.info was called with the expected message
expect(infoSpy).toHaveBeenCalledWith('Using mirror URL: https://nodejs.org/download/v8-canary');
expect(distributionMirrorUrl).toBe('https://nodejs.org/download/v8-canary');
// Optional: Restore the original function after the test
infoSpy.mockRestore();
});
});

View File

@ -1,4 +1,5 @@
import * as core from '@actions/core';
import 'jest';
import * as exec from '@actions/exec';
import * as tc from '@actions/tool-cache';
import * as cache from '@actions/cache';
@ -14,6 +15,11 @@ import * as main from '../src/main';
import * as util from '../src/util';
import OfficialBuilds from '../src/distributions/official_builds/official_builds';
import * as installerFactory from '../src/distributions/installer-factory';
jest.mock('../src/distributions/installer-factory', () => ({
getNodejsDistribution: jest.fn()
}));
describe('main tests', () => {
let inputs = {} as any;
let os = {} as any;
@ -281,3 +287,115 @@ describe('main tests', () => {
});
});
});
// Create a mock object that satisfies the BaseDistribution interface
const createMockNodejsDistribution = () => ({
setupNodeJs: jest.fn(),
httpClient: {}, // Mocking the httpClient (you can replace this with more detailed mocks if needed)
osPlat: 'darwin', // Mocking osPlat (the platform the action will run on, e.g., 'darwin', 'win32', 'linux')
nodeInfo: {
version: '14.x',
arch: 'x64',
platform: 'darwin',
},
getDistributionUrl: jest.fn().mockReturnValue('https://nodejs.org/dist/'), // Example URL
install: jest.fn(),
validate: jest.fn(),
// Add any other methods/properties required by your BaseDistribution type
});
describe('Mirror URL Tests', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should pass mirror URL correctly when provided', async () => {
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
if (name === 'mirror-url') return 'https://custom-mirror-url.com';
if (name === 'node-version') return '14.x';
return '';
});
const mockNodejsDistribution = createMockNodejsDistribution();
(installerFactory.getNodejsDistribution as jest.Mock).mockReturnValue(mockNodejsDistribution);
await main.run();
// Ensure setupNodeJs is called with the correct parameters, including the mirror URL
expect(mockNodejsDistribution.setupNodeJs).toHaveBeenCalledWith({
versionSpec: '14.x',
checkLatest: false,
auth: undefined,
stable: true,
arch: 'x64',
mirrorURL: 'https://custom-mirror-url.com',
});
});
it('should use default mirror URL when no mirror URL is provided', async () => {
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
if (name === 'mirror-url') return '';
if (name === 'node-version') return '14.x';
return '';
});
const mockNodejsDistribution = createMockNodejsDistribution();
(installerFactory.getNodejsDistribution as jest.Mock).mockReturnValue(mockNodejsDistribution);
await main.run();
// Expect that setupNodeJs is called with an empty mirror URL
expect(mockNodejsDistribution.setupNodeJs).toHaveBeenCalledWith(expect.objectContaining({
mirrorURL: '', // Default URL is expected to be handled internally
}));
});
it('should handle mirror URL with spaces correctly', async () => {
const mirrorURL = 'https://custom-mirror-url.com ';
const expectedTrimmedURL = 'https://custom-mirror-url.com';
// Mock the setupNodeJs function
const mockNodejsDistribution = {
setupNodeJs: jest.fn(),
};
// Simulate calling the main function that will trigger setupNodeJs
await main.run({ mirrorURL });
// Debugging: Log the arguments that setupNodeJs was called with
console.log('setupNodeJs calls:', mockNodejsDistribution.setupNodeJs.mock.calls);
// Assert that setupNodeJs was called with the correct trimmed mirrorURL
expect(mockNodejsDistribution.setupNodeJs).toHaveBeenCalledWith(
expect.objectContaining({
mirrorURL: expectedTrimmedURL,
})
);
});
it('should warn if architecture is provided but node-version is missing', async () => {
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
if (name === 'architecture') return 'x64';
if (name === 'node-version') return '';
return '';
});
const mockWarning = jest.spyOn(core, 'warning');
const mockNodejsDistribution = createMockNodejsDistribution();
(installerFactory.getNodejsDistribution as jest.Mock).mockReturnValue(mockNodejsDistribution);
await main.run();
expect(mockWarning).toHaveBeenCalledWith(
"`architecture` is provided but `node-version` is missing. In this configuration, the version/architecture of Node will not be changed. To fix this, provide `architecture` in combination with `node-version`"
);
expect(mockNodejsDistribution.setupNodeJs).not.toHaveBeenCalled(); // Setup Node should not be called
});
});
function someFunctionThatCallsSetupNodeJs(arg0: { mirrorURL: string; }) {
throw new Error('Function not implemented.');
}

View File

@ -10,8 +10,8 @@ import osm from 'os';
import path from 'path';
import * as main from '../src/main';
import * as auth from '../src/authutil';
import {INodeVersion} from '../src/distributions/base-models';
import {INodeVersion, NodeInputs} from '../src/distributions/base-models';
import NightlyNodejs from '../src/distributions/nightly/nightly_builds';
import nodeTestManifest from './data/versions-manifest.json';
import nodeTestDist from './data/node-dist-index.json';
import nodeTestDistNightly from './data/node-nightly-index.json';
@ -606,3 +606,100 @@ describe('setup-node', () => {
);
});
});
// Mock core.info to track the log output
jest.mock('@actions/core', () => ({
info: jest.fn(),
}));
// Create a subclass to access the protected method for testing purposes
class TestNightlyNodejs extends NightlyNodejs {
public getDistributionUrlPublic() {
return this.getDistributionUrl(); // This allows us to call the protected method
}
}
describe('NightlyNodejs', () => {
it('uses mirror URL when provided', async () => {
const mirrorURL = 'https://my.custom.mirror/nodejs/nightly';
const nodeInfo: NodeInputs = {
mirrorURL: '', versionSpec: '18.0.0-nightly', arch: 'x64',
checkLatest: false,
stable: false
};
const nightlyNode = new TestNightlyNodejs(nodeInfo);
const distributionUrl = nightlyNode.getDistributionUrlPublic();
expect(distributionUrl).toBe(mirrorURL);
expect(core.info).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`);
});
it('falls back to default distribution URL when no mirror URL is provided', async () => {
const nodeInfo: NodeInputs = {
versionSpec: '18.0.0-nightly', arch: 'x64',
checkLatest: false,
stable: false
}; const nightlyNode = new TestNightlyNodejs(nodeInfo);
const distributionUrl = nightlyNode.getDistributionUrlPublic();
expect(distributionUrl).toBe('https://nodejs.org/download/nightly');
expect(core.info).toHaveBeenCalledWith('Using default distribution URL for nightly Node.js.');
});
it('logs mirror URL when provided', async () => {
const mirrorURL = 'https://custom.mirror/nodejs/nightly';
const nodeInfo: NodeInputs = {
mirrorURL: '', versionSpec: '18.0.0-nightly', arch: 'x64',
checkLatest: false,
stable: false
};
const nightlyNode = new TestNightlyNodejs(nodeInfo);
nightlyNode.getDistributionUrlPublic();
expect(core.info).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`);
});
it('logs default URL when no mirror URL is provided', async () => {
const nodeInfo: NodeInputs = {
versionSpec: '18.0.0-nightly', arch: 'x64',
checkLatest: false,
stable: false
}; const nightlyNode = new TestNightlyNodejs(nodeInfo);
nightlyNode.getDistributionUrlPublic();
expect(core.info).toHaveBeenCalledWith('Using default distribution URL for nightly Node.js.');
});
it('falls back to default distribution URL if mirror URL is an empty string', async () => {
const nodeInfo: NodeInputs = {
mirrorURL: '', versionSpec: '18.0.0-nightly', arch: 'x64',
checkLatest: false,
stable: false
};
const nightlyNode = new TestNightlyNodejs(nodeInfo);
const distributionUrl = nightlyNode.getDistributionUrlPublic();
expect(distributionUrl).toBe('https://nodejs.org/download/nightly');
expect(core.info).toHaveBeenCalledWith('Using default distribution URL for nightly Node.js.');
});
it('falls back to default distribution URL if mirror URL is undefined', async () => {
const nodeInfo: NodeInputs = {
mirrorURL: '', versionSpec: '18.0.0-nightly', arch: 'x64',
checkLatest: false,
stable: false
};
const nightlyNode = new TestNightlyNodejs(nodeInfo);
const distributionUrl = nightlyNode.getDistributionUrlPublic();
expect(distributionUrl).toBe('https://nodejs.org/download/nightly');
expect(core.info).toHaveBeenCalledWith('Using default distribution URL for nightly Node.js.');
});
});

View File

@ -11,7 +11,7 @@ import path from 'path';
import * as main from '../src/main';
import * as auth from '../src/authutil';
import OfficialBuilds from '../src/distributions/official_builds/official_builds';
import {INodeVersion} from '../src/distributions/base-models';
import {INodeVersion, NodeInputs} from '../src/distributions/base-models';
import nodeTestManifest from './data/versions-manifest.json';
import nodeTestDist from './data/node-dist-index.json';
@ -829,3 +829,107 @@ describe('setup-node', () => {
);
});
});
describe('OfficialBuilds - Mirror URL functionality', () => {
const nodeInfo: NodeInputs = {
mirrorURL: '', versionSpec: '18.0.0-nightly', arch: 'x64',
checkLatest: false,
stable: false
};
it('should download using the mirror URL when provided', async () => {
const mirrorURL = 'https://my.custom.mirror/nodejs';
nodeInfo.mirrorURL = mirrorURL;
const officialBuilds = new OfficialBuilds(nodeInfo);
// Mock download from mirror URL
const mockDownloadPath = '/some/temp/path';
jest.spyOn(tc, 'downloadTool').mockResolvedValue(mockDownloadPath);
await officialBuilds.setupNodeJs();
expect(core.info).toHaveBeenCalledWith('Attempting to download using mirror URL...');
expect(core.info).toHaveBeenCalledWith('downloadPath from downloadFromMirrorURL() /some/temp/path');
expect(core.addPath).toHaveBeenCalledWith(mockDownloadPath);
});
it('should log a message when mirror URL is used', async () => {
const mirrorURL = 'https://my.custom.mirror/nodejs';
nodeInfo.mirrorURL = mirrorURL;
const officialBuilds = new OfficialBuilds(nodeInfo);
const mockDownloadPath = '/some/temp/path';
jest.spyOn(tc, 'downloadTool').mockResolvedValue(mockDownloadPath);
await officialBuilds.setupNodeJs();
expect(core.info).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`);
});
it('should fall back to default URL if mirror URL is not provided', async () => {
nodeInfo.mirrorURL = ''; // No mirror URL provided
const officialBuilds = new OfficialBuilds(nodeInfo);
const mockDownloadPath = '/some/temp/path';
jest.spyOn(tc, 'downloadTool').mockResolvedValue(mockDownloadPath);
await officialBuilds.setupNodeJs();
expect(core.info).toHaveBeenCalledWith('Attempting to download from default Node.js URL...');
expect(core.addPath).toHaveBeenCalledWith(mockDownloadPath);
});
it('should log an error and handle failure during mirror URL download', async () => {
const mirrorURL = 'https://my.custom.mirror/nodejs';
nodeInfo.mirrorURL = mirrorURL;
const officialBuilds = new OfficialBuilds(nodeInfo);
// Simulate an error during the download process
const errorMessage = 'Network error';
jest.spyOn(tc, 'downloadTool').mockRejectedValue(new Error(errorMessage));
await officialBuilds.setupNodeJs();
expect(core.info).toHaveBeenCalledWith('Attempting to download using mirror URL...');
expect(core.error).toHaveBeenCalledWith(errorMessage);
expect(core.debug).toHaveBeenCalledWith(expect.stringContaining('empty stack'));
});
it('should log a fallback message if downloading from the mirror URL fails', async () => {
const mirrorURL = 'https://my.custom.mirror/nodejs';
nodeInfo.mirrorURL = mirrorURL;
const officialBuilds = new OfficialBuilds(nodeInfo);
// Simulate download failure and fallback to default URL
const errorMessage = 'Network error';
jest.spyOn(tc, 'downloadTool').mockRejectedValue(new Error(errorMessage));
const mockDownloadPath = '/some/temp/path';
jest.spyOn(tc, 'downloadTool').mockResolvedValue(mockDownloadPath);
await officialBuilds.setupNodeJs();
expect(core.info).toHaveBeenCalledWith('Failed to download from mirror URL. Falling back to default Node.js URL...');
expect(core.addPath).toHaveBeenCalledWith(mockDownloadPath);
});
it('should throw an error if mirror URL is not provided and downloading from both mirror and default fails', async () => {
nodeInfo.mirrorURL = ''; // No mirror URL
const officialBuilds = new OfficialBuilds(nodeInfo);
// Simulate failure in both mirror and default download
const errorMessage = 'Network error';
jest.spyOn(tc, 'downloadTool').mockRejectedValue(new Error(errorMessage));
await expect(officialBuilds.setupNodeJs()).rejects.toThrowError(new Error('Unable to find Node version for platform linux and architecture x64.'));
});
it('should throw an error if mirror URL is undefined and not provided', async () => {
// Mock missing mirror URL
process.env.MIRROR_URL = undefined; // Simulate missing mirror URL
// Mock the version lookup method to avoid triggering version errors
jest.spyOn(OfficialBuilds, 'findVersionInHostedToolCacheDirectory').mockResolvedValue(null); // Simulate "not found"
// Now we expect the function to throw the "Mirror URL is undefined" error
await expect(officialBuilds.setupNodeJs()).rejects.toThrowError('Mirror URL is undefined');
});
});

View File

@ -10,12 +10,13 @@ import osm from 'os';
import path from 'path';
import * as main from '../src/main';
import * as auth from '../src/authutil';
import {INodeVersion} from '../src/distributions/base-models';
import {INodeVersion, NodeInputs} from '../src/distributions/base-models';
import nodeTestDist from './data/node-dist-index.json';
import nodeTestDistNightly from './data/node-nightly-index.json';
import nodeTestDistRc from './data/node-rc-index.json';
import nodeV8CanaryTestDist from './data/v8-canary-dist-index.json';
import RcBuild from '../src/distributions/rc/rc_builds';
describe('setup-node', () => {
let inputs = {} as any;
@ -144,6 +145,10 @@ describe('setup-node', () => {
const toolPath = path.normalize('/cache/node/12.0.0-rc.1/x64');
findSpy.mockImplementation(() => toolPath);
// Ensure spies are set up before running the main logic
const logSpy = jest.spyOn(console, 'log'); // Ensure this is spying on console.log
const cnSpy = jest.spyOn(process.stdout, 'write'); // Ensure this spies on the correct add-path function
await main.run();
expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`);
@ -156,6 +161,10 @@ describe('setup-node', () => {
const toolPath = path.normalize('/cache/node/12.0.0-rc.1/x64');
findSpy.mockImplementation(() => toolPath);
// Ensure spies are set up before running the main logic
const logSpy = jest.spyOn(console, 'log'); // Ensure this is spying on console.log
await main.run();
expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`);
@ -168,6 +177,10 @@ describe('setup-node', () => {
const toolPath = path.normalize('/cache/node/12.0.0-rc.1/x64');
findSpy.mockImplementation(() => toolPath);
// Ensure spies are set up before running the main logic
const logSpy = jest.spyOn(console, 'log'); // Ensure this is spying on console.log
const cnSpy = jest.spyOn(process.stdout, 'write'); // Ensure this spies on the correct add-path function
await main.run();
const expPath = path.join(toolPath, 'bin');
@ -224,6 +237,10 @@ describe('setup-node', () => {
inputs['node-version'] = versionSpec;
findSpy.mockImplementation(() => '');
// Ensure spies are set up before running the main logic
const logSpy = jest.spyOn(console, 'log'); // Ensure this is spying on console.log
const cnSpy = jest.spyOn(process.stdout, 'write'); // Ensure this spies on the correct add-path function
await main.run();
expect(cnSpy).toHaveBeenCalledWith(
@ -247,6 +264,11 @@ describe('setup-node', () => {
dlSpy.mockImplementation(() => {
throw new Error(errMsg);
});
// Ensure spies are set up before running the main logic
const logSpy = jest.spyOn(console, 'log'); // Ensure this is spying on console.log
const cnSpy = jest.spyOn(process.stdout, 'write'); // Ensure this spies on the correct add-path function
await main.run();
expect(cnSpy).toHaveBeenCalledWith(`::error::${errMsg}${osm.EOL}`);
@ -281,6 +303,9 @@ describe('setup-node', () => {
const toolPath = path.normalize(`/cache/node/${version}/${arch}`);
exSpy.mockImplementation(async () => '/some/other/temp/path');
cacheSpy.mockImplementation(async () => toolPath);
// Ensure spies are set up before running the main logic
const logSpy = jest.spyOn(console, 'log'); // Ensure this is spying on console.log
const cnSpy = jest.spyOn(process.stdout, 'write'); // Ensure this spies on the correct add-path function
await main.run();
expect(dlSpy).toHaveBeenCalled();
@ -331,6 +356,11 @@ describe('setup-node', () => {
inputs['node-version'] = input;
os['arch'] = 'x64';
os['platform'] = 'linux';
// Ensure spies are set up before running the main logic
const logSpy = jest.spyOn(console, 'log'); // Ensure this is spying on console.log
const cnSpy = jest.spyOn(process.stdout, 'write'); // Ensure this spies on the correct add-path function
// act
await main.run();
@ -352,32 +382,52 @@ describe('setup-node', () => {
'finds the %s version in the hostedToolcache',
async (input, expectedVersion) => {
const toolPath = path.normalize(`/cache/node/${expectedVersion}/x64`);
findSpy.mockImplementation((_, version) =>
path.normalize(`/cache/node/${version}/x64`)
);
// Mocking the behavior of findSpy and findAllVersionsSpy
findSpy.mockImplementation((_, version) => {
console.log(`findSpy called for version: ${version}`); // Debugging line
return path.normalize(`/cache/node/${version}/x64`);
});
findAllVersionsSpy.mockReturnValue([
'2.2.2-rc.2',
'1.1.1-rc.1',
'99.1.1',
expectedVersion,
expectedVersion, // This should be the expected version
'88.1.1',
'3.3.3-rc.3'
]);
inputs['node-version'] = input;
os['arch'] = 'x64';
os['platform'] = 'linux';
// act
// Ensure spies are set up before running the main logic
const logSpy = jest.spyOn(console, 'log'); // Ensure this is spying on console.log
const cnSpy = jest.spyOn(process.stdout, 'write'); // Ensure this spies on the correct add-path function
// Act: Run the main function (your application logic)
await main.run();
// assert
// Debugging output to check if logSpy was called
console.log('logSpy calls:', logSpy.mock.calls); // Debugging line
// Assert: Check that the logSpy was called with the correct message
expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`);
// Assert: Check that cnSpy was called with the correct add-path action
expect(cnSpy).toHaveBeenCalledWith(
`::add-path::${path.join(toolPath, 'bin')}${osm.EOL}`
);
// Clean up spies
logSpy.mockRestore();
cnSpy.mockRestore();
}
);
it('throws an error if version is not found', async () => {
const versionSpec = '19.0.0-rc.3';
@ -390,6 +440,10 @@ describe('setup-node', () => {
inputs['node-version'] = versionSpec;
os['arch'] = 'x64';
os['platform'] = 'linux';
// Ensure spies are set up before running the main logic
const logSpy = jest.spyOn(console, 'log'); // Ensure this is spying on console.log
const cnSpy = jest.spyOn(process.stdout, 'write'); // Ensure this spies on the correct add-path function
// act
await main.run();
@ -400,3 +454,86 @@ describe('setup-node', () => {
});
});
});
describe('RcBuild - Mirror URL functionality', () => {
const nodeInfo: NodeInputs = {
versionSpec: '18.0.0-rc', arch: 'x64', mirrorURL: '',
checkLatest: false,
stable: false
};
it('should return the default distribution URL if no mirror URL is provided', () => {
const rcBuild = new RcBuild(nodeInfo);
const distributionUrl = rcBuild.getDistributionUrl();
expect(distributionUrl).toBe('https://nodejs.org/download/rc');
});
it('should use the mirror URL from nodeInfo if provided', () => {
const mirrorURL = 'https://my.custom.mirror/nodejs';
nodeInfo.mirrorURL = mirrorURL;
const rcBuild = new RcBuild(nodeInfo);
// @ts-ignore: Accessing protected method for testing purposes
const distributionMirrorUrl = rcBuild.getDistributionMirrorUrl();
expect(core.info).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`);
expect(distributionMirrorUrl).toBe(mirrorURL);
});
it('should fall back to the default distribution URL if mirror URL is not provided', () => {
nodeInfo.mirrorURL = ''; // No mirror URL
const rcBuild = new RcBuild(nodeInfo);
// @ts-ignore: Accessing protected method for testing purposes
const distributionMirrorUrl = rcBuild.getDistributionMirrorUrl();
expect(core.info).toHaveBeenCalledWith(`Using mirror URL: https://nodejs.org/download/rc`);
expect(distributionMirrorUrl).toBe('https://nodejs.org/download/rc');
});
it('should log the correct info when mirror URL is not provided', () => {
nodeInfo.mirrorURL = ''; // No mirror URL
const rcBuild = new RcBuild(nodeInfo);
// @ts-ignore: Accessing protected method for testing purposes
const distributionMirrorUrl = rcBuild.getDistributionMirrorUrl();
expect(core.info).toHaveBeenCalledWith('Using mirror URL: https://nodejs.org/download/rc');
});
it('should throw an error if mirror URL is undefined and not provided', async () => {
nodeInfo.mirrorURL = undefined; // Undefined mirror URL
const rcBuild = new RcBuild(nodeInfo);
// @ts-ignore: Accessing protected method for testing purposes
await expect(rcBuild['getDistributionMirrorUrl']()).resolves.toBe('https://nodejs.org/download/rc');
});
it('should return mirror URL if provided in nodeInfo', () => {
const mirrorURL = 'https://custom.mirror.url';
nodeInfo.mirrorURL = mirrorURL;
const rcBuild = new RcBuild(nodeInfo);
// @ts-ignore: Accessing protected method for testing purposes
// @ts-ignore: Accessing protected method for testing purposes
const url = rcBuild['getDistributionMirrorUrl']();
expect(core.info).toHaveBeenCalledWith(`Using mirror URL: ${mirrorURL}`);
expect(url).toBe(mirrorURL);
});
it('should use the default URL if mirror URL is empty string', () => {
nodeInfo.mirrorURL = ''; // Empty string for mirror URL
const rcBuild = new RcBuild(nodeInfo);
// @ts-ignore: Accessing protected method for testing purposes
const url = rcBuild['getDistributionMirrorUrl']();
expect(core.info).toHaveBeenCalledWith('Using mirror URL: https://nodejs.org/download/rc');
expect(url).toBe('https://nodejs.org/download/rc');
});
});

View File

@ -14,6 +14,9 @@ inputs:
check-latest:
description: 'Set this option if you want the action to check for the latest available version that satisfies the version spec.'
default: false
mirror-url:
description: 'Custom mirror URL to download Node.js from (optional)'
required: false
registry-url:
description: 'Optional registry to set up for auth. Will set the registry in a project level .npmrc and .yarnrc file, and set up auth to read in from env.NODE_AUTH_TOKEN.'
scope:

313
dist/setup/index.js vendored
View File

@ -100154,6 +100154,14 @@ class BaseDistribution {
return response.result || [];
});
}
getMirrorUrlVersions() {
return __awaiter(this, void 0, void 0, function* () {
const initialUrl = this.getDistributionUrl();
const dataUrl = `${initialUrl}/index.json`;
const response = yield this.httpClient.getJson(dataUrl);
return response.result || [];
});
}
getNodejsDistInfo(version) {
const osArch = this.translateArchToDistUrl(this.nodeInfo.arch);
version = semver_1.default.clean(version) || '';
@ -100174,6 +100182,26 @@ class BaseDistribution {
fileName: fileName
};
}
getNodejsMirrorURLInfo(version) {
const mirrorURL = this.nodeInfo.mirrorURL;
const osArch = this.translateArchToDistUrl(this.nodeInfo.arch);
version = semver_1.default.clean(version) || '';
const fileName = this.osPlat == 'win32'
? `node-v${version}-win-${osArch}`
: `node-v${version}-${this.osPlat}-${osArch}`;
const urlFileName = this.osPlat == 'win32'
? this.nodeInfo.arch === 'arm64'
? `${fileName}.zip`
: `${fileName}.7z`
: `${fileName}.tar.gz`;
const url = `${mirrorURL}/v${version}/${urlFileName}`;
return {
downloadUrl: url,
resolvedVersion: version,
arch: osArch,
fileName: fileName
};
}
downloadNodejs(info) {
return __awaiter(this, void 0, void 0, function* () {
let downloadPath = '';
@ -100185,8 +100213,9 @@ class BaseDistribution {
if (err instanceof tc.HTTPError &&
err.httpStatusCode == 404 &&
this.osPlat == 'win32') {
return yield this.acquireWindowsNodeFromFallbackLocation(info.resolvedVersion, info.arch);
return yield this.acquireWindowsNodeFromFallbackLocation(info.resolvedVersion, info.arch, info.downloadUrl);
}
core.error(`Download failed from ${info.downloadUrl}. Please check the URl and try again.`);
throw err;
}
const toolPath = yield this.extractArchive(downloadPath, info, true);
@ -100202,8 +100231,9 @@ class BaseDistribution {
return { range: valid, options };
}
acquireWindowsNodeFromFallbackLocation(version_1) {
return __awaiter(this, arguments, void 0, function* (version, arch = os_1.default.arch()) {
return __awaiter(this, arguments, void 0, function* (version, arch = os_1.default.arch(), downloadUrl) {
const initialUrl = this.getDistributionUrl();
core.info('url: ' + initialUrl);
const osArch = this.translateArchToDistUrl(arch);
// Create temporary folder to download to
const tempDownloadFolder = `temp_${(0, uuid_1.v4)()}`;
@ -100217,6 +100247,9 @@ class BaseDistribution {
exeUrl = `${initialUrl}/v${version}/win-${osArch}/node.exe`;
libUrl = `${initialUrl}/v${version}/win-${osArch}/node.lib`;
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 = yield tc.downloadTool(exeUrl);
yield io.cp(exePath, path.join(tempDir, 'node.exe'));
const libPath = yield tc.downloadTool(libUrl);
@ -100381,17 +100414,54 @@ exports.getNodejsDistribution = getNodejsDistribution;
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const base_distribution_prerelease_1 = __importDefault(__nccwpck_require__(957));
const core = __importStar(__nccwpck_require__(2186));
class NightlyNodejs extends base_distribution_prerelease_1.default {
constructor(nodeInfo) {
super(nodeInfo);
this.distribution = 'nightly';
}
getDistributionMirrorUrl() {
// Implement the method to return the mirror URL or an empty string if not available
return this.nodeInfo.mirrorURL || '';
}
// Updated getDistributionUrl method to handle mirror URL or fallback
getDistributionUrl() {
// Check if mirrorUrl exists in the nodeInfo and return it if available
const mirrorUrl = this.nodeInfo.mirrorURL;
if (mirrorUrl) {
core.info(`Downloding Using mirror URL: ${mirrorUrl}`);
return mirrorUrl;
}
// Default to the official Node.js nightly distribution URL if no mirror URL is provided
core.info('Using default distribution URL for nightly Node.js.');
return 'https://nodejs.org/download/nightly';
}
}
@ -100451,73 +100521,91 @@ class OfficialBuilds extends base_distribution_1.default {
}
setupNodeJs() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
let manifest;
let nodeJsVersions;
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 = yield this.getManifest();
this.nodeInfo.versionSpec = this.resolveLtsAliasFromManifest(this.nodeInfo.versionSpec, this.nodeInfo.stable, manifest);
}
if (this.isLatestSyntax(this.nodeInfo.versionSpec)) {
nodeJsVersions = yield 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 = yield 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`);
}
}
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 = yield this.getInfoFromManifest(this.nodeInfo.versionSpec, this.nodeInfo.stable, osArch, manifest);
if (versionInfo) {
core.info(`Acquiring ${versionInfo.resolvedVersion} - ${versionInfo.arch} from ${versionInfo.downloadUrl}`);
downloadPath = yield tc.downloadTool(versionInfo.downloadUrl, undefined, this.nodeInfo.auth);
var _a, _b;
if (this.nodeInfo.mirrorURL) {
let downloadPath = '';
let toolPath = '';
try {
core.info(`Attempting to download using mirror URL...`);
downloadPath = yield this.downloadFromMirrorURL(); // Attempt to download from the mirror
core.info('downloadPath from downloadFromMirrorURL() ' + downloadPath);
if (downloadPath) {
toolPath = yield this.extractArchive(downloadPath, versionInfo, false);
toolPath = downloadPath;
}
}
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 {
catch (err) {
core.info(err.message);
core.debug((_a = err.stack) !== null && _a !== void 0 ? _a : 'empty stack');
}
core.debug((_a = err.stack) !== null && _a !== void 0 ? _a : 'empty stack');
core.info('Falling back to download directly from Node');
}
if (!toolPath) {
toolPath = yield this.downloadDirectlyFromNode();
else {
let manifest;
let nodeJsVersions;
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 = yield this.getManifest();
this.nodeInfo.versionSpec = this.resolveLtsAliasFromManifest(this.nodeInfo.versionSpec, this.nodeInfo.stable, manifest);
}
if (this.isLatestSyntax(this.nodeInfo.versionSpec)) {
nodeJsVersions = yield 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 = yield 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`);
}
}
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 = yield this.getInfoFromManifest(this.nodeInfo.versionSpec, this.nodeInfo.stable, osArch, manifest);
if (versionInfo) {
core.info(`Acquiring ${versionInfo.resolvedVersion} - ${versionInfo.arch} from ${versionInfo.downloadUrl}`);
downloadPath = yield tc.downloadTool(versionInfo.downloadUrl, undefined, this.nodeInfo.auth);
if (downloadPath) {
toolPath = yield 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.message);
}
core.debug((_b = err.stack) !== null && _b !== void 0 ? _b : 'empty stack');
core.info('Falling back to download directly from Node');
}
if (!toolPath) {
toolPath = yield this.downloadDirectlyFromNode();
}
if (this.osPlat != 'win32') {
toolPath = path_1.default.join(toolPath, 'bin');
}
core.addPath(toolPath);
}
if (this.osPlat != 'win32') {
toolPath = path_1.default.join(toolPath, 'bin');
}
core.addPath(toolPath);
});
}
addToolPath(toolPath) {
@ -100559,6 +100647,9 @@ class OfficialBuilds extends base_distribution_1.default {
return version;
}
getDistributionUrl() {
if (this.nodeInfo.mirrorURL) {
return this.nodeInfo.mirrorURL;
}
return `https://nodejs.org/dist`;
}
getManifest() {
@ -100626,6 +100717,33 @@ class OfficialBuilds extends base_distribution_1.default {
isLatestSyntax(versionSpec) {
return ['current', 'latest', 'node'].includes(versionSpec);
}
downloadFromMirrorURL() {
return __awaiter(this, void 0, void 0, function* () {
const nodeJsVersions = yield 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}.`);
}
const toolName = this.getNodejsMirrorURLInfo(evaluatedVersion);
try {
const toolPath = yield 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;
}
});
}
}
exports["default"] = OfficialBuilds;
@ -100637,11 +100755,35 @@ exports["default"] = OfficialBuilds;
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const base_distribution_1 = __importDefault(__nccwpck_require__(7));
const core = __importStar(__nccwpck_require__(2186));
class RcBuild extends base_distribution_1.default {
constructor(nodeInfo) {
super(nodeInfo);
@ -100649,6 +100791,16 @@ class RcBuild extends base_distribution_1.default {
getDistributionUrl() {
return 'https://nodejs.org/download/rc';
}
getDistributionMirrorUrl() {
// Check if mirrorUrl exists in the nodeInfo and return it if available
const mirrorUrl = this.nodeInfo.mirrorURL;
if (mirrorUrl) {
core.info(`Using mirror URL: ${mirrorUrl}`);
return mirrorUrl;
}
// Return the default URL if no mirror URL is provided
return this.getDistributionUrl();
}
}
exports["default"] = RcBuild;
@ -100660,11 +100812,35 @@ exports["default"] = RcBuild;
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const base_distribution_prerelease_1 = __importDefault(__nccwpck_require__(957));
const core = __importStar(__nccwpck_require__(2186));
class CanaryBuild extends base_distribution_prerelease_1.default {
constructor(nodeInfo) {
super(nodeInfo);
@ -100673,6 +100849,15 @@ class CanaryBuild extends base_distribution_prerelease_1.default {
getDistributionUrl() {
return 'https://nodejs.org/download/v8-canary';
}
getDistributionMirrorUrl() {
// Check if mirrorUrl exists in the nodeInfo and return it if available
const mirrorUrl = this.nodeInfo.mirrorURL;
if (mirrorUrl) {
core.info(`Using mirror URL: ${mirrorUrl}`);
return mirrorUrl;
}
return 'https://nodejs.org/download/v8-canary';
}
}
exports["default"] = CanaryBuild;
@ -100748,6 +100933,7 @@ function run() {
if (!arch) {
arch = os_1.default.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}`;
@ -100758,7 +100944,8 @@ function run() {
checkLatest,
auth,
stable,
arch
arch,
mirrorURL
};
const nodeDistribution = (0, installer_factory_1.getNodejsDistribution)(nodejsInfo);
yield nodeDistribution.setupNodeJs();

View File

@ -104,6 +104,15 @@ export default abstract class BaseDistribution {
return response.result || [];
}
protected async getMirrorUrlVersions(): Promise<INodeVersion[]> {
const initialUrl = this.getDistributionUrl();
const dataUrl = `${initialUrl}/index.json`;
const response = await this.httpClient.getJson<INodeVersion[]>(dataUrl);
return response.result || [];
}
protected getNodejsDistInfo(version: string) {
const osArch: string = this.translateArchToDistUrl(this.nodeInfo.arch);
version = semver.clean(version) || '';
@ -128,6 +137,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 +179,13 @@ export default abstract class BaseDistribution {
) {
return await this.acquireWindowsNodeFromFallbackLocation(
info.resolvedVersion,
info.arch
info.arch,
info.downloadUrl
);
}
core.error(
`Download failed from ${info.downloadUrl}. Please check the URl and try again.`
);
throw err;
}
@ -166,9 +206,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 +227,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,31 @@
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 getDistributionMirrorUrl(): string {
// Implement the method to return the mirror URL or an empty string if not available
return this.nodeInfo.mirrorURL || '';
}
// Updated getDistributionUrl method to handle mirror URL or fallback
protected getDistributionUrl(): string {
// Check if mirrorUrl exists in the nodeInfo and return it if available
const mirrorUrl = this.nodeInfo.mirrorURL;
if (mirrorUrl) {
core.info(`Downloding Using mirror URL: ${mirrorUrl}`);
return mirrorUrl;
}
// Default to the official Node.js nightly distribution URL if no mirror URL is provided
core.info('Using default distribution URL for nightly Node.js.');
return 'https://nodejs.org/download/nightly';
}
}

View File

@ -15,115 +15,131 @@ 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.nodeInfo.mirrorURL) {
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 = 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...');
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();
// 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`
this.nodeInfo.versionSpec = this.resolveLtsAliasFromManifest(
this.nodeInfo.versionSpec,
this.nodeInfo.stable,
manifest
);
}
}
let toolPath = this.findVersionInHostedToolCacheDirectory();
if (this.isLatestSyntax(this.nodeInfo.versionSpec)) {
nodeJsVersions = await this.getNodeJsVersions();
const versions = this.filterVersions(nodeJsVersions);
this.nodeInfo.versionSpec = this.evaluateVersions(versions);
if (toolPath) {
core.info(`Found in cache @ ${toolPath}`);
this.addToolPath(toolPath);
return;
}
core.info('getting latest node version...');
}
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}`
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
);
downloadPath = await tc.downloadTool(
versionInfo.downloadUrl,
undefined,
this.nodeInfo.auth
);
if (downloadPath) {
toolPath = await this.extractArchive(
downloadPath,
versionInfo,
false
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,8 +193,12 @@ export default class OfficialBuilds extends BaseDistribution {
}
protected getDistributionUrl(): string {
if (this.nodeInfo.mirrorURL) {
return this.nodeInfo.mirrorURL;
}
return `https://nodejs.org/dist`;
}
private getManifest(): Promise<tc.IToolRelease[]> {
core.debug('Getting manifest from actions/node-versions@main');
@ -291,4 +311,43 @@ 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}.`
);
}
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,7 +1,9 @@
import BaseDistribution from '../base-distribution';
import {NodeInputs} from '../base-models';
import * as core from '@actions/core';
export default class RcBuild extends BaseDistribution {
constructor(nodeInfo: NodeInputs) {
super(nodeInfo);
}
@ -9,4 +11,16 @@ export default class RcBuild extends BaseDistribution {
getDistributionUrl(): string {
return 'https://nodejs.org/download/rc';
}
protected getDistributionMirrorUrl(): string {
// Check if mirrorUrl exists in the nodeInfo and return it if available
const mirrorUrl = this.nodeInfo.mirrorURL;
if (mirrorUrl) {
core.info(`Using mirror URL: ${mirrorUrl}`);
return mirrorUrl;
}
// Return the default URL if no mirror URL is provided
return this.getDistributionUrl();
}
}

View File

@ -1,7 +1,8 @@
import BasePrereleaseNodejs from '../base-distribution-prerelease';
import {NodeInputs} from '../base-models';
import * as core from '@actions/core';
export default class CanaryBuild extends BasePrereleaseNodejs {
protected distribution = 'v8-canary';
constructor(nodeInfo: NodeInputs) {
super(nodeInfo);
@ -10,4 +11,14 @@ export default class CanaryBuild extends BasePrereleaseNodejs {
protected getDistributionUrl(): string {
return 'https://nodejs.org/download/v8-canary';
}
protected getDistributionMirrorUrl(): string {
// Check if mirrorUrl exists in the nodeInfo and return it if available
const mirrorUrl = this.nodeInfo.mirrorURL;
if (mirrorUrl) {
core.info(`Using mirror URL: ${mirrorUrl}`);
return mirrorUrl;
}
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();