Running dynamically user typed functions within Nodejs application without server restart?

134 views Asked by At

I am trying to look at a possibility of having users add their own code within a Node app running an express server.

Scenario -

User clicks on save of a form and wants to do self business validations for example. This can be used as middleware or just some function that can be run before saving. But the challenge is the code is already built and the node is in a running state. How can I use the code added by the user and validate it against some data?

I can definitely use eval or new Function or vm2 (while will isolate things - this I exactly need). But eval and function I definitely don't want to use. VM2 is definitely a choice but on a grand scale of things, it will be poor in performance and would take time to execute.

Have someone used or worked on something similar? I can definitely use Serverless functions but that would become costly if there are multiple applications. Please help me understand how can this work out?

Thanks.

1

There are 1 answers

3
in need of help On

I don't understand why not eval. Other than that you can write the code to a new file and run a new node process on this file. If you need further isolation you can build a nodejs docker with said file, run that docker so it's isolated from your actual host.