Node: Feature request: Inform the "line" event on a Readline interface of the type of end-of-line input

Created on 3 Aug 2016  路  12Comments  路  Source: nodejs/node

I was using the Readline module to rewrite a file line by line. While doing that I noticed that I had no idea which end-of-line (\r, \n, \r\n) caused the line break so I could not reassemble the file exactly as it was.

Of course I could detect the first EOL and then just use that for everything but in most cases I want to leave the line untouched anyway. Wouldn't it be a good idea to pass the found EOL string as a second argument to the callback of a line event emitted by a readline.Interface?

feature request readline

Most helpful comment

I started with something but I'd really be completely fine to give that job over to @JungMinu since I realized that I'm actually pretty occupied with more important programming stuff for the next couple of weeks.
So @JungMinu, if you want to give it a shot, go for it. :+1:

All 12 comments

@Loilo

detect the first EOL and then just use that for everything

It is not a reliable approach unless you read the whole file first.

  1. A stream chunk could be aborted between \r and \n in \r\n.
  2. You can't be sure if there is a line ending in the end of a file.

This is good idea, I think. It would definitely need to be line-by-line because a single doc could contain a mix. The key challenge with implementing this is that the current impl does not record the line-ending currently. It wouldn't be that difficult to add in tho.

Interested in doing a PR? :-)

I'd love to. I'll give it a try but it's quite possible that I'll just fail to understand the bigger picture of the project. Got a pretty promising looks on the readline.js file though so let's see what comes around, :)

@Loilo Cool! If there鈥檚 anything you need or you鈥檇 like to ask, you can do that here or e.g. in #node-dev on Freenode!

+1 to what @addaleax said :-) If you get stuck or need someone to bounce ideas off of, just let one of us know!

I'd love to do a PR, please let me know if I can help :)

I think @Loilo is already working on something? :)

I started with something but I'd really be completely fine to give that job over to @JungMinu since I realized that I'm actually pretty occupied with more important programming stuff for the next couple of weeks.
So @JungMinu, if you want to give it a shot, go for it. :+1:

Yep, I will do a PR soon @Loilo @addaleax

@JungMinu Are you still planning on doing this? If not, should we unassign it so someone else can pick it up?

ping @JungMinu

I just planned on implementing this I came to a point where it's difficult to identify the actual end-of-line.

Readline just receives arbitrary chunks and we have to delay emitting the last line in case the chunk contains a carriage return at the end of it. Otherwise it's not possible to guarantee that it's the correct line ending. We also have the crlfDelay option with a default of 100 ms. That means we'll have to trigger a timeout in such cases to emit the line in case no new chunk is incoming during that time. Having such a delay seems pretty bad for most applications, even though it's probably a pretty rare case.
It should also probably only work in case the terminal option is set to false.

I am for these reasons not sure anymore if we should really implement this.
Any opinions?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

seishun picture seishun  路  3Comments

danialkhansari picture danialkhansari  路  3Comments

vsemozhetbyt picture vsemozhetbyt  路  3Comments

akdor1154 picture akdor1154  路  3Comments

cong88 picture cong88  路  3Comments