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) {
|
2021-06-19 05:07:51 +12:00
|
|
|
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() {
|
2021-06-19 05:07:51 +12:00
|
|
|
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
|
|
|
|
2021-05-12 04:49:26 +12:00
|
|
|
ctx.body = executor.execute()
|
|
|
|
}
|
|
|
|
|
2021-05-12 04:50:26 +12:00
|
|
|
exports.save = async function (ctx) {
|
2021-05-12 04:49:26 +12:00
|
|
|
ctx.throw(501, "Not currently implemented")
|
2021-03-26 00:17:04 +13:00
|
|
|
}
|