const { VM } = require('vm2') const { Server } = require('socket.io') const util = require('util') const { stylize } = require('./colors') const { Worker } = require('worker_threads') const path = require('path') const io = new Server(3069) io.on('connection', (socket) => { let worker function reset () { worker = new Worker(path.join(__dirname, 'vm.js')) worker.on('message', (msg) => { switch (msg.type) { case 'socketEmit': socket.emit(...msg.data) break case 'socketOnce': socket.once(...msg.data) break } }) } reset() socket.on('setFunctions', (jsonArray) => { worker.postMessage({ type: 'setFunctions', jsonArray }) }) socket.on('runCode', (transactionId, code) => { worker.postMessage({ type: 'runCode', code }) worker.on('message', ({ type, error, output }) => { if (type !== 'codeOutput') return socket.emit('codeOutput', transactionId, error, output) }) }) socket.on('reset', reset) }) process.on('uncaughtException', (e) => { console.log(`Caught an uncaught exception!\n${e.stack}`) })