Add support for v8-canary, nightly and rc (#655)

This commit is contained in:
Dmitry Shibanov 2023-01-05 13:16:21 +01:00 committed by GitHub
parent 92a57f4a93
commit 64ed1c7eab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 3976 additions and 1648 deletions

View file

@ -0,0 +1,53 @@
import * as tc from '@actions/tool-cache';
import semver from 'semver';
import BaseDistribution from './base-distribution';
import {NodeInputs} from './base-models';
export default abstract class BasePrereleaseNodejs extends BaseDistribution {
protected abstract distribution: string;
constructor(nodeInfo: NodeInputs) {
super(nodeInfo);
}
protected findVersionInHostedToolCacheDirectory(): string {
let toolPath = '';
const localVersionPaths = tc
.findAllVersions('node', this.nodeInfo.arch)
.filter(i => {
const prerelease = semver.prerelease(i);
if (!prerelease) {
return false;
}
return prerelease[0].includes(this.distribution);
});
localVersionPaths.sort(semver.rcompare);
const localVersion = this.evaluateVersions(localVersionPaths);
if (localVersion) {
toolPath = tc.find('node', localVersion, this.nodeInfo.arch);
}
return toolPath;
}
protected validRange(versionSpec: string) {
let range: string;
const [raw, prerelease] = this.splitVersionSpec(versionSpec);
const isValidVersion = semver.valid(raw);
const rawVersion = (isValidVersion ? raw : semver.coerce(raw))!;
if (prerelease !== this.distribution) {
range = versionSpec;
} else {
range = `${semver.validRange(`^${rawVersion}-${this.distribution}`)}-0`;
}
return {range, options: {includePrerelease: !isValidVersion}};
}
protected splitVersionSpec(versionSpec: string) {
return versionSpec.split(/-(.*)/s);
}
}

View file

@ -0,0 +1,287 @@
import * as tc from '@actions/tool-cache';
import * as hc from '@actions/http-client';
import * as core from '@actions/core';
import * as io from '@actions/io';
import semver from 'semver';
import * as assert from 'assert';
import * as path from 'path';
import os from 'os';
import fs from 'fs';
import {NodeInputs, INodeVersion, INodeVersionInfo} from './base-models';
export default abstract class BaseDistribution {
protected httpClient: hc.HttpClient;
protected osPlat = os.platform();
constructor(protected nodeInfo: NodeInputs) {
this.httpClient = new hc.HttpClient('setup-node', [], {
allowRetries: true,
maxRetries: 3
});
}
protected abstract getDistributionUrl(): string;
public async setupNodeJs() {
let nodeJsVersions: INodeVersion[] | undefined;
if (this.nodeInfo.checkLatest) {
const evaluatedVersion = await this.findVersionInDist(nodeJsVersions);
this.nodeInfo.versionSpec = evaluatedVersion;
}
let toolPath = this.findVersionInHostedToolCacheDirectory();
if (toolPath) {
core.info(`Found in cache @ ${toolPath}`);
} else {
const evaluatedVersion = await this.findVersionInDist(nodeJsVersions);
const toolName = this.getNodejsDistInfo(evaluatedVersion);
toolPath = await this.downloadNodejs(toolName);
}
if (this.osPlat != 'win32') {
toolPath = path.join(toolPath, 'bin');
}
core.addPath(toolPath);
}
protected async findVersionInDist(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 = '';
const {range, options} = this.validRange(this.nodeInfo.versionSpec);
core.debug(`evaluating ${versions.length} versions`);
for (let potential of versions) {
const satisfied: boolean = semver.satisfies(potential, range, options);
if (satisfied) {
version = potential;
break;
}
}
if (version) {
core.debug(`matched: ${version}`);
} else {
core.debug('match not found');
}
return version;
}
protected findVersionInHostedToolCacheDirectory() {
return tc.find('node', this.nodeInfo.versionSpec, this.nodeInfo.arch);
}
protected async getNodeJsVersions(): Promise<INodeVersion[]> {
const initialUrl = this.getDistributionUrl();
const dataUrl = `${initialUrl}/index.json`;
let response = await this.httpClient.getJson<INodeVersion[]>(dataUrl);
return response.result || [];
}
protected getNodejsDistInfo(version: string) {
let osArch: string = this.translateArchToDistUrl(this.nodeInfo.arch);
version = semver.clean(version) || '';
let fileName: string =
this.osPlat == 'win32'
? `node-v${version}-win-${osArch}`
: `node-v${version}-${this.osPlat}-${osArch}`;
let urlFileName: string =
this.osPlat == 'win32' ? `${fileName}.7z` : `${fileName}.tar.gz`;
const initialUrl = this.getDistributionUrl();
const url = `${initialUrl}/v${version}/${urlFileName}`;
return <INodeVersionInfo>{
downloadUrl: url,
resolvedVersion: version,
arch: osArch,
fileName: fileName
};
}
protected async downloadNodejs(info: INodeVersionInfo) {
let downloadPath = '';
core.info(
`Acquiring ${info.resolvedVersion} - ${info.arch} from ${info.downloadUrl}`
);
try {
downloadPath = await tc.downloadTool(info.downloadUrl);
} catch (err) {
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
return await this.acquireNodeFromFallbackLocation(
info.resolvedVersion,
info.arch
);
}
throw err;
}
let toolPath = await this.extractArchive(downloadPath, info);
core.info('Done');
return toolPath;
}
protected validRange(versionSpec: string) {
let options: semver.Options | undefined;
const c = semver.clean(versionSpec) || '';
const valid = semver.valid(c) ?? versionSpec;
return {range: valid, options};
}
protected async acquireNodeFromFallbackLocation(
version: string,
arch: string = os.arch()
): Promise<string> {
const initialUrl = this.getDistributionUrl();
let osArch: string = this.translateArchToDistUrl(arch);
// Create temporary folder to download in to
const tempDownloadFolder: string =
'temp_' + Math.floor(Math.random() * 2000000000);
const tempDirectory = process.env['RUNNER_TEMP'] || '';
assert.ok(tempDirectory, 'Expected RUNNER_TEMP to be defined');
const tempDir: string = path.join(tempDirectory, tempDownloadFolder);
await io.mkdirP(tempDir);
let exeUrl: string;
let libUrl: string;
try {
exeUrl = `${initialUrl}/v${version}/win-${osArch}/node.exe`;
libUrl = `${initialUrl}/v${version}/win-${osArch}/node.lib`;
core.info(`Downloading only node binary from ${exeUrl}`);
const exePath = await tc.downloadTool(exeUrl);
await io.cp(exePath, path.join(tempDir, 'node.exe'));
const libPath = await tc.downloadTool(libUrl);
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);
await io.cp(exePath, path.join(tempDir, 'node.exe'));
const libPath = await tc.downloadTool(libUrl);
await io.cp(libPath, path.join(tempDir, 'node.lib'));
} else {
throw err;
}
}
const toolPath = await tc.cacheDir(tempDir, 'node', version, arch);
return toolPath;
}
protected async extractArchive(
downloadPath: string,
info: INodeVersionInfo | null
) {
//
// Extract
//
core.info('Extracting ...');
let extPath: string;
info = info || ({} as INodeVersionInfo); // satisfy compiler, never null when reaches here
if (this.osPlat == 'win32') {
const _7zPath = path.join(__dirname, '../..', 'externals', '7zr.exe');
extPath = await tc.extract7z(downloadPath, undefined, _7zPath);
// 7z extracts to folder matching file name
const nestedPath = path.join(
extPath,
path.basename(info.fileName, '.7z')
);
if (fs.existsSync(nestedPath)) {
extPath = nestedPath;
}
} else {
extPath = await tc.extractTar(downloadPath, undefined, [
'xz',
'--strip',
'1'
]);
}
//
// Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded
//
core.info('Adding to the cache ...');
const toolPath = await tc.cacheDir(
extPath,
'node',
info.resolvedVersion,
info.arch
);
return toolPath;
}
protected getDistFileName(): string {
let osArch: string = this.translateArchToDistUrl(this.nodeInfo.arch);
// node offers a json list of versions
let dataFileName: string;
switch (this.osPlat) {
case 'linux':
dataFileName = `linux-${osArch}`;
break;
case 'darwin':
dataFileName = `osx-${osArch}-tar`;
break;
case 'win32':
dataFileName = `win-${osArch}-exe`;
break;
default:
throw new Error(`Unexpected OS '${this.osPlat}'`);
}
return dataFileName;
}
protected filterVersions(nodeJsVersions: INodeVersion[]) {
const versions: string[] = [];
const dataFileName = this.getDistFileName();
nodeJsVersions.forEach((nodeVersion: INodeVersion) => {
// ensure this version supports your os and platform
if (nodeVersion.files.indexOf(dataFileName) >= 0) {
versions.push(nodeVersion.version);
}
});
return versions.sort(semver.rcompare);
}
protected translateArchToDistUrl(arch: string): string {
switch (arch) {
case 'arm':
return 'armv7l';
default:
return arch;
}
}
}

