Added mirror option to select alternative download source

no issue
This commit is contained in:
Daniel Lockyer 2020-04-06 17:14:56 +01:00
parent 83c9f7a7df
commit 905173b259
4 changed files with 49 additions and 32 deletions

View file

@ -19,7 +19,10 @@ interface INodeVersion {
files: string[];
}
export async function getNode(versionSpec: string) {
export async function getNode(
versionSpec: string,
mirrorUrl: string = 'https://nodejs.org/dist/'
) {
// check cache
let toolPath: string;
toolPath = tc.find('node', versionSpec);
@ -33,8 +36,8 @@ export async function getNode(versionSpec: string) {
// version to download
version = versionSpec;
} else {
// query nodejs.org for a matching version
version = await queryLatestMatch(versionSpec);
// query mirrorUrl for a matching version
version = await queryLatestMatch(versionSpec, mirrorUrl);
if (!version) {
throw new Error(
`Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`
@ -47,7 +50,7 @@ export async function getNode(versionSpec: string) {
if (!toolPath) {
// download, extract, cache
toolPath = await acquireNode(version);
toolPath = await acquireNode(version, mirrorUrl);
}
}
@ -65,7 +68,10 @@ export async function getNode(versionSpec: string) {
core.addPath(toolPath);
}
async function queryLatestMatch(versionSpec: string): Promise<string> {
async function queryLatestMatch(
versionSpec: string,
mirrorUrl: string
): Promise<string> {
// node offers a json list of versions
let dataFileName: string;
switch (osPlat) {
@ -83,7 +89,7 @@ async function queryLatestMatch(versionSpec: string): Promise<string> {
}
let versions: string[] = [];
let dataUrl = 'https://nodejs.org/dist/index.json';
let dataUrl = `${mirrorUrl}index.json`;
let httpClient = new hc.HttpClient('setup-node', [], {
allowRetries: true,
maxRetries: 3
@ -130,7 +136,10 @@ function evaluateVersions(versions: string[], versionSpec: string): string {
return version;
}
async function acquireNode(version: string): Promise<string> {
async function acquireNode(
version: string,
mirrorUrl: string
): Promise<string> {
//
// Download - a tool installer intimately knows how to get the tool (and construct urls)
//
@ -141,7 +150,7 @@ async function acquireNode(version: string): Promise<string> {
: `node-v${version}-${osPlat}-${osArch}`;
let urlFileName: string =
osPlat == 'win32' ? `${fileName}.7z` : `${fileName}.tar.gz`;
let downloadUrl = `https://nodejs.org/dist/v${version}/${urlFileName}`;
let downloadUrl = `${mirrorUrl}v${version}/${urlFileName}`;
let downloadPath: string;
@ -149,7 +158,7 @@ async function acquireNode(version: string): Promise<string> {
downloadPath = await tc.downloadTool(downloadUrl);
} catch (err) {
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
return await acquireNodeFromFallbackLocation(version);
return await acquireNodeFromFallbackLocation(version, mirrorUrl);
}
throw err;
@ -186,7 +195,8 @@ async function acquireNode(version: string): Promise<string> {
// Note also that the files are normally zipped but in this case they are just an exe
// and lib file in a folder, not zipped.
async function acquireNodeFromFallbackLocation(
version: string
version: string,
mirrorUrl: string
): Promise<string> {
// Create temporary folder to download in to
const tempDownloadFolder: string =
@ -198,8 +208,8 @@ async function acquireNodeFromFallbackLocation(
let exeUrl: string;
let libUrl: string;
try {
exeUrl = `https://nodejs.org/dist/v${version}/win-${osArch}/node.exe`;
libUrl = `https://nodejs.org/dist/v${version}/win-${osArch}/node.lib`;
exeUrl = `${mirrorUrl}v${version}/win-${osArch}/node.exe`;
libUrl = `${mirrorUrl}v${version}/win-${osArch}/node.lib`;
const exePath = await tc.downloadTool(exeUrl);
await io.cp(exePath, path.join(tempDir, 'node.exe'));
@ -207,8 +217,8 @@ async function acquireNodeFromFallbackLocation(
await io.cp(libPath, path.join(tempDir, 'node.lib'));
} catch (err) {
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
exeUrl = `https://nodejs.org/dist/v${version}/node.exe`;
libUrl = `https://nodejs.org/dist/v${version}/node.lib`;
exeUrl = `${mirrorUrl}v${version}/node.exe`;
libUrl = `${mirrorUrl}v${version}/node.lib`;
const exePath = await tc.downloadTool(exeUrl);
await io.cp(exePath, path.join(tempDir, 'node.exe'));