eval-server/index.js
2023-08-19 10:43:53 +07:00

58 lines
1.4 KiB
JavaScript

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
let isFirst = true
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
}
})
// ohio
if (!isFirst) {
worker.postMessage({ type: 'setFunctions', jsonArray })
} else isFirst = false
}
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}`)
})