Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

REPL net sockets should act like TTYs #2917

Closed
mranney opened this issue Mar 12, 2012 · 5 comments
Closed

REPL net sockets should act like TTYs #2917

mranney opened this issue Mar 12, 2012 · 5 comments
Labels

Comments

@mranney
Copy link

mranney commented Mar 12, 2012

It would be very useful if REPL net sockets could work like @TooTallNate ' s example:

https://gist.github.com/d9f15b31b60b09b84e54

So we could get readline functionality over REPL.

@TooTallNate
Copy link

It looks like the first step it to allow tty.ReadStream to accept a net.Socket instance so that you can retrieve "keypress" events properly.

Likely the inverse will be needed as well (passing a net.Socket to the tty.WriteStream constructor), but I haven't looked too deeply into that yet.

@TooTallNate
Copy link

I was able to get it working as expected with a lot of hacking:

// repl-server.js
var repl = require('repl')
var net = require('net')

// hack #1
process.stdin.resume()

net.createServer(function (socket) {

  // hack #2
  socket.isTTY = true

  // hack #3
  socket.getWindowSize = function () { return [ 80, 40 ] }

  // hack #4
  socket.cursorTo = require('tty').WriteStream.prototype.cursorTo
  socket.clearLine = require('tty').WriteStream.prototype.clearLine

  // hack #5
  socket._emitKey = require('tty').ReadStream.prototype._emitKey
  socket.on('data', function (b) {
    socket._emitKey(b)
  })

  var r = repl.start('socket repl > ', socket)

  // if REPLServer was an EE, and fired "exit", then this *would* work
  /*r.on('exit', function () {
    console.error('repl exit event, closing socket')
    socket.end()
  })*/
}).listen(1337)
// repl-client.js
var tty = require('tty')
var net = require('net')

var sock = net.createConnection(1337)

sock.on('connect', function () {
  process.stdin.resume();
  tty.setRawMode(true)
})

process.stdin.pipe(sock)
sock.pipe(process.stdout)

sock.on('end', function () {
  tty.setRawMode(false)
  process.stdin.pause()
})

Would have posted as a gist but github is erroring on me :(

@TooTallNate
Copy link

So we need to:

  • Extend tty module to accept existing net.Socket instances. Currently it always creates a new instance.
  • Remove existing "tty" semantics from "readline". Instead just turn this into an explicit "enabled" option/argument to readline.Interface.
  • Move the "SIGWINCH" process handler from readline itself to somewhere else (or better detection for when process.stdin/stdout is being used)...
  • Would also be nice to turn REPLServer into an EventEmitter, so that it can fire "exit" event and "error" events. Currently there's no way to really be sure when .exit was typed.

Comments?

@mranney
Copy link
Author

mranney commented Mar 12, 2012

This is great work, thanks. The value of a good interactive net REPL is perhaps hard to understand until you need to fix tricky problems in production.

@TooTallNate
Copy link

Fixed by #2922 :)

richardlau pushed a commit to ibmruntimes/node that referenced this issue Oct 13, 2015
* ICU 56 was just released yesterday. Update to it.
* Notable changes: Unicode 8, CLDR 28, 2-3x number format perf,
  20% improvement in Collator startup
  * more at http://site.icu-project.org/download/56 or in nodejs#2917

Also:

* cleanup out/**/*.d and deps/icu  on "make clean"
* cleanup deps/icu on "vcbuild clean"

When building from an non-clean directory, it's important to
run `make clean` or `vcbuild clean` to remove the existing
ICU 55 from the deps path before building.

Fixes: nodejs/node#2917
PR-URL: nodejs/node#3281
Reviewed-By: James M Snell <jasnell@gmail.com>
richardlau pushed a commit to ibmruntimes/node that referenced this issue Oct 13, 2015
* ICU 56 was just released yesterday. Update to it.
* Notable changes: Unicode 8, CLDR 28, 2-3x number format perf,
  20% improvement in Collator startup
  * more at http://site.icu-project.org/download/56 or in nodejs#2917

Also:

* cleanup out/**/*.d and deps/icu  on "make clean"
* cleanup deps/icu on "vcbuild clean"

When building from an non-clean directory, it's important to
run `make clean` or `vcbuild clean` to remove the existing
ICU 55 from the deps path before building.

Fixes: nodejs/node#2917
PR-URL: nodejs/node#3281
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants