Referring to node/readline
line 313:
Interface.prototype.write = function(d, key) {
if (this.paused) this.resume();
this.terminal ? this._ttyWrite(d, key) : this._normalWrite(d);
};
by calling rl.write()
you either write to tty or call _normalWrite()
whose definition follows the block.
Interface.prototype._normalWrite = function(b) {
// some code
// .......
if (newPartContainsEnding) {
this._sawReturn = /\r$/.test(string);
// got one or more newlines; process into "line" events
var lines = string.split(lineEnding);
// either '' or (concievably) the unfinished portion of the next line
string = lines.pop();
this._line_buffer = string;
lines.forEach(function(line) {
this._onLine(line);
}, this);
} else if (string) {
// no newlines this time, save what we have for next time
this._line_buffer = string;
}
};
Output is written into _line_buffer
.
line 96:
function onend() {
if (util.isString(self._line_buffer) && self._line_buffer.length > 0) {
self.emit('line', self._line_buffer);
}
self.close();
}
We find, _line_buffer
is emitted to line
event eventually. That's why you cannot write output to writeStream. To solve this problem, you can simply open a file using fs.openSync()
, and fs.write()
in rl.on('line', function(line){})
callback.
Sample code:
var rl = readline.createInterface({
input: fs.createReadStream(temp + '/export.json'),
output: process.stdout,
terminal: false
});
fd = fs.openSync('filename', 'w');
rl.on('line', function(line) {
fs.write(fd, line);
});