Fix several crashing issues (#869)

* Fix canary issue and stream crashing.

* Fix process package by NodeJS.

* Move ipc calls to IPC module.
This commit is contained in:
Strencher 2021-07-08 00:34:04 +02:00 committed by GitHub
parent d25e56cc23
commit 959bdc3a4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 6 deletions

View File

@ -11,4 +11,5 @@ export const CLOSE_DEVTOOLS = "bd-close-devtools";
export const OPEN_WINDOW = "bd-open-window"; export const OPEN_WINDOW = "bd-open-window";
export const INSPECT_ELEMENT = "bd-inspect-element"; export const INSPECT_ELEMENT = "bd-inspect-element";
export const MINIMUM_SIZE = "bd-minimum-size"; export const MINIMUM_SIZE = "bd-minimum-size";
export const DEVTOOLS_WARNING = "bd-remove-devtools-message"; export const DEVTOOLS_WARNING = "bd-remove-devtools-message";
export const REGISTER_PRELOAD = "bd-register-preload";

View File

@ -9,8 +9,6 @@ import CSP from "./modules/csp";
if (!process.argv.includes("--vanilla")) { if (!process.argv.includes("--vanilla")) {
process.env.NODE_OPTIONS = "--no-force-async-hooks-checks"; process.env.NODE_OPTIONS = "--no-force-async-hooks-checks";
app.commandLine.appendSwitch("no-force-async-hooks-checks"); app.commandLine.appendSwitch("no-force-async-hooks-checks");
process.electronBinding("command_line").appendSwitch("no-force-async-hooks-checks");
// Patch and replace the built-in BrowserWindow // Patch and replace the built-in BrowserWindow
BrowserWindow.patchBrowserWindow(); BrowserWindow.patchBrowserWindow();

View File

@ -77,6 +77,10 @@ const setMinimumSize = (event, width, height) => {
const stopDevtoolsWarning = event => event.sender.removeAllListeners("devtools-opened"); const stopDevtoolsWarning = event => event.sender.removeAllListeners("devtools-opened");
const registerPreload = (event, path) => {
app.commandLine.appendSwitch("preload", path);
};
export default class IPCMain { export default class IPCMain {
static registerEvents() { static registerEvents() {
ipc.on(IPCEvents.GET_PATH, getPath); ipc.on(IPCEvents.GET_PATH, getPath);
@ -86,6 +90,7 @@ export default class IPCMain {
ipc.on(IPCEvents.INSPECT_ELEMENT, inspectElement); ipc.on(IPCEvents.INSPECT_ELEMENT, inspectElement);
ipc.on(IPCEvents.MINIMUM_SIZE, setMinimumSize); ipc.on(IPCEvents.MINIMUM_SIZE, setMinimumSize);
ipc.on(IPCEvents.DEVTOOLS_WARNING, stopDevtoolsWarning); ipc.on(IPCEvents.DEVTOOLS_WARNING, stopDevtoolsWarning);
ipc.on(IPCEvents.REGISTER_PRELOAD, registerPreload);
ipc.handle(IPCEvents.RUN_SCRIPT, runScript); ipc.handle(IPCEvents.RUN_SCRIPT, runScript);
ipc.handle(IPCEvents.OPEN_WINDOW, createBrowserWindow); ipc.handle(IPCEvents.OPEN_WINDOW, createBrowserWindow);
} }

View File

@ -1,6 +1,17 @@
const Module = require("module"); const Module = require("module");
const path = require("path"); const path = require("path");
const electron = require("electron"); const electron = require("electron");
const NodeEvents = require("events");
const cloneObject = function (target, newObject = {}, keys) {
if (!Array.isArray(keys)) keys = Object.keys(Object.getOwnPropertyDescriptors(target));
return keys.reduce((clone, key) => {
if (typeof(target[key]) === "object" && !Array.isArray(target[key]) && target[key] !== null && !(target[key] instanceof NodeEvents)) clone[key] = cloneObject(target[key], {});
else clone[key] = target[key];
return clone;
}, newObject);
};
/* global window:false */ /* global window:false */
@ -11,16 +22,26 @@ Object.defineProperty(window, "webpackJsonp", {
electron.webFrame.top.context.global = electron.webFrame.top.context; electron.webFrame.top.context.global = electron.webFrame.top.context;
electron.webFrame.top.context.require = require; electron.webFrame.top.context.require = require;
electron.webFrame.top.context.process = process;
electron.webFrame.top.context.Buffer = Buffer; electron.webFrame.top.context.Buffer = Buffer;
electron.webFrame.top.context.process = new class PatchedProcess extends NodeEvents {
get __ORIGINAL_PROCESS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED__() {return process;}
constructor() {
super();
Object.assign(this,
cloneObject(process, {}, Object.keys(NodeEvents.prototype)),
cloneObject(process, {})
);
}
};
// Load Discord's original preload // Load Discord's original preload
const preload = process.env.DISCORD_PRELOAD; const preload = process.env.DISCORD_PRELOAD;
if (preload) { if (preload) {
// Restore original preload for future windows // Restore original preload for future windows
process.electronBinding("command_line").appendSwitch("preload", preload); electron.ipcRenderer.send("bd-register-preload", preload);
// Run original preload // Run original preload
try { try {
const originalKill = process.kill; const originalKill = process.kill;

View File

@ -7,6 +7,10 @@ export default function() {
const load = Module._load; const load = Module._load;
Module._load = function(request) { Module._load = function(request) {
if (request === "process") {
return window.process;
}
if (request === namespace || request.startsWith(prefix)) { if (request === namespace || request.startsWith(prefix)) {
const requested = request.substr(prefix.length); const requested = request.substr(prefix.length);
if (requested == "bdapi") return BdApi; if (requested == "bdapi") return BdApi;