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

tty → readline ansi/vt100 migration #2922

Closed
wants to merge 21 commits into from
Closed

tty → readline ansi/vt100 migration #2922

wants to merge 21 commits into from

Conversation

TooTallNate
Copy link

This is the result of a discussion @piscisaureus, @isaacs and I were having. The overall goal here is to make readline more interoperable with other node Streams like say a net.Socket instance, in "enabled" mode.

There's a lot going on here, I'll try to break it down:

  • move tty.setRawMode() to tty.ReadStream's prototype
  • remove tty.getWindowSize() and tty.setWindowSize(), redundant/incomplete
  • add a tty.ReadStream#isRaw flag (so process.stdin.isRaw). gets set when setRawMode() is called.
  • remove use of tty module in readline module.
  • move the "keypress" logic from tty → readline
  • move WriteStream#cursorTo from tty → readline
  • move WriteStream#moveTo from tty → readline
  • move WriteStream#clearLine from tty → readline
  • move the "SIGWINCH" handling code from readline → tty, make process.stdout emit a "resize" event
  • add process.stdout.rows and process.stdout.columns; deprecate process.stdout.getWindowSize()

@TooTallNate
Copy link
Author

So this pull + #2919 makes this code work with this "full-featured" client:

var repl = require('repl')
var net = require('net')

net.createServer(function (socket) {

  // hack #1
  socket.isTTY = true

  var r = repl.start('socket repl> ', socket)
  r.on('exit', function () {
    console.error('repl exit event, closing socket')
    socket.end()
  })
}).listen(1337)

Which leaves one final ugly wart: isTTY. readline should not be checking this property.

I personally think we should change readline.Interface and repl.REPLServer to both accept an Options object. That way you would could explicitly specify "enabled: true" (maybe a better name "immediate"?), and "colors: true" and this wouldn't be so hacky.

So maybe, in the end, we could write code like this:

var repl = require('repl')
var net = require('net')

net.createServer(function (socket) {
  repl.start({
      prompt: 'repl> '
    , socket: socket
    , enabled: true
    , useGlobal: false
  }).on('exit', function () {
    socket.end()
  })
}).listen(1337)

But these changes belong in another pull request I think...

@ghost
Copy link

ghost commented Mar 13, 2012

Awesome, this is all the things I did for my mintty hack and more to allow it to spit out ansi to a Socket. This raises the issue with window size though.

@TooTallNate
Copy link
Author

So I ended up just adding explicit support for the enabled option of the readline interface, thus removing the final ugly wart: isTTY from readline. So now that code is clean as far as responsibilities are concerned.

So basically, this code actually works!!!

var repl = require('repl')
var net = require('net')

net.createServer(function (socket) {
  repl.start({
      socket: socket
    , enabled: true
  }).on('exit', function () {
    socket.end()
  })
}).listen(1337)

@TooTallNate
Copy link
Author

Also keeping backwards-compatibility for now with exports.setRawMode().
Gets set to true/false when setRawMode() is called.
This adds:

 * process.stdout.rows
 * process.stdout.columns
 * process.stdout.on('resize', ...)

Deprecates:

 * process.stdout.getWindowSize()
It doesn't belong in tty. Moving it here will allow us to reuse the logic
with other kinds of streams, namely a net.Socket.
They didn't belong on tty.WriteStream and this will allow us to resuse this
logic on streams other than process.stdout, namely a net.Socket.
Part of the Stream contract.
For easier interoperability with net.Socket instances.

A proper telnet server could set the columns/rows properties
manually after negotiating NAWS, for example.
Need to update the process REPL (and probably the debugger) to
set this to true.
Support for backwards-compat as well.
Again, support for the backwards-compat API as well.
It belongs here, since if someone is actually running a net.Socket repl
server, then we don't necessarily want this check to take place.
@rlidwka
Copy link

rlidwka commented Mar 18, 2012

Awesome, this is all the things I did for my mintty hack and more to allow it to spit out ansi to a Socket. This raises the issue with window size though.

Yeah, i did smth simular for my telnet server.

+1 for resize event, will be useful.

@TooTallNate but SIGWINCH is for stdout only (not for all writable streams), right? It means that it probably should be written like that:

  if (self === process.stdout) process.on('SIGWINCH', function() {
    var winSize = self._handle.getWindowSize();
    self.columns = winSize[0];
    self.rows = winSize[1];
    self.emit('resize', winSize);
 });

I dont know if it works though. I had an issue recently when resizing main window affected telnet windows.

@rlidwka
Copy link

rlidwka commented Mar 19, 2012

Some tests have failed with this. Is that critical?

=== release test-debugger-repl ===                                         
Path: simple/test-debugger-repl
 != /break in .*:11/
Command: out/Release/node /home/alex/node-work-2/node/test/simple/test-debugger-repl.js

