Use remote vm for better error messages

This commit is contained in:
Zack Rauen 2022-09-26 03:01:16 -04:00
parent b5a69db0b3
commit 8601193b51
4 changed files with 24 additions and 3 deletions

View File

@ -36,6 +36,7 @@ export * as filesystem from "./filesystem";
export {default as https} from "./https";
export * as electron from "./electron";
export * as crypto from "./crypto";
export * as vm from "./vm";
// We can expose that without any issues.
export * as path from "path";

14
preload/src/api/vm.js Normal file
View File

@ -0,0 +1,14 @@
import vm from "vm";
export function compileFunction(code, params = [], options = {}) {
try {
return vm.compileFunction(code, params, options);
}
catch (error) {
return {
name: error.name,
message: error.message,
stack: error.stack
};
}
}

View File

@ -117,7 +117,7 @@ export default new class PluginManager extends AddonManager {
try {
const module = {filename, exports: {}};
// Test if the code is valid gracefully
vm.compileFunction(addon.fileContent, ["require", "module", "exports", "__filename", "__dirname"]);
vm.compileFunction(addon.fileContent, ["require", "module", "exports", "__filename", "__dirname"], {filename: path.basename(filename)});
addon.fileContent += normalizeExports(addon.exports || addon.name);
addon.fileContent += `\n//# sourceURL=betterdiscord://plugins/${addon.filename}`;
const wrappedPlugin = new Function(["require", "module", "exports", "__filename", "__dirname"], addon.fileContent); // eslint-disable-line no-new-func

View File

@ -1,3 +1,9 @@
export const compileFunction = function (code, params = [], filename = "") {
return window.eval(`(${params.join(", ")}) => {${code}//# sourceURL=${filename.replace(/\\/g, "\\")}\n}`); // eslint-disable-line no-eval
import Remote from "./remote";
export const compileFunction = function(code, params = [], options = {}) {
const returned = Remote.vm.compileFunction(code, params, options);
if (typeof(returned) === "function") return returned;
const syntaxError = new SyntaxError(returned.message);
syntaxError.stack = returned.stack;
throw syntaxError;
};