-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtail.js
executable file
·99 lines (91 loc) · 2.38 KB
/
tail.js
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
const chokidar = require('chokidar');
const EventEmitter = require('events');
const fs = require('fs');
const os = require('os');
const watcher = Symbol('watcher');
const fd = Symbol('fd');
function closeFile() {
if (this[td]) {
fs.close(this[fd], (err) => {
if (err) {
return;
}
this[td] = undefined;
});
}
}
class Tail extends EventEmitter {
constructor(filename, options) {
super();
this.filename = filename;
this.options = Object.assign(options || {}, {
alwaysStat: true,
ignoreInitial: false,
persistent: true,
});
this[watcher] = undefined;
this[fd] = undefined;
}
watch() {
let lastSize = 0;
this[watcher] = chokidar.watch(this.filename, this.options)
.on('add', (path, stats) => {
if (lastSize > 0) {
const diff = stats.size - lastSize;
if (diff <= 0) {
lastSize = stats.size;
return;
}
const buffer = Buffer.alloc(diff);
this[fd] = fs.openSync(path, 'r');
fs.read(this[fd], buffer, 0, diff, lastSize, (err) => {
if (err) {
return;
}
fs.closeSync(this[fd]);
buffer.toString().split(os.EOL).forEach((line, idx, ar) => {
if (idx < ar.length && line) {
this.emit('line', line);
}
});
});
lastSize = stats.size;
} else {
lastSize = stats.size;
}
})
.on('change', (path, stats) => {
const diff = stats.size - lastSize;
if (diff <= 0) {
lastSize = stats.size;
return;
}
const buffer = Buffer.alloc(diff);
this[fd] = fs.openSync(path, 'r');
fs.read(this[fd], buffer, 0, diff, lastSize, (err) => {
if (err) {
return;
}
fs.closeSync(this[fd]);
buffer.toString().split(os.EOL).forEach((line, idx, ar) => {
if (idx < ar.length && line) {
this.emit('line', line);
}
});
});
lastSize = stats.size;
})
.on('unlink', () => {
closeFile.bind(this);
});
}
close() {
if (this[watcher]) {
this[watcher].unwatch(this.filename);
this[watcher].close();
this[watcher] = undefined;
}
this.emit('close');
}
}
module.exports = Tail