fix: build Windows exe from Windows node binary on non-Windows CI
build-windows-exe / build (push) Successful in 48s

This commit is contained in:
lzstealth committed 2026-10-18 16:47:14 +01:00
1 parent 54a0be4836
commit 2af31a09a9
3 files changed
+48 -3

No files matched your search

+45
View File
@@ -1,3 +1,4 @@
const crypto = require('crypto');
const fs = require('fs');
const os = require('os');
const path = require('path');
@@ -10,6 +11,39 @@ function normalizeBuildVersion(value) {
return version.startsWith('v') ? version : `v${version}`;
}
async function download(url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to download ${url}: ${response.status} ${response.statusText}`);
}
return Buffer.from(await response.arrayBuffer());
}
// SEA blobs must be injected into a node binary of the same version, and non-Windows hosts need a Windows base binary.
async function getWindowsNodeBinary(tempDir) {
if (process.platform === 'win32') {
return process.execPath;
}
const baseUrl = `https://nodejs.org/dist/${process.version}`;
const [binary, shasums] = await Promise.all([
download(`${baseUrl}/win-x64/node.exe`),
download(`${baseUrl}/SHASUMS256.txt`),
]);
const expected = shasums.toString('utf8').split('\n')
.map(line => line.trim().split(/\s+/))
.find(([, file]) => file === 'win-x64/node.exe');
const actual = crypto.createHash('sha256').update(binary).digest('hex');
if (!expected || expected[0] !== actual) {
throw new Error('Checksum verification failed for downloaded win-x64/node.exe');
}
const binaryPath = path.join(tempDir, 'node.exe');
fs.writeFileSync(binaryPath, binary);
return binaryPath;
}
async function main() {
const distDir = path.resolve(__dirname, '..', 'dist');
if (!fs.existsSync(distDir)) {
@@ -33,8 +67,11 @@ async function main() {
logLevel: 'info',
});
const executable = await getWindowsNodeBinary(tempDir);
fs.writeFileSync(seaConfigPath, JSON.stringify({
main: bundlePath,
executable,
output: outputFile,
mainFormat: 'commonjs',
disableExperimentalSEAWarning: true,
@@ -43,6 +80,14 @@ async function main() {
}, null, 2));
execFileSync(process.execPath, ['--build-sea', seaConfigPath], {stdio: 'inherit'});
const header = Buffer.alloc(2);
const fd = fs.openSync(outputFile, 'r');
fs.readSync(fd, header, 0, 2, 0);
fs.closeSync(fd);
if (header.toString('latin1') !== 'MZ') {
throw new Error(`${outputFile} is not a Windows PE executable`);
}
}
main().catch(error => {