From a525cb52347e249258b65770bfd4e0ed0b97ac20 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Thu, 12 May 2022 21:57:38 +0200 Subject: fix: preload script detection in unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Kristóf Marussy --- src/preload-safe-debug.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'src/preload-safe-debug.ts') diff --git a/src/preload-safe-debug.ts b/src/preload-safe-debug.ts index d96ea9017..1136e93c1 100644 --- a/src/preload-safe-debug.ts +++ b/src/preload-safe-debug.ts @@ -1,8 +1,3 @@ -/* - eslint-disable global-require -- - This file contains a workaround for situations were global require is problematic. -*/ - /** * Make sure we don't try to load `debug` in the preload script. * @@ -10,13 +5,13 @@ * because `debug` will try to access `localStorage` to save the log level: * https://www.npmjs.com/package/debug#user-content-browser-support * - * We check for the presence of `ipcRenderer`, a render-only electron API, - * to detect whether we're in the renderer process. - * We serve the user interface from the `file://` origin, so any different origin - * must be a preload script. + * We disable the `debug` package in context isolated renderers, + * because they correspond to preload scripts. */ module.exports = function debug(namespace: string): (...params: any[]) => void { - if ('ipcRenderer' in require('electron') && window.origin !== 'file://') { + if (typeof process === 'object' && + 'contextIsolated' in process && + (process as unknown as { contextIsolated: string }).contextIsolated) { // Only output debug messages to the console if debugging is requested. // We don't reimplement the matching algorithm from `debug` and just dump all // messages to the console if some form of `Ferdium` debugging is enabled. @@ -25,5 +20,9 @@ module.exports = function debug(namespace: string): (...params: any[]) => void { } return () => { }; } + /* + eslint-disable-next-line global-require -- + This file contains a workaround for situations were global require is problematic. + */ return require('debug')(namespace); } -- cgit v1.2.3-54-g00ecf