1
0
Fork 0
mirror of synced 2024-07-04 22:11:23 +12:00
budibase/packages/server/src/api/controllers/script.js

27 lines
610 B
JavaScript
Raw Normal View History

2021-03-27 03:56:34 +13:00
const fetch = require("node-fetch")
2021-03-26 00:17:04 +13:00
const vm = require("vm")
class ScriptExecutor {
2021-03-27 03:56:34 +13:00
constructor(body) {
const code = `let fn = () => {\n${body.script}\n}; out = fn();`
this.script = new vm.Script(code)
2021-03-27 03:56:34 +13:00
this.context = vm.createContext(body.context)
this.context.fetch = fetch
2021-03-26 00:17:04 +13:00
}
execute() {
this.script.runInContext(this.context)
return this.context.out
2021-03-26 00:17:04 +13:00
}
}
2021-05-04 20:55:14 +12:00
exports.execute = async function (ctx) {
2021-03-27 03:56:34 +13:00
const executor = new ScriptExecutor(ctx.request.body)
2021-03-26 00:17:04 +13:00
ctx.body = executor.execute()
}
2021-05-12 04:50:26 +12:00
exports.save = async function (ctx) {
ctx.throw(501, "Not currently implemented")
2021-03-26 00:17:04 +13:00
}