const { VM } = require('@n8n/vm2') const { Server } = require('socket.io') const util = require('util') const { stylize } = require('./colors') const randomstring = require('randomstring') const ChatMessage = require('prismarine-chat')('1.20.1') const mc = require('minecraft-protocol') const moment = require('moment-timezone') const crypto = require('crypto') const nbt = require('prismarine-nbt') const BRIDGE_PREFIX = 'function:' const io = new Server(3069) io.on('connection', async (socket) => { let functions let proxy let vm const handler = { get (target, prop) { if (!target[prop]) throw new Error(`Function "${prop}" not available`) return (...args) => target[prop](...args) } } socket.on('setFunctions', (jsonArray) => { const parsed = JSON.parse(jsonArray) functions = {} for (const eachFuntion of parsed) { functions[eachFuntion] = (...args) => { socket.emit(BRIDGE_PREFIX + eachFuntion, ...args) return new Promise((resolve) => { socket.once(`functionOutput:${eachFuntion}`, (message, parseJSON) => { if (parseJSON) resolve(JSON.parse(message)) else resolve(message) }) }) } } proxy = new Proxy(functions, handler) resetVM() }) async function resetVM () { vm = new VM({ timeout: 1000, sandbox: { get bridge () { return proxy }, randomstring, ChatMessage, mc, moment, crypto, nbt } }) } resetVM() socket.on('runCode', async (transactionId, code) => { try { const output = vm.run(code) socket.emit('codeOutput', transactionId, false, util.inspect(output, { stylize })) } catch (e) { socket.emit('codeOutput', transactionId, true, e.toString()) } }) socket.on('reset', resetVM) }) process.on('uncaughtException', (e) => { console.log(`Caught an uncaught exception!\n${e.stack}`) })