-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfileLines.js
60 lines (52 loc) · 1.77 KB
/
fileLines.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
var util = require('util');
var Transform = require('stream').Transform;
util.inherits(FileLines, Transform);
/**
* Stream file by line breaks
*
* Implements a Node Transform Stream interface, so should be used like:
* var fileLineStream = fs.createReadStream(filename).pipe(new FileLines());
*
* @param {Object} options Set maxInBoundBuffer if desired, to change it from 10 MB default
*/
function FileLines(options) {
if (!(this instanceof FileLines)) return new FileLines(options);
Transform.call(this, { objectMode: true });
if (typeof options === 'undefined') {
options = {};
}
if (typeof options.maxInboundBuffer == 'undefined') {
options.maxInboundBuffer = 1024*10*10;
}
this._inBuffer = new Buffer(options.maxInboundBuffer);
this._inCursor = 0;
}
FileLines.prototype._transform = function(chunk, encoding, callback) {
if (chunk.length + this._inCursor > this._inBuffer.length) {
this.emit('error', new Error('Line parsing exceeded the internal receiving buffer'));
return;
}
chunk.copy(this._inBuffer, this._inCursor);
this._inCursor += chunk.length;
var i = 0;
var lastFound = 0
while (i < this._inCursor) {
//console.log(i, this._inBuffer[i], String.fromCharCode(this._inBuffer[i]));
if (this._inBuffer[i] == 10 || this._inBuffer[i] == 13) {
this.push(this._inBuffer.toString('utf8', lastFound, i));
lastFound = i+1;
}
i++;
}
// Parsed as much as possible, move data and cursor
this._inBuffer.copy(this._inBuffer, 0, lastFound, this._inCursor);
this._inCursor -= lastFound;
callback(); // Done with this chunk
};
FileLines.prototype._flush = function(callback) {
if (this._inCursor > 0) {
this.push(this._inBuffer.toString('utf8', 0, this._inCursor));
}
callback();
};
module.exports = FileLines;