├── LICENSE ├── README.md ├── docs ├── screenshot-flow.png └── screenshot-settings.png ├── file-function ├── file-function.html ├── file-function.js └── icons │ └── file-function-icon.png └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | Licensed under the Apache License, Version 2.0 (the "License"); 2 | you may not use this file except in compliance with the License. 3 | You may obtain a copy of the License at 4 | 5 | http://www.apache.org/licenses/LICENSE-2.0 6 | 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # node-red-contrib-file-function 2 | 3 | **This Node-RED node is just like the [core node "function"](http://nodered.org/docs/writing-functions.html), only that this node loads the script to be executed from an actual file on your drive.** 4 | 5 | This may help you developing for Node RED. Instead of having to write your Javascript code in that small textfield in your browser you can use your favorite editor/IDE. 6 | 7 | ![screenshot of settings](https://raw.githubusercontent.com/emiloberg/node-red-contrib-file-function/master/docs/screenshot-settings.png) 8 | 9 | 10 | 11 | ## Status 12 | What? | Status | What? | Status 13 | ------- | ------ | ------- | ------ 14 | Code Climate GPA | [![Code Climate](https://codeclimate.com/github/emiloberg/node-red-contrib-file-function/badges/gpa.svg)](https://codeclimate.com/github/emiloberg/node-red-contrib-file-function) | Licence | [![Licence](https://img.shields.io/npm/l/node-red-contrib-file-function.svg)](https://github.com/emiloberg/node-red-contrib-file-function/blob/master/LICENSE) 15 | Codacy | [![Codacy Badge](https://www.codacy.com/project/badge/f51ca088d01f4af6b83ed2e2529b51dd)](https://www.codacy.com/public/emiloberg/node-red-contrib-file-function) | Tag | [![Tag](https://img.shields.io/github/tag/emiloberg/node-red-contrib-file-function.svg)](https://github.com/emiloberg/node-red-contrib-file-function/tags) 16 | Issues | [![Issues](https://img.shields.io/github/issues/emiloberg/node-red-contrib-file-function.svg)](https://github.com/emiloberg/node-red-contrib-file-function/issues) | GitHub Forks | [![Forks](https://img.shields.io/github/forks/emiloberg/node-red-contrib-file-function.svg)](https://github.com/emiloberg/node-red-contrib-file-function/network) 17 | GitHub Version | [![GitHub version](https://badge.fury.io/gh/emiloberg%2Fnode-red-contrib-file-function.svg)](http://badge.fury.io/gh/emiloberg%2Fnode-red-contrib-file-function) | GitHub Followers | [![Followers](https://img.shields.io/github/followers/emiloberg.svg)](https://github.com/emiloberg/followers) 18 | NPM Version | [![npm version](https://badge.fury.io/js/node-red-contrib-file-function.svg)](http://badge.fury.io/js/node-red-contrib-file-function) | Dependencies | ![Dependencies](https://david-dm.org/emiloberg/node-red-contrib-file-function.svg) 19 | 20 | 21 | ## Filename 22 | The file path will be relative from the path set in _settings.userDir_, or if not set from the Node-RED install directory. 23 | 24 | Either set the filename in the configuration dialog of the node, or override it by the `msg.filename` property of the incoming message. 25 | 26 | ## Cache 27 | By checking the _"Reread file from disk every time node is invoked?"_ checkbox the file will be read every time the node is called, so there's no need to redeploy or restart Node-RED. If the checkbox is unchecked, the file will be read on deploy/start. 28 | 29 | If the checkbox is set to only read the file once (when flow is deployed/Node-RED is started) but another filename is sent in msg.filename, it will read the file from disk and cache it anyways. Only the last called file will be cached. If you're alternating between two different files you're better of creating two different nodes if you're looking for perfomance. 30 | 31 | Unless you're working with functions called __very__ often or with __very__ large functions you can probably just leave it to reload the file every time it's invoked. 32 | 33 | 34 | ## Writing functions 35 | 36 | Writing functions in this node works works just like functions in the the original function node (except that you write it in an actual file and no in an input field): 37 | 38 | > The message is passed in as a JavaScript object called msg. 39 | > 40 | > By convention it will have a `msg.payload` property containing the body of the message. 41 | > 42 | > The function should return the messages it wants to pass on to the next nodes in the flow. It can return: 43 | > 44 | > * a single message object - passed to nodes connected to the first output 45 | > * an array of message objects - passed to nodes connected to the corresponding outputs 46 | > 47 | > If any element of the array is itself an array of messages, multiple messages are sent to the corresponding output. 48 | > 49 | > If null is returned, either by itself or as an element of the array, no message is passed on. 50 | > 51 | > See the [online documentation](http://nodered.org/docs/writing-functions.html) for more help. 52 | 53 | 54 | ## Sample 55 | Create a file called `sample-file-function.js` in your Node-RED root folder. Add the following content to that file: 56 | 57 | ```javascript 58 | var reversedPayload = msg.payload.split("").reverse().join(""); 59 | 60 | return { 61 | payload: 'This is the input payload, but reversed: ' + reversedPayload 62 | }; 63 | ``` 64 | 65 | ![screenshot of sample flow](https://raw.githubusercontent.com/emiloberg/node-red-contrib-file-function/master/docs/screenshot-flow.png) 66 | 67 | Import this flow (or add it manually by creating a simple [inject] > [file function] > [debug] flow yourself. Add the value `sample-file-function.js` to the _filename_ field in the _file function_ node): 68 | 69 | ```javascript 70 | [{"id":"62efe026.9d102","type":"inject","name":"Inject","topic":"this is topic from the function","payload":"this data is feeded to the function","payloadType":"string","repeat":"","crontab":"","once":false,"x":303,"y":119,"z":"dd1ad5c3.22e528","wires":[["fd11ceda.02ee3"]]},{"id":"fd11ceda.02ee3","type":"file function","name":"","filename":"sample-file-function.js","outputs":"1","x":508,"y":119,"z":"dd1ad5c3.22e528","wires":[["7e85f5db.817a0c"]]},{"id":"7e85f5db.817a0c","type":"debug","name":"","active":true,"console":"false","complete":"true","x":723,"y":118,"z":"dd1ad5c3.22e528","wires":[]}] 71 | ``` 72 | 73 | Deploy the changes and click the inject node - check the output in the debug sidebar! 74 | -------------------------------------------------------------------------------- /docs/screenshot-flow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/emiloberg/node-red-contrib-file-function/07f80cd2821822df87b29b8d58f3b399e71ed086/docs/screenshot-flow.png -------------------------------------------------------------------------------- /docs/screenshot-settings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/emiloberg/node-red-contrib-file-function/07f80cd2821822df87b29b8d58f3b399e71ed086/docs/screenshot-settings.png -------------------------------------------------------------------------------- /file-function/file-function.html: -------------------------------------------------------------------------------- 1 | 14 | 15 | 38 | 39 | 62 | 63 | 89 | -------------------------------------------------------------------------------- /file-function/file-function.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | **/ 15 | 16 | module.exports = function(RED) { 17 | "use strict"; 18 | var util = require("util"); 19 | var vm = require("vm"); 20 | var fs = require("fs"); 21 | 22 | function FunctionNode(n) { 23 | RED.nodes.createNode(this, n); 24 | 25 | this.filename = n.filename || ""; 26 | this.loadedScript = ''; 27 | this.loadedFilename = ''; 28 | 29 | var node = this; 30 | 31 | // Read and file when node is initialized, 32 | // if user didn't check the "reload file every time"-checkbox, we'll be using 33 | // this when node is invoked. 34 | if (this.filename !== '') { 35 | node.loadedFilename = this.filename; 36 | fs.readFile(this.filename, {encoding: 'utf-8'}, function (err, fileContent) { 37 | if (err) { 38 | if (err.code === 'ENOENT') { 39 | node.warn('Could not find file "' + err.path + '". Hint: File path is relative to "' + process.env.PWD + '"'); 40 | } else { 41 | node.warn(err); 42 | } 43 | } else { 44 | node.loadedScript = fileContent; 45 | } 46 | }); 47 | } 48 | 49 | 50 | // On invocation 51 | this.on("input", function (msg) { 52 | var filename = msg.filename || this.filename; 53 | 54 | if (filename === '') { 55 | node.warn('No filename specified'); 56 | } else if (n.reloadfile === false && filename === node.loadedFilename && node.loadedScript !== ''){ // Run script from "cache" 57 | runScript(node, msg, node.loadedScript); 58 | } else { // Read script from disk and run 59 | fs.readFile(filename, {encoding: 'utf-8'}, function (err, fileContent) { 60 | if (err) { 61 | if (err.code === 'ENOENT') { 62 | node.warn('Could not find file "' + err.path + '". Hint: File path is relative to "' + process.env.PWD + '"'); 63 | } else { 64 | node.warn(err); 65 | } 66 | msg.error = err; 67 | } else { 68 | node.loadedScript = fileContent; 69 | node.loadedFilename = filename; 70 | runScript(node, msg, fileContent); 71 | } 72 | }); 73 | } 74 | }); 75 | } 76 | 77 | 78 | function runScript(node, msg, script) { 79 | var functionText = "var results = null; results = (function(msg){"+script+"\n})(msg);"; 80 | 81 | var sandbox = { 82 | console:console, 83 | util:util, 84 | Buffer:Buffer, 85 | context: { 86 | global:RED.settings.functionGlobalContext || {} 87 | } 88 | }; 89 | 90 | var context = vm.createContext(sandbox); 91 | var vmScript = vm.createScript(functionText); 92 | 93 | try { 94 | var start = process.hrtime(); 95 | context.msg = msg; 96 | vmScript.runInContext(context); 97 | var results = context.results; 98 | if (results == null) { 99 | results = []; 100 | } else if (results.length == null) { 101 | results = [results]; 102 | } 103 | 104 | if (msg.topic) { 105 | for (var m in results) { 106 | if (results[m]) { 107 | if (util.isArray(results[m])) { 108 | for (var n=0; n < results[m].length; n++) { 109 | results[m][n].topic = msg.topic; 110 | } 111 | } else { 112 | results[m].topic = msg.topic; 113 | } 114 | } 115 | } 116 | } 117 | 118 | node.send(results); 119 | var duration = process.hrtime(start); 120 | if (process.env.NODE_RED_FUNCTION_TIME) { 121 | this.status({fill:"yellow",shape:"dot",text:""+Math.floor((duration[0]* 1e9 + duration[1])/10000)/100}); 122 | } 123 | 124 | } catch(err) { 125 | node.warn(err); 126 | } 127 | } 128 | 129 | RED.nodes.registerType("file function",FunctionNode); 130 | RED.library.register("functions"); 131 | }; 132 | -------------------------------------------------------------------------------- /file-function/icons/file-function-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/emiloberg/node-red-contrib-file-function/07f80cd2821822df87b29b8d58f3b399e71ed086/file-function/icons/file-function-icon.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-red-contrib-file-function", 3 | "version": "1.1.2", 4 | "description": "Read function javascript from file. This way you can use your favorite editor/IDE to develop Node-RED functions", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/emiloberg/node-red-contrib-file-function.git" 8 | }, 9 | "keywords": [ 10 | "node-red", 11 | "node-red-contrib", 12 | "contrib", 13 | "function", 14 | "development", 15 | "file" 16 | ], 17 | "node-red": { 18 | "nodes": { 19 | "file-function": "file-function/file-function.js" 20 | } 21 | }, 22 | "author": "Emil Oberg", 23 | "license": "Apache", 24 | "bugs": { 25 | "url": "https://github.com/emiloberg/node-red-contrib-file-function/issues" 26 | }, 27 | "homepage": "https://github.com/emiloberg/node-red-contrib-file-function" 28 | } 29 | --------------------------------------------------------------------------------