Files
directus/api/src/operations/exec/index.ts
Rijk van Zanten a21b28b423 Add "Run Script" operation (#15101)
* [WIP] Start working on script operation

* WIP Remove npx-import experiment

* Don't break on string value in options scope

* WIP Add module whitelist

* Mission critical import cleanup work

* Add translated labels

* Use nodeVM for everything

Keeps the export function callback structure consistent

* Tweak wording

* WIP start on tests

* Improve typing in shared define types

* Add missing base reactive properties in server store

* Add tests for exec index in app

* Add tests

* Optimize error handling
2022-08-18 15:39:25 -04:00

35 lines
733 B
TypeScript

import { defineOperationApi, toArray } from '@directus/shared/utils';
import { NodeVM, NodeVMOptions, VMScript } from 'vm2';
type Options = {
code: string;
};
export default defineOperationApi<Options>({
id: 'exec',
handler: async ({ code }, { data, env }) => {
const allowedModules = env.FLOWS_EXEC_ALLOWED_MODULES ? toArray(env.FLOWS_EXEC_ALLOWED_MODULES) : [];
const opts: NodeVMOptions = {
eval: false,
wasm: false,
};
if (allowedModules.length > 0) {
opts.require = {
external: {
modules: allowedModules,
transitive: false,
},
};
}
const vm = new NodeVM(opts);
const script = new VMScript(code).compile();
const fn = await vm.run(script);
return await fn(data);
},
});