-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
49 lines (44 loc) · 1.39 KB
/
utils.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
//
// Miscellaneous Utilities
//
// Various utilities to support other classes.
//
const Transform = require('stream').Transform
const Writeable = require('stream').Writable
class NewLineCounterStream extends Writeable {
constructor(options){
super(options)
this.numberLines = 0
}
_write(chunk, encoding, callback){
this.numberLines += chunk.toString().match(/\n/g).length
callback()
}
}
class TailStream extends Transform {
constructor(startLine, options){
super(options)
this.currentLine = 0
this.startLine = startLine
}
_transform(chunk, encoding, callback) {
if (this.currentLine < this.startLine){
let chunksegments = chunk.toString().match(/(.*\n)/g)
if (this.currentLine + chunksegments.length < this.startLine){ this.currentLine += chunksegments.length }
else {
// Ensure we grab a partial last line if necessary.
let chunksegments = chunk.toString().match(/(.*\n)|(.+$)/g)
while (this.currentLine < this.startLine){
chunksegments.shift()
this.currentLine++
}
chunksegments.forEach( i => { this.push(i) } )
}
}
else {
this.push(chunk)
}
callback()
}
}
module.exports = { NewLineCounterStream, TailStream }