Skip to content

Commit

Permalink
Switch to protocol.handle()
Browse files Browse the repository at this point in the history
In Electron 25, `protocol.interceptFileProtocol()` is [deprecated](https://www.electronjs.org/docs/latest/breaking-changes#deprecated-protocolregisterinterceptbufferstringstreamfilehttpprotocol), so update our template/blueprint file to use the new `protocol.handle()` if present, and fall back on `protocol.interceptFileProtocol()` if not.
  • Loading branch information
bendemboski committed Dec 6, 2023
1 parent b7bea18 commit f64f440
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions forge/files/src/handle-file-urls.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { fileURLToPath } = require('url');
const { net } = require('electron');
const { fileURLToPath, pathToFileURL } = require('url');
const path = require('path');
const fs = require('fs');
const { promisify } = require('util');
Expand Down Expand Up @@ -34,9 +35,20 @@ async function getAssetPath(emberAppDir, url) {
module.exports = function handleFileURLs(emberAppDir) {
const { protocol } = require('electron');

protocol.interceptFileProtocol('file', async ({ url }, callback) => {
callback(await getAssetPath(emberAppDir, url));
});
if (protocol.handle) {
// Electron >= 25
protocol.handle('file', async ({ url }) => {
let path = await getAssetPath(emberAppDir, url);
return net.fetch(pathToFileURL(path), {
bypassCustomProtocolHandlers: true,
});
});
} else {
// Electron < 25
protocol.interceptFileProtocol('file', async ({ url }, callback) => {
callback(await getAssetPath(emberAppDir, url));
});
}
};

module.exports.getAssetPath = getAssetPath;

0 comments on commit f64f440

Please sign in to comment.