-
Notifications
You must be signed in to change notification settings - Fork 39
/
line-by-line.js
141 lines (109 loc) · 2.71 KB
/
line-by-line.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
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
/*
* Line By Line
*
* A NodeJS module that helps you reading large text files, line by line,
* without buffering the files into memory.
*
* Copyright (c) 2012 Markus von der Wehd <[email protected]>
* MIT License, see LICENSE.txt, see http://www.opensource.org/licenses/mit-license.php
*/
var path = require('path');
var fs = require('fs');
var events = require("events");
var LineByLineReader = function (filepath, options) {
var self = this;
this._filepath = path.normalize(filepath);
this._encoding = options && options.encoding || 'utf8';
this._skipEmptyLines = options && options.skipEmptyLines || false;
this._readStream = null;
this._lines = [];
this._lineFragment = '';
this._paused = false;
this._end = false;
events.EventEmitter.call(this);
process.nextTick(function () {
self._initStream();
});
};
LineByLineReader.prototype = Object.create(events.EventEmitter.prototype, {
constructor: {
value: LineByLineReader,
enumerable: false
}
});
LineByLineReader.prototype._initStream = function () {
var self = this,
readStream = fs.createReadStream(this._filepath, { encoding: this._encoding });
readStream.on('error', function (err) {
self.emit('error', err);
});
readStream.on('data', function (data) {
self._readStream.pause();
self._lines = self._lines.concat(data.split(/(?:\n|\r\n|\r)/g));
self._lines[0] = self._lineFragment + self._lines[0];
self._lineFragment = self._lines.pop() || '';
process.nextTick(function () {
self._nextLine();
});
});
readStream.on('end', function () {
self._end = true;
process.nextTick(function () {
self._nextLine();
});
});
this._readStream = readStream;
};
LineByLineReader.prototype._nextLine = function () {
var self = this,
line;
if (this._end && !!this._lineFragment) {
this.emit('line', this._lineFragment);
this._lineFragment = '';
if (!this._paused) {
process.nextTick(function () {
self.emit('end');
});
}
return;
}
if (this._end) {
this.emit('end');
return;
}
if (this._paused) {
return;
}
if (this._lines.length === 0) {
this._readStream.resume();
return;
}
line = this._lines.shift();
if (!this._skipEmptyLines || line.length > 0) {
this.emit('line', line);
}
if (!this._paused) {
process.nextTick(function () {
self._nextLine();
});
}
};
LineByLineReader.prototype.pause = function () {
this._paused = true;
};
LineByLineReader.prototype.resume = function () {
var self = this;
this._paused = false;
process.nextTick(function () {
self._nextLine();
});
};
LineByLineReader.prototype.close = function () {
var self = this;
this._readStream.destroy();
this._end = true;
process.nextTick(function () {
self._nextLine();
});
};
module.exports = LineByLineReader;