improve by using proxy !!
This commit is contained in:
parent
dd037643aa
commit
e2c8374431
1 changed files with 23 additions and 12 deletions
33
index.js
33
index.js
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
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,
|
||||
|
|
Loading…
Reference in a new issue