Skip to content

Commit

Permalink
feat!: bump electron@^29.0.0 w/ supporting changes (#263)
Browse files Browse the repository at this point in the history
* npm(dep)!: bump electron@^28.1.4
* feat!: replace deprecated usage registerFileProtocol & drop interceptFileProtocol
* feat!: disable default sandbox setting
* feat!: improve configureProtocol to support for older electron versions
* npm(dep)!: bump electron@^29.0.0
  • Loading branch information
erisu authored Feb 20, 2024
1 parent 7053f35 commit 8fcde3a
Show file tree
Hide file tree
Showing 3 changed files with 533 additions and 252 deletions.
32 changes: 25 additions & 7 deletions bin/templates/platform_www/cdv-electron-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const {
app,
BrowserWindow,
protocol,
ipcMain
ipcMain,
net
} = require('electron');
// Electron settings from .json file.
const cdvElectronSettings = require('./cdv-electron-settings.json');
Expand Down Expand Up @@ -74,6 +75,9 @@ function createWindow () {
const browserWindowOpts = Object.assign({}, cdvElectronSettings.browserWindow, { icon: appIcon });
browserWindowOpts.webPreferences.preload = path.join(app.getAppPath(), 'cdv-electron-preload.js');
browserWindowOpts.webPreferences.contextIsolation = true;
// @todo review if using default "sandbox" is possible. When enabled, "Unable to load preload script:" error occurs.
// Other require statements also fails.
browserWindowOpts.webPreferences.sandbox = false;

mainWindow = new BrowserWindow(browserWindowOpts);

Expand All @@ -99,12 +103,26 @@ function createWindow () {
}

function configureProtocol () {
protocol.registerFileProtocol(scheme, (request, cb) => {
const url = request.url.substr(basePath.length + 1);
cb({ path: path.normalize(path.join(__dirname, url)) }); // eslint-disable-line node/no-callback-literal
});

protocol.interceptFileProtocol('file', (_, cb) => { cb(null); });
// `protocol.handle` was added in Electron 25.0 and replaced the deprecated
// `protocol.{register,intercept}{String,Buffer,Stream,Http,File}Protocol`.
if (protocol.handle) {
// If using Electron 25.0+
protocol.handle(scheme, request => {
const url = request.url.substr(basePath.length + 1);
const fileUrl = `file://${path.normalize(path.join(__dirname, url))}`;
return net.fetch(fileUrl);
});
} else if (protocol.registerFileProtocol) {
// If using Electron 24.x and older
protocol.registerFileProtocol(scheme, (request, cb) => {
const url = request.url.substr(basePath.length + 1);
cb({ path: path.normalize(path.join(__dirname, url)) }); // eslint-disable-line node/no-callback-literal
});
protocol.interceptFileProtocol('file', (_, cb) => { cb(null); });
} else {
// Cant configure if missing `protocol.handle` and `protocol.registerFileProtocol`...
console.info('Unable to configure the protocol.');
}
}

// This method will be called when Electron has finished
Expand Down
Loading

0 comments on commit 8fcde3a

Please sign in to comment.