1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// 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, frombeginning) {
var stats,
_this = this;
this.filename = filename;
this.separator = separator != null ? separator : '\n';
this.fsWatchOptions = fsWatchOptions != null ? fsWatchOptions : {};
this.frombeginning = frombeginning != null ? frombeginning : false;
this.readBlock = __bind(this.readBlock, this);
this.buffer = '';
this.internalDispatcher = new events.EventEmitter();
this.queue = [];
this.isWatching = false;
stats = fs.statSync(this.filename);
this.internalDispatcher.on('next', function() {
return _this.readBlock();
});
this.pos = this.frombeginning ? 0 : stats.size;
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 stats,
_this = this;
if (e === 'change') {
stats = fs.statSync(this.filename);
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;