1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00
node-red-nodes/storage/tail/28-tail.js
Dave Conway-Jones 55698e637d
Update tail node to accept/display regex.
To address Issue #555
2019-07-12 12:22:13 +01:00

61 lines
2.0 KiB
JavaScript

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.replace(/\\r/g,'\r').replace(/\\n/g,'\n').replace(/\\t/g,'\t') || "[\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);
}