Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactor index.ts to separate files for files & integrity operations #121

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/asar-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ export const mergeASARs = async ({
}: MergeASARsOptions): Promise<void> => {
d(`merging ${x64AsarPath} and ${arm64AsarPath}`);

const x64Files = new Set(asar.listPackage(x64AsarPath).map(toRelativePath));
const arm64Files = new Set(asar.listPackage(arm64AsarPath).map(toRelativePath));
const x64Files = new Set(asar.listPackage(x64AsarPath, { isPack: false }).map(toRelativePath));
const arm64Files = new Set(
asar.listPackage(arm64AsarPath, { isPack: false }).map(toRelativePath),
);

//
// Build set of unpacked directories and files
Expand Down
193 changes: 192 additions & 1 deletion src/file-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { spawn, ExitCodeError } from '@malept/cross-spawn-promise';
import * as asar from '@electron/asar';
import { ExitCodeError, spawn } from '@malept/cross-spawn-promise';
import * as dircompare from 'dir-compare';
import * as fs from 'fs-extra';
import { minimatch } from 'minimatch';
import * as path from 'path';
import { MakeUniversalOpts } from '.';
import { generateAsarIntegrity } from './asar-utils';
import { d } from './debug';
import { sha } from './sha';

const MACHO_PREFIX = 'Mach-O ';

Expand Down Expand Up @@ -71,3 +78,187 @@ export const getAllAppFiles = async (appPath: string): Promise<AppFile[]> => {

return files;
};

export async function mergeAppFiles(
tmpApp: string,
opts: MakeUniversalOpts,
knownMergedMachOFiles: Set<unknown>,
tmpDir: string,
) {
d('checking if the x64 and arm64 app folders are identical');
const comparison = await dircompare.compare(
path.resolve(tmpApp, 'Contents', 'Resources', 'app'),
path.resolve(opts.arm64AppPath, 'Contents', 'Resources', 'app'),
{ compareSize: true, compareContent: true },
);
const differences = comparison.diffSet!.filter((difference) => difference.state !== 'equal');
d(`Found ${differences.length} difference(s) between the x64 and arm64 folders`);
const nonMergedDifferences = differences.filter(
(difference) =>
!difference.name1 ||
!knownMergedMachOFiles.has(
path.join('Contents', 'Resources', 'app', difference.relativePath, difference.name1),
),
);
d(`After discluding MachO files merged with lipo ${nonMergedDifferences.length} remain.`);

if (nonMergedDifferences.length === 0) {
d('x64 and arm64 app folders are the same');
return;
}

d('x64 and arm64 app folders are different, creating dynamic entry ASAR');
await fs.move(
path.resolve(tmpApp, 'Contents', 'Resources', 'app'),
path.resolve(tmpApp, 'Contents', 'Resources', 'app-x64'),
);
await fs.copy(
path.resolve(opts.arm64AppPath, 'Contents', 'Resources', 'app'),
path.resolve(tmpApp, 'Contents', 'Resources', 'app-arm64'),
);

const entryAsar = path.resolve(tmpDir, 'entry-asar');
await fs.mkdir(entryAsar);
await fs.copy(
path.resolve(__dirname, '..', '..', 'entry-asar', 'no-asar.js'),
path.resolve(entryAsar, 'index.js'),
);
let pj = await fs.readJson(
path.resolve(opts.x64AppPath, 'Contents', 'Resources', 'app', 'package.json'),
);
pj.main = 'index.js';
await fs.writeJson(path.resolve(entryAsar, 'package.json'), pj);
await asar.createPackage(entryAsar, path.resolve(tmpApp, 'Contents', 'Resources', 'app.asar'));
}

export async function lipoMachOFiles(x64Files: AppFile[], tmpApp: string, opts: MakeUniversalOpts) {
const knownMergedMachOFiles = new Set();
for (const machOFile of x64Files.filter((f) => f.type === AppFileType.MACHO)) {
const first = await fs.realpath(path.resolve(tmpApp, machOFile.relativePath));
const second = await fs.realpath(path.resolve(opts.arm64AppPath, machOFile.relativePath));

const x64Sha = await sha(path.resolve(opts.x64AppPath, machOFile.relativePath));
const arm64Sha = await sha(path.resolve(opts.arm64AppPath, machOFile.relativePath));
if (x64Sha === arm64Sha) {
if (
opts.x64ArchFiles === undefined ||
!minimatch(machOFile.relativePath, opts.x64ArchFiles, { matchBase: true })
) {
throw new Error(
`Detected file "${machOFile.relativePath}" that's the same in both x64 and arm64 builds and not covered by the ` +
`x64ArchFiles rule: "${opts.x64ArchFiles}"`,
);
}

d(
'SHA for Mach-O file',
machOFile.relativePath,
`matches across builds ${x64Sha}===${arm64Sha}, skipping lipo`,
);
continue;
}

d('joining two MachO files with lipo', {
first,
second,
});
await spawn('lipo', [
first,
second,
'-create',
'-output',
await fs.realpath(path.resolve(tmpApp, machOFile.relativePath)),
]);
knownMergedMachOFiles.add(machOFile.relativePath);
}
return knownMergedMachOFiles;
}

export async function copySnapshots(
arm64Files: AppFile[],
opts: MakeUniversalOpts,
tmpApp: string,
) {
for (const snapshotsFile of arm64Files.filter((f) => f.type === AppFileType.SNAPSHOT)) {
d('copying snapshot file', snapshotsFile.relativePath, 'to target application');
await fs.copy(
path.resolve(opts.arm64AppPath, snapshotsFile.relativePath),
path.resolve(tmpApp, snapshotsFile.relativePath),
);
}
}

// returns true if asar is split and shim is added
export async function copyAndShimAsarIfNeeded(
tmpApp: string,
opts: MakeUniversalOpts,
tmpDir: string,
generatedIntegrity: Record<string, { algorithm: 'SHA256'; hash: string }>,
) {
d('checking if the x64 and arm64 asars are identical');
const x64AsarSha = await sha(path.resolve(tmpApp, 'Contents', 'Resources', 'app.asar'));
const arm64AsarSha = await sha(
path.resolve(opts.arm64AppPath, 'Contents', 'Resources', 'app.asar'),
);

if (x64AsarSha !== arm64AsarSha) {
d('x64 and arm64 asars are different');
const x64AsarPath = path.resolve(tmpApp, 'Contents', 'Resources', 'app-x64.asar');
await fs.move(path.resolve(tmpApp, 'Contents', 'Resources', 'app.asar'), x64AsarPath);
const x64Unpacked = path.resolve(tmpApp, 'Contents', 'Resources', 'app.asar.unpacked');
if (await fs.pathExists(x64Unpacked)) {
await fs.move(
x64Unpacked,
path.resolve(tmpApp, 'Contents', 'Resources', 'app-x64.asar.unpacked'),
);
}

const arm64AsarPath = path.resolve(tmpApp, 'Contents', 'Resources', 'app-arm64.asar');
await fs.copy(
path.resolve(opts.arm64AppPath, 'Contents', 'Resources', 'app.asar'),
arm64AsarPath,
);
const arm64Unpacked = path.resolve(
opts.arm64AppPath,
'Contents',
'Resources',
'app.asar.unpacked',
);
if (await fs.pathExists(arm64Unpacked)) {
await fs.copy(
arm64Unpacked,
path.resolve(tmpApp, 'Contents', 'Resources', 'app-arm64.asar.unpacked'),
);
}

const entryAsar = path.resolve(tmpDir, 'entry-asar');
await fs.mkdir(entryAsar);
await fs.copy(
path.resolve(__dirname, '..', '..', 'entry-asar', 'has-asar.js'),
path.resolve(entryAsar, 'index.js'),
);
let pj = JSON.parse(
asar
.extractFile(
path.resolve(opts.x64AppPath, 'Contents', 'Resources', 'app.asar'),
'package.json',
)
.toString('utf8'),
);
pj.main = 'index.js';
await fs.writeJson(path.resolve(entryAsar, 'package.json'), pj);
const asarPath = path.resolve(tmpApp, 'Contents', 'Resources', 'app.asar');
await asar.createPackage(entryAsar, asarPath);

generatedIntegrity['Resources/app.asar'] = generateAsarIntegrity(asarPath);
generatedIntegrity['Resources/app-x64.asar'] = generateAsarIntegrity(x64AsarPath);
generatedIntegrity['Resources/app-arm64.asar'] = generateAsarIntegrity(arm64AsarPath);
return true;
}

d('x64 and arm64 asars are the same');
generatedIntegrity['Resources/app.asar'] = generateAsarIntegrity(
path.resolve(tmpApp, 'Contents', 'Resources', 'app.asar'),
);
return false;
}
Loading