Skip to content

Optional output stream gets ended after read() #23

@limulus

Description

@limulus

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions