From 070af066c170d887cc700680cc9003a6d919adf6 Mon Sep 17 00:00:00 2001 From: Ben Demboski <demboskb@gmail.com> Date: Wed, 6 Dec 2023 13:22:53 -0800 Subject: [PATCH] Switch to protocol.handle() 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. --- forge/files/src/handle-file-urls.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/forge/files/src/handle-file-urls.js b/forge/files/src/handle-file-urls.js index f6eeb170..52bd653f 100644 --- a/forge/files/src/handle-file-urls.js +++ b/forge/files/src/handle-file-urls.js @@ -1,4 +1,4 @@ -const { fileURLToPath } = require('url'); +const { fileURLToPath, pathToFileURL } = require('url'); const path = require('path'); const fs = require('fs'); const { promisify } = require('util'); @@ -32,11 +32,22 @@ async function getAssetPath(emberAppDir, url) { } module.exports = function handleFileURLs(emberAppDir) { - const { protocol } = require('electron'); + const { protocol, net } = 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;