-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathReadStreamLineEmitter.js
executable file
·64 lines (51 loc) · 1.6 KB
/
ReadStreamLineEmitter.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
/*jshint node:true, es5:true, devel:true, globalstrict:true, regexp:false*/
"use strict";
var EventEmitter = require('events').EventEmitter,
util = require('util');
/**
* Reads from a read-stream and emits <code>line</code> events.
*
* @param stream the read stream, e.g. process.stdin
* @param options
* @constructor
*/
function ReadStreamLineEmitter(stream, options) {
var incompleteLine,
self = this;
EventEmitter.call(this);
// prepare stream for character processing and listen to events
stream.setEncoding(options.encoding);
function processData(data) {
var lastLineIncomplete, lines;
if (incompleteLine) {
// append data to not yet emitted, incomplete line
data = incompleteLine + data;
incompleteLine = null;
}
lastLineIncomplete = data && data.charAt(data.length - 1) !== '\n';
lines = data.split(/\n/gm);
if (lastLineIncomplete) {
// wait to emit last line until rest of line comes in
incompleteLine = lines.pop();
}
else {
// remove last '' after newline
lines.pop();
}
lines.forEach(function (line, i) {
self.emit('line', line);
});
}
stream.on('data', processData);
stream.on('end', function () {
if (incompleteLine) {
processData('\n');
}
self.emit('end');
});
stream.on('error', function (err) {
self.emit('error', err);
});
}
util.inherits(ReadStreamLineEmitter, EventEmitter);
module.exports = ReadStreamLineEmitter;