tty: add raw-vt and io raw modes

Signed-off-by: Samuel Williams <samuel.williams@shopify.com>
PR-URL: https://github.com/nodejs/node/pull/64140
Refs: https://github.com/nodejs/node/issues/63059
Refs: https://github.com/libuv/libuv/issues/32
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
Samuel Williams authored and Antoine du Hamel committed 2026-09-07 11:53:59 +02:00
1 parent 67da38cada
commit 600663b23f
5 files changed
+108 -12

No files matched your search

+26 -4
View File
@@ -69,12 +69,18 @@ A `boolean` that is always `true` for `tty.ReadStream` instances.
<!-- YAML
added: v0.7.7
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/64140
description: The `mode` argument supports `'raw'` and `'io'`.
-->
* `mode` {boolean} If `true`, configures the `tty.ReadStream` to operate as a
raw device. If `false`, configures the `tty.ReadStream` to operate in its
default mode. The `readStream.isRaw` property will be set to the resulting
mode.
* `mode` {boolean|string} If `true` or `'raw'`, configures the
`tty.ReadStream` to operate as a raw device. If `'io'`, configures the
`tty.ReadStream` to operate in binary-safe I/O mode. If `false`, configures
the `tty.ReadStream` to operate in its default mode. The `readStream.isRaw`
property will be set to whether the stream is in raw mode, and the
`readStream.rawMode` property will be set to the resulting mode.
* Returns: {this} The read stream instance.
Allows configuration of `tty.ReadStream` so that it operates as a raw device.
@@ -91,6 +97,22 @@ buffer. When opening `"\\\\.\\CONIN$"` with the [`fs.open()`][] family of APIs
(for passing into `new tty.ReadStream()`), be sure to use a read/write flag
such as `'r+'`.
When in binary-safe I/O mode, terminal output processing is also disabled.
This corresponds to libuv's `UV_TTY_MODE_IO` mode and is not supported on
Windows.
### `readStream.rawMode`
<!-- YAML
added: REPLACEME
-->
* {boolean|string}
The current raw mode for the `tty.ReadStream`. This is `false` when the stream
is in its default mode, `'raw'` when raw input mode is enabled, and `'io'` when
binary-safe I/O mode is enabled.
## Class: `tty.WriteStream`
<!-- YAML
+28 -5
View File
@@ -27,10 +27,17 @@ const {
} = primordials;
const net = require('net');
const { TTY, isTTY } = internalBinding('tty_wrap');
const {
TTY,
UV_TTY_MODE_IO,
UV_TTY_MODE_NORMAL,
UV_TTY_MODE_RAW_VT,
isTTY,
} = internalBinding('tty_wrap');
const {
ErrnoException,
codes: {
ERR_INVALID_ARG_VALUE,
ERR_INVALID_FD,
ERR_TTY_INIT_FAILED,
},
@@ -68,20 +75,36 @@ function ReadStream(fd, options) {
});
this.isRaw = false;
this.rawMode = false;
this.isTTY = true;
}
ObjectSetPrototypeOf(ReadStream.prototype, net.Socket.prototype);
ObjectSetPrototypeOf(ReadStream, net.Socket);
ReadStream.prototype.setRawMode = function(flag) {
flag = !!flag;
const err = this._handle?.setRawMode(flag);
ReadStream.prototype.setRawMode = function(mode) {
let rawMode;
if (mode === 'io' || mode === 'raw') {
rawMode = mode;
} else if (typeof mode === 'string') {
throw new ERR_INVALID_ARG_VALUE(
'mode', mode, "must be true, false, 'raw', or 'io'");
} else {
rawMode = mode ? 'raw' : false;
}
let ttyMode = UV_TTY_MODE_NORMAL;
if (rawMode === 'io') {
ttyMode = UV_TTY_MODE_IO;
} else if (rawMode === 'raw') {
ttyMode = UV_TTY_MODE_RAW_VT;
}
const err = this._handle?.setRawMode(ttyMode);
if (err) {
this.emit('error', new ErrnoException(err, 'setRawMode'));
return this;
}
this.isRaw = flag;
this.isRaw = rawMode !== false;
this.rawMode = rawMode;
return this;
};
+7 -3
View File
@@ -68,6 +68,9 @@ void TTYWrap::Initialize(Local<Object> target,
SetProtoMethod(isolate, t, "setRawMode", SetRawMode);
SetMethodNoSideEffect(context, target, "isTTY", IsTTY);
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_NORMAL);
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_IO);
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_RAW_VT);
Local<Value> func;
if (t->GetFunction(context).ToLocal(&func) &&
@@ -124,9 +127,10 @@ void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
// 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);
Environment* env = Environment::GetCurrent(args);
int mode;
if (!args[0]->Int32Value(env->context()).To(&mode)) return;
int err = uv_tty_set_mode(&wrap->handle_, static_cast<uv_tty_mode_t>(mode));
args.GetReturnValue().Set(err);
}
@@ -0,0 +1,43 @@
'use strict';
require('../common');
const assert = require('assert');
const { spawnSync } = require('child_process');
function isOnlcrEnabled() {
const { stdout, stderr, status } = spawnSync('stty', ['-a'], {
encoding: 'utf8',
stdio: ['inherit', 'pipe', 'pipe'],
});
assert.strictEqual(status, 0, stderr);
return /(?:^|[\s;])onlcr(?:[\s;]|$)/.test(stdout);
}
process.stdin.setRawMode(true);
console.log(`raw=${isOnlcrEnabled()}`);
assert.strictEqual(process.stdin.isRaw, true);
assert.strictEqual(process.stdin.rawMode, 'raw');
process.stdin.setRawMode(false);
console.log(`normal=${process.stdin.isRaw}`);
assert.strictEqual(process.stdin.rawMode, false);
assert.throws(
() => process.stdin.setRawMode('raw-vt'),
{
code: 'ERR_INVALID_ARG_VALUE',
name: 'TypeError',
});
assert.strictEqual(process.stdin.rawMode, false);
process.stdin.setRawMode('raw');
console.log(`raw-string=${isOnlcrEnabled()}`);
assert.strictEqual(process.stdin.isRaw, true);
assert.strictEqual(process.stdin.rawMode, 'raw');
process.stdin.setRawMode(false);
process.stdin.setRawMode('io');
console.log(`io=${isOnlcrEnabled()}`);
assert.strictEqual(process.stdin.isRaw, true);
assert.strictEqual(process.stdin.rawMode, 'io');
process.stdin.setRawMode(false);
@@ -0,0 +1,4 @@
raw=true
normal=false
raw-string=true
io=false