=== release test-debugger-repl-utf8 ===                                    
Path: simple/test-debugger-repl-utf8
 != /break in .*:11/
Command: out/Release/node /home/alex/node-work-2/node/test/simple/test-debugger-repl-utf8.js

@bnoordhuis
Copy link
Member

Some tests have failed with this. Is that critical?

No. Those tests have been failing consistently for weeks now, maybe longer. :-/

richardlau pushed a commit to ibmruntimes/node that referenced this pull request Nov 5, 2015
Notable changes:

* buffer: (Breaking) Removed both 'raw' and 'raws' encoding types from Buffer,
  these have been deprecated for a long time (Sakthipriyan Vairamani) nodejs#2859.
* console: (Breaking) Values reported by console.time() now have 3 decimals of
  accuracy added (Michaël Zasso) nodejs#3166.
* fs:
  - fs.readFile*(), fs.writeFile*(), and fs.appendFile*() now also accept a file
    descriptor as their first argument (Johannes Wüller) nodejs#3163.
  - (Breaking) In fs.readFile(), if an encoding is specified and the internal
    toString() fails the error is no longer thrown but is passed to the callback
    (Evan Lucas) nodejs#3485.
  - (Breaking) In fs.read() (using the fs.read(fd, length, position, encoding,
    callback) form), if the internal toString() fails the error is no longer
    thrown but is passed to the callback (Evan Lucas) nodejs#3503.
