Skip to content

Commit ec12c5a

Browse files
committed
feat: Use unofficial builds in SEA
riscv64 and loong64 have unofficial builds at https://unofficial-builds.nodejs.org and can be used in SEA build. This is in conjunction with yao-pkg/pkg-fetch#62.
1 parent 55c70fa commit ec12c5a

File tree

1 file changed

+51
-13
lines changed

1 file changed

+51
-13
lines changed

lib/sea.ts

+51-13
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,15 @@ function getNodeOs(platform: string) {
156156

157157
/** Get the node arch based on target arch */
158158
function getNodeArch(arch: string) {
159-
const allowedArchs = ['x64', 'arm64', 'armv7l', 'ppc64', 's390x'];
159+
const allowedArchs = [
160+
'x64',
161+
'arm64',
162+
'armv7l',
163+
'ppc64',
164+
's390x',
165+
'riscv64',
166+
'loong64',
167+
];
160168

161169
if (!allowedArchs.includes(arch)) {
162170
throw new Error(`Unsupported architecture: ${arch}`);
@@ -166,7 +174,7 @@ function getNodeArch(arch: string) {
166174
}
167175

168176
/** Get latest node version based on the provided partial version */
169-
async function getNodeVersion(nodeVersion: string) {
177+
async function getNodeVersion(os: string, arch: string, nodeVersion: string) {
170178
// validate nodeVersion using regex. Allowed formats: 16, 16.0, 16.0.0
171179
const regex = /^\d{1,2}(\.\d{1,2}){0,2}$/;
172180
if (!regex.test(nodeVersion)) {
@@ -183,23 +191,38 @@ async function getNodeVersion(nodeVersion: string) {
183191
return nodeVersion;
184192
}
185193

186-
const response = await fetch('https://nodejs.org/dist/index.json');
194+
let url;
195+
switch (arch) {
196+
case 'riscv64':
197+
case 'loong64':
198+
url = 'https://unofficial-builds.nodejs.org/download/release/index.json';
199+
break;
200+
default:
201+
url = 'https://nodejs.org/dist/index.json';
202+
break;
203+
}
204+
205+
const response = await fetch(url);
187206

188207
if (!response.ok) {
189208
throw new Error('Failed to fetch node versions');
190209
}
191210

192211
const versions = await response.json();
193212

194-
const latestVersion = versions
195-
.map((v: { version: string }) => v.version)
196-
.find((v: string) => v.startsWith(`v${nodeVersion}`));
213+
const latestVersionAndFiles = versions
214+
.map((v: { version: string; files: string[] }) => [v.version, v.files])
215+
.find(
216+
([v, files]: [string, string[]]) =>
217+
v.startsWith(`v${nodeVersion}`) &&
218+
files.find((f: string) => f.startsWith(`${os}-${arch}`)),
219+
);
197220

198-
if (!latestVersion) {
221+
if (!latestVersionAndFiles) {
199222
throw new Error(`Node version ${nodeVersion} not found`);
200223
}
201224

202-
return latestVersion;
225+
return latestVersionAndFiles[0];
203226
}
204227

205228
/** Fetch, validate and extract nodejs binary. Returns a path to it */
@@ -222,16 +245,31 @@ async function getNodejsExecutable(
222245
return process.execPath;
223246
}
224247

248+
const os = getNodeOs(target.platform);
249+
const arch = getNodeArch(target.arch);
250+
225251
const nodeVersion = await getNodeVersion(
252+
os,
253+
arch,
226254
target.nodeRange.replace('node', ''),
227255
);
228256

229-
const os = getNodeOs(target.platform);
230-
const arch = getNodeArch(target.arch);
231-
232257
const fileName = `node-${nodeVersion}-${os}-${arch}.${os === 'win' ? 'zip' : 'tar.gz'}`;
233-
const url = `https://nodejs.org/dist/${nodeVersion}/${fileName}`;
234-
const checksumUrl = `https://nodejs.org/dist/${nodeVersion}/SHASUMS256.txt`;
258+
259+
let url;
260+
let checksumUrl;
261+
switch (arch) {
262+
case 'riscv64':
263+
case 'loong64':
264+
url = `https://unofficial-builds.nodejs.org/download/release/${nodeVersion}/${fileName}`;
265+
checksumUrl = `https://unofficial-builds.nodejs.org/download/release/${nodeVersion}/SHASUMS256.txt`;
266+
break;
267+
default:
268+
url = `https://nodejs.org/dist/${nodeVersion}/${fileName}`;
269+
checksumUrl = `https://nodejs.org/dist/${nodeVersion}/SHASUMS256.txt`;
270+
break;
271+
}
272+
235273
const downloadDir = join(homedir(), '.pkg-cache', 'sea');
236274

237275
// Ensure the download directory exists

0 commit comments

Comments
 (0)