diff --git a/index.js b/index.js index 1213286..3135f87 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -const { Isolate } = require('isolated-vm') +const { VM } = require('@n8n/vm2') const { Server } = require('socket.io') const util = require('util') const { stylize } = require('./colors') @@ -13,10 +13,10 @@ const BRIDGE_PREFIX = 'function:' const io = new Server(3069) -io.on('connection', (socket) => { +io.on('connection', async (socket) => { let functions - let proxy + let vm const handler = { get (target, prop) { @@ -49,33 +49,26 @@ io.on('connection', (socket) => { resetVM() }) - const isolate = new Isolate({ memoryLimit: 256 }) - - let context async function resetVM () { - context = await isolate.createContext() - - // TODO: fix - - // const global = context.global - - // await global.set('this', global.derefInto()) - // await global.set('global', global.derefInto()) - - // await global.set('bridge', () => { return proxy }) - // await global.set('randomstring', () => randomstring) - // await global.set('ChatMessage', () => ChatMessage) - // await global.set('mc', () => mc) - // await global.set('moment', () => moment) - // await global.set('crypto', () => crypto) - // await global.set('nbt', () => nbt) + 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 = await context.eval(code, { timeout: 1000 }) + const output = vm.run(code) socket.emit('codeOutput', transactionId, false, util.inspect(output, { stylize })) } catch (e) { diff --git a/package.json b/package.json index 6308c03..d77a5ed 100644 --- a/package.json +++ b/package.json @@ -10,14 +10,13 @@ "author": "", "license": "ISC", "dependencies": { - "isolated-vm": "^4.6.0", + "@n8n/vm2": "^3.9.25", "minecraft-protocol": "^1.43.1", "moment": "^2.29.4", "moment-timezone": "^0.5.43", "prismarine-chat": "^1.8.0", "prismarine-nbt": "^2.2.1", "randomstring": "^1.3.0", - "socket.io": "^4.7.1", - "vm2": "^3.9.19" + "socket.io": "^4.7.1" } }