Files
Netcatty/scripts/afterPackMacUuid.test.cjs
T
陈大猫 ef4caf8b08 chore(packaging): omit duplicate renderer and unused native files (#3726)
Keep renderer-only libraries available as development dependencies so Vite
bundles their runtime code without shipping their dependency trees again.
Exclude native compilation helpers from packaged resources, and prune
node-pty/serialport prebuilds for other platforms and architectures while
preserving matching universal binaries and rebuilt runtime files.

Validation:
- npm run lint; 33 packaging tests pass.
- npm run pack:dir, real packaged application UI, and 29 packaged runtime
  smoke checks pass (SDK imports, PTY, serial port listing, SQLite query).
- All 999 UI files retain their code/content; differences are generated
  asset references and the embedded dependency classification metadata.
- Dependency versions are unchanged.
- macOS arm64 app: 394,959,135 -> 360,740,625 bytes.
- macOS arm64 ZIP: 150,847,369 -> 143,151,622 bytes; ZIP integrity passes.
- Full suite: 14,035 pass, 36 skipped, 5 fail. The four FIDO helper failures
  and transfer FileHandle-GC failure reproduce on unchanged main with Node 26.
2026-10-18 15:26:58 +08:00

411 lines
15 KiB
JavaScript

const test = require("node:test");
const assert = require("node:assert/strict");
const {
adHocSignAppBundle,
deriveUuid,
patchMachOBuffer,
pruneAsarHeaderEntries,
pruneCursorSdkPlatformPackages,
pruneNativePrebuilds,
readAsarHeader,
} = require("./afterPackMacUuid.cjs");
const LC_UUID = 0x1b;
const LC_OTHER = 0x19;
const MH_MAGIC_64 = 0xfeedfacf;
function align4(value) {
return value + ((4 - (value % 4)) % 4);
}
function writeFakeAsar(asarPath, header, payload = Buffer.from("packed-payload")) {
const headerString = JSON.stringify(header);
const headerStringLength = Buffer.byteLength(headerString);
const headerPayloadSize = 4 + align4(headerStringLength);
const headerSize = 4 + headerPayloadSize;
const sizeBuf = Buffer.alloc(8);
const headerBuf = Buffer.alloc(headerSize);
sizeBuf.writeUInt32LE(4, 0);
sizeBuf.writeUInt32LE(headerSize, 4);
headerBuf.writeUInt32LE(headerPayloadSize, 0);
headerBuf.writeInt32LE(headerStringLength, 4);
headerBuf.write(headerString, 8, headerStringLength, "utf8");
require("node:fs").writeFileSync(asarPath, Buffer.concat([sizeBuf, headerBuf, payload]));
return headerSize;
}
// Build a minimal thin little-endian 64-bit Mach-O with two load commands:
// one dummy command and one LC_UUID carrying `uuidBytes`.
function buildThinMachO(uuidBytes) {
const header = Buffer.alloc(32);
header.writeUInt32LE(MH_MAGIC_64, 0); // magic
header.writeUInt32LE(0x0100000c, 4); // cputype arm64 (value irrelevant)
header.writeUInt32LE(0, 8); // cpusubtype
header.writeUInt32LE(2, 12); // filetype
header.writeUInt32LE(2, 16); // ncmds
header.writeUInt32LE(16 + 24, 20); // sizeofcmds
header.writeUInt32LE(0, 24); // flags
header.writeUInt32LE(0, 28); // reserved
const dummy = Buffer.alloc(16);
dummy.writeUInt32LE(LC_OTHER, 0); // cmd
dummy.writeUInt32LE(16, 4); // cmdsize
dummy.fill(0xab, 8); // payload sentinel
const uuidCmd = Buffer.alloc(24);
uuidCmd.writeUInt32LE(LC_UUID, 0); // cmd
uuidCmd.writeUInt32LE(24, 4); // cmdsize
uuidBytes.copy(uuidCmd, 8);
return Buffer.concat([header, dummy, uuidCmd]);
}
// Wrap one or more thin slices in a big-endian 32-bit fat binary.
function buildFatMachO(slices) {
const headerSize = 8 + slices.length * 20;
const header = Buffer.alloc(headerSize);
header.writeUInt32BE(0xcafebabe, 0); // FAT_MAGIC
header.writeUInt32BE(slices.length, 4);
let offset = headerSize;
const offsets = [];
for (let i = 0; i < slices.length; i += 1) {
const archOff = 8 + i * 20;
header.writeUInt32BE(0x0100000c, archOff); // cputype
header.writeUInt32BE(0, archOff + 4); // cpusubtype
header.writeUInt32BE(offset, archOff + 8); // offset
header.writeUInt32BE(slices[i].length, archOff + 12); // size
header.writeUInt32BE(0, archOff + 16); // align
offsets.push(offset);
offset += slices[i].length;
}
return Buffer.concat([header, ...slices]);
}
test("deriveUuid is deterministic and 16 bytes", () => {
const a = deriveUuid("com.netcatty.app");
const b = deriveUuid("com.netcatty.app");
assert.equal(a.length, 16);
assert.ok(a.equals(b));
});
for (const [platform, arch, keep] of [
["darwin", 3, ["darwin-arm64", "darwin-x64+arm64"]],
["darwin", 1, ["darwin-x64", "darwin-x64+arm64"]],
["darwin", 4, ["darwin-arm64", "darwin-x64", "darwin-x64+arm64"]],
["linux", 3, ["linux-arm64"]],
["linux", 1, ["linux-x64"]],
["linux", 2, ["linux-arm"]],
["win32", 1, ["win32-x64"]],
["win32", 3, ["win32-arm64"]],
["win32", 0, ["win32-ia32"]],
["darwin", "unknown-arch", null],
["darwin", 99, null],
["unknown-platform", 1, null],
]) {
test(`native prebuild pruning preserves the ${platform}/${arch} runtime and ASAR payload`, (t) => {
const fs = require("node:fs");
const path = require("node:path");
const { getTempFilePath } = require("../electron/bridges/tempDirBridge.cjs");
const tempDir = getTempFilePath("native-prebuild-pruning");
t.after(() => fs.rmSync(tempDir, { recursive: true, force: true }));
const resources = platform === "darwin"
? path.join(tempDir, "Netcatty.app", "Contents", "Resources")
: path.join(tempDir, "resources");
const unpacked = path.join(resources, "app.asar.unpacked");
const payload = Buffer.from("preserved-packed-payload");
const header = { files: { "packed.txt": { size: payload.length, offset: "0" } } };
const tuples = [
"darwin-arm64", "darwin-x64", "darwin-x64+arm64", "linux-arm64", "linux-x64",
"linux-arm", "win32-x64", "win32-arm64", "win32-ia32", "android-arm64", "custom",
];
const paths = [];
for (const packageName of ["node-pty", "@serialport/bindings-cpp"]) {
for (const directory of [...tuples.map((tuple) => `prebuilds/${tuple}`), "build/Release"]) {
const relative = `node_modules/${packageName}/${directory}/runtime.node`;
const file = path.join(unpacked, relative);
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, "native-runtime");
let node = header;
const segments = relative.split("/");
for (const segment of segments.slice(0, -1)) {
node.files ??= {};
node = node.files[segment] ??= { files: {} };
}
node.files[segments.at(-1)] = { size: 14, unpacked: true };
paths.push(relative);
}
}
const asarPath = path.join(resources, "app.asar");
const originalHeaderSize = writeFakeAsar(asarPath, header, payload);
const context = {
electronPlatformName: platform, arch, appOutDir: tempDir,
packager: { appInfo: { productFilename: "Netcatty" } },
};
const removed = pruneNativePrebuilds(context);
const updated = readAsarHeader(asarPath);
assert.equal(updated.headerSize, originalHeaderSize);
assert.deepEqual(fs.readFileSync(asarPath).subarray(8 + originalHeaderSize), payload);
for (const relative of paths) {
const tuple = relative.split("/prebuilds/")[1]?.split("/")[0];
const retained = keep === null || !tuple || tuple === "custom" || keep.includes(tuple);
assert.equal(fs.existsSync(path.join(unpacked, relative)), retained, relative);
const entry = relative.split("/").reduce((node, segment) => node?.files?.[segment], updated.header);
assert.equal(Boolean(entry), retained, `ASAR ${relative}`);
}
if (keep === null) assert.deepEqual(removed, []);
assert.deepEqual(pruneNativePrebuilds(context), []);
});
}
test("deriveUuid differs per appId and sets version/variant bits", () => {
const a = deriveUuid("com.netcatty.app");
const b = deriveUuid("com.example.other");
assert.ok(!a.equals(b));
assert.equal(a[6] & 0xf0, 0x50); // version 5
assert.equal(a[8] & 0xc0, 0x80); // RFC 4122 variant
});
test("patchMachOBuffer rewrites LC_UUID in a thin Mach-O and leaves the rest intact", () => {
const original = Buffer.alloc(16, 0x11);
const buf = buildThinMachO(original);
const uuid = deriveUuid("com.netcatty.app");
const { patched, oldUuids } = patchMachOBuffer(buf, uuid);
assert.equal(patched, 1);
assert.equal(oldUuids[0], original.toString("hex"));
// LC_UUID payload is now our derived uuid (uuid command starts at byte 48).
assert.ok(buf.subarray(48 + 8, 48 + 24).equals(uuid));
// Header magic + the dummy command's payload are untouched.
assert.equal(buf.readUInt32LE(0), MH_MAGIC_64);
assert.equal(buf.readUInt32LE(32), LC_OTHER);
assert.ok(buf.subarray(32 + 8, 32 + 16).equals(Buffer.alloc(8, 0xab)));
});
test("patchMachOBuffer patches every slice of a fat binary", () => {
const slice1 = buildThinMachO(Buffer.alloc(16, 0x22));
const slice2 = buildThinMachO(Buffer.alloc(16, 0x33));
const fat = buildFatMachO([slice1, slice2]);
const uuid = deriveUuid("com.netcatty.app");
const { patched } = patchMachOBuffer(fat, uuid);
assert.equal(patched, 2);
});
test("patchMachOBuffer reports zero when there is no LC_UUID", () => {
// A thin Mach-O whose single command is not LC_UUID.
const header = Buffer.alloc(32);
header.writeUInt32LE(MH_MAGIC_64, 0);
header.writeUInt32LE(1, 16); // ncmds
const cmd = Buffer.alloc(16);
cmd.writeUInt32LE(LC_OTHER, 0);
cmd.writeUInt32LE(16, 4);
const buf = Buffer.concat([header, cmd]);
const { patched } = patchMachOBuffer(buf, deriveUuid("com.netcatty.app"));
assert.equal(patched, 0);
});
test("adHocSignAppBundle signs the full app bundle on macOS hosts", () => {
const calls = [];
const didSign = adHocSignAppBundle("/tmp/Netcatty.app", {
hostPlatform: "darwin",
execFileSync: (bin, args, options) => {
calls.push({ bin, args, options });
},
});
assert.equal(didSign, true);
assert.deepEqual(calls, [
{
bin: "codesign",
args: [
"--force",
"--deep",
"--sign",
"-",
"--timestamp=none",
"/tmp/Netcatty.app",
],
options: { stdio: ["ignore", "pipe", "pipe"] },
},
]);
});
test("adHocSignAppBundle skips non-macOS hosts", () => {
let called = false;
const didSign = adHocSignAppBundle("/tmp/Netcatty.app", {
hostPlatform: "linux",
execFileSync: () => {
called = true;
},
});
assert.equal(didSign, false);
assert.equal(called, false);
});
test("pruneAsarHeaderEntries removes package records without moving packed payload", (t) => {
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-prune-asar-"));
t.after(() => fs.rmSync(tempDir, { recursive: true, force: true }));
const asarPath = path.join(tempDir, "app.asar");
const payload = Buffer.from("packed-payload");
const headerSize = writeFakeAsar(
asarPath,
{
files: {
node_modules: {
files: {
"@cursor": {
files: {
"sdk-darwin-arm64": {
files: {
"package.json": { size: 2, unpacked: true },
},
},
"sdk-darwin-x64": {
files: {
"package.json": { size: 2, unpacked: true },
},
},
},
},
},
},
"packed.txt": { size: payload.length, offset: "0" },
},
},
payload,
);
const removed = pruneAsarHeaderEntries(asarPath, ["node_modules/@cursor/sdk-darwin-x64"]);
const { header, headerSize: updatedHeaderSize } = readAsarHeader(asarPath);
const packedPayload = fs.readFileSync(asarPath).subarray(8 + headerSize);
assert.deepEqual(removed, ["node_modules/@cursor/sdk-darwin-x64"]);
assert.equal(updatedHeaderSize, headerSize);
assert.ok(header.files.node_modules.files["@cursor"].files["sdk-darwin-arm64"]);
assert.equal(header.files.node_modules.files["@cursor"].files["sdk-darwin-x64"], undefined);
assert.equal(packedPayload.toString("utf8"), payload.toString("utf8"));
});
test("pruneCursorSdkPlatformPackages keeps only the target macOS arch package", (t) => {
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-prune-cursor-"));
t.after(() => fs.rmSync(tempDir, { recursive: true, force: true }));
const cursorRoot = path.join(
tempDir,
"Netcatty.app",
"Contents",
"Resources",
"app.asar.unpacked",
"node_modules",
"@cursor",
);
fs.mkdirSync(path.join(cursorRoot, "sdk-darwin-arm64"), { recursive: true });
fs.mkdirSync(path.join(cursorRoot, "sdk-darwin-x64"), { recursive: true });
writeFakeAsar(path.join(tempDir, "Netcatty.app", "Contents", "Resources", "app.asar"), {
files: {
node_modules: {
files: {
"@cursor": {
files: {
"sdk-darwin-arm64": { files: { "package.json": { size: 2, unpacked: true } } },
"sdk-darwin-x64": { files: { "package.json": { size: 2, unpacked: true } } },
},
},
},
},
},
});
const removed = pruneCursorSdkPlatformPackages({
electronPlatformName: "darwin",
arch: 3,
appOutDir: tempDir,
packager: { appInfo: { productFilename: "Netcatty" } },
});
assert.deepEqual(removed, ["sdk-darwin-x64"]);
assert.ok(fs.existsSync(path.join(cursorRoot, "sdk-darwin-arm64")));
assert.ok(!fs.existsSync(path.join(cursorRoot, "sdk-darwin-x64")));
const { header } = readAsarHeader(
path.join(tempDir, "Netcatty.app", "Contents", "Resources", "app.asar"),
);
assert.ok(header.files.node_modules.files["@cursor"].files["sdk-darwin-arm64"]);
assert.equal(header.files.node_modules.files["@cursor"].files["sdk-darwin-x64"], undefined);
});
test("pruneCursorSdkPlatformPackages keeps both macOS packages for universal builds", (t) => {
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-prune-cursor-"));
t.after(() => fs.rmSync(tempDir, { recursive: true, force: true }));
const cursorRoot = path.join(
tempDir,
"Netcatty.app",
"Contents",
"Resources",
"app.asar.unpacked",
"node_modules",
"@cursor",
);
fs.mkdirSync(path.join(cursorRoot, "sdk-darwin-arm64"), { recursive: true });
fs.mkdirSync(path.join(cursorRoot, "sdk-darwin-x64"), { recursive: true });
const removed = pruneCursorSdkPlatformPackages({
electronPlatformName: "darwin",
arch: 4,
appOutDir: tempDir,
packager: { appInfo: { productFilename: "Netcatty" } },
});
assert.deepEqual(removed, []);
assert.ok(fs.existsSync(path.join(cursorRoot, "sdk-darwin-arm64")));
assert.ok(fs.existsSync(path.join(cursorRoot, "sdk-darwin-x64")));
});
test("pruneCursorSdkPlatformPackages keeps only the target Linux arch package", (t) => {
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-prune-cursor-"));
t.after(() => fs.rmSync(tempDir, { recursive: true, force: true }));
const cursorRoot = path.join(
tempDir,
"resources",
"app.asar.unpacked",
"node_modules",
"@cursor",
);
fs.mkdirSync(path.join(cursorRoot, "sdk-linux-arm64"), { recursive: true });
fs.mkdirSync(path.join(cursorRoot, "sdk-linux-x64"), { recursive: true });
const removed = pruneCursorSdkPlatformPackages({
electronPlatformName: "linux",
arch: 1,
appOutDir: tempDir,
packager: { appInfo: { productFilename: "netcatty" } },
});
assert.deepEqual(removed, ["sdk-linux-arm64"]);
assert.ok(!fs.existsSync(path.join(cursorRoot, "sdk-linux-arm64")));
assert.ok(fs.existsSync(path.join(cursorRoot, "sdk-linux-x64")));
});