2024-10-03 02:10:44 +00:00
|
|
|
/* eslint-disable import/newline-after-import */
|
|
|
|
/* eslint-disable import/order */
|
2024-01-24 15:54:32 +00:00
|
|
|
// Initialize ASAR support in fs module.
|
|
|
|
import { wrapFsWithAsar } from './asar-fs-wrapper';
|
|
|
|
wrapFsWithAsar(require('fs'));
|
|
|
|
|
2024-04-17 16:39:13 +00:00
|
|
|
// See ElectronRendererClient::DidCreateScriptContext.
|
2024-06-11 21:59:53 +00:00
|
|
|
if ((globalThis as any).blinkfetch) {
|
|
|
|
const keys = ['fetch', 'Response', 'FormData', 'Request', 'Headers'];
|
|
|
|
for (const key of keys) {
|
|
|
|
(globalThis as any)[key] = (globalThis as any)[`blink${key}`];
|
|
|
|
}
|
2024-04-17 16:39:13 +00:00
|
|
|
}
|
|
|
|
|
2024-01-24 15:54:32 +00:00
|
|
|
// Hook child_process.fork.
|
2024-01-30 23:38:56 +00:00
|
|
|
import cp = require('child_process'); // eslint-disable-line import/first
|
2024-01-24 15:54:32 +00:00
|
|
|
const originalFork = cp.fork;
|
2024-01-30 23:38:56 +00:00
|
|
|
cp.fork = (modulePath, args?, options?: cp.ForkOptions) => {
|
2024-01-24 15:54:32 +00:00
|
|
|
// Parse optional args.
|
|
|
|
if (args == null) {
|
|
|
|
args = [];
|
|
|
|
} else if (typeof args === 'object' && !Array.isArray(args)) {
|
2024-01-30 23:38:56 +00:00
|
|
|
options = args as cp.ForkOptions;
|
2024-01-24 15:54:32 +00:00
|
|
|
args = [];
|
|
|
|
}
|
|
|
|
// Fallback to original fork to report arg type errors.
|
|
|
|
if (typeof modulePath !== 'string' || !Array.isArray(args) ||
|
|
|
|
(typeof options !== 'object' && typeof options !== 'undefined')) {
|
|
|
|
return originalFork(modulePath, args, options);
|
|
|
|
}
|
|
|
|
// When forking a child script, we setup a special environment to make
|
|
|
|
// the electron binary run like upstream Node.js.
|
|
|
|
options = options ?? {};
|
|
|
|
options.env = Object.create(options.env || process.env);
|
2024-01-30 23:38:56 +00:00
|
|
|
options.env!.ELECTRON_RUN_AS_NODE = '1';
|
2024-01-24 15:54:32 +00:00
|
|
|
// On mac the child script runs in helper executable.
|
|
|
|
if (!options.execPath && process.platform === 'darwin') {
|
|
|
|
options.execPath = process.helperExecPath;
|
|
|
|
}
|
|
|
|
return originalFork(modulePath, args, options);
|
|
|
|
};
|
2024-01-26 08:29:04 +00:00
|
|
|
|
|
|
|
// Prevent Node from adding paths outside this app to search paths.
|
2024-01-30 23:38:56 +00:00
|
|
|
import path = require('path'); // eslint-disable-line import/first
|
2024-01-26 08:29:04 +00:00
|
|
|
const Module = require('module') as NodeJS.ModuleInternal;
|
|
|
|
const resourcesPathWithTrailingSlash = process.resourcesPath + path.sep;
|
|
|
|
const originalNodeModulePaths = Module._nodeModulePaths;
|
2024-01-30 23:38:56 +00:00
|
|
|
Module._nodeModulePaths = function (from) {
|
2024-01-26 08:29:04 +00:00
|
|
|
const paths: string[] = originalNodeModulePaths(from);
|
|
|
|
const fromPath = path.resolve(from) + path.sep;
|
|
|
|
// If "from" is outside the app then we do nothing.
|
|
|
|
if (fromPath.startsWith(resourcesPathWithTrailingSlash)) {
|
|
|
|
return paths.filter(function (candidate) {
|
|
|
|
return candidate.startsWith(resourcesPathWithTrailingSlash);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
};
|