improve by using proxy !!

This commit is contained in:
Chayapak 2023-07-02 20:11:38 +07:00
parent dd037643aa
commit e2c8374431

View file

@ -12,24 +12,35 @@ const BRIDGE_PREFIX = 'function:'
const io = new Server(3069)
function checkString (string) {
if (typeof(string) !== 'string') throw new Error('Must be a string')
}
io.on('connection', (socket) => {
let functions
let proxy
const handler = {
get (target, prop) {
if (!target[prop]) throw new Error(`Function "${prop}" not available`)
return (...args) => target[prop](...args)
}
}
socket.on('setFunctions', (...args) => {
functions = {}
for (const eachFuntion of args) {
functions[eachFuntion] = (...args) => socket.emit(BRIDGE_PREFIX + eachFuntion, ...args)
}
proxy = new Proxy(functions, handler)
})
let vm
function resetVM () {
vm = new VM({
timeout: 3000,
sandbox: {
chat: (message) => {
checkString(message)
socket.emit(BRIDGE_PREFIX + 'chat', message)
},
core: (command) => {
checkString(command)
socket.emit(BRIDGE_PREFIX + 'core', command)
},
bridge: () => proxy,
randomstring,
ChatMessage,
mc,