View file

@ -0,0 +1,19 @@
export interface NodeInputs {
versionSpec: string;
arch: string;
auth?: string;
checkLatest: boolean;
stable: boolean;
}
export interface INodeVersionInfo {
downloadUrl: string;
resolvedVersion: string;
arch: string;
fileName: string;
}
export interface INodeVersion {
version: string;
files: string[];
}

View file

@ -0,0 +1,31 @@
import BaseDistribution from './base-distribution';
import {NodeInputs} from './base-models';
import NightlyNodejs from './nightly/nightly_builds';
import OfficialBuilds from './official_builds/official_builds';
import RcBuild from './rc/rc_builds';
import CanaryBuild from './v8-canary/canary_builds';
enum Distributions {
DEFAULT = '',
CANARY = 'v8-canary',
NIGHTLY = 'nightly',
RC = 'rc'
}
export function getNodejsDistribution(
installerOptions: NodeInputs
): BaseDistribution {
const versionSpec = installerOptions.versionSpec;
let distribution: BaseDistribution;
if (versionSpec.includes(Distributions.NIGHTLY)) {
distribution = new NightlyNodejs(installerOptions);
} else if (versionSpec.includes(Distributions.CANARY)) {
distribution = new CanaryBuild(installerOptions);
} else if (versionSpec.includes(Distributions.RC)) {
distribution = new RcBuild(installerOptions);
} else {
distribution = new OfficialBuilds(installerOptions);
}
return distribution;
}

