Skip to content

Commit

Permalink
Upgraded stdin handling for node v0.10. Fixes #1.
Browse files Browse the repository at this point in the history
  • Loading branch information
dshaw committed Apr 9, 2013
1 parent fa08f14 commit 0f516fc
Showing 1 changed file with 20 additions and 36 deletions.
56 changes: 20 additions & 36 deletions repl-client.js
Expand Up @@ -35,43 +35,27 @@ if (!options.path) {
var socket = net.connect(options)
, streams2 = !!stream.Transform

process.stdin.pipe(socket, { end: false }).pipe(process.stdout)
process.stdin.pipe(socket)
socket.pipe(process.stdout)

function destroySocket () {
process.stdin.on('end', function () {
socket.destroy()
console.log()
}

process.stdin.on('end', destroySocket)

if (streams2) {

socket.on('connect', function () {
process.stdin.setRawMode(true)
})

socket.on('close', function () {
process.stdin.setRawMode(false)
process.exit()
})

} else { // node < v0.10
})

socket.on('connect', function () {
process.stdin.resume()
process.stdin.setRawMode(true)
})

socket.on('close', function done () {
process.stdin.setRawMode(false)
process.stdin.pause()
socket.removeListener('close', done)
})

process.stdin.on('data', function (buffer) {
if (buffer.length === 1 && buffer[0] === 4) {
destroySocket()
}
})

}
process.stdin.on('data', function (buffer) {
if (buffer.length === 1 && buffer[0] === 4) { // EOT (end-of-transmission) Ctrl-D
process.stdin.emit('end')
}
})

socket.on('connect', function () {
if (!streams2) process.stdin.resume()
process.stdin.setRawMode(true)
})

socket.on('close', function done () {
process.stdin.setRawMode(false)
if (!streams2) process.stdin.pause()
socket.removeListener('close', done)
})

0 comments on commit 0f516fc

Please sign in to comment.