[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / core_nodes / core / 80-function.js
1 /**
2  * Copyright 2013 IBM Corp.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  **/
16
17 module.exports = function(RED) {
18     "use strict";
19     var util = require("util");
20     var vm = require("vm");
21
22     function FunctionNode(n) {
23         RED.nodes.createNode(this,n);
24         this.name = n.name;
25         this.func = n.func;
26         var functionText = "var results = null; results = (function(msg){"+this.func+"\n})(msg);";
27         this.topic = n.topic;
28         var sandbox = {
29             console:console,
30             util:util,
31             Buffer:Buffer,
32             context: {
33                 global:RED.settings.functionGlobalContext || {}
34             }
35         };
36         var context = vm.createContext(sandbox);
37         try {
38             this.script = vm.createScript(functionText);
39             this.on("input", function(msg) {
40                 try {
41                     var start = process.hrtime();
42                     context.msg = msg;
43                     this.script.runInContext(context);
44                     var results = context.results;
45                     if (results == null) {
46                         results = [];
47                     } else if (results.length == null) {
48                         results = [results];
49                     }
50                     if (msg._topic) {
51                         for (var m in results) {
52                             if (results[m]) {
53                                 if (util.isArray(results[m])) {
54                                     for (var n=0; n < results[m].length; n++) {
55                                         results[m][n]._topic = msg._topic;
56                                     }
57                                 } else {
58                                     results[m]._topic = msg._topic;
59                                 }
60                             }
61                         }
62                     }
63                     this.send(results);
64                     var duration = process.hrtime(start);
65                     if (process.env.NODE_RED_FUNCTION_TIME) {
66                         this.status({fill:"yellow",shape:"dot",text:""+Math.floor((duration[0]* 1e9 +  duration[1])/10000)/100});
67                     }
68                 } catch(err) {
69                     this.error(err.toString());
70                 }
71             });
72         } catch(err) {
73             this.error(err);
74         }
75     }
76
77     RED.nodes.registerType("function",FunctionNode);
78     RED.library.register("functions");
79 }