Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

readline does not send last line #7238

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

readline does not send last line #7238

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

Comments

@lapo-luchini
Copy link

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
Copy link

yorkie commented Mar 4, 2014

It has been added in cfe0bab

@lapo-luchini
Copy link
Author

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

@lapo-luchini
Copy link
Author

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
Copy link

yorkie commented Oct 13, 2014

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

@yorkie
Copy link

yorkie commented Oct 13, 2014

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
Copy link
Author

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
Copy link

yorkie commented Oct 13, 2014

@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
Copy link
Author

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

@mmontag
Copy link

mmontag commented May 21, 2015

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

@jasnell
Copy link
Member

jasnell commented May 21, 2015

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants