Compare commits

...
Author SHA1 Message Date
陈大猫 3260bf4206 fix(electron): isolate unpackaged app profiles 2026-11-18 14:05:02 +08:00
3 changed files with 75 additions and 9 deletions

No files matched your search

+20
View File
@@ -0,0 +1,20 @@
const fs = require("node:fs");
const path = require("node:path");
function configureAppProfile(app, devServerUrl) {
const unpackaged = !app.isPackaged;
const isDev = unpackaged && Boolean(devServerUrl);
if (unpackaged) {
// Keep the existing development profile path for both Vite and dist.
app.setName("Netcatty Dev");
const devProfile = path.join(app.getPath("userData"), "dev");
fs.mkdirSync(devProfile, { recursive: true });
app.setPath("userData", devProfile);
// Chromium's Local Storage, IndexedDB and other session state must
// follow the profile even if Electron has already resolved sessionData.
app.setPath("sessionData", devProfile);
}
return { isDev, effectiveDevServerUrl: isDev ? devServerUrl : undefined };
}
module.exports = { configureAppProfile };
+51
View File
@@ -0,0 +1,51 @@
const assert = require("node:assert/strict");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const { test } = require("node:test");
const { configureAppProfile } = require("./appProfile.cjs");
function fakeApp(t, isPackaged) {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-profile-"));
t.after(() => fs.rmSync(tmp, { recursive: true, force: true }));
const production = path.join(tmp, "netcatty");
const paths = { userData: production, sessionData: production };
const app = {
isPackaged,
name: "Netcatty",
setName(name) { this.name = name; },
getPath(name) { return paths[name]; },
setPath(name, value) {
assert.ok(fs.statSync(value).isDirectory());
paths[name] = value;
},
};
return { app, paths, production };
}
test("unpackaged dist launches isolate both application and Chromium state", (t) => {
const { app, paths, production } = fakeApp(t, false);
assert.deepEqual(configureAppProfile(app, undefined), {
isDev: false, effectiveDevServerUrl: undefined,
});
const dev = path.join(production, "dev");
assert.deepEqual(paths, { userData: dev, sessionData: dev });
assert.equal(app.name, "Netcatty Dev");
});
test("Vite launches keep the development profile and URL", (t) => {
const { app, paths, production } = fakeApp(t, false);
const url = "http://localhost:5173";
assert.deepEqual(configureAppProfile(app, url), { isDev: true, effectiveDevServerUrl: url });
const dev = path.join(production, "dev");
assert.deepEqual(paths, { userData: dev, sessionData: dev });
});
test("packaged launches keep their data paths and ignore a Vite URL", (t) => {
const { app, paths, production } = fakeApp(t, true);
assert.deepEqual(configureAppProfile(app, "http://localhost:5173"), {
isDev: false, effectiveDevServerUrl: undefined,
});
assert.deepEqual(paths, { userData: production, sessionData: production });
assert.equal(app.name, "Netcatty");
});
+4 -9
View File
@@ -88,6 +88,10 @@ const path = require("node:path");
const os = require("node:os");
const fs = require("node:fs");
const { execFile } = require("node:child_process");
// Separate profile selection from renderer loading. In particular, source
// builds serving dist/ still need a non-production profile.
const { configureAppProfile } = require("./appProfile.cjs");
const { isDev, effectiveDevServerUrl } = configureAppProfile(app, process.env.VITE_DEV_SERVER_URL);
const { getCliDiscoveryFilePath } = require("./cli/discoveryPath.cjs");
const {
SSH_DEEP_LINK_CHANNEL,
@@ -291,15 +295,6 @@ app.on("web-contents-created", (_event, contents) => {
});
});
// Application configuration
const devServerUrl = process.env.VITE_DEV_SERVER_URL;
// Never treat a packaged app as "dev" even if the user has VITE_DEV_SERVER_URL set globally.
const isDev = !app.isPackaged && !!devServerUrl;
const effectiveDevServerUrl = isDev ? devServerUrl : undefined;
if (isDev) {
app.setName("Netcatty Dev");
app.setPath("userData", path.join(app.getPath("userData"), "dev"));
}
const { applyPortableDataDirectory } = require("./portableData.cjs");
const portableData = applyPortableDataDirectory({ app });
if (portableData) {