mirror of
https://github.com/directus/directus.git
synced 2026-02-19 10:14:33 -05:00
* [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
35 lines
733 B
TypeScript
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);
|
|
},
|
|
});
|