NodeJS bugfix, js_samples script

This commit is contained in:
DarkCat09 2022-12-26 17:24:34 +04:00
parent 1f93640139
commit 7662f02d9c
3 changed files with 115 additions and 6 deletions

View file

@ -104,6 +104,7 @@ class NodeInterpreter(Interpreter):
def get_var(self, name: str) -> Any:
resp = requests.post(self.url, data=name)
resp.raise_for_status()
logging.debug('NodeJS response: %s', resp.content)
return json.loads(resp.content)
def __del__(self) -> None:

View file

@ -3,11 +3,21 @@ const process = require('process')
const { VM } = require('vm2')
args = process.argv.slice(2)
const args = process.argv.slice(2)
const port = args[0] || 8000
const host = args[1] || 'localhost'
const vm = new VM({
timeout: 2000,
allowAsync: false,
sandbox: {
atob: atob,
setTimeout: (_a, _b) => {},
setInterval: (_a, _b) => {},
},
})
vm.run('var window = global; var document = {}')
const listener = (req, res) => {
if (req.method != 'POST')
@ -18,15 +28,12 @@ const listener = (req, res) => {
req.on('end', () => {
let resp
try { resp = JSON.stringify(new VM().run(body)) }
try { resp = JSON.stringify(vm.run(body)) }
catch (ex) { resp = ex.message }
res.writeHead(200)
res.end(resp)
})
}
window = global
document = window.document || {}
const server = http.createServer(listener)
server.listen(port, host, () => console.log('OK'))