Skip to content

Commit 181f80d

Browse files
feat(electron): support object-based electronSwitches
Adds js/electron_helper.js with applyElectronSwitches(), which handles both string and object entries in the electronSwitches config array and logs each activated switch. The hardcoded autoplay-policy default switch is moved into the helper so all switch logic lives in one place. Fixes #4159
1 parent 3c514df commit 181f80d

3 files changed

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

0 commit comments

Comments
 (0)