3

Is it possible to define a NodeJs function which evaluates dynamic nodejs code?

Here is the context:

The user creates custom javascript function which should return true / false.

I need to "evaluate" the user code in a AWS Lambda container, which runs on NodeJs.

Is it possible?

Should i use something similar to javascript eval function?

EDIT

Here is what i tried

'use strict';

exports.handler = (event, context, callback) => {
    var body = "function test() { return 10; };";
    console.log("body", body);

    eval(body);

    var result = test();

    callback(null, result);
};

And i get an error saying "test" is not defined, therefore eval was not evaluated properly.

START RequestId: 6e9abd93-bd69-11e7-a43f-c75328d778e1 Version: $LATEST
2017-10-30T11:56:58.569Z    6e9abd93-bd69-11e7-a43f-c75328d778e1    body function test() { return 10; };
2017-10-30T11:56:58.581Z    6e9abd93-bd69-11e7-a43f-c75328d778e1    ReferenceError: test is not defined
    at exports.handler (/var/task/index.js:11:18)
END RequestId: 6e9abd93-bd69-11e7-a43f-c75328d778e1
REPORT RequestId: 6e9abd93-bd69-11e7-a43f-c75328d778e1  Duration: 32.78 ms  Billed Duration: 100 ms     Memory Size: 128 MB Max Memory Used: 19 MB  
RequestId: 6e9abd93-bd69-11e7-a43f-c75328d778e1 Process exited before completing request
2
  • Good question, I am contemplating the architectures of a system that needs to run customized user code and it’s good to know that Lambda is an option. Commented Nov 22, 2017 at 4:28
  • @MatthewJamesBriggs I find Lambda as a very good solution for this type of arhitecture. Commented Nov 22, 2017 at 14:17

2 Answers 2

1

eval works fine in Lambda. Remove the 'use strict' and it will work fine, outputting 10.

strict mode doesn't allow creating global variables, that's why you're getting the error.

A second option is to explicity add the function to the global context:

'use strict';

exports.handler = (event, context, callback) => {
    var body = "global.test = function() { return 10; };";
    console.log("body", body);

    eval(body);

    var result = test();

    callback(null, result);
};
Sign up to request clarification or add additional context in comments.

Comments

0

Node is just a runtime to execute javascript on the server side.You can write any valid Javascript code inside Node (Lambda).

As I could see in code 'eval' is not executing can you please try by installing the package 'npm install eval' - https://www.npmjs.com/package/eval

2 Comments

@Catalin Then perhaps you should post the actual error or issue you are experiencing if it isn't working for you.
Just to verify- can you please try installing the npm package 'npm install eval' and check. npmjs.com/package/eval

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.