Skip to content

Commit

Permalink
Prepare migration for sandboxing (path.join in preload)
Browse files Browse the repository at this point in the history
  • Loading branch information
th-ch committed Sep 4, 2022
1 parent 91e4433 commit 1f5f597
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ function createMainWindow() {
preload: path.join(__dirname, "preload.js"),
nodeIntegrationInSubFrames: true,
affinity: "main-window", // main window, and addition windows should work in one process
...(isTesting()
...(!isTesting()
? {
// Only necessary when testing with Spectron
contextIsolation: false,
nodeIntegration: true,
}
// Sandbox is only enabled in tests for now
// See https://www.electronjs.org/docs/latest/tutorial/sandbox#preload-scripts
sandbox: false,
}
: undefined),
},
frame: !is.macOS() && !useInlineMenu,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
}
},
"scripts": {
"test": "playwright test",
"test:debug": "DEBUG=pw:browser* playwright test",
"test": "NODE_ENV=test playwright test",
"test:debug": "DEBUG=pw:browser* NODE_ENV=test playwright test",
"start": "electron .",
"start:debug": "ELECTRON_ENABLE_LOGGING=1 electron .",
"icon": "rimraf assets/generated && electron-icon-maker --input=assets/youtube-music.png --output=assets/generated",
Expand Down
29 changes: 23 additions & 6 deletions preload.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const path = require("path");
require("./providers/front-logger")();
const config = require("./config");
const { fileExists } = require("./plugins/utils");
Expand All @@ -10,14 +9,26 @@ const plugins = config.plugins.getEnabled();

let api;

plugins.forEach(([plugin, options]) => {
const preloadPath = path.join(__dirname, "plugins", plugin, "preload.js");
plugins.forEach(async ([plugin, options]) => {
const preloadPath = await ipcRenderer.invoke(
"getPath",
__dirname,
"plugins",
plugin,
"preload.js"
);
fileExists(preloadPath, () => {
const run = require(preloadPath);
run(options);
});

const actionPath = path.join(__dirname, "plugins", plugin, "actions.js");
const actionPath = await ipcRenderer.invoke(
"getPath",
__dirname,
"plugins",
plugin,
"actions.js"
);
fileExists(actionPath, () => {
const actions = require(actionPath).actions || {};

Expand All @@ -30,8 +41,14 @@ plugins.forEach(([plugin, options]) => {
});

document.addEventListener("DOMContentLoaded", () => {
plugins.forEach(([plugin, options]) => {
const pluginPath = path.join(__dirname, "plugins", plugin, "front.js");
plugins.forEach(async ([plugin, options]) => {
const pluginPath = await ipcRenderer.invoke(
"getPath",
__dirname,
"plugins",
plugin,
"front.js"
);
fileExists(pluginPath, () => {
const run = require(pluginPath);
run(options);
Expand Down
3 changes: 3 additions & 0 deletions providers/app-controls.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const path = require("path");

const is = require("electron-is");

const { app, BrowserWindow, ipcMain, ipcRenderer } = require("electron");
Expand All @@ -11,6 +13,7 @@ module.exports.setupAppControls = () => {
ipcMain.on('restart', restart);
ipcMain.handle('getDownloadsFolder', () => app.getPath("downloads"));
ipcMain.on('reload', () => BrowserWindow.getFocusedWindow().webContents.loadURL(config.get("url")));
ipcMain.handle('getPath', (_, ...args) => path.join(...args));
}

function restart() {
Expand Down

0 comments on commit 1f5f597

Please sign in to comment.