From 5b34702ac498e7a29bcf4f7c4e83723b473a81d3 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Wed, 24 Oct 2018 15:08:42 +0100 Subject: [PATCH] Add Tail node as separate node package - adds windows support --- storage/tail/28-tail.html | 63 +++++++++++++++++++++++++ storage/tail/28-tail.js | 60 +++++++++++++++++++++++ storage/tail/LICENSE | 14 ++++++ storage/tail/README.md | 17 +++++++ storage/tail/locales/en-US/28-tail.json | 20 ++++++++ storage/tail/package.json | 27 +++++++++++ 6 files changed, 201 insertions(+) create mode 100644 storage/tail/28-tail.html create mode 100644 storage/tail/28-tail.js create mode 100644 storage/tail/LICENSE create mode 100644 storage/tail/README.md create mode 100644 storage/tail/locales/en-US/28-tail.json create mode 100644 storage/tail/package.json diff --git a/storage/tail/28-tail.html b/storage/tail/28-tail.html new file mode 100644 index 00000000..9910de31 --- /dev/null +++ b/storage/tail/28-tail.html @@ -0,0 +1,63 @@ + + + + + + diff --git a/storage/tail/28-tail.js b/storage/tail/28-tail.js new file mode 100644 index 00000000..4fde885d --- /dev/null +++ b/storage/tail/28-tail.js @@ -0,0 +1,60 @@ + +module.exports = function(RED) { + "use strict"; + var fs = require('fs'); + var Tail = require('tail').Tail; + + function TailNode(n) { + RED.nodes.createNode(this,n); + + this.filename = n.filename; + this.filetype = n.filetype || "text"; + this.split = new RegExp(n.split || "[\r]{0,1}\n"); + var node = this; + + var fileTail = function() { + if (fs.existsSync(node.filename)) { + if (node.filetype === "text") { + node.tail = new Tail(node.filename,{separator:node.split, flushAtEOF:true}); + } + else { + node.tail = new Tail(node.filename,{separator:null, flushAtEOF:true, encoding:"binary"}); + } + + node.tail.on("line", function(data) { + if (data.length > 0) { + var msg = { topic:node.filename }; + if (node.filetype === "text") { + msg.payload = data.toString(); + node.send(msg); + } + else { + msg.payload = Buffer.from(data,"binary"); + //msg.payload = data; + node.send(msg); + } + } + }); + + node.tail.on("error", function(err) { + node.error(err.toString()); + }); + } + else { + node.tout = setTimeout(function() { fileTail(); },10000); + node.warn(RED._("tail.errors.filenotfound") + node.filename); + } + } + + fileTail(); + + node.on("close", function() { + /* istanbul ignore else */ + if (node.tail) { node.tail.unwatch(); } + delete node.tail; + if (node.tout) { clearTimeout(node.tout); } + }); + } + + RED.nodes.registerType("tail",TailNode); +} diff --git a/storage/tail/LICENSE b/storage/tail/LICENSE new file mode 100644 index 00000000..f5b60114 --- /dev/null +++ b/storage/tail/LICENSE @@ -0,0 +1,14 @@ +Copyright 2016 JS Foundation and other contributors, https://js.foundation/ +Copyright 2013-2016 IBM Corp. + +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. diff --git a/storage/tail/README.md b/storage/tail/README.md new file mode 100644 index 00000000..e6c9a935 --- /dev/null +++ b/storage/tail/README.md @@ -0,0 +1,17 @@ +node-red-node-tail +================== + +A Node-Red node to tail a file and inject the contents into the flow. + +Install +------- + +Either use the Menu - Manage palette option, or run the following command in your Node-RED user directory - typically `~/.node-red` + + npm install node-red-node-tail + + +Usage +----- + +Allows diff --git a/storage/tail/locales/en-US/28-tail.json b/storage/tail/locales/en-US/28-tail.json new file mode 100644 index 00000000..dd2da852 --- /dev/null +++ b/storage/tail/locales/en-US/28-tail.json @@ -0,0 +1,20 @@ +{ + "tail": { + "tail": "tail", + "label": { + "filename": "Filename", + "type": "File type", + "splitlines": "Split on", + "name": "Name", + "regex": "split character or regex" + }, + "action": { + "text": "Text - returns String", + "binary": "Binary - returns Buffer" + }, + "errors": { + "windowsnotsupport": "Not currently supported on Windows.", + "filenotfound": "File not found" + } + } +} diff --git a/storage/tail/package.json b/storage/tail/package.json new file mode 100644 index 00000000..c3f7ddba --- /dev/null +++ b/storage/tail/package.json @@ -0,0 +1,27 @@ +{ + "name": "node-red-node-tail", + "version": "0.0.1", + "description": "A node to tail files for Node-RED", + "dependencies": { + "tail": "^2.0.0" + }, + "repository": { + "type": "git", + "url": "https://github.com/node-red/node-red-nodes/storage/tail/" + }, + "license": "Apache-2.0", + "keywords": [ + "node-red", + "tail" + ], + "node-red": { + "nodes": { + "tail": "28-tail.js" + } + }, + "author": { + "name": "Dave Conway-Jones", + "email": "ceejay@vnet.ibm.com", + "url": "http://nodered.org" + } +}