aboutsummaryrefslogtreecommitdiffstats
path: root/src/preload-safe-debug.ts
blob: 905140306e09bcd1d3a769f6fea19d6706323986 (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
30
31
32
/**
 * 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 disable the `debug` package in context isolated renderers,
 * because they correspond to preload scripts.
 */

module.exports = function debug(namespace: string): (...params: any[]) => void {
  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.
    if (process.env.DEBUG?.startsWith('Ferdium:')) {
      // eslint-disable-next-line no-console
      return (...params) => console.debug(`[${namespace}]`, ...params);
    }
    return () => {};
  }
  /*
    This file contains a workaround for situations were global require is problematic.
  */
  // eslint-disable-next-line global-require
  return require('debug')(namespace);
};