init.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import * as path from 'path';
  2. import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
  3. import type * as ipcRendererInternalModule from '@electron/internal/renderer/ipc-renderer-internal';
  4. import type * as ipcRendererUtilsModule from '@electron/internal/renderer/ipc-renderer-internal-utils';
  5. const Module = require('module');
  6. // Make sure globals like "process" and "global" are always available in preload
  7. // scripts even after they are deleted in "loaded" script.
  8. //
  9. // Note 1: We rely on a Node patch to actually pass "process" and "global" and
  10. // other arguments to the wrapper.
  11. //
  12. // Note 2: Node introduced a new code path to use native code to wrap module
  13. // code, which does not work with this hack. However by modifying the
  14. // "Module.wrapper" we can force Node to use the old code path to wrap module
  15. // code with JavaScript.
  16. //
  17. // Note 3: We provide the equivalent extra variables internally through the
  18. // webpack ProvidePlugin in webpack.config.base.js. If you add any extra
  19. // variables to this wrapper please ensure to update that plugin as well.
  20. Module.wrapper = [
  21. '(function (exports, require, module, __filename, __dirname, process, global, Buffer) { ' +
  22. // By running the code in a new closure, it would be possible for the module
  23. // code to override "process" and "Buffer" with local variables.
  24. 'return function (exports, require, module, __filename, __dirname) { ',
  25. '\n}.call(this, exports, require, module, __filename, __dirname); });'
  26. ];
  27. // We modified the original process.argv to let node.js load the
  28. // init.js, we need to restore it here.
  29. process.argv.splice(1, 1);
  30. // Clear search paths.
  31. require('../common/reset-search-paths');
  32. // Import common settings.
  33. require('@electron/internal/common/init');
  34. const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal') as typeof ipcRendererInternalModule;
  35. const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils') as typeof ipcRendererUtilsModule;
  36. process.getProcessMemoryInfo = () => {
  37. return ipcRendererInternal.invoke<Electron.ProcessMemoryInfo>(IPC_MESSAGES.BROWSER_GET_PROCESS_MEMORY_INFO);
  38. };
  39. // Process command line arguments.
  40. const { hasSwitch, getSwitchValue } = process._linkedBinding('electron_common_command_line');
  41. const { mainFrame } = process._linkedBinding('electron_renderer_web_frame');
  42. const nodeIntegration = mainFrame.getWebPreference('nodeIntegration');
  43. const appPath = hasSwitch('app-path') ? getSwitchValue('app-path') : null;
  44. // Common renderer initialization
  45. require('@electron/internal/renderer/common-init');
  46. if (nodeIntegration) {
  47. // Export node bindings to global.
  48. const { makeRequireFunction } = __non_webpack_require__('internal/modules/cjs/helpers') // eslint-disable-line
  49. global.module = new Module('electron/js2c/renderer_init');
  50. global.require = makeRequireFunction(global.module);
  51. // Set the __filename to the path of html file if it is file: protocol.
  52. if (window.location.protocol === 'file:') {
  53. const location = window.location;
  54. let pathname = location.pathname;
  55. if (process.platform === 'win32') {
  56. if (pathname[0] === '/') pathname = pathname.substr(1);
  57. const isWindowsNetworkSharePath = location.hostname.length > 0 && process.resourcesPath.startsWith('\\');
  58. if (isWindowsNetworkSharePath) {
  59. pathname = `//${location.host}/${pathname}`;
  60. }
  61. }
  62. global.__filename = path.normalize(decodeURIComponent(pathname));
  63. global.__dirname = path.dirname(global.__filename);
  64. // Set module's filename so relative require can work as expected.
  65. global.module.filename = global.__filename;
  66. // Also search for module under the html file.
  67. global.module.paths = Module._nodeModulePaths(global.__dirname);
  68. } else {
  69. // For backwards compatibility we fake these two paths here
  70. global.__filename = path.join(process.resourcesPath, 'electron.asar', 'renderer', 'init.js');
  71. global.__dirname = path.join(process.resourcesPath, 'electron.asar', 'renderer');
  72. if (appPath) {
  73. // Search for module under the app directory
  74. global.module.paths = Module._nodeModulePaths(appPath);
  75. }
  76. }
  77. // Redirect window.onerror to uncaughtException.
  78. window.onerror = function (_message, _filename, _lineno, _colno, error) {
  79. if (global.process.listenerCount('uncaughtException') > 0) {
  80. // We do not want to add `uncaughtException` to our definitions
  81. // because we don't want anyone else (anywhere) to throw that kind
  82. // of error.
  83. global.process.emit('uncaughtException', error as any);
  84. return true;
  85. } else {
  86. return false;
  87. }
  88. };
  89. } else {
  90. // Delete Node's symbols after the Environment has been loaded in a
  91. // non context-isolated environment
  92. if (!process.contextIsolated) {
  93. process.once('loaded', function () {
  94. delete (global as any).process;
  95. delete (global as any).Buffer;
  96. delete (global as any).setImmediate;
  97. delete (global as any).clearImmediate;
  98. delete (global as any).global;
  99. delete (global as any).root;
  100. delete (global as any).GLOBAL;
  101. });
  102. }
  103. }
  104. const { preloadPaths } = ipcRendererUtils.invokeSync(IPC_MESSAGES.BROWSER_NONSANDBOX_LOAD);
  105. // Load the preload scripts.
  106. for (const preloadScript of preloadPaths) {
  107. try {
  108. Module._load(preloadScript);
  109. } catch (error) {
  110. console.error(`Unable to load preload script: ${preloadScript}`);
  111. console.error(error);
  112. ipcRendererInternal.send(IPC_MESSAGES.BROWSER_PRELOAD_ERROR, preloadScript, error);
  113. }
  114. }