aboutsummaryrefslogtreecommitdiffstats
path: root/src/preload-safe-debug.ts
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2022-04-23 01:59:21 +0200
committerLibravatar GitHub <noreply@github.com>2022-04-22 23:59:21 +0000
commitd02644f7c41150709795e57bfd40351b4da35a7b (patch)
tree2403fb76bd5fae1703f8b55172ffce9e0a5d2bce /src/preload-safe-debug.ts
parentComplete tray icons redesign for all platforms (#28) (diff)
downloadferdium-app-d02644f7c41150709795e57bfd40351b4da35a7b.tar.gz
ferdium-app-d02644f7c41150709795e57bfd40351b4da35a7b.tar.zst
ferdium-app-d02644f7c41150709795e57bfd40351b4da35a7b.zip
Preload safe debug shim (#29)
In https://github.com/ferdium/ferdium-app/pull/23 we removed usages of the debug package due to an electron bug. This patch aims to restore some debug functionality by introducing a shim. The shim detect whether if it is being introduced in a preload script where the electron but would be triggered, and falls back to a simple replacement for debug. However, in the main and renderer processes, where a preload script is not being used, we still get full debug functionality. In this way, a module can be used both in a preload script and outside of it, while still preserving debug functionality whenever possible. Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'src/preload-safe-debug.ts')
-rw-r--r--src/preload-safe-debug.ts29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/preload-safe-debug.ts b/src/preload-safe-debug.ts
new file mode 100644
index 000000000..d96ea9017
--- /dev/null
+++ b/src/preload-safe-debug.ts
@@ -0,0 +1,29 @@
1/*
2 eslint-disable global-require --
3 This file contains a workaround for situations were global require is problematic.
4*/
5
6/**
7 * Make sure we don't try to load `debug` in the preload script.
8 *
9 * Doing so would trigger the bug https://github.com/electron/electron/issues/31689
10 * because `debug` will try to access `localStorage` to save the log level:
11 * https://www.npmjs.com/package/debug#user-content-browser-support
12 *
13 * We check for the presence of `ipcRenderer`, a render-only electron API,
14 * to detect whether we're in the renderer process.
15 * We serve the user interface from the `file://` origin, so any different origin
16 * must be a preload script.
17 */
18module.exports = function debug(namespace: string): (...params: any[]) => void {
19 if ('ipcRenderer' in require('electron') && window.origin !== 'file://') {
20 // Only output debug messages to the console if debugging is requested.
21 // We don't reimplement the matching algorithm from `debug` and just dump all
22 // messages to the console if some form of `Ferdium` debugging is enabled.
23 if (process.env.DEBUG?.startsWith('Ferdium:')) {
24 return (...params) => console.debug(`[${namespace}]`, ...params);
25 }
26 return () => { };
27 }
28 return require('debug')(namespace);
29}