2023-07-01 09:07:51 -04:00
|
|
|
const { VM } = require('vm2')
|
|
|
|
const { Server } = require('socket.io')
|
|
|
|
const util = require('util')
|
|
|
|
const { stylize } = require('./colors')
|
2023-08-17 21:58:54 -04:00
|
|
|
const { Worker } = require('worker_threads')
|
|
|
|
const path = require('path')
|
2023-07-01 09:07:51 -04:00
|
|
|
|
|
|
|
const io = new Server(3069)
|
|
|
|
|
|
|
|
io.on('connection', (socket) => {
|
2023-08-17 21:58:54 -04:00
|
|
|
let worker
|
2023-07-22 22:10:01 -04:00
|
|
|
|
2023-08-17 21:58:54 -04:00
|
|
|
function reset () {
|
|
|
|
worker = new Worker(path.join(__dirname, 'vm.js'))
|
2023-07-02 09:11:38 -04:00
|
|
|
|
2023-08-17 21:58:54 -04:00
|
|
|
worker.on('message', (msg) => {
|
|
|
|
switch (msg.type) {
|
|
|
|
case 'socketEmit':
|
|
|
|
socket.emit(...msg.data)
|
2023-08-01 22:29:48 -04:00
|
|
|
|
2023-08-17 21:58:54 -04:00
|
|
|
break
|
|
|
|
case 'socketOnce':
|
|
|
|
socket.once(...msg.data)
|
2023-07-02 09:11:38 -04:00
|
|
|
|
2023-08-17 21:58:54 -04:00
|
|
|
break
|
2023-07-01 09:07:51 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-17 21:58:54 -04:00
|
|
|
reset()
|
|
|
|
|
|
|
|
socket.on('setFunctions', (jsonArray) => {
|
|
|
|
worker.postMessage({ type: 'setFunctions', jsonArray })
|
|
|
|
})
|
2023-07-01 09:07:51 -04:00
|
|
|
|
|
|
|
socket.on('runCode', (transactionId, code) => {
|
2023-08-17 21:58:54 -04:00
|
|
|
worker.postMessage({ type: 'runCode', code })
|
2023-07-01 09:07:51 -04:00
|
|
|
|
2023-08-17 21:58:54 -04:00
|
|
|
worker.on('message', ({ type, error, output }) => {
|
|
|
|
if (type !== 'codeOutput') return
|
|
|
|
|
|
|
|
socket.emit('codeOutput', transactionId, error, output)
|
|
|
|
})
|
2023-07-01 09:07:51 -04:00
|
|
|
})
|
|
|
|
|
2023-08-17 21:58:54 -04:00
|
|
|
socket.on('reset', reset)
|
2023-07-01 09:07:51 -04:00
|
|
|
})
|
2023-07-02 02:45:11 -04:00
|
|
|
|
|
|
|
process.on('uncaughtException', (e) => {
|
|
|
|
console.log(`Caught an uncaught exception!\n${e.stack}`)
|
|
|
|
})
|