aboutsummaryrefslogtreecommitdiffstats
path: root/src/preload-safe-debug.ts
blob: d96ea9017e6182b9bb8547ca462fc38988bbdb40 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
  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.
 *
 * Doing so would trigger the bug https://github.com/electron/electron/issues/31689
 * 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.
 */
module.exports = function debug(namespace: string): (...params: any[]) => void {
  if ('ipcRenderer' in require('electron') && window.origin !== 'file://') {
    // 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.
    if (process.env.DEBUG?.startsWith('Ferdium:')) {
      return (...params) => console.debug(`[${namespace}]`, ...params);
    }
    return () => { };
  }
  return require('debug')(namespace);
}