aboutsummaryrefslogtreecommitdiffstats
path: root/src/preload-safe-debug.ts
diff options
context:
space:
mode:
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}