diff --git a/index.js b/index.js index 2f432b6..a06ef48 100644 --- a/index.js +++ b/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) + } + } + + 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,