2022-04-13 12:46:52 +00:00
|
|
|
import { preloadModules } from "../../common/preload_modules";
|
|
|
|
|
2022-03-27 07:55:29 +00:00
|
|
|
const { parentPort, workerData } = require("worker_threads");
|
2022-04-21 09:46:33 +00:00
|
|
|
const { VM, VMScript } = require(`${workerData}/vm2`);
|
|
|
|
const fetch = require(`${workerData}/node-fetch`);
|
|
|
|
const WebSocket = require(`${workerData}/ws`);
|
2022-03-27 07:55:29 +00:00
|
|
|
|
|
|
|
// console.log("Process env", process.env);
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
let loadedFunctions = new Map<string, Function>();
|
|
|
|
let pendingRequests = new Map<
|
2022-03-21 14:21:34 +00:00
|
|
|
number,
|
|
|
|
{
|
|
|
|
resolve: (result: unknown) => void;
|
|
|
|
reject: (e: any) => void;
|
|
|
|
}
|
|
|
|
>();
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-03-24 09:48:56 +00:00
|
|
|
let syscallReqId = 0;
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
let vm = new VM({
|
|
|
|
sandbox: {
|
2022-04-21 09:46:33 +00:00
|
|
|
// Exposing some "safe" APIs
|
2022-04-09 12:28:41 +00:00
|
|
|
console,
|
2022-04-19 14:54:47 +00:00
|
|
|
setTimeout,
|
|
|
|
clearTimeout,
|
|
|
|
setInterval,
|
|
|
|
clearInterval,
|
2022-04-21 09:46:33 +00:00
|
|
|
fetch,
|
|
|
|
WebSocket,
|
|
|
|
// This is only going to be called for pre-bundled modules, we won't allow
|
|
|
|
// arbitrary requiring of modules
|
2022-04-13 12:46:52 +00:00
|
|
|
require: (moduleName: string): any => {
|
2022-04-19 14:54:47 +00:00
|
|
|
// console.log("Loading", moduleName);
|
2022-04-13 12:46:52 +00:00
|
|
|
if (preloadModules.includes(moduleName)) {
|
|
|
|
return require(`${workerData}/${moduleName}`);
|
|
|
|
} else {
|
|
|
|
throw Error("Cannot import arbitrary modules");
|
|
|
|
}
|
|
|
|
},
|
2022-03-20 08:56:28 +00:00
|
|
|
self: {
|
2022-03-24 09:48:56 +00:00
|
|
|
syscall: (name: string, ...args: any[]) => {
|
2022-03-20 08:56:28 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-03-24 09:48:56 +00:00
|
|
|
syscallReqId++;
|
|
|
|
pendingRequests.set(syscallReqId, { resolve, reject });
|
2022-03-20 08:56:28 +00:00
|
|
|
parentPort.postMessage({
|
|
|
|
type: "syscall",
|
2022-03-24 09:48:56 +00:00
|
|
|
id: syscallReqId,
|
2022-03-20 08:56:28 +00:00
|
|
|
name,
|
|
|
|
// TODO: Figure out why this is necessary (to avoide a CloneError)
|
|
|
|
args: JSON.parse(JSON.stringify(args)),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-03-21 14:21:34 +00:00
|
|
|
function wrapScript(code: string) {
|
2022-03-20 08:56:28 +00:00
|
|
|
return `(${code})["default"]`;
|
|
|
|
}
|
|
|
|
|
2022-03-21 14:21:34 +00:00
|
|
|
function safeRun(fn: any) {
|
|
|
|
fn().catch((e: any) => {
|
2022-03-20 08:56:28 +00:00
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-21 14:21:34 +00:00
|
|
|
parentPort.on("message", (data: any) => {
|
2022-03-20 08:56:28 +00:00
|
|
|
safeRun(async () => {
|
|
|
|
switch (data.type) {
|
|
|
|
case "load":
|
|
|
|
loadedFunctions.set(data.name, new VMScript(wrapScript(data.code)));
|
|
|
|
parentPort.postMessage({
|
|
|
|
type: "inited",
|
|
|
|
name: data.name,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "invoke":
|
|
|
|
let fn = loadedFunctions.get(data.name);
|
|
|
|
if (!fn) {
|
|
|
|
throw new Error(`Function not loaded: ${data.name}`);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
let r = vm.run(fn);
|
|
|
|
let result = await Promise.resolve(r(...data.args));
|
|
|
|
parentPort.postMessage({
|
|
|
|
type: "result",
|
|
|
|
id: data.id,
|
2022-03-21 14:21:34 +00:00
|
|
|
// TOOD: Figure out if this is necessary, because it's expensive
|
|
|
|
result: result && JSON.parse(JSON.stringify(result)),
|
2022-03-20 08:56:28 +00:00
|
|
|
});
|
2022-03-21 14:21:34 +00:00
|
|
|
} catch (e: any) {
|
2022-03-20 08:56:28 +00:00
|
|
|
parentPort.postMessage({
|
|
|
|
type: "result",
|
|
|
|
id: data.id,
|
|
|
|
error: e.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "syscall-response":
|
|
|
|
let syscallId = data.id;
|
|
|
|
const lookup = pendingRequests.get(syscallId);
|
|
|
|
if (!lookup) {
|
|
|
|
throw Error("Invalid request id");
|
|
|
|
}
|
|
|
|
pendingRequests.delete(syscallId);
|
|
|
|
if (data.error) {
|
2022-03-28 13:25:05 +00:00
|
|
|
console.log("Got rejection", data.error);
|
2022-03-20 08:56:28 +00:00
|
|
|
lookup.reject(new Error(data.error));
|
|
|
|
} else {
|
|
|
|
lookup.resolve(data.result);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|