1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Fix Function node

accidental leakage of in-progress work
This commit is contained in:
Nick O'Leary 2014-04-02 23:56:46 +01:00
parent bcefa6c9ef
commit 214338eb62

View File

@ -14,59 +14,58 @@
* limitations under the License.
**/
module.exports = function(RED) {
var RED = require(process.env.NODE_RED_HOME+"/red/red");
var util = require("util");
var vm = require("vm");
var fs = require('fs');
var fspath = require('path');
var util = require("util");
var vm = require("vm");
var fs = require('fs');
var fspath = require('path');
function FunctionNode(n) {
RED.nodes.createNode(this,n);
this.name = n.name;
this.func = n.func;
var functionText = "var results = (function(msg){"+this.func+"\n})(msg);";
this.topic = n.topic;
this.context = {global:RED.settings.functionGlobalContext || {}};
try {
this.script = vm.createScript(functionText);
this.on("input", function(msg) {
if (msg != null) {
var sandbox = {msg:msg,console:console,util:util,Buffer:Buffer,context:this.context};
try {
this.script.runInNewContext(sandbox);
var results = sandbox.results;
function FunctionNode(n) {
RED.nodes.createNode(this,n);
this.name = n.name;
this.func = n.func;
var functionText = "var results = (function(msg){"+this.func+"\n})(msg);";
this.topic = n.topic;
this.context = {global:RED.settings.functionGlobalContext || {}};
try {
this.script = vm.createScript(functionText);
this.on("input", function(msg) {
if (msg != null) {
var sandbox = {msg:msg,console:console,util:util,Buffer:Buffer,context:this.context};
try {
this.script.runInNewContext(sandbox);
var results = sandbox.results;
if (results == null) {
results = [];
} else if (results.length == null) {
results = [results];
}
if (msg._topic) {
for (var m in results) {
if (results[m]) {
if (util.isArray(results[m])) {
for (var n in results[m]) {
results[m][n]._topic = msg._topic;
}
} else {
results[m]._topic = msg._topic;
if (results == null) {
results = [];
} else if (results.length == null) {
results = [results];
}
if (msg._topic) {
for (var m in results) {
if (results[m]) {
if (util.isArray(results[m])) {
for (var n in results[m]) {
results[m][n]._topic = msg._topic;
}
} else {
results[m]._topic = msg._topic;
}
}
}
this.send(results);
} catch(err) {
this.error(err.message);
}
}
});
} catch(err) {
this.error(err.message);
}
}
this.send(results);
RED.nodes.registerType("function",FunctionNode);
RED.library.register("functions");
} catch(err) {
this.error(err.message);
}
}
});
} catch(err) {
this.error(err.message);
}
}
RED.nodes.registerType("function",FunctionNode);
RED.library.register("functions");