View file

@ -0,0 +1,13 @@
import BasePrereleaseNodejs from '../base-distribution-prerelease';
import {NodeInputs} from '../base-models';
export default class NightlyNodejs extends BasePrereleaseNodejs {
protected distribution = 'nightly';
constructor(nodeInfo: NodeInputs) {
super(nodeInfo);
}
protected getDistributionUrl(): string {
return 'https://nodejs.org/download/nightly';
}
}

View file

@ -0,0 +1,258 @@
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
import path from 'path';
import BaseDistribution from '../base-distribution';
import {NodeInputs, INodeVersion, INodeVersionInfo} from '../base-models';
interface INodeRelease extends tc.IToolRelease {
lts?: string;
}
export default class OfficialBuilds extends BaseDistribution {
constructor(nodeInfo: NodeInputs) {
super(nodeInfo);
}
public async setupNodeJs() {
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...');
// 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`
);
}
}
let toolPath = this.findVersionInHostedToolCacheDirectory();
if (toolPath) {
core.info(`Found in cache @ ${toolPath}`);
} else {
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}`
);
downloadPath = await tc.downloadTool(
versionInfo.downloadUrl,
undefined,
this.nodeInfo.auth
);
if (downloadPath) {
toolPath = await this.extractArchive(downloadPath, versionInfo);
}
} 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(err.stack);
core.info('Falling back to download directly from Node');
}
if (!toolPath) {
const 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}.`
);
}
const toolName = this.getNodejsDistInfo(evaluatedVersion);
toolPath = await this.downloadNodejs(toolName);
}
}
if (this.osPlat != 'win32') {
toolPath = path.join(toolPath, 'bin');
}
core.addPath(toolPath);
}
protected evaluateVersions(versions: string[]): string {
let version = '';
if (this.isLatestSyntax(this.nodeInfo.versionSpec)) {
core.info(`getting latest node version...`);
return versions[0];
}
version = super.evaluateVersions(versions);
return version;
}
protected getDistributionUrl(): string {
return `https://nodejs.org/dist`;
}
private getManifest(): Promise<tc.IToolRelease[]> {
core.debug('Getting manifest from actions/node-versions@main');
return tc.getManifestFromRepo(
'actions',
'node-versions',
this.nodeInfo.auth,
'main'
);
}
private resolveLtsAliasFromManifest(
versionSpec: string,
stable: boolean,
manifest: INodeRelease[]
): string {
const alias = versionSpec.split('lts/')[1]?.toLowerCase();
if (!alias) {
throw new Error(
`Unable to parse LTS alias for Node version '${versionSpec}'`
);
}
core.debug(`LTS alias '${alias}' for Node version '${versionSpec}'`);
// Supported formats are `lts/<alias>`, `lts/*`, and `lts/-n`. Where asterisk means highest possible LTS and -n means the nth-highest.
const n = Number(alias);
const aliases = Object.fromEntries(
manifest
.filter(x => x.lts && x.stable === stable)
.map(x => [x.lts!.toLowerCase(), x])
.reverse()
);
const numbered = Object.values(aliases);
const release =
alias === '*'
? numbered[numbered.length - 1]
: n < 0
? numbered[numbered.length - 1 + n]
: aliases[alias];
if (!release) {
throw new Error(
`Unable to find LTS release '${alias}' for Node version '${versionSpec}'.`
);
}
core.debug(
`Found LTS release '${release.version}' for Node version '${versionSpec}'`
);
return release.version.split('.')[0];
}
private async resolveVersionFromManifest(
versionSpec: string,
stable: boolean,
osArch: string,
manifest: tc.IToolRelease[] | undefined
): Promise<string | undefined> {
try {
const info = await this.getInfoFromManifest(
versionSpec,
stable,
osArch,
manifest
);
return info?.resolvedVersion;
} catch (err) {
core.info('Unable to resolve version from manifest...');
core.debug(err.message);
}
}
private async getInfoFromManifest(
versionSpec: string,
stable: boolean,
osArch: string,
manifest: tc.IToolRelease[] | undefined
): Promise<INodeVersionInfo | null> {
let info: INodeVersionInfo | null = null;
if (!manifest) {
core.debug('No manifest cached');
manifest = await this.getManifest();
}
const rel = await tc.findFromManifest(
versionSpec,
stable,
manifest,
osArch
);
if (rel && rel.files.length > 0) {
info = <INodeVersionInfo>{};
info.resolvedVersion = rel.version;
info.arch = rel.files[0].arch;
info.downloadUrl = rel.files[0].download_url;
info.fileName = rel.files[0].filename;
}
return info;
}
private isLtsAlias(versionSpec: string): boolean {
return versionSpec.startsWith('lts/');
}
private isLatestSyntax(versionSpec): boolean {
return ['current', 'latest', 'node'].includes(versionSpec);
}
}

View file

@ -0,0 +1,12 @@
import BaseDistribution from '../base-distribution';
import {NodeInputs} from '../base-models';
export default class RcBuild extends BaseDistribution {
constructor(nodeInfo: NodeInputs) {
super(nodeInfo);
}
getDistributionUrl(): string {
return 'https://nodejs.org/download/rc';
}
}

View file

@ -0,0 +1,13 @@
import BasePrereleaseNodejs from '../base-distribution-prerelease';
import {NodeInputs} from '../base-models';
export default class CanaryBuild extends BasePrereleaseNodejs {
protected distribution = 'v8-canary';
constructor(nodeInfo: NodeInputs) {
super(nodeInfo);
}
protected getDistributionUrl(): string {
return 'https://nodejs.org/download/v8-canary';
}
}