perf(electron): speed up Windows packaging with ASAR (#1190)

* perf(electron): package Windows app with ASAR

* fix(electron): use portable ASAR unpack glob

* fix(electron): unpack native modules from ASAR
This commit is contained in:
Tommaso Casaburi
2026-08-01 12:50:23 +02:00
committed by GitHub
parent f2f6c0ecb7
commit a67c66de0d
7 changed files with 93 additions and 26 deletions
+16
View File
@@ -0,0 +1,16 @@
import path from 'node:path';
export const getIpfsBinaryName = (platform = process.platform) => (platform === 'win32' ? 'ipfs.exe' : 'ipfs');
const getPlatformDirectory = (platform) => {
if (platform === 'win32') return 'win';
if (platform === 'darwin') return 'mac';
return 'linux';
};
export const getBundledKuboPath = (rootPath, platform = process.platform) => path.join(rootPath, 'bin', getPlatformDirectory(platform), getIpfsBinaryName(platform));
export const getPackagedKuboPaths = (resourcesPath, platform = process.platform) => [
getBundledKuboPath(path.join(resourcesPath, 'app.asar.unpacked'), platform),
getBundledKuboPath(path.join(resourcesPath, 'app'), platform),
];
+18
View File
@@ -0,0 +1,18 @@
import path from 'node:path';
import { describe, expect, it } from 'vitest';
import { getPackagedKuboPaths } from './kubo-paths.js';
describe('getPackagedKuboPaths', () => {
it.each([
['win32', 'win', 'ipfs.exe'],
['darwin', 'mac', 'ipfs'],
['linux', 'linux', 'ipfs'],
])('prefers the unpacked ASAR binary on %s and retains the loose-package fallback', (platform, platformDirectory, binaryName) => {
const resourcesPath = path.join('/tmp', '5chan', 'resources');
expect(getPackagedKuboPaths(resourcesPath, platform)).toEqual([
path.join(resourcesPath, 'app.asar.unpacked', 'bin', platformDirectory, binaryName),
path.join(resourcesPath, 'app', 'bin', platformDirectory, binaryName),
]);
});
});
+7 -19
View File
@@ -6,22 +6,11 @@ import ps from 'node:process';
import proxyServer from './proxy-server.js';
import tcpPortUsed from 'tcp-port-used';
import { fileURLToPath, pathToFileURL } from 'url';
import { getBundledKuboPath, getIpfsBinaryName, getPackagedKuboPaths } from './kubo-paths.js';
import { getPkcDataPath } from './pkc-paths.js';
const dirname = path.join(path.dirname(fileURLToPath(import.meta.url)));
const projectRoot = path.join(dirname, '..');
// Get platform-specific binary name
const getIpfsBinaryName = () => (process.platform === 'win32' ? 'ipfs.exe' : 'ipfs');
// Get platform subdirectory name for bin/ folder
const getPlatformDir = () => {
if (process.platform === 'win32') return 'win';
if (process.platform === 'darwin') return 'mac';
return 'linux';
};
const getBundledKuboPath = (rootPath) => path.join(rootPath, 'bin', getPlatformDir(), getIpfsBinaryName());
const downloadBundledIpfsClients = async () => {
// before-pack.js is excluded from packaged builds, so only load it in dev.
const { downloadIpfsClients } = await import('./before-pack.js');
@@ -50,17 +39,16 @@ const getKuboPath = async () => {
throw new Error(`Kubo binary download completed but '${bundledKuboPath}' was not found`);
} else {
// In production, the binary is downloaded to bin/<platform>/ipfs by generateAssets hook
// With asar: false, files are at resources/app/ instead of resources/app.asar.unpacked
const appPath = process.resourcesPath;
const binaryName = getIpfsBinaryName();
// Try the bin/ directory first (where generateAssets downloads binaries)
const binDirPath = getBundledKuboPath(path.join(appPath, 'app'));
if (fs.existsSync(binDirPath)) {
return binDirPath;
// Prefer the unpacked ASAR path, then support older loose-file packages.
const packagedBinPaths = getPackagedKuboPaths(appPath);
const packagedBinPath = packagedBinPaths.find((candidatePath) => fs.existsSync(candidatePath));
if (packagedBinPath) {
return packagedBinPath;
}
// Fallback: try app.asar.unpacked for ASAR builds (if we ever re-enable ASAR)
const unpackedPath = path.join(appPath, 'app.asar.unpacked');
const kuboModulePath = path.join(unpackedPath, 'node_modules', 'kubo');
@@ -84,7 +72,7 @@ const getKuboPath = async () => {
return appKuboBinPath;
}
throw new Error(`Could not find kubo binary. Checked: ${binDirPath}, ${kuboBinPath}, ${appKuboBinPath}`);
throw new Error(`Could not find kubo binary. Checked: ${[...packagedBinPaths, kuboBinPath, appKuboBinPath].join(', ')}`);
}
}
};