Skip to content

Commit

Permalink
Add Tail node as separate node package - adds windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Conway-Jones committed Oct 24, 2018
1 parent 425f5c1 commit 5b34702
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 0 deletions.
63 changes: 63 additions & 0 deletions storage/tail/28-tail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

<script type="text/x-red" data-template-name="tail">
<div class="form-row">
<label for="node-input-filename"><i class="fa fa-file"></i> <span data-i18n="tail.label.filename"></span></label>
<input id="node-input-filename" type="text">
</div>
<div class="form-row">
<label for="node-input-filetype"><i class="fa fa-file-text-o"></i> <span data-i18n="tail.label.type"></span></label>
<select type="text" id="node-input-filetype">
<option value="text" data-i18n="tail.action.text"></option>
<option value="binary" data-i18n="tail.action.binary"></option>
</select>
</div>
<div class="form-row" id="node-tail-split">
<!-- <label>&nbsp;</label>
<input type="checkbox" id="node-input-split" placeholder="Name" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-split" style="width: 70%;"><span data-i18n="tail.label.splitlines"></span></label> -->
<label for="node-input-split"><i class="fa fa-tag"></i> <span data-i18n="tail.label.splitlines"></span></label>
<input type="text" id="node-input-split" data-i18n="[placeholder]tail.label.regex">
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="tail.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]tail.label.name">
</div>
</script>

<script type="text/x-red" data-help-name="tail">
<p>Tails (watches for things to be added) to the configured file. (Linux/Mac ONLY)</p>
<p>This will not work on Windows filesystems, as it relies on the <b>tail -F</b> command.</p>
<h3>Outputs</h3>
<ul>
<li>Text (UTF-8) files will be returned as strings.</li>
<li>Binary files will be returned as Buffer objects.</li>
</ul>
</script>

<script type="text/javascript">
RED.nodes.registerType('tail',{
category: 'storage-input',
defaults: {
name: {value:""},
filetype: {value:"text"},
split: {value:"[\r]{0,1}\n"},
filename: {value:"",required:true}
},
color:"BurlyWood",
inputs:0,
outputs:1,
icon: "file.png",
label: function() {
return this.name||this.filename||this._("tail.tail");
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: function() {
$("#node-input-filetype").on("change",function() {
if (this.value === "text") { $("#node-tail-split").show(); }
else { $("#node-tail-split").hide(); }
});
}
});
</script>
60 changes: 60 additions & 0 deletions storage/tail/28-tail.js
Original file line number Diff line number Diff line change
@@ -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);
}
14 changes: 14 additions & 0 deletions storage/tail/LICENSE
Original file line number Diff line number Diff line change
@@ -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.
17 changes: 17 additions & 0 deletions storage/tail/README.md
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions storage/tail/locales/en-US/28-tail.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
27 changes: 27 additions & 0 deletions storage/tail/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}

0 comments on commit 5b34702

Please sign in to comment.