Skip to content

Commit 6a399da

Browse files
committed
Add mirror option
1 parent 6ecfd2d commit 6a399da

4 files changed

Lines changed: 40 additions & 28 deletions

File tree

lib/installer.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ if (!tempDirectory) {
4242
}
4343
tempDirectory = path.join(baseLocation, 'actions', 'temp');
4444
}
45-
function getNode(versionSpec) {
45+
function getNode(versionSpec, mirror) {
4646
return __awaiter(this, void 0, void 0, function* () {
4747
// check cache
4848
let toolPath;
@@ -58,7 +58,7 @@ function getNode(versionSpec) {
5858
}
5959
else {
6060
// query nodejs.org for a matching version
61-
version = yield queryLatestMatch(versionSpec);
61+
version = yield queryLatestMatch(versionSpec, mirror);
6262
if (!version) {
6363
throw new Error(`Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`);
6464
}
@@ -67,7 +67,7 @@ function getNode(versionSpec) {
6767
}
6868
if (!toolPath) {
6969
// download, extract, cache
70-
toolPath = yield acquireNode(version);
70+
toolPath = yield acquireNode(version, mirror);
7171
}
7272
}
7373
//
@@ -84,7 +84,7 @@ function getNode(versionSpec) {
8484
});
8585
}
8686
exports.getNode = getNode;
87-
function queryLatestMatch(versionSpec) {
87+
function queryLatestMatch(versionSpec, mirror) {
8888
return __awaiter(this, void 0, void 0, function* () {
8989
// node offers a json list of versions
9090
let dataFileName;
@@ -102,7 +102,7 @@ function queryLatestMatch(versionSpec) {
102102
throw new Error(`Unexpected OS '${osPlat}'`);
103103
}
104104
let versions = [];
105-
let dataUrl = 'https://nodejs.org/dist/index.json';
105+
let dataUrl = `${mirror}/index.json`;
106106
let rest = new restm.RestClient('setup-node');
107107
let nodeVersions = (yield rest.get(dataUrl)).result || [];
108108
nodeVersions.forEach((nodeVersion) => {
@@ -142,7 +142,7 @@ function evaluateVersions(versions, versionSpec) {
142142
}
143143
return version;
144144
}
145-
function acquireNode(version) {
145+
function acquireNode(version, mirror) {
146146
return __awaiter(this, void 0, void 0, function* () {
147147
//
148148
// Download - a tool installer intimately knows how to get the tool (and construct urls)
@@ -152,14 +152,14 @@ function acquireNode(version) {
152152
? 'node-v' + version + '-win-' + os.arch()
153153
: 'node-v' + version + '-' + osPlat + '-' + os.arch();
154154
let urlFileName = osPlat == 'win32' ? fileName + '.7z' : fileName + '.tar.gz';
155-
let downloadUrl = 'https://nodejs.org/dist/v' + version + '/' + urlFileName;
155+
let downloadUrl = `${mirror}/v` + version + '/' + urlFileName;
156156
let downloadPath;
157157
try {
158158
downloadPath = yield tc.downloadTool(downloadUrl);
159159
}
160160
catch (err) {
161161
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
162-
return yield acquireNodeFromFallbackLocation(version);
162+
return yield acquireNodeFromFallbackLocation(version, mirror);
163163
}
164164
throw err;
165165
}
@@ -193,7 +193,7 @@ function acquireNode(version) {
193193
// This method attempts to download and cache the resources from these alternative locations.
194194
// Note also that the files are normally zipped but in this case they are just an exe
195195
// and lib file in a folder, not zipped.
196-
function acquireNodeFromFallbackLocation(version) {
196+
function acquireNodeFromFallbackLocation(version, mirror) {
197197
return __awaiter(this, void 0, void 0, function* () {
198198
// Create temporary folder to download in to
199199
let tempDownloadFolder = 'temp_' + Math.floor(Math.random() * 2000000000);
@@ -202,17 +202,17 @@ function acquireNodeFromFallbackLocation(version) {
202202
let exeUrl;
203203
let libUrl;
204204
try {
205-
exeUrl = `https://nodejs.org/dist/v${version}/win-${os.arch()}/node.exe`;
206-
libUrl = `https://nodejs.org/dist/v${version}/win-${os.arch()}/node.lib`;
205+
exeUrl = `${mirror}/v${version}/win-${os.arch()}/node.exe`;
206+
libUrl = `${mirror}/v${version}/win-${os.arch()}/node.lib`;
207207
const exePath = yield tc.downloadTool(exeUrl);
208208
yield io.cp(exePath, path.join(tempDir, 'node.exe'));
209209
const libPath = yield tc.downloadTool(libUrl);
210210
yield io.cp(libPath, path.join(tempDir, 'node.lib'));
211211
}
212212
catch (err) {
213213
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
214-
exeUrl = `https://nodejs.org/dist/v${version}/node.exe`;
215-
libUrl = `https://nodejs.org/dist/v${version}/node.lib`;
214+
exeUrl = `${mirror}/v${version}/node.exe`;
215+
libUrl = `${mirror}/v${version}/node.lib`;
216216
const exePath = yield tc.downloadTool(exeUrl);
217217
yield io.cp(exePath, path.join(tempDir, 'node.exe'));
218218
const libPath = yield tc.downloadTool(libUrl);

lib/setup-node.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ function run() {
3030
if (!version) {
3131
version = core.getInput('node-version');
3232
}
33+
let mirror = core.getInput('mirror');
34+
if (!mirror) {
35+
mirror = core.getInput('node-mirror') || 'https://nodejs.org/dist';
36+
}
3337
if (version) {
3438
// TODO: installer doesn't support proxy
35-
yield installer.getNode(version);
39+
yield installer.getNode(version, mirror);
3640
}
3741
const registryUrl = core.getInput('registry-url');
3842
const alwaysAuth = core.getInput('always-auth');

src/installer.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ interface INodeVersion {
3535
files: string[];
3636
}
3737

38-
export async function getNode(versionSpec: string) {
38+
export async function getNode(versionSpec: string, mirror: string) {
3939
// check cache
4040
let toolPath: string;
4141
toolPath = tc.find('node', versionSpec);
@@ -50,7 +50,7 @@ export async function getNode(versionSpec: string) {
5050
version = versionSpec;
5151
} else {
5252
// query nodejs.org for a matching version
53-
version = await queryLatestMatch(versionSpec);
53+
version = await queryLatestMatch(versionSpec, mirror);
5454
if (!version) {
5555
throw new Error(
5656
`Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`
@@ -63,7 +63,7 @@ export async function getNode(versionSpec: string) {
6363

6464
if (!toolPath) {
6565
// download, extract, cache
66-
toolPath = await acquireNode(version);
66+
toolPath = await acquireNode(version, mirror);
6767
}
6868
}
6969

@@ -81,7 +81,10 @@ export async function getNode(versionSpec: string) {
8181
core.addPath(toolPath);
8282
}
8383

84-
async function queryLatestMatch(versionSpec: string): Promise<string> {
84+
async function queryLatestMatch(
85+
versionSpec: string,
86+
mirror: string
87+
): Promise<string> {
8588
// node offers a json list of versions
8689
let dataFileName: string;
8790
switch (osPlat) {
@@ -99,7 +102,7 @@ async function queryLatestMatch(versionSpec: string): Promise<string> {
99102
}
100103

101104
let versions: string[] = [];
102-
let dataUrl = 'https://nodejs.org/dist/index.json';
105+
let dataUrl = `${mirror}/index.json`;
103106
let rest: restm.RestClient = new restm.RestClient('setup-node');
104107
let nodeVersions: INodeVersion[] =
105108
(await rest.get<INodeVersion[]>(dataUrl)).result || [];
@@ -143,7 +146,7 @@ function evaluateVersions(versions: string[], versionSpec: string): string {
143146
return version;
144147
}
145148

146-
async function acquireNode(version: string): Promise<string> {
149+
async function acquireNode(version: string, mirror: string): Promise<string> {
147150
//
148151
// Download - a tool installer intimately knows how to get the tool (and construct urls)
149152
//
@@ -155,15 +158,15 @@ async function acquireNode(version: string): Promise<string> {
155158
let urlFileName: string =
156159
osPlat == 'win32' ? fileName + '.7z' : fileName + '.tar.gz';
157160

158-
let downloadUrl = 'https://nodejs.org/dist/v' + version + '/' + urlFileName;
161+
let downloadUrl = `${mirror}/v` + version + '/' + urlFileName;
159162

160163
let downloadPath: string;
161164

162165
try {
163166
downloadPath = await tc.downloadTool(downloadUrl);
164167
} catch (err) {
165168
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
166-
return await acquireNodeFromFallbackLocation(version);
169+
return await acquireNodeFromFallbackLocation(version, mirror);
167170
}
168171

169172
throw err;
@@ -200,7 +203,8 @@ async function acquireNode(version: string): Promise<string> {
200203
// Note also that the files are normally zipped but in this case they are just an exe
201204
// and lib file in a folder, not zipped.
202205
async function acquireNodeFromFallbackLocation(
203-
version: string
206+
version: string,
207+
mirror: string
204208
): Promise<string> {
205209
// Create temporary folder to download in to
206210
let tempDownloadFolder: string =
@@ -210,17 +214,17 @@ async function acquireNodeFromFallbackLocation(
210214
let exeUrl: string;
211215
let libUrl: string;
212216
try {
213-
exeUrl = `https://nodejs.org/dist/v${version}/win-${os.arch()}/node.exe`;
214-
libUrl = `https://nodejs.org/dist/v${version}/win-${os.arch()}/node.lib`;
217+
exeUrl = `${mirror}/v${version}/win-${os.arch()}/node.exe`;
218+
libUrl = `${mirror}/v${version}/win-${os.arch()}/node.lib`;
215219

216220
const exePath = await tc.downloadTool(exeUrl);
217221
await io.cp(exePath, path.join(tempDir, 'node.exe'));
218222
const libPath = await tc.downloadTool(libUrl);
219223
await io.cp(libPath, path.join(tempDir, 'node.lib'));
220224
} catch (err) {
221225
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
222-
exeUrl = `https://nodejs.org/dist/v${version}/node.exe`;
223-
libUrl = `https://nodejs.org/dist/v${version}/node.lib`;
226+
exeUrl = `${mirror}/v${version}/node.exe`;
227+
libUrl = `${mirror}/v${version}/node.lib`;
224228

225229
const exePath = await tc.downloadTool(exeUrl);
226230
await io.cp(exePath, path.join(tempDir, 'node.exe'));

src/setup-node.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ async function run() {
1313
if (!version) {
1414
version = core.getInput('node-version');
1515
}
16+
let mirror = core.getInput('mirror');
17+
if (!mirror) {
18+
mirror = core.getInput('node-mirror') || 'https://nodejs.org/dist';
19+
}
1620
if (version) {
1721
// TODO: installer doesn't support proxy
18-
await installer.getNode(version);
22+
await installer.getNode(version, mirror);
1923
}
2024

2125
const registryUrl: string = core.getInput('registry-url');

0 commit comments

Comments
 (0)