readline does not send last line #7238

Closed
lapo-luchini opened this Issue Mar 4, 2014 · 10 comments

Projects

None yet

4 participants

@lapo-luchini

When the input file does not have a \n in the last line, that data is never sent to the 'line' event (nor any other).
It might be nice to generate a 'line' event on 'close' with remaining data.

@yorkie
Node.js Foundation member

It has been added in cfe0bab

@lapo-luchini

Nice, I guess this will close the bug in 0.10.27 then.

@lapo-luchini

Seems to be still broken in v0.10.32:

% ( echo a ; echo b ; echo -n c ) > a
% node testBR a                      
LINE a
LINE b
CLOSE undefined

where testBR.js:

function log(prefix) {
    return function (s) { console.log(prefix, s); };
}

var is = require('fs').createReadStream(process.argv[2], { encoding: 'utf8' });
is.on('error', log('ERROR'));
require('readline').createInterface({
    input: is,
    terminal: false
}).on('line', log('LINE'))
.on('close', log('CLOSE'));
@lapo-luchini lapo-luchini reopened this Oct 13, 2014
@yorkie
Node.js Foundation member

@lapo-luchini which error do you get, break? in my node 0.10.32 runtime, it prints 5 lines

@yorkie
Node.js Foundation member

yup, you are right, however it just because 0.10.32 doesn't pick this commit, i guess it will be merged to 0.12.x maybe :)

@lapo-luchini

OK, I will wait 0.12.x then.

In the meantime I'm trying out this work-around:

function NewlineReadStream() {
    NewlineReadStream.super_.call(this);
}
require('util').inherits(NewlineReadStream, require('stream').Transform);
NewlineReadStream.prototype._transform = function (chunk, enc, cb) {
    this.push(chunk, enc);
    cb();
};
NewlineReadStream.prototype._flush = function (cb) {
    this.push('\n');
    cb();
};

(warning: this adds a newline even to files which already have it; that was not an issue in my use case)

@yorkie
Node.js Foundation member

@lapo-luchini
maybe you don't need to wait that, actually the readline source file(https://github.com/joyent/node/blob/cfe0bab85b8b0974c3683c434fa33663e0b02906/lib/readline.js) doesn't depend on any other core module which are not able to be touched at user-land, so just copy the fixed version of readline.js to a separate file, and require it

haha, i guess this will work for you, but unsure any side-effect :p

@lapo-luchini

I had to rewrite isFunction/isUndefined/isString using typeof, but it works ok, thanks for the hint.

@mmontag

brew upgrade node and now my code works (node 0.12.2_1). 😄 Maybe this can be closed again?

@jasnell
Node.js Foundation member

Confirmed, working in v0.12. Closing. Can reopen if necessary.

@jasnell jasnell closed this May 21, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment