Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional output stream gets ended after read() #23

Open
limulus opened this issue Feb 23, 2015 · 0 comments
Open

Optional output stream gets ended after read() #23

limulus opened this issue Feb 23, 2015 · 0 comments

Comments

@limulus
Copy link

limulus commented Feb 23, 2015

TL;DR: The mute-stream module needs a quick fix: npm/mute-stream#5

I thought it might be a good idea to document how the above bug affects this module (and dependent modules like prompt). Calling read() with a WriteStream other than process.stdout as the output option (see #5) winds up causing end() to be called on the WriteStream instance. If you’re just doing a single prompt for user input this is no big deal, but subsequent prompts to the same WriteStream instance will not be shown to the user. The following code will reproduce the issue:

var read = require("read")
var tty = require("tty")
var fs = require("fs")

var ttyFD = fs.openSync("/dev/tty", "r+")
var ttyWS = new tty.WriteStream(ttyFD)

read({ output: ttyWS, prompt: "1st: " }, function (err, first) {
  read({ output: ttyWS, prompt: "2nd: " }, function (err, second) {
    console.log({ first: first, second: second })
  })
})

The 1st: prompt will be printed, but the 2nd: prompt will not be, because ttyWS got ended. Presumably this does not affect the default of process.stdout because of some io.js magic that re-opens process.stdout if there is an attempt to write to it when it has already been ended.

One workaround for users of the read module is to no-op the WriteStream instance’s end method, like so:

ttyWS.end = function () {}

Ultimately, the culprit here is this bug in mute-stream: npm/mute-stream#5, which lets the end() call pipe to the WriteStream instance. Fixing that and updating the dependency should fix this bug.

Thanks for taking a look at this!

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

No branches or pull requests

1 participant