This repository was archived by the owner on Aug 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlog-reader.js
More file actions
55 lines (48 loc) · 1.26 KB
/
log-reader.js
File metadata and controls
55 lines (48 loc) · 1.26 KB
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
'use strict'
// credit: https://stackoverflow.com/questions/16010915/parsing-huge-logfiles-in-node-js-read-in-line-by-line
// example usage
// let reader = CSVReader('path_to_file.csv')
// reader.read(() => reader.continue())
const fs = require('fs'),
es = require('event-stream'),
parse = require("csv-parse");
class LogReader {
constructor(filename, columns) {
this.reader = fs.createReadStream(filename);
this.parseOptions = {
columns: columns,
skip_empty_lines: true
}
}
stop() {
this.reader.pause();
this.reader.close();
this.reader.destroy();
}
read(callback, errorCallback, onComplete) {
this.reader
.pipe(es.split())
.pipe(es.mapSync(line => {
this.reader.pause();
// parse data and push to line data
parse(line, this.parseOptions, (err, output) => {
if (err) {
errorCallback(err);
return
}
if (output && output[0]) {
callback(output[0])
}
});
})
.on('error', function () {
errorCallback('Error while reading file.')
})
.on('end', () => onComplete())
);
}
continue () {
this.reader.resume()
}
}
module.exports = LogReader