Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

End mute stream so it un-pipes

Fix #11
  • Loading branch information...
commit f3fbf9a9d30d13351be8001217fbf49a294e81c5 1 parent 5117e61
@isaacs authored
Showing with 78 additions and 1 deletion.
  1. +2 −1  lib/read.js
  2. +76 −0 test/many.js
View
3  lib/read.js
@@ -12,7 +12,7 @@ function read (opts, cb) {
var input = opts.input || process.stdin
var output = opts.output || process.stdout
var m = new Mute({ replace: opts.replace })
- m.pipe(output)
+ m.pipe(output, {end: false})
output = m
var def = opts.default || ''
var terminal = !!(opts.terminal || output.isTTY)
@@ -65,6 +65,7 @@ function read (opts, cb) {
rl.close()
clearTimeout(timer)
output.mute()
+ output.end()
}
function onError (er) {
View
76 test/many.js
@@ -0,0 +1,76 @@
+var tap = require('tap')
+var read = require('../lib/read.js')
+
+if (process.argv[2] === 'child') {
+ return child()
+}
+
+var spawn = require('child_process').spawn
+function child () {
+ read({prompt:'1'}, function (er, r1) {if (er) throw er
+ read({prompt:'2'}, function (er, r2) {if (er) throw er
+ read({prompt:'3'}, function (er, r3) {if (er) throw er
+ read({prompt:'4'}, function (er, r4) {if (er) throw er
+ read({prompt:'5'}, function (er, r5) {if (er) throw er
+ read({prompt:'6'}, function (er, r6) {if (er) throw er
+ read({prompt:'7'}, function (er, r7) {if (er) throw er
+ read({prompt:'8'}, function (er, r8) {if (er) throw er
+ read({prompt:'9'}, function (er, r9) {if (er) throw er
+ read({prompt:'10'}, function (er, r10) {if (er) throw er
+ read({prompt:'11'}, function (er, r11) {if (er) throw er
+ read({prompt:'12'}, function (er, r12) {if (er) throw er
+ read({prompt:'13'}, function (er, r13) {if (er) throw er
+ read({prompt:'14'}, function (er, r14) {if (er) throw er
+ read({prompt:'15'}, function (er, r15) {if (er) throw er
+ read({prompt:'16'}, function (er, r16) {if (er) throw er
+ read({prompt:'17'}, function (er, r17) {if (er) throw er
+ read({prompt:'18'}, function (er, r18) {if (er) throw er
+ console.log(r1, r2, r3, r4, r5, r6, r7, r8, r9, r10,
+ r11, r12, r13, r14, r15, r16, r17, r18)
+ })})})})})})})})})})})})})})})})})})
+}
+
+tap.test('many reads', function (t) {
+ var child = spawn(process.execPath, [__filename, 'child'])
+ var n = 0
+ var output = ''
+ var expect = '1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ' +
+ '16 17 18 1 2 3 4 5 6 7 8 9 10 11 12 ' +
+ '13 14 15 16 17 18\n'
+ var write = child.stdin.write.bind(child.stdin)
+ var answers =
+ [ '1\n',
+ '2\n',
+ '3\n',
+ '4\n',
+ '5\n',
+ '6\n',
+ '7\n',
+ '8\n',
+ '9\n',
+ '10\n',
+ '11\n',
+ '12\n',
+ '13\n',
+ '14\n',
+ '15\n',
+ '16\n',
+ '17\n',
+ '18\n' ]
+ child.stdout.on('data', function (c) {
+ n++;
+ output += c
+ if (answers.length) {
+ write(answers.shift())
+ }
+ })
+ child.stderr.on('data', function (c) {
+ output += c
+ console.error('' + c)
+ })
+ child.on('close', function (c) {
+ t.equal(output, expect)
+ t.equal(n, 19)
+ t.end()
+ })
+})
Please sign in to comment.
Something went wrong with that request. Please try again.