eval-server/index.js

63 lines
1.6 KiB
JavaScript
Raw Permalink 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-08-18 23:43:53 -04:00
let isFirst = true
2023-08-18 23:48:17 -04:00
let jsonArray
2023-07-02 09:11:38 -04:00
function reset () {
worker = new Worker(path.join(__dirname, 'vm.js'))
2023-09-15 05:59:58 -04:00
worker.on('message', (msg) => {
switch (msg.type) {
case 'socketEmit':
socket.emit(...msg.data)
2023-09-15 05:59:58 -04:00
break
case 'socketOnce':
socket.once(msg.name, (message, parseJSON) => {
if (parseJSON) worker.postMessage({ type: 'resolvePromise', uuid: msg.uuid, data: JSON.parse(message) })
else worker.postMessage({ type: 'resolvePromise', uuid: msg.uuid, data: message })
2023-09-15 05:59:58 -04:00
})
2023-07-02 09:11:38 -04:00
break
2024-10-18 22:18:01 -04:00
}
})
// ohio
if (!isFirst) {
worker.postMessage({ type: 'setFunctions', jsonArray })
} else isFirst = false
2023-09-15 05:59:58 -04:00
}
2023-07-01 09:07:51 -04:00
reset()
2023-07-01 09:07:51 -04:00
socket.on('setFunctions', (_jsonArray) => {
jsonArray = _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)
})
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}`)
})