forked from nacholibre/node-readlines
-
Notifications
You must be signed in to change notification settings - Fork 1
/
readlines.js
145 lines (106 loc) · 3.26 KB
/
readlines.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
142
143
144
145
'use strict';
var fs = require('fs');
function LineByLine(file, options) {
options = options || {};
if (!options.readChunk) {
options.readChunk = 1024;
}
if (!options.newLineCharacter) {
options.newLineCharacter = 0x0a; //linux line ending
} else {
options.newLineCharacter = options.newLineCharacter.charCodeAt(0);
}
this.options = options;
this.bufferData = null;
this.bytesRead = 0;
this.bufferPosition = 0;
this.eofReached = false;
if (typeof file === 'number') {
this.fd = file;
} else {
this.fd = fs.openSync(file, 'r');
}
this.line = '';
this.linesCache = [];
this.newLineCharacter = options.newLineCharacter;
this.lastBytePosition = null;
this.fdPosition = 0;
}
LineByLine.prototype._extractLines = function(buffer) {
var line;
var lines = [];
var bufferPosition = 0;
var lastNewLineBufferPosition = 0;
while (true) {
var bufferPositionValue = buffer[bufferPosition++];
if (bufferPositionValue === this.newLineCharacter) {
line = buffer.slice(lastNewLineBufferPosition, bufferPosition);
lines.push(line);
lastNewLineBufferPosition = bufferPosition;
} else if (!bufferPositionValue) {
break;
}
}
var leftovers = buffer.slice(lastNewLineBufferPosition, bufferPosition);
if (leftovers.length) {
lines.push(leftovers);
}
return lines;
};
LineByLine.prototype._readChunk = function(lineLeftovers) {
var bufferData = new Buffer(this.options.readChunk);
if(this.fd){
var bytesRead = fs.readSync(this.fd, bufferData, 0, this.options.readChunk, this.fdPosition);
this.fdPosition = this.fdPosition + bytesRead;
if (bytesRead < this.options.readChunk) {
this.eofReached = true;
bufferData = bufferData.slice(0, bytesRead);
}
if (bytesRead) {
this.linesCache = this._extractLines(bufferData);
if (lineLeftovers) {
this.linesCache[0] = Buffer.concat([lineLeftovers, this.linesCache[0]]);
}
}
return bytesRead;
}
return this.fd;
};
LineByLine.prototype.reset = function() {
this.eofReached = false;
this.fdPosition = 0;
return this.fdPosition;
};
LineByLine.prototype.next = function() {
var line = false;
if (this.eofReached && this.linesCache.length === 0) {
return line;
}
var bytesRead;
if (!this.linesCache.length) {
bytesRead = this._readChunk();
}
if (this.linesCache.length) {
line = this.linesCache.shift();
var lastLineCharacter = line[line.length-1];
if (lastLineCharacter !== 0x0a) {
bytesRead = this._readChunk(line);
if (bytesRead) {
line = this.linesCache.shift();
}
}
}
if (this.eofReached && this.linesCache.length === 0) {
this.close();
}
if (line && line[line.length-1] === this.newLineCharacter) {
line = line.slice(0, line.length-1);
}
return line;
};
LineByLine.prototype.close = function() {
fs.closeSync(this.fd);
this.fd = null;
return this.fd;
};
module.exports = LineByLine;