Skip to content

Commit e55b11b

Browse files
feat(electron): support object-based electronSwitches (#4161)
Previously, `electronSwitches` only accepted strings. This PR adds support for object entries, allowing switches with values: ```js electronSwitches: [ "no-sandbox", { "js-flags": "--max-old-space-size=8192" } ] ``` I decided to put the logic into a helper file so it can be unit-tested independently, since electron.js itself requires a live Electron environment and cannot be tested in isolation. Fixes #4159
1 parent fd687bf commit e55b11b

3 files changed

Lines changed: 104 additions & 4 deletions

File tree

js/electron.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const electron = require("electron");
44
const core = require("./app");
55
const Log = require("./logger");
6+
const { applyElectronSwitches } = require("./electron_helper");
67

78
// Config
89
let config = process.env.config ? JSON.parse(process.env.config) : {};
@@ -43,10 +44,7 @@ function createWindow () {
4344
Log.warn("Could not get display size, using defaults ...");
4445
}
4546

46-
app.commandLine.appendSwitch("autoplay-policy", "no-user-gesture-required");
47-
for (const electronSwitch of (config.electronSwitches || [])) {
48-
app.commandLine.appendSwitch(electronSwitch);
49-
}
47+
applyElectronSwitches(app.commandLine, config.electronSwitches);
5048
let electronOptionsDefaults = {
5149
width: electronSize.width,
5250
height: electronSize.height,

js/electron_helper.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const Log = require("./logger");
2+
3+
/**
4+
* Applies Electron command-line switches from config.
5+
* @param {object} commandLine Electron commandLine API
6+
* @param {Array<string|object>} [electronSwitches] User-configured switches
7+
*/
8+
function applyElectronSwitches (commandLine, electronSwitches) {
9+
if (electronSwitches === undefined) return;
10+
if (!Array.isArray(electronSwitches)) {
11+
Log.error(`electronSwitches must be an array of strings or objects, got: ${JSON.stringify(electronSwitches)}`);
12+
return;
13+
}
14+
15+
for (const sw of electronSwitches) {
16+
if (typeof sw === "string") {
17+
commandLine.appendSwitch(sw);
18+
Log.debug(`Activated switch: ${sw}`);
19+
} else if (sw && typeof sw === "object" && !Array.isArray(sw)) {
20+
for (const [name, value] of Object.entries(sw)) {
21+
commandLine.appendSwitch(name, String(value));
22+
Log.debug(`Activated switch: ${name}=${value}`);
23+
}
24+
} else {
25+
Log.error(`Invalid electronSwitches entry: ${JSON.stringify(sw)}`);
26+
}
27+
}
28+
}
29+
30+
module.exports = { applyElectronSwitches };
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const Log = require("logger");
2+
const { applyElectronSwitches } = require("../../../js/electron_helper");
3+
4+
describe("electron switches", () => {
5+
let commandLine;
6+
7+
beforeEach(() => {
8+
commandLine = {
9+
appendSwitch: vi.fn()
10+
};
11+
vi.spyOn(Log, "error").mockImplementation(() => {});
12+
});
13+
14+
it("does nothing when electronSwitches is undefined", () => {
15+
applyElectronSwitches(commandLine, undefined);
16+
17+
expect(commandLine.appendSwitch).not.toHaveBeenCalled();
18+
expect(Log.error).not.toHaveBeenCalled();
19+
});
20+
21+
it("applies string entries as switches without values", () => {
22+
applyElectronSwitches(commandLine, ["no-sandbox", "disable-http-cache"]);
23+
24+
expect(commandLine.appendSwitch).toHaveBeenCalledTimes(2);
25+
expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(1, "no-sandbox");
26+
expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(2, "disable-http-cache");
27+
expect(Log.error).not.toHaveBeenCalled();
28+
});
29+
30+
it("applies object entries as switches with values", () => {
31+
applyElectronSwitches(commandLine, [
32+
{ "js-flags": "--max-old-space-size=8192" },
33+
{ "password-store": "basic" }
34+
]);
35+
36+
expect(commandLine.appendSwitch).toHaveBeenCalledTimes(2);
37+
expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(1, "js-flags", "--max-old-space-size=8192");
38+
expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(2, "password-store", "basic");
39+
expect(Log.error).not.toHaveBeenCalled();
40+
});
41+
42+
it("allows one object entry to define multiple switches with values", () => {
43+
applyElectronSwitches(commandLine, [
44+
"no-sandbox",
45+
{
46+
"js-flags": "--max-old-space-size=8192",
47+
"password-store": "basic"
48+
}
49+
]);
50+
51+
expect(commandLine.appendSwitch).toHaveBeenCalledTimes(3);
52+
expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(1, "no-sandbox");
53+
expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(2, "js-flags", "--max-old-space-size=8192");
54+
expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(3, "password-store", "basic");
55+
expect(Log.error).not.toHaveBeenCalled();
56+
});
57+
58+
it("logs an error for invalid entries", () => {
59+
applyElectronSwitches(commandLine, ["no-sandbox", ["js-flags", "--max-old-space-size=8192"], null]);
60+
61+
expect(commandLine.appendSwitch).toHaveBeenCalledTimes(1);
62+
expect(commandLine.appendSwitch).toHaveBeenCalledWith("no-sandbox");
63+
expect(Log.error).toHaveBeenCalledTimes(2);
64+
});
65+
66+
it("logs an error when electronSwitches is not an array", () => {
67+
applyElectronSwitches(commandLine, { "js-flags": "--max-old-space-size=8192" });
68+
69+
expect(commandLine.appendSwitch).not.toHaveBeenCalled();
70+
expect(Log.error).toHaveBeenCalledWith(expect.stringContaining("electronSwitches must be an array of strings or objects"));
71+
});
72+
});

0 commit comments

Comments
 (0)