Commit 7648ccbe authored by Leo Iannacone's avatar Leo Iannacone

added tail module

parent 476e0180
#tail
To install:
```bash
npm install tail
```
#Use:
```javascript
Tail = require('tail').Tail;
tail = new Tail("fileToTail");
tail.on("line", function(data) {
console.log(data);
});
````
Tail accepts the line separator as second parameter. If nothing is passed it is defaulted to new line '\n'.
```javascript
var lineSeparator= "-";
new Tail("fileToTail",lineSeparator)
```
Tail emits two type of events:
* line
```
function(data){}
```
* error
```
function(exception){}
```
If you simply want to stop the tail:
```javascript
tail.unwatch()
```
And to start watching again:
```javascript
tail.watch()
```
#Want to fork ?
Tail is written in [CoffeeScript](http://jashkenas.github.com/coffee-script/).
The Cakefile generates the javascript that is then published to npm.
#License
MIT. Please see License file for more details.
{
"author": {
"name": "Luca Grulla"
},
"contributors": [
{
"name": "Luca Grulla"
},
{
"name": "Tom Hall"
},
{
"name": "Andy Kent"
}
],
"name": "tail",
"description": "tail a file in node",
"version": "0.3.5",
"repository": {
"type": "git",
"url": "git://github.com/lucagrulla/node-tail.git"
},
"main": "tail",
"engines": {
"node": ">= 0.4.0"
},
"dependencies": {},
"devDependencies": {
"coffee-script": "1.6.2"
},
"readme": "#tail\n\nTo install:\n\n```bash\nnpm install tail\n```\n\n#Use:\n```javascript\nTail = require('tail').Tail;\n\ntail = new Tail(\"fileToTail\");\n\ntail.on(\"line\", function(data) {\n console.log(data);\n});\n````\n\nTail accepts the line separator as second parameter. If nothing is passed it is defaulted to new line '\\n'.\n\n```javascript\n\nvar lineSeparator= \"-\";\n\nnew Tail(\"fileToTail\",lineSeparator)\n```\n\nTail emits two type of events:\n\n* line \n```\nfunction(data){}\n```\n* error\n```\nfunction(exception){}\n```\n\nIf you simply want to stop the tail:\n\n```javascript\ntail.unwatch()\n```\n\nAnd to start watching again:\n```javascript\ntail.watch()\n```\n\n#Want to fork ?\n\nTail is written in [CoffeeScript](http://jashkenas.github.com/coffee-script/).\n\nThe Cakefile generates the javascript that is then published to npm.\n\n#License\nMIT. Please see License file for more details.\n",
"readmeFilename": "README.md",
"bugs": {
"url": "https://github.com/lucagrulla/node-tail/issues"
},
"_id": "tail@0.3.5",
"_from": "tail@*"
}
// Generated by CoffeeScript 1.6.2
var Tail, environment, events, fs,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
events = require("events");
fs = require('fs');
environment = process.env['NODE_ENV'] || 'development';
Tail = (function(_super) {
__extends(Tail, _super);
Tail.prototype.readBlock = function() {
var block, stream,
_this = this;
if (this.queue.length >= 1) {
block = this.queue.shift();
if (block.end > block.start) {
stream = fs.createReadStream(this.filename, {
start: block.start,
end: block.end - 1,
encoding: "utf-8"
});
stream.on('error', function(error) {
console.log("Tail error:" + error);
return _this.emit('error', error);
});
stream.on('end', function() {
if (_this.queue.length >= 1) {
return _this.internalDispatcher.emit("next");
}
});
return stream.on('data', function(data) {
var chunk, parts, _i, _len, _results;
_this.buffer += data;
parts = _this.buffer.split(_this.separator);
_this.buffer = parts.pop();
_results = [];
for (_i = 0, _len = parts.length; _i < _len; _i++) {
chunk = parts[_i];
_results.push(_this.emit("line", chunk));
}
return _results;
});
}
}
};
function Tail(filename, separator, fsWatchOptions) {
var stats,
_this = this;
this.filename = filename;
this.separator = separator != null ? separator : '\n';
this.fsWatchOptions = fsWatchOptions != null ? fsWatchOptions : {};
this.readBlock = __bind(this.readBlock, this);
this.buffer = '';
this.internalDispatcher = new events.EventEmitter();
this.queue = [];
this.isWatching = false;
stats = fs.statSync(this.filename);
this.pos = stats.size;
this.internalDispatcher.on('next', function() {
return _this.readBlock();
});
this.watch();
}
Tail.prototype.watch = function() {
var _this = this;
if (this.isWatching) {
return;
}
this.isWatching = true;
if (fs.watch) {
return this.watcher = fs.watch(this.filename, this.fsWatchOptions, function(e) {
return _this.watchEvent(e);
});
} else {
return fs.watchFile(this.filename, this.fsWatchOptions, function(curr, prev) {
return _this.watchFileEvent(curr, prev);
});
}
};
Tail.prototype.watchEvent = function(e) {
var _this = this;
if (e === 'change') {
return fs.stat(this.filename, function(err, stats) {
if (err) {
_this.emit('error', err);
}
if (stats.size < _this.pos) {
_this.pos = stats.size;
}
if (stats.size > _this.pos) {
_this.queue.push({
start: _this.pos,
end: stats.size
});
_this.pos = stats.size;
if (_this.queue.length === 1) {
return _this.internalDispatcher.emit("next");
}
}
});
} else if (e === 'rename') {
this.unwatch();
return setTimeout((function() {
return _this.watch();
}), 1000);
}
};
Tail.prototype.watchFileEvent = function(curr, prev) {
if (curr.size > prev.size) {
this.queue.push({
start: prev.size,
end: curr.size
});
if (this.queue.length === 1) {
return this.internalDispatcher.emit("next");
}
}
};
Tail.prototype.unwatch = function() {
if (fs.watch && this.watcher) {
this.watcher.close();
this.pos = 0;
} else {
fs.unwatchFile(this.filename);
}
this.isWatching = false;
return this.queue = [];
};
return Tail;
})(events.EventEmitter);
exports.Tail = Tail;
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment