You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by mr...@apache.org on 2019/06/18 23:06:22 UTC
[incubator-openwhisk-runtime-nodejs] branch master updated: Global
scope eval. (#136)
This is an automated email from the ASF dual-hosted git repository.
mrutkowski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk-runtime-nodejs.git
The following commit(s) were added to refs/heads/master by this push:
new afc93ba Global scope eval. (#136)
afc93ba is described below
commit afc93ba231de2970df4fd7228cb5c04327addc10
Author: rodric rabbah <ro...@gmail.com>
AuthorDate: Tue Jun 18 19:06:17 2019 -0400
Global scope eval. (#136)
---
core/nodejsActionBase/runner.js | 16 ++++++++++++--
.../NodeJsActionContainerTests.scala | 25 ++++++++++++++++++++++
2 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/core/nodejsActionBase/runner.js b/core/nodejsActionBase/runner.js
index c65bcde..08bb07e 100644
--- a/core/nodejsActionBase/runner.js
+++ b/core/nodejsActionBase/runner.js
@@ -54,7 +54,7 @@ class NodeActionRunner {
// The module to require.
let whatToRequire = index !== undefined ? path.join(moduleDir, index) : moduleDir;
- this.userScriptMain = eval('require("' + whatToRequire + '").' + main);
+ this.userScriptMain = evalScript(main, whatToRequire)
assertMainIsFunction(this.userScriptMain, message.main);
// The value 'true' has no special meaning here; the successful state is
@@ -64,7 +64,7 @@ class NodeActionRunner {
.catch(error => Promise.reject(error));
} else try {
// The code is a plain old JS file.
- this.userScriptMain = eval('(function(){' + message.code + '\nreturn ' + message.main + '})()');
+ this.userScriptMain = evalScript(message.main, false, message.code)
assertMainIsFunction(this.userScriptMain, message.main);
return Promise.resolve(true); // See comment above about 'true'; it has no specific meaning.
@@ -169,4 +169,16 @@ function assertMainIsFunction(userScriptMain, main) {
}
}
+/**
+ * Evals the code to execute. This is a global function so that the eval is in the global context
+ * and hence functions which use variables without 'var' are permitted.
+ */
+function evalScript(main, whatToRequire, code) {
+ if (whatToRequire) {
+ return eval('require("' + whatToRequire + '").' + main);
+ } else {
+ return eval('(function(){' + code + '\nreturn ' + main + '})()');
+ }
+}
+
module.exports = NodeActionRunner;
diff --git a/tests/src/test/scala/runtime/actionContainers/NodeJsActionContainerTests.scala b/tests/src/test/scala/runtime/actionContainers/NodeJsActionContainerTests.scala
index 20a5fdb..f677882 100644
--- a/tests/src/test/scala/runtime/actionContainers/NodeJsActionContainerTests.scala
+++ b/tests/src/test/scala/runtime/actionContainers/NodeJsActionContainerTests.scala
@@ -266,6 +266,31 @@ abstract class NodeJsActionContainerTests extends BasicActionRunnerTests with Ws
}, 2)
}
+ it should "support variables with no var declaration" in {
+ val (out, err) = withNodeJsContainer { c =>
+ val code =
+ """
+ | function main(params) {
+ | greeting = 'hello, ' + params.payload + '!'
+ | console.log(greeting);
+ | return {payload: greeting}
+ | }
+ """.stripMargin
+
+ c.init(initPayload(code))._1 should be(200)
+
+ val (runCode, result) = c.run(runPayload(JsObject("payload" -> JsString("test"))))
+ runCode should be(200)
+ result should be(Some(JsObject("payload" -> JsString("hello, test!"))))
+ }
+
+ checkStreams(out, err, {
+ case (o, e) =>
+ o shouldBe "hello, test!"
+ e shouldBe empty
+ })
+ }
+
it should "error when requiring a non-existent package" in {
// NPM package names cannot start with a dot, and so there is no danger
// of the package below ever being valid.