Skip to content

Commit 20e8dda

Browse files
authored
feat: use unofficial builds in SEA (#126)
1 parent 55c70fa commit 20e8dda

File tree

1 file changed

+52
-13
lines changed

1 file changed

+52
-13
lines changed

lib/sea.ts

Lines changed: 52 additions & 13 deletions
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,39 @@ 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 nodeOS = os === 'darwin' ? 'osx' : os;
214+
const latestVersionAndFiles = versions
215+
.map((v: { version: string; files: string[] }) => [v.version, v.files])
216+
.find(
217+
([v, files]: [string, string[]]) =>
218+
v.startsWith(`v${nodeVersion}`) &&
219+
files.find((f: string) => f.startsWith(`${nodeOS}-${arch}`)),
220+
);
197221

198-
if (!latestVersion) {
222+
if (!latestVersionAndFiles) {
199223
throw new Error(`Node version ${nodeVersion} not found`);
200224
}
201225

202-
return latestVersion;
226+
return latestVersionAndFiles[0];
203227
}
204228

205229
/** Fetch, validate and extract nodejs binary. Returns a path to it */
@@ -222,16 +246,31 @@ async function getNodejsExecutable(
222246
return process.execPath;
223247
}
224248

249+
const os = getNodeOs(target.platform);
250+
const arch = getNodeArch(target.arch);
251+
225252
const nodeVersion = await getNodeVersion(
253+
os,
254+
arch,
226255
target.nodeRange.replace('node', ''),
227256
);
228257

229-
const os = getNodeOs(target.platform);
230-
const arch = getNodeArch(target.arch);
231-
232258
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`;
259+
260+
let url;
261+
let checksumUrl;
262+
switch (arch) {
263+
case 'riscv64':
264+
case 'loong64':
265+
url = `https://unofficial-builds.nodejs.org/download/release/${nodeVersion}/${fileName}`;
266+
checksumUrl = `https://unofficial-builds.nodejs.org/download/release/${nodeVersion}/SHASUMS256.txt`;
267+
break;
268+
default:
269+
url = `https://nodejs.org/dist/${nodeVersion}/${fileName}`;
270+
checksumUrl = `https://nodejs.org/dist/${nodeVersion}/SHASUMS256.txt`;
271+
break;
272+
}
273+
235274
const downloadDir = join(homedir(), '.pkg-cache', 'sea');
236275

237276
// Ensure the download directory exists

0 commit comments

Comments
 (0)