* http:
  - Fixed a bug where pipelined http requests would stall (Fedor Indutny) nodejs#3342.
  - (Breaking) When parsing HTTP, don't add duplicates of the following headers:
    Retry-After, ETag, Last-Modified, Server, Age, Expires. This is in addition
    to the following headers which already block duplicates: Content-Type,
    Content-Length, User-Agent, Referer, Host, Authorization,
    Proxy-Authorization, If-Modified-Since, If-Unmodified-Since, From, Location,
    Max-Forwards (James M Snell) nodejs#3090.
  - (Breaking) The callback argument to OutgoingMessage#setTimeout() must be a
    function or a TypeError is thrown (James M Snell) nodejs#3090.
  - (Breaking) HTTP methods and header names must now conform to the RFC 2616
    "token" rule, a list of allowed characters that excludes control characters
    and a number of separator characters. Specifically, methods and header names
    must now match /^[a-zA-Z0-9_!#$%&'*+.^`|~-]+$/ or a TypeError will be thrown
    (James M Snell) nodejs#2526.
* node:
  - (Breaking) Deprecated the _linklist module (Rich Trott) nodejs#3078.
  - (Breaking) Removed require.paths and require.registerExtension(), both had
    been previously set to throw Error when accessed
    (Sakthipriyan Vairamani) nodejs#2922.
* npm: Upgraded to version 3.3.6 from 2.14.7, see
  https://github.com/npm/npm/releases/tag/v3.3.6 for more details. This is a
  major version bump for npm and it has seen a significant amount of change.
  Please see the original npm v3.0.0 release notes for a list of major changes
  (Rebecca Turner) nodejs#3310.
* src: (Breaking) Bumped NODE_MODULE_VERSION to 47 from 46, this is necessary
  due to the V8 upgrade. Native add-ons will need to be recompiled
  (Rod Vagg) nodejs#3400.
* timers: Attempt to reuse the timer handle for setTimeout().unref(). This fixes
  a long-standing known issue where unrefed timers would perviously hold
  beforeExit open (Fedor Indutny) nodejs#3407.
* tls:
  - Added ALPN Support (Shigeki Ohtsu) nodejs#2564.
  - TLS options can now be passed in an object to createSecurePair()
    (Коренберг Марк) nodejs#2441.
  - (Breaking) The default minimum DH key size for tls.connect() is now 1024
    bits and a warning is shown when DH key size is less than 2048 bits. This a security consideration to prevent "logjam" attacks. A new minDHSize TLS
    option can be used to override the default. (Shigeki Ohtsu) nodejs#1831.
* util:
  - (Breaking) util.p() was deprecated for years, and has now been removed
    (Wyatt Preul) nodejs#3432.
  - (Breaking) util.inherits() can now work with ES6 classes. This is considered
    a breaking change because of potential subtle side-effects caused by a
    change from directly reassigning the prototype of the constructor using
    `ctor.prototype = Object.create(superCtor.prototype, { constructor: { ... } })`
    to using `Object.setPrototypeOf(ctor.prototype, superCtor.prototype)`
    (Michaël Zasso) nodejs#3455.
* v8: (Breaking) Upgraded to 4.6.85.25 from 4.5.103.35 (Ali Ijaz Sheikh) nodejs#3351.
  - Implements the spread operator, see
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator
    for further information.
  - Implements new.target, see
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new.target
    for further information.
* zlib: Decompression now throws on truncated input (e.g. unexpected end of
  file) (Yuval Brik) nodejs#2595.

PR-URL: nodejs/node#3466
richardlau pushed a commit to ibmruntimes/node that referenced this pull request Nov 5, 2015
Notable changes:

* buffer: (Breaking) Removed both 'raw' and 'raws' encoding types from Buffer,
  these have been deprecated for a long time (Sakthipriyan Vairamani) nodejs#2859.
* console: (Breaking) Values reported by console.time() now have 3 decimals of
  accuracy added (Michaël Zasso) nodejs#3166.
* fs:
  - fs.readFile*(), fs.writeFile*(), and fs.appendFile*() now also accept a file
    descriptor as their first argument (Johannes Wüller) nodejs#3163.
  - (Breaking) In fs.readFile(), if an encoding is specified and the internal
    toString() fails the error is no longer thrown but is passed to the callback
    (Evan Lucas) nodejs#3485.
  - (Breaking) In fs.read() (using the fs.read(fd, length, position, encoding,
    callback) form), if the internal toString() fails the error is no longer
    thrown but is passed to the callback (Evan Lucas) nodejs#3503.
* http:
  - Fixed a bug where pipelined http requests would stall (Fedor Indutny) nodejs#3342.
  - (Breaking) When parsing HTTP, don't add duplicates of the following headers:
    Retry-After, ETag, Last-Modified, Server, Age, Expires. This is in addition
    to the following headers which already block duplicates: Content-Type,
    Content-Length, User-Agent, Referer, Host, Authorization,
    Proxy-Authorization, If-Modified-Since, If-Unmodified-Since, From, Location,
    Max-Forwards (James M Snell) nodejs#3090.
  - (Breaking) The callback argument to OutgoingMessage#setTimeout() must be a
    function or a TypeError is thrown (James M Snell) nodejs#3090.
  - (Breaking) HTTP methods and header names must now conform to the RFC 2616
    "token" rule, a list of allowed characters that excludes control characters
    and a number of separator characters. Specifically, methods and header names
    must now match /^[a-zA-Z0-9_!#$%&'*+.^`|~-]+$/ or a TypeError will be thrown
    (James M Snell) nodejs#2526.
* node:
  - (Breaking) Deprecated the _linklist module (Rich Trott) nodejs#3078.
  - (Breaking) Removed require.paths and require.registerExtension(), both had
    been previously set to throw Error when accessed
    (Sakthipriyan Vairamani) nodejs#2922.
* npm: Upgraded to version 3.3.6 from 2.14.7, see
  https://github.com/npm/npm/releases/tag/v3.3.6 for more details. This is a
  major version bump for npm and it has seen a significant amount of change.
  Please see the original npm v3.0.0 release notes for a list of major changes
  (Rebecca Turner) nodejs#3310.
* src: (Breaking) Bumped NODE_MODULE_VERSION to 47 from 46, this is necessary
  due to the V8 upgrade. Native add-ons will need to be recompiled
  (Rod Vagg) nodejs#3400.
* timers: Attempt to reuse the timer handle for setTimeout().unref(). This fixes
  a long-standing known issue where unrefed timers would perviously hold
  beforeExit open (Fedor Indutny) nodejs#3407.
* tls:
  - Added ALPN Support (Shigeki Ohtsu) nodejs#2564.
  - TLS options can now be passed in an object to createSecurePair()
    (Коренберг Марк) nodejs#2441.
  - (Breaking) The default minimum DH key size for tls.connect() is now 1024
    bits and a warning is shown when DH key size is less than 2048 bits. This a security consideration to prevent "logjam" attacks. A new minDHSize TLS
    option can be used to override the default. (Shigeki Ohtsu) nodejs#1831.
* util:
  - (Breaking) util.p() was deprecated for years, and has now been removed
    (Wyatt Preul) nodejs#3432.
  - (Breaking) util.inherits() can now work with ES6 classes. This is considered
    a breaking change because of potential subtle side-effects caused by a
    change from directly reassigning the prototype of the constructor using
    `ctor.prototype = Object.create(superCtor.prototype, { constructor: { ... } })`
    to using `Object.setPrototypeOf(ctor.prototype, superCtor.prototype)`
    (Michaël Zasso) nodejs#3455.
* v8: (Breaking) Upgraded to 4.6.85.25 from 4.5.103.35 (Ali Ijaz Sheikh) nodejs#3351.
  - Implements the spread operator, see
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator
    for further information.
  - Implements new.target, see
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new.target
    for further information.
* zlib: Decompression now throws on truncated input (e.g. unexpected end of
  file) (Yuval Brik) nodejs#2595.

PR-URL: nodejs/node#3466
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants