9c94fd7afb
* chore: upgrade to Node.js v20 * src: allow embedders to override NODE_MODULE_VERSION https://github.com/nodejs/node/pull/49279 * src: fix missing trailing , https://github.com/nodejs/node/pull/46909 * src,tools: initialize cppgc https://github.com/nodejs/node/pull/45704 * tools: allow passing absolute path of config.gypi in js2c https://github.com/nodejs/node/pull/49162 * tools: port js2c.py to C++ https://github.com/nodejs/node/pull/46997 * doc,lib: disambiguate the old term, NativeModule https://github.com/nodejs/node/pull/45673 * chore: fixup Node.js BSSL tests * https://github.com/nodejs/node/pull/49492 * https://github.com/nodejs/node/pull/44498 * deps: upgrade to libuv 1.45.0 https://github.com/nodejs/node/pull/48078 * deps: update V8 to 10.7 https://github.com/nodejs/node/pull/44741 * test: use gcUntil() in test-v8-serialize-leak https://github.com/nodejs/node/pull/49168 * module: make CJS load from ESM loader https://github.com/nodejs/node/pull/47999 * src: make BuiltinLoader threadsafe and non-global https://github.com/nodejs/node/pull/45942 * chore: address changes to CJS/ESM loading * module: make CJS load from ESM loader (https://github.com/nodejs/node/pull/47999) * lib: improve esm resolve performance (https://github.com/nodejs/node/pull/46652) * bootstrap: optimize modules loaded in the built-in snapshot https://github.com/nodejs/node/pull/45849 * test: mark test-runner-output as flaky https://github.com/nodejs/node/pull/49854 * lib: lazy-load deps in modules/run_main.js https://github.com/nodejs/node/pull/45849 * url: use private properties for brand check https://github.com/nodejs/node/pull/46904 * test: refactor `test-node-output-errors` https://github.com/nodejs/node/pull/48992 * assert: deprecate callTracker https://github.com/nodejs/node/pull/47740 * src: cast v8::Object::GetInternalField() return value to v8::Value https://github.com/nodejs/node/pull/48943 * test: adapt test-v8-stats for V8 update https://github.com/nodejs/node/pull/45230 * tls: ensure TLS Sockets are closed if the underlying wrap closes https://github.com/nodejs/node/pull/49327 * test: deflake test-tls-socket-close https://github.com/nodejs/node/pull/49575 * net: fix crash due to simultaneous close/shutdown on JS Stream Sockets https://github.com/nodejs/node/pull/49400 * net: use asserts in JS Socket Stream to catch races in future https://github.com/nodejs/node/pull/49400 * lib: fix BroadcastChannel initialization location https://github.com/nodejs/node/pull/46864 * src: create BaseObject with node::Realm https://github.com/nodejs/node/pull/44348 * src: implement DataQueue and non-memory resident Blob https://github.com/nodejs/node/pull/45258 * sea: add support for V8 bytecode-only caching https://github.com/nodejs/node/pull/48191 * chore: fixup patch indices * gyp: put filenames in variables https://github.com/nodejs/node/pull/46965 * build: modify js2c.py into GN executable * fix: (WIP) handle string replacement of fs -> original-fs * [v20.x] backport vm-related memory fixes https://github.com/nodejs/node/pull/49874 * src: make BuiltinLoader threadsafe and non-global https://github.com/nodejs/node/pull/45942 * src: avoid copying string in fs_permission https://github.com/nodejs/node/pull/47746 * look upon my works ye mighty and dispair * chore: patch cleanup * [api] Remove AllCan Read/Write https://chromium-review.googlesource.com/c/v8/v8/+/5006387 * fix: missing include for NODE_EXTERN * chore: fixup patch indices * fix: fail properly when js2c fails in Node.js * build: fix js2c root_gen_dir * fix: lib/fs.js -> lib/original-fs.js * build: fix original-fs file xforms * fixup! module: make CJS load from ESM loader * build: get rid of CppHeap for now * build: add patch to prevent extra fs lookup on esm load * build: greatly simplify js2c modifications Moves our original-fs modifications back into a super simple python script action, wires up the output of that action into our call to js2c * chore: update to handle moved internal/modules/helpers file * test: update @types/node test * feat: enable preventing cppgc heap creation * feat: optionally prevent calling V8::EnableWebAssemblyTrapHandler * fix: no cppgc initialization in the renderer * gyp: put filenames in variables https://github.com/nodejs/node/pull/46965 * test: disable single executable tests * fix: nan tests failing on node headers missing file * tls,http2: send fatal alert on ALPN mismatch https://github.com/nodejs/node/pull/44031 * test: disable snapshot tests * https://github.com/nodejs/node/pull/47887 * https://github.com/nodejs/node/pull/49684 * https://github.com/nodejs/node/pull/44193 * build: use deps/v8 for v8/tools Node.js hard depends on these in their builtins * test: fix edge snapshot stack traces https://github.com/nodejs/node/pull/49659 * build: remove js2c //base dep * build: use electron_js2c_toolchain to build node_js2c * fix: don't create SafeSet outside packageResolve Fixes failure in parallel/test-require-delete-array-iterator: === release test-require-delete-array-iterator === Path: parallel/test-require-delete-array-iterator node:internal/per_context/primordials:426 constructor(i) { super(i); } // eslint-disable-line no-useless-constructor ^ TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator)) at new Set (<anonymous>) at new SafeSet (node:internal/per_context/primordials:426:22) * fix: failing crashReporter tests on Linux These were failing because our change from node::InitializeNodeWithArgs to node::InitializeOncePerProcess meant that we now inadvertently called PlatformInit, which reset signal handling. This meant that our intentional crash function ElectronBindings::Crash no longer worked and the renderer process no longer crashed when process.crash() was called. We don't want to use Node.js' default signal handling in the renderer process, so we disable it by passing kNoDefaultSignalHandling to node::InitializeOncePerProcess. * build: only create cppgc heap on non-32 bit platforms * chore: clean up util:CompileAndCall * src: fix compatility with upcoming V8 12.1 APIs https://github.com/nodejs/node/pull/50709 * fix: use thread_local BuiltinLoader * chore: fixup v8 patch indices --------- Co-authored-by: Keeley Hammond <vertedinde@electronjs.org> Co-authored-by: Samuel Attard <marshallofsound@electronjs.org>
161 lines
6.4 KiB
TypeScript
161 lines
6.4 KiB
TypeScript
import * as path from 'path';
|
|
import { pathToFileURL } from 'url';
|
|
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
|
|
|
|
import type * as ipcRendererInternalModule from '@electron/internal/renderer/ipc-renderer-internal';
|
|
import type * as ipcRendererUtilsModule from '@electron/internal/renderer/ipc-renderer-internal-utils';
|
|
|
|
const Module = require('module') as NodeJS.ModuleInternal;
|
|
|
|
// 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.
|
|
//
|
|
// Note 3: We provide the equivalent extra variables internally through the
|
|
// webpack ProvidePlugin in webpack.config.base.js. If you add any extra
|
|
// variables to this wrapper please ensure to update that plugin as well.
|
|
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); });'
|
|
];
|
|
|
|
// We modified the original process.argv to let node.js load the
|
|
// init.js, we need to restore it here.
|
|
process.argv.splice(1, 1);
|
|
|
|
// Clear search paths.
|
|
require('../common/reset-search-paths');
|
|
|
|
// Import common settings.
|
|
require('@electron/internal/common/init');
|
|
|
|
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal') as typeof ipcRendererInternalModule;
|
|
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils') as typeof ipcRendererUtilsModule;
|
|
|
|
process.getProcessMemoryInfo = () => {
|
|
return ipcRendererInternal.invoke<Electron.ProcessMemoryInfo>(IPC_MESSAGES.BROWSER_GET_PROCESS_MEMORY_INFO);
|
|
};
|
|
|
|
// Process command line arguments.
|
|
const { hasSwitch, getSwitchValue } = process._linkedBinding('electron_common_command_line');
|
|
const { mainFrame } = process._linkedBinding('electron_renderer_web_frame');
|
|
|
|
const nodeIntegration = mainFrame.getWebPreference('nodeIntegration');
|
|
const appPath = hasSwitch('app-path') ? getSwitchValue('app-path') : null;
|
|
|
|
// Common renderer initialization
|
|
require('@electron/internal/renderer/common-init');
|
|
|
|
if (nodeIntegration) {
|
|
// Export node bindings to global.
|
|
const { makeRequireFunction } = __non_webpack_require__('internal/modules/helpers');
|
|
global.module = new Module('electron/js2c/renderer_init');
|
|
global.require = makeRequireFunction(global.module);
|
|
|
|
// Set the __filename to the path of html file if it is file: protocol.
|
|
if (window.location.protocol === 'file:') {
|
|
const location = window.location;
|
|
let pathname = location.pathname;
|
|
|
|
if (process.platform === 'win32') {
|
|
if (pathname[0] === '/') pathname = pathname.substr(1);
|
|
|
|
const isWindowsNetworkSharePath = location.hostname.length > 0 && process.resourcesPath.startsWith('\\');
|
|
if (isWindowsNetworkSharePath) {
|
|
pathname = `//${location.host}/${pathname}`;
|
|
}
|
|
}
|
|
|
|
global.__filename = path.normalize(decodeURIComponent(pathname));
|
|
global.__dirname = path.dirname(global.__filename);
|
|
|
|
// Set module's filename so relative require can work as expected.
|
|
global.module.filename = global.__filename;
|
|
|
|
// Also search for module under the html file.
|
|
global.module.paths = Module._nodeModulePaths(global.__dirname);
|
|
} else {
|
|
// For backwards compatibility we fake these two paths here
|
|
global.__filename = path.join(process.resourcesPath, 'electron.asar', 'renderer', 'init.js');
|
|
global.__dirname = path.join(process.resourcesPath, 'electron.asar', 'renderer');
|
|
|
|
if (appPath) {
|
|
// Search for module under the app directory
|
|
global.module.paths = Module._nodeModulePaths(appPath);
|
|
}
|
|
}
|
|
|
|
// Redirect window.onerror to uncaughtException.
|
|
window.onerror = function (_message, _filename, _lineno, _colno, error) {
|
|
if (global.process.listenerCount('uncaughtException') > 0) {
|
|
// We do not want to add `uncaughtException` to our definitions
|
|
// because we don't want anyone else (anywhere) to throw that kind
|
|
// of error.
|
|
global.process.emit('uncaughtException', error as any);
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
};
|
|
} else {
|
|
// Delete Node's symbols after the Environment has been loaded in a
|
|
// non context-isolated environment
|
|
if (!process.contextIsolated) {
|
|
process.once('loaded', function () {
|
|
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;
|
|
});
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
if (esmPreloads.length) {
|
|
const { loadESM } = __non_webpack_require__('internal/process/esm_loader');
|
|
|
|
loadESM(async (esmLoader: any) => {
|
|
// Load the preload scripts.
|
|
for (const preloadScript of esmPreloads) {
|
|
await esmLoader.import(pathToFileURL(preloadScript).toString(), undefined, Object.create(null)).catch((err: Error) => {
|
|
console.error(`Unable to load preload script: ${preloadScript}`);
|
|
console.error(err);
|
|
|
|
ipcRendererInternal.send(IPC_MESSAGES.BROWSER_PRELOAD_ERROR, preloadScript, err);
|
|
});
|
|
}
|
|
}).finally(() => appCodeLoaded!());
|
|
} else {
|
|
appCodeLoaded!();
|
|
}
|