Node JS REPL, Sockets, and Telnet - Tab Completion, Arrow Keys, etc
Asked Answered
H

2

3

I have been playing around with Node's REPL. I thought it would be pretty cool to make it available via a Socket, connect to it via Telnet/puTTY/whatever, and debug my server on-the-fly.

I used the example found here: http://nodejs.org/docs/latest/api/repl.html, which basically looks like this...

net.createServer(function (socket) {
  var cmd = repl.start(">", socket);
  //... some other stuff here.... not important
}).listen(5001);

OK, great! Now I can connect to port 5001 with Telnet and interact with the REPL. But, I am running into issues with control characters (i.e. Tab, Ctrl+C, up arrow, down arrow, etc.). How can I get these to work? For example, if I connect using telnet, type "1+1<Enter>", I get 2. But, then when I hit "<Up Arrow><Enter>", I get "...", as if the REPL is waiting for me to finish the command. But, really, all I want to do is see the last command that I executed. I know Telnet likes to hold onto its output until a line feed is inputted, but is there any way to avoid this, as well?

$ telnet localhost 6634
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
>1+1
2
>^[[A
...

EDIT: I also found this issue, which may or may not be related - Arrow keys turn into control characters in Telnet

EDIT 2: Hmmm... rlwrap seems to solve most of my problem:

$ rlwrap telnet localhost 6634

Only thing that doesn't work is tab completions of local/global variables, which I suppose I can live without. I was mostly concerned with the command history. rlwrap is neat!

Hectorhecuba answered 27/12, 2011 at 22:18 Comment(0)
H
1

See above.

rlwrap telnet localhost 6634

Hectorhecuba answered 30/5, 2012 at 19:3 Comment(1)
you could mimic tab completion using the -c -f switches: blog.lishman.com/2008/04/rlwrap.html obviously you'll need a node.js keyword listNolasco
P
0

To get the built-in tab completion and history in REPLServer working on a socket you need to explicitly provide the terminal: true option when instantiating REPLServer to tell it behave as if it's connected to a terminal, and then on the client side you put the terminal in raw mode (see e.g. repl-client on npm).

Pence answered 10/7, 2023 at 2:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.