diff --git a/js/electron.js b/js/electron.js index 3105966801..1714eab579 100644 --- a/js/electron.js +++ b/js/electron.js @@ -3,6 +3,7 @@ const electron = require("electron"); const core = require("./app"); const Log = require("./logger"); +const { applyElectronSwitches } = require("./electron_helper"); // Config let config = process.env.config ? JSON.parse(process.env.config) : {}; @@ -43,10 +44,7 @@ function createWindow () { Log.warn("Could not get display size, using defaults ..."); } - app.commandLine.appendSwitch("autoplay-policy", "no-user-gesture-required"); - for (const electronSwitch of (config.electronSwitches || [])) { - app.commandLine.appendSwitch(electronSwitch); - } + applyElectronSwitches(app.commandLine, config.electronSwitches); let electronOptionsDefaults = { width: electronSize.width, height: electronSize.height, diff --git a/js/electron_helper.js b/js/electron_helper.js new file mode 100644 index 0000000000..6eca6e9178 --- /dev/null +++ b/js/electron_helper.js @@ -0,0 +1,30 @@ +const Log = require("./logger"); + +/** + * Applies Electron command-line switches from config. + * @param {object} commandLine Electron commandLine API + * @param {Array} [electronSwitches] User-configured switches + */ +function applyElectronSwitches (commandLine, electronSwitches) { + if (electronSwitches === undefined) return; + if (!Array.isArray(electronSwitches)) { + Log.error(`electronSwitches must be an array of strings or objects, got: ${JSON.stringify(electronSwitches)}`); + return; + } + + for (const sw of electronSwitches) { + if (typeof sw === "string") { + commandLine.appendSwitch(sw); + Log.debug(`Activated switch: ${sw}`); + } else if (sw && typeof sw === "object" && !Array.isArray(sw)) { + for (const [name, value] of Object.entries(sw)) { + commandLine.appendSwitch(name, String(value)); + Log.debug(`Activated switch: ${name}=${value}`); + } + } else { + Log.error(`Invalid electronSwitches entry: ${JSON.stringify(sw)}`); + } + } +} + +module.exports = { applyElectronSwitches }; diff --git a/tests/unit/functions/electron_helper_spec.js b/tests/unit/functions/electron_helper_spec.js new file mode 100644 index 0000000000..ff2c09231f --- /dev/null +++ b/tests/unit/functions/electron_helper_spec.js @@ -0,0 +1,72 @@ +const Log = require("logger"); +const { applyElectronSwitches } = require("../../../js/electron_helper"); + +describe("electron switches", () => { + let commandLine; + + beforeEach(() => { + commandLine = { + appendSwitch: vi.fn() + }; + vi.spyOn(Log, "error").mockImplementation(() => {}); + }); + + it("does nothing when electronSwitches is undefined", () => { + applyElectronSwitches(commandLine, undefined); + + expect(commandLine.appendSwitch).not.toHaveBeenCalled(); + expect(Log.error).not.toHaveBeenCalled(); + }); + + it("applies string entries as switches without values", () => { + applyElectronSwitches(commandLine, ["no-sandbox", "disable-http-cache"]); + + expect(commandLine.appendSwitch).toHaveBeenCalledTimes(2); + expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(1, "no-sandbox"); + expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(2, "disable-http-cache"); + expect(Log.error).not.toHaveBeenCalled(); + }); + + it("applies object entries as switches with values", () => { + applyElectronSwitches(commandLine, [ + { "js-flags": "--max-old-space-size=8192" }, + { "password-store": "basic" } + ]); + + expect(commandLine.appendSwitch).toHaveBeenCalledTimes(2); + expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(1, "js-flags", "--max-old-space-size=8192"); + expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(2, "password-store", "basic"); + expect(Log.error).not.toHaveBeenCalled(); + }); + + it("allows one object entry to define multiple switches with values", () => { + applyElectronSwitches(commandLine, [ + "no-sandbox", + { + "js-flags": "--max-old-space-size=8192", + "password-store": "basic" + } + ]); + + expect(commandLine.appendSwitch).toHaveBeenCalledTimes(3); + expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(1, "no-sandbox"); + expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(2, "js-flags", "--max-old-space-size=8192"); + expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(3, "password-store", "basic"); + expect(Log.error).not.toHaveBeenCalled(); + }); + + it("logs an error for invalid entries", () => { + applyElectronSwitches(commandLine, ["no-sandbox", ["js-flags", "--max-old-space-size=8192"], null]); + + expect(commandLine.appendSwitch).toHaveBeenCalledTimes(1); + expect(commandLine.appendSwitch).toHaveBeenCalledWith("no-sandbox"); + expect(Log.error).toHaveBeenCalledTimes(2); + }); + + it("logs an error when electronSwitches is not an array", () => { + applyElectronSwitches(commandLine, { "js-flags": "--max-old-space-size=8192" }); + + expect(commandLine.appendSwitch).not.toHaveBeenCalled(); + expect(Log.error).toHaveBeenCalledWith(expect.stringContaining("electronSwitches must be an array of strings or objects")); + }); +});