2020-10-13 23:11:06 +02:00
|
|
|
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
|
2021-05-06 03:05:01 +02:00
|
|
|
import type * as ipcRendererInternalModule from '@electron/internal/renderer/ipc-renderer-internal';
|
2022-06-15 13:22:28 -07:00
|
|
|
import type * as ipcRendererUtilsModule from '@electron/internal/renderer/ipc-renderer-internal-utils';
|
2021-05-06 03:05:01 +02:00
|
|
|
|
2024-10-02 19:10:44 -07:00
|
|
|
import * as path from 'path';
|
|
|
|
import { pathToFileURL } from 'url';
|
|
|
|
|
2023-09-07 00:04:25 +02:00
|
|
|
const Module = require('module') as NodeJS.ModuleInternal;
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2024-01-26 17:29:04 +09:00
|
|
|
// We do not want to allow use of the VM module in the renderer process as
|
|
|
|
// it conflicts with Blink's V8::Context internal logic.
|
|
|
|
const originalModuleLoad = Module._load;
|
|
|
|
Module._load = function (request: string) {
|
|
|
|
if (request === 'vm') {
|
|
|
|
console.warn('The vm module of Node.js is deprecated in the renderer process and will be removed.');
|
|
|
|
}
|
|
|
|
return originalModuleLoad.apply(this, arguments as any);
|
|
|
|
};
|
|
|
|
|
2019-04-26 18:55:12 +09:00
|
|
|
// Make sure globals like "process" and "global" are always available in preload
|
|
|
|
// scripts even after they are deleted in "loaded" script.
|
|
|
|
//
|
|
|
|
// Note 1: We rely on a Node patch to actually pass "process" and "global" and
|
|
|
|
// other arguments to the wrapper.
|
|
|
|
//
|
|
|
|
// Note 2: Node introduced a new code path to use native code to wrap module
|
|
|
|
// code, which does not work with this hack. However by modifying the
|
|
|
|
// "Module.wrapper" we can force Node to use the old code path to wrap module
|
|
|
|
// code with JavaScript.
|
2019-06-02 13:03:03 -07:00
|
|
|
//
|
2019-06-20 08:11:38 -07:00
|
|
|
// Note 3: We provide the equivalent extra variables internally through the
|
2019-06-02 13:03:03 -07:00
|
|
|
// webpack ProvidePlugin in webpack.config.base.js. If you add any extra
|
|
|
|
// variables to this wrapper please ensure to update that plugin as well.
|
2019-04-26 18:55:12 +09:00
|
|
|
Module.wrapper = [
|
|
|
|
'(function (exports, require, module, __filename, __dirname, process, global, Buffer) { ' +
|
|
|
|
// By running the code in a new closure, it would be possible for the module
|
|
|
|
// code to override "process" and "Buffer" with local variables.
|
|
|
|
'return function (exports, require, module, __filename, __dirname) { ',
|
|
|
|
'\n}.call(this, exports, require, module, __filename, __dirname); });'
|
2020-03-20 13:28:31 -07:00
|
|
|
];
|
2019-04-26 18:55:12 +09:00
|
|
|
|
2016-01-14 11:10:12 -08:00
|
|
|
// We modified the original process.argv to let node.js load the
|
2017-03-08 18:58:54 +09:00
|
|
|
// init.js, we need to restore it here.
|
2020-03-20 13:28:31 -07:00
|
|
|
process.argv.splice(1, 1);
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// Import common settings.
|
2020-03-20 13:28:31 -07:00
|
|
|
require('@electron/internal/common/init');
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2021-05-06 03:05:01 +02:00
|
|
|
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal') as typeof ipcRendererInternalModule;
|
2022-06-15 13:22:28 -07:00
|
|
|
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils') as typeof ipcRendererUtilsModule;
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2021-10-06 07:30:31 +09:00
|
|
|
process.getProcessMemoryInfo = () => {
|
|
|
|
return ipcRendererInternal.invoke<Electron.ProcessMemoryInfo>(IPC_MESSAGES.BROWSER_GET_PROCESS_MEMORY_INFO);
|
|
|
|
};
|
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// Process command line arguments.
|
2020-06-22 20:32:45 -07:00
|
|
|
const { hasSwitch, getSwitchValue } = process._linkedBinding('electron_common_command_line');
|
2021-04-12 16:35:18 -07:00
|
|
|
const { mainFrame } = process._linkedBinding('electron_renderer_web_frame');
|
|
|
|
|
|
|
|
const nodeIntegration = mainFrame.getWebPreference('nodeIntegration');
|
2020-11-10 09:06:03 -08:00
|
|
|
const appPath = hasSwitch('app-path') ? getSwitchValue('app-path') : null;
|
2018-12-12 22:31:16 +01:00
|
|
|
|
2021-11-10 01:59:33 +01:00
|
|
|
// Common renderer initialization
|
|
|
|
require('@electron/internal/renderer/common-init');
|
2019-01-22 02:08:16 +01:00
|
|
|
|
2018-10-08 15:51:31 +02:00
|
|
|
if (nodeIntegration) {
|
2016-01-14 10:35:29 -08:00
|
|
|
// Export node bindings to global.
|
2023-11-30 15:51:35 +01:00
|
|
|
const { makeRequireFunction } = __non_webpack_require__('internal/modules/helpers');
|
2020-03-20 13:28:31 -07:00
|
|
|
global.module = new Module('electron/js2c/renderer_init');
|
|
|
|
global.require = makeRequireFunction(global.module);
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// Set the __filename to the path of html file if it is file: protocol.
|
2016-01-11 18:40:23 -08:00
|
|
|
if (window.location.protocol === 'file:') {
|
2020-03-20 13:28:31 -07:00
|
|
|
const location = window.location;
|
|
|
|
let pathname = location.pathname;
|
2018-03-15 11:45:13 +09:00
|
|
|
|
|
|
|
if (process.platform === 'win32') {
|
2020-03-20 13:28:31 -07:00
|
|
|
if (pathname[0] === '/') pathname = pathname.substr(1);
|
2018-03-15 11:45:13 +09:00
|
|
|
|
2020-03-20 13:28:31 -07:00
|
|
|
const isWindowsNetworkSharePath = location.hostname.length > 0 && process.resourcesPath.startsWith('\\');
|
2018-03-15 11:45:13 +09:00
|
|
|
if (isWindowsNetworkSharePath) {
|
2020-03-20 13:28:31 -07:00
|
|
|
pathname = `//${location.host}/${pathname}`;
|
2018-03-15 11:45:13 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 13:28:31 -07:00
|
|
|
global.__filename = path.normalize(decodeURIComponent(pathname));
|
|
|
|
global.__dirname = path.dirname(global.__filename);
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// Set module's filename so relative require can work as expected.
|
2020-03-20 13:28:31 -07:00
|
|
|
global.module.filename = global.__filename;
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// Also search for module under the html file.
|
2020-03-20 13:28:31 -07:00
|
|
|
global.module.paths = Module._nodeModulePaths(global.__dirname);
|
2016-01-11 18:40:23 -08:00
|
|
|
} else {
|
2019-06-03 17:03:59 -07:00
|
|
|
// For backwards compatibility we fake these two paths here
|
2020-03-20 13:28:31 -07:00
|
|
|
global.__filename = path.join(process.resourcesPath, 'electron.asar', 'renderer', 'init.js');
|
|
|
|
global.__dirname = path.join(process.resourcesPath, 'electron.asar', 'renderer');
|
2017-04-03 20:12:02 +09:00
|
|
|
|
2017-04-04 09:36:01 +09:00
|
|
|
if (appPath) {
|
2017-04-03 22:11:29 +09:00
|
|
|
// Search for module under the app directory
|
2020-03-20 13:28:31 -07:00
|
|
|
global.module.paths = Module._nodeModulePaths(appPath);
|
2017-04-03 22:11:29 +09:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// Redirect window.onerror to uncaughtException.
|
2019-02-19 17:05:14 +00:00
|
|
|
window.onerror = function (_message, _filename, _lineno, _colno, error) {
|
2019-05-01 15:07:57 +02:00
|
|
|
if (global.process.listenerCount('uncaughtException') > 0) {
|
2019-02-19 17:05:14 +00:00
|
|
|
// We do not want to add `uncaughtException` to our definitions
|
|
|
|
// because we don't want anyone else (anywhere) to throw that kind
|
|
|
|
// of error.
|
2021-01-29 21:41:59 +01:00
|
|
|
global.process.emit('uncaughtException', error as any);
|
2020-03-20 13:28:31 -07:00
|
|
|
return true;
|
2016-01-11 18:40:23 -08:00
|
|
|
} else {
|
2020-03-20 13:28:31 -07:00
|
|
|
return false;
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
2020-03-20 13:28:31 -07:00
|
|
|
};
|
2016-01-11 18:40:23 -08:00
|
|
|
} else {
|
2019-06-28 14:37:00 -07:00
|
|
|
// Delete Node's symbols after the Environment has been loaded in a
|
|
|
|
// non context-isolated environment
|
2021-11-08 17:57:56 +01:00
|
|
|
if (!process.contextIsolated) {
|
2019-06-28 14:37:00 -07:00
|
|
|
process.once('loaded', function () {
|
2020-08-24 11:23:25 -07:00
|
|
|
delete (global as any).process;
|
|
|
|
delete (global as any).Buffer;
|
|
|
|
delete (global as any).setImmediate;
|
|
|
|
delete (global as any).clearImmediate;
|
|
|
|
delete (global as any).global;
|
|
|
|
delete (global as any).root;
|
|
|
|
delete (global as any).GLOBAL;
|
2020-03-20 13:28:31 -07:00
|
|
|
});
|
2019-06-28 14:37:00 -07:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
|
|
|
|
2023-08-30 17:38:07 -07:00
|
|
|
const { appCodeLoaded } = process;
|
|
|
|
delete process.appCodeLoaded;
|
|
|
|
|
|
|
|
const { preloadPaths } = ipcRendererUtils.invokeSync<{ preloadPaths: string[] }>(IPC_MESSAGES.BROWSER_NONSANDBOX_LOAD);
|
|
|
|
const cjsPreloads = preloadPaths.filter(p => path.extname(p) !== '.mjs');
|
|
|
|
const esmPreloads = preloadPaths.filter(p => path.extname(p) === '.mjs');
|
|
|
|
if (cjsPreloads.length) {
|
|
|
|
// Load the preload scripts.
|
|
|
|
for (const preloadScript of cjsPreloads) {
|
|
|
|
try {
|
|
|
|
Module._load(preloadScript);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`Unable to load preload script: ${preloadScript}`);
|
|
|
|
console.error(error);
|
|
|
|
|
|
|
|
ipcRendererInternal.send(IPC_MESSAGES.BROWSER_PRELOAD_ERROR, preloadScript, error);
|
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
|
|
|
}
|
2023-08-30 17:38:07 -07:00
|
|
|
if (esmPreloads.length) {
|
2024-05-13 11:43:14 -04:00
|
|
|
const { runEntryPointWithESMLoader } = __non_webpack_require__('internal/modules/run_main');
|
2023-08-30 17:38:07 -07:00
|
|
|
|
2024-05-13 11:43:14 -04:00
|
|
|
runEntryPointWithESMLoader(async (cascadedLoader: any) => {
|
2023-08-30 17:38:07 -07:00
|
|
|
// Load the preload scripts.
|
|
|
|
for (const preloadScript of esmPreloads) {
|
2024-05-13 11:43:14 -04:00
|
|
|
await cascadedLoader.import(pathToFileURL(preloadScript).toString(), undefined, Object.create(null)).catch((err: Error) => {
|
2023-08-30 17:38:07 -07:00
|
|
|
console.error(`Unable to load preload script: ${preloadScript}`);
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
ipcRendererInternal.send(IPC_MESSAGES.BROWSER_PRELOAD_ERROR, preloadScript, err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}).finally(() => appCodeLoaded!());
|
|
|
|
} else {
|
|
|
|
appCodeLoaded!();
|
|
|
|
}
|