eval-server/index.js

60 lines
1.4 KiB
JavaScript
Raw Normal View History

2023-07-01 09:07:51 -04:00
const { Server } = require('socket.io')
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) => {
let worker
2023-07-22 22:10:01 -04:00
2023-08-18 23:43:53 -04:00
let isFirst = true
2023-08-18 23:48:17 -04:00
let jsonArray
function reset () {
worker = new Worker(path.join(__dirname, 'vm.js'))
2023-07-02 09:11:38 -04:00
worker.on('message', (msg) => {
switch (msg.type) {
case 'socketEmit':
socket.emit(...msg.data)
break
case 'socketOnce':
socket.once(...msg.data)
2023-07-02 09:11:38 -04:00
break
2023-07-01 09:07:51 -04:00
}
})
2023-08-18 23:43:53 -04:00
// ohio
if (!isFirst) {
worker.postMessage({ type: 'setFunctions', jsonArray })
} else isFirst = false
2023-07-01 09:07:51 -04:00
}
reset()
2023-08-18 23:48:17 -04:00
socket.on('setFunctions', (_jsonArray) => {
jsonArray = _jsonArray
worker.postMessage({ type: 'setFunctions', jsonArray })
})
2023-07-01 09:07:51 -04:00
socket.on('runCode', (transactionId, code) => {
worker.postMessage({ type: 'runCode', code })
2023-07-01 09:07:51 -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
})
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}`)
})