2013-09-05 16:02:48 +02:00
|
|
|
/**
|
2015-02-03 15:46:36 +01:00
|
|
|
* Copyright 2013,2015 IBM Corp.
|
2013-09-05 16:02:48 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
module.exports = function(RED) {
|
2014-07-08 13:27:09 +02:00
|
|
|
"use strict";
|
2014-05-04 00:32:04 +02:00
|
|
|
var util = require("util");
|
|
|
|
var vm = require("vm");
|
2014-05-05 09:58:53 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
function FunctionNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.name = n.name;
|
|
|
|
this.func = n.func;
|
2014-09-05 11:40:57 +02:00
|
|
|
var functionText = "var results = null; results = (function(msg){"+this.func+"\n})(msg);";
|
2014-05-04 00:32:04 +02:00
|
|
|
this.topic = n.topic;
|
2014-09-05 11:40:57 +02:00
|
|
|
var sandbox = {
|
|
|
|
console:console,
|
|
|
|
util:util,
|
|
|
|
Buffer:Buffer,
|
|
|
|
context: {
|
|
|
|
global:RED.settings.functionGlobalContext || {}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var context = vm.createContext(sandbox);
|
2014-05-04 00:32:04 +02:00
|
|
|
try {
|
|
|
|
this.script = vm.createScript(functionText);
|
|
|
|
this.on("input", function(msg) {
|
2014-09-08 22:10:06 +02:00
|
|
|
try {
|
|
|
|
var start = process.hrtime();
|
|
|
|
context.msg = msg;
|
|
|
|
this.script.runInContext(context);
|
|
|
|
var results = context.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=0; n < results[m].length; n++) {
|
|
|
|
results[m][n]._topic = msg._topic;
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
2014-09-08 22:10:06 +02:00
|
|
|
} else {
|
|
|
|
results[m]._topic = msg._topic;
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-08 22:10:06 +02:00
|
|
|
this.send(results);
|
2015-02-03 15:46:36 +01:00
|
|
|
if (RED.settings.metricsOn && (RED.settings.metricsOn === true)) {
|
|
|
|
var duration = process.hrtime(start);
|
|
|
|
var converted = Math.floor((duration[0]* 1e9 + duration[1])/10000)/100;
|
|
|
|
this.metric("duration", results, converted);
|
|
|
|
this.status({fill:"yellow",shape:"dot",text:""+converted});
|
2014-09-08 22:10:06 +02:00
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
this.error(err.toString());
|
2014-05-05 09:58:53 +02:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
} catch(err) {
|
|
|
|
this.error(err);
|
|
|
|
}
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
2014-05-05 09:58:53 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
RED.nodes.registerType("function",FunctionNode);
|
|
|
|
RED.library.register("functions");
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|