tty: use terminal VT mode on Windows

As of 0315283cbd, the libuv `UV_TTY_MODE_RAW_VT` flag is available for
applications on Windows in order to have the terminal itself translate
keypresses into control sequences, rather than libuv.
This aligns the TTY setup more closely with POSIX platforms, and
enables handling of control sequences in applications which libuv
is not able to emit at all.

Since the Node.js `readline` implementation already handles divergences
between different control character sequences for the same keypresses
(e.g. `<ESC>[[A` vs `<ESC>OP` for the F1 key), this should not present
a visible change for typical Node.js TTY applications.

Testing is handled on the libuv side, and not easily feasible with
Node.js’s current TTY test setup.

PR-URL: https://github.com/nodejs/node/pull/58358
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
Anna Henningsen 2025-05-19 00:43:58 +02:00 committed by GitHub
parent 61aecf2e58
commit db2aae8022
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -116,7 +116,17 @@ void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
TTYWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(
&wrap, args.This(), args.GetReturnValue().Set(UV_EBADF));
int err = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
// UV_TTY_MODE_RAW_VT is a variant of UV_TTY_MODE_RAW that
// enables control sequence processing on the TTY implementer side,
// rather than having libuv translate keypress events into
// control sequences, aligning behavior more closely with
// POSIX platforms. This is also required to support some control
// sequences at all on Windows, such as bracketed paste mode.
// The Node.js readline implementation handles differences between
// these modes.
int err = uv_tty_set_mode(
&wrap->handle_,
args[0]->IsTrue() ? UV_TTY_MODE_RAW_VT : UV_TTY_MODE_NORMAL);
args.GetReturnValue().Set(err);
}