feat: support private mirrors (#1240)

* feat: support private mirrors

* chore: change fallback message with mirrors
This commit is contained in:
Marco Ippolito 2025-04-02 17:49:47 +02:00 committed by GitHub
parent 40337cb8f7
commit e3ce749e20
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 322 additions and 44 deletions

View file

@ -24,7 +24,7 @@ export default abstract class BaseDistribution {
});
}
protected abstract getDistributionUrl(): string;
protected abstract getDistributionUrl(mirror: string): string;
public async setupNodeJs() {
let nodeJsVersions: INodeVersion[] | undefined;
@ -97,10 +97,19 @@ export default abstract class BaseDistribution {
}
protected async getNodeJsVersions(): Promise<INodeVersion[]> {
const initialUrl = this.getDistributionUrl();
const initialUrl = this.getDistributionUrl(this.nodeInfo.mirror);
const dataUrl = `${initialUrl}/index.json`;
const response = await this.httpClient.getJson<INodeVersion[]>(dataUrl);
const headers = {};
if (this.nodeInfo.mirrorToken) {
headers['Authorization'] = `Bearer ${this.nodeInfo.mirrorToken}`;
}
const response = await this.httpClient.getJson<INodeVersion[]>(
dataUrl,
headers
);
return response.result || [];
}
@ -117,7 +126,7 @@ export default abstract class BaseDistribution {
? `${fileName}.zip`
: `${fileName}.7z`
: `${fileName}.tar.gz`;
const initialUrl = this.getDistributionUrl();
const initialUrl = this.getDistributionUrl(this.nodeInfo.mirror);
const url = `${initialUrl}/v${version}/${urlFileName}`;
return <INodeVersionInfo>{
@ -134,7 +143,11 @@ export default abstract class BaseDistribution {
`Acquiring ${info.resolvedVersion} - ${info.arch} from ${info.downloadUrl}`
);
try {
downloadPath = await tc.downloadTool(info.downloadUrl);
downloadPath = await tc.downloadTool(
info.downloadUrl,
undefined,
this.nodeInfo.mirrorToken
);
} catch (err) {
if (
err instanceof tc.HTTPError &&
@ -168,7 +181,7 @@ export default abstract class BaseDistribution {
version: string,
arch: string = os.arch()
): Promise<string> {
const initialUrl = this.getDistributionUrl();
const initialUrl = this.getDistributionUrl(this.nodeInfo.mirror);
const osArch: string = this.translateArchToDistUrl(arch);
// Create temporary folder to download to
@ -185,18 +198,34 @@ export default abstract class BaseDistribution {
core.info(`Downloading only node binary from ${exeUrl}`);
const exePath = await tc.downloadTool(exeUrl);
const exePath = await tc.downloadTool(
exeUrl,
undefined,
this.nodeInfo.mirrorToken
);
await io.cp(exePath, path.join(tempDir, 'node.exe'));
const libPath = await tc.downloadTool(libUrl);
const libPath = await tc.downloadTool(
libUrl,
undefined,
this.nodeInfo.mirrorToken
);
await io.cp(libPath, path.join(tempDir, 'node.lib'));
} catch (err) {
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
exeUrl = `${initialUrl}/v${version}/node.exe`;
libUrl = `${initialUrl}/v${version}/node.lib`;
const exePath = await tc.downloadTool(exeUrl);
const exePath = await tc.downloadTool(
exeUrl,
undefined,
this.nodeInfo.mirrorToken
);
await io.cp(exePath, path.join(tempDir, 'node.exe'));
const libPath = await tc.downloadTool(libUrl);
const libPath = await tc.downloadTool(
libUrl,
undefined,
this.nodeInfo.mirrorToken
);
await io.cp(libPath, path.join(tempDir, 'node.lib'));
} else {
throw err;

View file

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

View file

@ -7,7 +7,8 @@ export default class NightlyNodejs extends BasePrereleaseNodejs {
super(nodeInfo);
}
protected getDistributionUrl(): string {
return 'https://nodejs.org/download/nightly';
protected getDistributionUrl(mirror: string): string {
const url = mirror || 'https://nodejs.org';
return `${url}/download/nightly`;
}
}

View file

@ -84,7 +84,7 @@ export default class OfficialBuilds extends BaseDistribution {
downloadPath = await tc.downloadTool(
versionInfo.downloadUrl,
undefined,
this.nodeInfo.auth
this.nodeInfo.mirror ? this.nodeInfo.mirrorToken : this.nodeInfo.auth
);
if (downloadPath) {
@ -96,7 +96,9 @@ export default class OfficialBuilds extends BaseDistribution {
}
} else {
core.info(
'Not found in manifest. Falling back to download directly from Node'
`Not found in manifest. Falling back to download directly from ${
this.nodeInfo.mirror || 'Node'
}`
);
}
} catch (err) {
@ -176,8 +178,9 @@ export default class OfficialBuilds extends BaseDistribution {
return version;
}
protected getDistributionUrl(): string {
return `https://nodejs.org/dist`;
protected getDistributionUrl(mirror: string): string {
const url = mirror || 'https://nodejs.org';
return `${url}/dist`;
}
private getManifest(): Promise<tc.IToolRelease[]> {
@ -185,7 +188,7 @@ export default class OfficialBuilds extends BaseDistribution {
return tc.getManifestFromRepo(
'actions',
'node-versions',
this.nodeInfo.auth,
this.nodeInfo.mirror ? this.nodeInfo.mirrorToken : this.nodeInfo.auth,
'main'
);
}

View file

@ -6,7 +6,8 @@ export default class RcBuild extends BaseDistribution {
super(nodeInfo);
}
getDistributionUrl(): string {
return 'https://nodejs.org/download/rc';
getDistributionUrl(mirror: string): string {
const url = mirror || 'https://nodejs.org';
return `${url}/download/rc`;
}
}

View file

@ -7,7 +7,8 @@ export default class CanaryBuild extends BasePrereleaseNodejs {
super(nodeInfo);
}
protected getDistributionUrl(): string {
return 'https://nodejs.org/download/v8-canary';
protected getDistributionUrl(mirror: string): string {
const url = mirror || 'https://nodejs.org';
return `${url}/download/v8-canary`;
}
}