Commit cf39500

Browse files
samuel-williams-shopifyaduh95
authored andcommitted
tty: add raw-vt and io raw modes
Signed-off-by: Samuel Williams <samuel.williams@shopify.com> PR-URL: #64140 Refs: #63059 Refs: libuv/libuv#32 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 7e9204f commit cf39500

5 files changed

Lines changed: 108 additions & 12 deletions

File tree

‎doc/api/tty.md‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,18 @@ A `boolean` that is always `true` for `tty.ReadStream` instances.
6969

7070
<!-- YAML
7171
added: v0.7.7
72+
changes:
73+
- version: REPLACEME
74+
pr-url: https://github.com/nodejs/node/pull/64140
75+
description: The `mode` argument supports `'raw'` and `'io'`.
7276
-->
7377

74-
*`mode` {boolean} If `true`, configures the `tty.ReadStream` to operate as a
75-
raw device. If `false`, configures the `tty.ReadStream` to operate in its
76-
default mode. The `readStream.isRaw` property will be set to the resulting
77-
mode.
78+
*`mode` {boolean|string} If `true` or `'raw'`, configures the
79+
`tty.ReadStream` to operate as a raw device. If `'io'`, configures the
80+
`tty.ReadStream` to operate in binary-safe I/O mode. If `false`, configures
81+
the `tty.ReadStream` to operate in its default mode. The `readStream.isRaw`
82+
property will be set to whether the stream is in raw mode, and the
83+
`readStream.rawMode` property will be set to the resulting mode.
7884
* Returns: {this} The read stream instance.
7985

8086
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
9197
(for passing into `new tty.ReadStream()`), be sure to use a read/write flag
9298
such as `'r+'`.
9399

100+
When in binary-safe I/O mode, terminal output processing is also disabled.
101+
This corresponds to libuv's `UV_TTY_MODE_IO` mode and is not supported on
102+
Windows.
103+
104+
### `readStream.rawMode`
105+
106+
<!-- YAML
107+
added: REPLACEME
108+
-->
109+
110+
* {boolean|string}
111+
112+
The current raw mode for the `tty.ReadStream`. This is `false` when the stream
113+
is in its default mode, `'raw'` when raw input mode is enabled, and `'io'` when
114+
binary-safe I/O mode is enabled.
115+
94116
## Class: `tty.WriteStream`
95117

96118
<!-- YAML

‎lib/tty.js‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,17 @@ const {
2727
}=primordials;
2828

2929
constnet=require('net');
30-
const{TTY, isTTY }=internalBinding('tty_wrap');
30+
const{
31+
TTY,
32+
UV_TTY_MODE_IO,
33+
UV_TTY_MODE_NORMAL,
34+
UV_TTY_MODE_RAW_VT,
35+
isTTY,
36+
}=internalBinding('tty_wrap');
3137
const{
3238
ErrnoException,
3339
codes: {
40+
ERR_INVALID_ARG_VALUE,
3441
ERR_INVALID_FD,
3542
ERR_TTY_INIT_FAILED,
3643
},
@@ -68,20 +75,36 @@ function ReadStream(fd, options) {
6875
});
6976

7077
this.isRaw=false;
78+
this.rawMode=false;
7179
this.isTTY=true;
7280
}
7381

7482
ObjectSetPrototypeOf(ReadStream.prototype,net.Socket.prototype);
7583
ObjectSetPrototypeOf(ReadStream,net.Socket);
7684

77-
ReadStream.prototype.setRawMode=function(flag){
78-
flag=!!flag;
79-
consterr=this._handle?.setRawMode(flag);
85+
ReadStream.prototype.setRawMode=function(mode){
86+
letrawMode;
87+
if(mode==='io'||mode==='raw'){
88+
rawMode=mode;
89+
}elseif(typeofmode==='string'){
90+
thrownewERR_INVALID_ARG_VALUE(
91+
'mode',mode,"must be true, false, 'raw', or 'io'");
92+
}else{
93+
rawMode=mode ? 'raw' : false;
94+
}
95+
letttyMode=UV_TTY_MODE_NORMAL;
96+
if(rawMode==='io'){
97+
ttyMode=UV_TTY_MODE_IO;
98+
}elseif(rawMode==='raw'){
99+
ttyMode=UV_TTY_MODE_RAW_VT;
100+
}
101+
consterr=this._handle?.setRawMode(ttyMode);
80102
if(err){
81103
this.emit('error',newErrnoException(err,'setRawMode'));
82104
returnthis;
83105
}
84-
this.isRaw=flag;
106+
this.isRaw=rawMode!==false;
107+
this.rawMode=rawMode;
85108
returnthis;
86109
};
87110

‎src/tty_wrap.cc‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ void TTYWrap::Initialize(Local<Object> target,
6868
SetProtoMethod(isolate, t, "setRawMode", SetRawMode);
6969

7070
SetMethodNoSideEffect(context, target, "isTTY", IsTTY);
71+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_NORMAL);
72+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_IO);
73+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_RAW_VT);
7174

7275
Local<Value> func;
7376
if (t->GetFunction(context).ToLocal(&func) &&
@@ -124,9 +127,10 @@ void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
124127
// sequences at all on Windows, such as bracketed paste mode.
125128
// The Node.js readline implementation handles differences between
126129
// these modes.
127-
int err = uv_tty_set_mode(
128-
&wrap->handle_,
129-
args[0]->IsTrue() ? UV_TTY_MODE_RAW_VT : UV_TTY_MODE_NORMAL);
130+
Environment* env = Environment::GetCurrent(args);
131+
int mode;
132+
if (!args[0]->Int32Value(env->context()).To(&mode)) return;
133+
int err = uv_tty_set_mode(&wrap->handle_, static_cast<uv_tty_mode_t>(mode));
130134
args.GetReturnValue().Set(err);
131135
}
132136

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
require('../common');
3+
constassert=require('assert');
4+
const{ spawnSync }=require('child_process');
5+
6+
functionisOnlcrEnabled(){
7+
const{ stdout, stderr, status }=spawnSync('stty',['-a'],{
8+
encoding: 'utf8',
9+
stdio: ['inherit','pipe','pipe'],
10+
});
11+
12+
assert.strictEqual(status,0,stderr);
13+
return/(?:^|[\s;])onlcr(?:[\s;]|$)/.test(stdout);
14+
}
15+
16+
process.stdin.setRawMode(true);
17+
console.log(`raw=${isOnlcrEnabled()}`);
18+
assert.strictEqual(process.stdin.isRaw,true);
19+
assert.strictEqual(process.stdin.rawMode,'raw');
20+
21+
process.stdin.setRawMode(false);
22+
console.log(`normal=${process.stdin.isRaw}`);
23+
assert.strictEqual(process.stdin.rawMode,false);
24+
assert.throws(
25+
()=>process.stdin.setRawMode('raw-vt'),
26+
{
27+
code: 'ERR_INVALID_ARG_VALUE',
28+
name: 'TypeError',
29+
});
30+
assert.strictEqual(process.stdin.rawMode,false);
31+
32+
process.stdin.setRawMode('raw');
33+
console.log(`raw-string=${isOnlcrEnabled()}`);
34+
assert.strictEqual(process.stdin.isRaw,true);
35+
assert.strictEqual(process.stdin.rawMode,'raw');
36+
37+
process.stdin.setRawMode(false);
38+
process.stdin.setRawMode('io');
39+
console.log(`io=${isOnlcrEnabled()}`);
40+
assert.strictEqual(process.stdin.isRaw,true);
41+
assert.strictEqual(process.stdin.rawMode,'io');
42+
43+
process.stdin.setRawMode(false);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
raw=true
2+
normal=false
3+
raw-string=true
4+
io=false

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit cf39500

Browse files
samuel-williams-shopifyaduh95
authored andcommitted
tty: add raw-vt and io raw modes
Signed-off-by: Samuel Williams <samuel.williams@shopify.com> PR-URL: #64140 Refs: #63059 Refs: libuv/libuv#32 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 7e9204f commit cf39500

5 files changed

Lines changed: 108 additions & 12 deletions

File tree

‎doc/api/tty.md‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,18 @@ A `boolean` that is always `true` for `tty.ReadStream` instances.
6969

7070
<!-- YAML
7171
added: v0.7.7
72+
changes:
73+
- version: REPLACEME
74+
pr-url: https://github.com/nodejs/node/pull/64140
75+
description: The `mode` argument supports `'raw'` and `'io'`.
7276
-->
7377

74-
*`mode` {boolean} If `true`, configures the `tty.ReadStream` to operate as a
75-
raw device. If `false`, configures the `tty.ReadStream` to operate in its
76-
default mode. The `readStream.isRaw` property will be set to the resulting
77-
mode.
78+
*`mode` {boolean|string} If `true` or `'raw'`, configures the
79+
`tty.ReadStream` to operate as a raw device. If `'io'`, configures the
80+
`tty.ReadStream` to operate in binary-safe I/O mode. If `false`, configures
81+
the `tty.ReadStream` to operate in its default mode. The `readStream.isRaw`
82+
property will be set to whether the stream is in raw mode, and the
83+
`readStream.rawMode` property will be set to the resulting mode.
7884
* Returns: {this} The read stream instance.
7985

8086
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
9197
(for passing into `new tty.ReadStream()`), be sure to use a read/write flag
9298
such as `'r+'`.
9399

100+
When in binary-safe I/O mode, terminal output processing is also disabled.
101+
This corresponds to libuv's `UV_TTY_MODE_IO` mode and is not supported on
102+
Windows.
103+
104+
### `readStream.rawMode`
105+
106+
<!-- YAML
107+
added: REPLACEME
108+
-->
109+
110+
* {boolean|string}
111+
112+
The current raw mode for the `tty.ReadStream`. This is `false` when the stream
113+
is in its default mode, `'raw'` when raw input mode is enabled, and `'io'` when
114+
binary-safe I/O mode is enabled.
115+
94116
## Class: `tty.WriteStream`
95117

96118
<!-- YAML

‎lib/tty.js‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,17 @@ const {
2727
}=primordials;
2828

2929
constnet=require('net');
30-
const{TTY, isTTY }=internalBinding('tty_wrap');
30+
const{
31+
TTY,
32+
UV_TTY_MODE_IO,
33+
UV_TTY_MODE_NORMAL,
34+
UV_TTY_MODE_RAW_VT,
35+
isTTY,
36+
}=internalBinding('tty_wrap');
3137
const{
3238
ErrnoException,
3339
codes: {
40+
ERR_INVALID_ARG_VALUE,
3441
ERR_INVALID_FD,
3542
ERR_TTY_INIT_FAILED,
3643
},
@@ -68,20 +75,36 @@ function ReadStream(fd, options) {
6875
});
6976

7077
this.isRaw=false;
78+
this.rawMode=false;
7179
this.isTTY=true;
7280
}
7381

7482
ObjectSetPrototypeOf(ReadStream.prototype,net.Socket.prototype);
7583
ObjectSetPrototypeOf(ReadStream,net.Socket);
7684

77-
ReadStream.prototype.setRawMode=function(flag){
78-
flag=!!flag;
79-
consterr=this._handle?.setRawMode(flag);
85+
ReadStream.prototype.setRawMode=function(mode){
86+
letrawMode;
87+
if(mode==='io'||mode==='raw'){
88+
rawMode=mode;
89+
}elseif(typeofmode==='string'){
90+
thrownewERR_INVALID_ARG_VALUE(
91+
'mode',mode,"must be true, false, 'raw', or 'io'");
92+
}else{
93+
rawMode=mode ? 'raw' : false;
94+
}
95+
letttyMode=UV_TTY_MODE_NORMAL;
96+
if(rawMode==='io'){
97+
ttyMode=UV_TTY_MODE_IO;
98+
}elseif(rawMode==='raw'){
99+
ttyMode=UV_TTY_MODE_RAW_VT;
100+
}
101+
consterr=this._handle?.setRawMode(ttyMode);
80102
if(err){
81103
this.emit('error',newErrnoException(err,'setRawMode'));
82104
returnthis;
83105
}
84-
this.isRaw=flag;
106+
this.isRaw=rawMode!==false;
107+
this.rawMode=rawMode;
85108
returnthis;
86109
};
87110

‎src/tty_wrap.cc‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ void TTYWrap::Initialize(Local<Object> target,
6868
SetProtoMethod(isolate, t, "setRawMode", SetRawMode);
6969

7070
SetMethodNoSideEffect(context, target, "isTTY", IsTTY);
71+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_NORMAL);
72+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_IO);
73+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_RAW_VT);
7174

7275
Local<Value> func;
7376
if (t->GetFunction(context).ToLocal(&func) &&
@@ -124,9 +127,10 @@ void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
124127
// sequences at all on Windows, such as bracketed paste mode.
125128
// The Node.js readline implementation handles differences between
126129
// these modes.
127-
int err = uv_tty_set_mode(
128-
&wrap->handle_,
129-
args[0]->IsTrue() ? UV_TTY_MODE_RAW_VT : UV_TTY_MODE_NORMAL);
130+
Environment* env = Environment::GetCurrent(args);
131+
int mode;
132+
if (!args[0]->Int32Value(env->context()).To(&mode)) return;
133+
int err = uv_tty_set_mode(&wrap->handle_, static_cast<uv_tty_mode_t>(mode));
130134
args.GetReturnValue().Set(err);
131135
}
132136

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
require('../common');
3+
constassert=require('assert');
4+
const{ spawnSync }=require('child_process');
5+
6+
functionisOnlcrEnabled(){
7+
const{ stdout, stderr, status }=spawnSync('stty',['-a'],{
8+
encoding: 'utf8',
9+
stdio: ['inherit','pipe','pipe'],
10+
});
11+
12+
assert.strictEqual(status,0,stderr);
13+
return/(?:^|[\s;])onlcr(?:[\s;]|$)/.test(stdout);
14+
}
15+
16+
process.stdin.setRawMode(true);
17+
console.log(`raw=${isOnlcrEnabled()}`);
18+
assert.strictEqual(process.stdin.isRaw,true);
19+
assert.strictEqual(process.stdin.rawMode,'raw');
20+
21+
process.stdin.setRawMode(false);
22+
console.log(`normal=${process.stdin.isRaw}`);
23+
assert.strictEqual(process.stdin.rawMode,false);
24+
assert.throws(
25+
()=>process.stdin.setRawMode('raw-vt'),
26+
{
27+
code: 'ERR_INVALID_ARG_VALUE',
28+
name: 'TypeError',
29+
});
30+
assert.strictEqual(process.stdin.rawMode,false);
31+
32+
process.stdin.setRawMode('raw');
33+
console.log(`raw-string=${isOnlcrEnabled()}`);
34+
assert.strictEqual(process.stdin.isRaw,true);
35+
assert.strictEqual(process.stdin.rawMode,'raw');
36+
37+
process.stdin.setRawMode(false);
38+
process.stdin.setRawMode('io');
39+
console.log(`io=${isOnlcrEnabled()}`);
40+
assert.strictEqual(process.stdin.isRaw,true);
41+
assert.strictEqual(process.stdin.rawMode,'io');
42+
43+
process.stdin.setRawMode(false);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
raw=true
2+
normal=false
3+
raw-string=true
4+
io=false

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit cf39500

Browse files
samuel-williams-shopifyaduh95
authored andcommitted
tty: add raw-vt and io raw modes
Signed-off-by: Samuel Williams <samuel.williams@shopify.com> PR-URL: #64140 Refs: #63059 Refs: libuv/libuv#32 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 7e9204f commit cf39500

5 files changed

Lines changed: 108 additions & 12 deletions

File tree

‎doc/api/tty.md‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,18 @@ A `boolean` that is always `true` for `tty.ReadStream` instances.
6969

7070
<!-- YAML
7171
added: v0.7.7
72+
changes:
73+
- version: REPLACEME
74+
pr-url: https://github.com/nodejs/node/pull/64140
75+
description: The `mode` argument supports `'raw'` and `'io'`.
7276
-->
7377

74-
*`mode` {boolean} If `true`, configures the `tty.ReadStream` to operate as a
75-
raw device. If `false`, configures the `tty.ReadStream` to operate in its
76-
default mode. The `readStream.isRaw` property will be set to the resulting
77-
mode.
78+
*`mode` {boolean|string} If `true` or `'raw'`, configures the
79+
`tty.ReadStream` to operate as a raw device. If `'io'`, configures the
80+
`tty.ReadStream` to operate in binary-safe I/O mode. If `false`, configures
81+
the `tty.ReadStream` to operate in its default mode. The `readStream.isRaw`
82+
property will be set to whether the stream is in raw mode, and the
83+
`readStream.rawMode` property will be set to the resulting mode.
7884
* Returns: {this} The read stream instance.
7985

8086
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
9197
(for passing into `new tty.ReadStream()`), be sure to use a read/write flag
9298
such as `'r+'`.
9399

100+
When in binary-safe I/O mode, terminal output processing is also disabled.
101+
This corresponds to libuv's `UV_TTY_MODE_IO` mode and is not supported on
102+
Windows.
103+
104+
### `readStream.rawMode`
105+
106+
<!-- YAML
107+
added: REPLACEME
108+
-->
109+
110+
* {boolean|string}
111+
112+
The current raw mode for the `tty.ReadStream`. This is `false` when the stream
113+
is in its default mode, `'raw'` when raw input mode is enabled, and `'io'` when
114+
binary-safe I/O mode is enabled.
115+
94116
## Class: `tty.WriteStream`
95117

96118
<!-- YAML

‎lib/tty.js‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,17 @@ const {
2727
}=primordials;
2828

2929
constnet=require('net');
30-
const{TTY, isTTY }=internalBinding('tty_wrap');
30+
const{
31+
TTY,
32+
UV_TTY_MODE_IO,
33+
UV_TTY_MODE_NORMAL,
34+
UV_TTY_MODE_RAW_VT,
35+
isTTY,
36+
}=internalBinding('tty_wrap');
3137
const{
3238
ErrnoException,
3339
codes: {
40+
ERR_INVALID_ARG_VALUE,
3441
ERR_INVALID_FD,
3542
ERR_TTY_INIT_FAILED,
3643
},
@@ -68,20 +75,36 @@ function ReadStream(fd, options) {
6875
});
6976

7077
this.isRaw=false;
78+
this.rawMode=false;
7179
this.isTTY=true;
7280
}
7381

7482
ObjectSetPrototypeOf(ReadStream.prototype,net.Socket.prototype);
7583
ObjectSetPrototypeOf(ReadStream,net.Socket);
7684

77-
ReadStream.prototype.setRawMode=function(flag){
78-
flag=!!flag;
79-
consterr=this._handle?.setRawMode(flag);
85+
ReadStream.prototype.setRawMode=function(mode){
86+
letrawMode;
87+
if(mode==='io'||mode==='raw'){
88+
rawMode=mode;
89+
}elseif(typeofmode==='string'){
90+
thrownewERR_INVALID_ARG_VALUE(
91+
'mode',mode,"must be true, false, 'raw', or 'io'");
92+
}else{
93+
rawMode=mode ? 'raw' : false;
94+
}
95+
letttyMode=UV_TTY_MODE_NORMAL;
96+
if(rawMode==='io'){
97+
ttyMode=UV_TTY_MODE_IO;
98+
}elseif(rawMode==='raw'){
99+
ttyMode=UV_TTY_MODE_RAW_VT;
100+
}
101+
consterr=this._handle?.setRawMode(ttyMode);
80102
if(err){
81103
this.emit('error',newErrnoException(err,'setRawMode'));
82104
returnthis;
83105
}
84-
this.isRaw=flag;
106+
this.isRaw=rawMode!==false;
107+
this.rawMode=rawMode;
85108
returnthis;
86109
};
87110

‎src/tty_wrap.cc‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ void TTYWrap::Initialize(Local<Object> target,
6868
SetProtoMethod(isolate, t, "setRawMode", SetRawMode);
6969

7070
SetMethodNoSideEffect(context, target, "isTTY", IsTTY);
71+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_NORMAL);
72+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_IO);
73+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_RAW_VT);
7174

7275
Local<Value> func;
7376
if (t->GetFunction(context).ToLocal(&func) &&
@@ -124,9 +127,10 @@ void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
124127
// sequences at all on Windows, such as bracketed paste mode.
125128
// The Node.js readline implementation handles differences between
126129
// these modes.
127-
int err = uv_tty_set_mode(
128-
&wrap->handle_,
129-
args[0]->IsTrue() ? UV_TTY_MODE_RAW_VT : UV_TTY_MODE_NORMAL);
130+
Environment* env = Environment::GetCurrent(args);
131+
int mode;
132+
if (!args[0]->Int32Value(env->context()).To(&mode)) return;
133+
int err = uv_tty_set_mode(&wrap->handle_, static_cast<uv_tty_mode_t>(mode));
130134
args.GetReturnValue().Set(err);
131135
}
132136

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
require('../common');
3+
constassert=require('assert');
4+
const{ spawnSync }=require('child_process');
5+
6+
functionisOnlcrEnabled(){
7+
const{ stdout, stderr, status }=spawnSync('stty',['-a'],{
8+
encoding: 'utf8',
9+
stdio: ['inherit','pipe','pipe'],
10+
});
11+
12+
assert.strictEqual(status,0,stderr);
13+
return/(?:^|[\s;])onlcr(?:[\s;]|$)/.test(stdout);
14+
}
15+
16+
process.stdin.setRawMode(true);
17+
console.log(`raw=${isOnlcrEnabled()}`);
18+
assert.strictEqual(process.stdin.isRaw,true);
19+
assert.strictEqual(process.stdin.rawMode,'raw');
20+
21+
process.stdin.setRawMode(false);
22+
console.log(`normal=${process.stdin.isRaw}`);
23+
assert.strictEqual(process.stdin.rawMode,false);
24+
assert.throws(
25+
()=>process.stdin.setRawMode('raw-vt'),
26+
{
27+
code: 'ERR_INVALID_ARG_VALUE',
28+
name: 'TypeError',
29+
});
30+
assert.strictEqual(process.stdin.rawMode,false);
31+
32+
process.stdin.setRawMode('raw');
33+
console.log(`raw-string=${isOnlcrEnabled()}`);
34+
assert.strictEqual(process.stdin.isRaw,true);
35+
assert.strictEqual(process.stdin.rawMode,'raw');
36+
37+
process.stdin.setRawMode(false);
38+
process.stdin.setRawMode('io');
39+
console.log(`io=${isOnlcrEnabled()}`);
40+
assert.strictEqual(process.stdin.isRaw,true);
41+
assert.strictEqual(process.stdin.rawMode,'io');
42+
43+
process.stdin.setRawMode(false);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
raw=true
2+
normal=false
3+
raw-string=true
4+
io=false

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit cf39500

Browse files
samuel-williams-shopifyaduh95
authored andcommitted
tty: add raw-vt and io raw modes
Signed-off-by: Samuel Williams <samuel.williams@shopify.com> PR-URL: #64140 Refs: #63059 Refs: libuv/libuv#32 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 7e9204f commit cf39500

5 files changed

Lines changed: 108 additions & 12 deletions

File tree

‎doc/api/tty.md‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,18 @@ A `boolean` that is always `true` for `tty.ReadStream` instances.
6969

7070
<!-- YAML
7171
added: v0.7.7
72+
changes:
73+
- version: REPLACEME
74+
pr-url: https://github.com/nodejs/node/pull/64140
75+
description: The `mode` argument supports `'raw'` and `'io'`.
7276
-->
7377

74-
*`mode` {boolean} If `true`, configures the `tty.ReadStream` to operate as a
75-
raw device. If `false`, configures the `tty.ReadStream` to operate in its
76-
default mode. The `readStream.isRaw` property will be set to the resulting
77-
mode.
78+
*`mode` {boolean|string} If `true` or `'raw'`, configures the
79+
`tty.ReadStream` to operate as a raw device. If `'io'`, configures the
80+
`tty.ReadStream` to operate in binary-safe I/O mode. If `false`, configures
81+
the `tty.ReadStream` to operate in its default mode. The `readStream.isRaw`
82+
property will be set to whether the stream is in raw mode, and the
83+
`readStream.rawMode` property will be set to the resulting mode.
7884
* Returns: {this} The read stream instance.
7985

8086
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
9197
(for passing into `new tty.ReadStream()`), be sure to use a read/write flag
9298
such as `'r+'`.
9399

100+
When in binary-safe I/O mode, terminal output processing is also disabled.
101+
This corresponds to libuv's `UV_TTY_MODE_IO` mode and is not supported on
102+
Windows.
103+
104+
### `readStream.rawMode`
105+
106+
<!-- YAML
107+
added: REPLACEME
108+
-->
109+
110+
* {boolean|string}
111+
112+
The current raw mode for the `tty.ReadStream`. This is `false` when the stream
113+
is in its default mode, `'raw'` when raw input mode is enabled, and `'io'` when
114+
binary-safe I/O mode is enabled.
115+
94116
## Class: `tty.WriteStream`
95117

96118
<!-- YAML

‎lib/tty.js‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,17 @@ const {
2727
}=primordials;
2828

2929
constnet=require('net');
30-
const{TTY, isTTY }=internalBinding('tty_wrap');
30+
const{
31+
TTY,
32+
UV_TTY_MODE_IO,
33+
UV_TTY_MODE_NORMAL,
34+
UV_TTY_MODE_RAW_VT,
35+
isTTY,
36+
}=internalBinding('tty_wrap');
3137
const{
3238
ErrnoException,
3339
codes: {
40+
ERR_INVALID_ARG_VALUE,
3441
ERR_INVALID_FD,
3542
ERR_TTY_INIT_FAILED,
3643
},
@@ -68,20 +75,36 @@ function ReadStream(fd, options) {
6875
});
6976

7077
this.isRaw=false;
78+
this.rawMode=false;
7179
this.isTTY=true;
7280
}
7381

7482
ObjectSetPrototypeOf(ReadStream.prototype,net.Socket.prototype);
7583
ObjectSetPrototypeOf(ReadStream,net.Socket);
7684

77-
ReadStream.prototype.setRawMode=function(flag){
78-
flag=!!flag;
79-
consterr=this._handle?.setRawMode(flag);
85+
ReadStream.prototype.setRawMode=function(mode){
86+
letrawMode;
87+
if(mode==='io'||mode==='raw'){
88+
rawMode=mode;
89+
}elseif(typeofmode==='string'){
90+
thrownewERR_INVALID_ARG_VALUE(
91+
'mode',mode,"must be true, false, 'raw', or 'io'");
92+
}else{
93+
rawMode=mode ? 'raw' : false;
94+
}
95+
letttyMode=UV_TTY_MODE_NORMAL;
96+
if(rawMode==='io'){
97+
ttyMode=UV_TTY_MODE_IO;
98+
}elseif(rawMode==='raw'){
99+
ttyMode=UV_TTY_MODE_RAW_VT;
100+
}
101+
consterr=this._handle?.setRawMode(ttyMode);
80102
if(err){
81103
this.emit('error',newErrnoException(err,'setRawMode'));
82104
returnthis;
83105
}
84-
this.isRaw=flag;
106+
this.isRaw=rawMode!==false;
107+
this.rawMode=rawMode;
85108
returnthis;
86109
};
87110

‎src/tty_wrap.cc‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ void TTYWrap::Initialize(Local<Object> target,
6868
SetProtoMethod(isolate, t, "setRawMode", SetRawMode);
6969

7070
SetMethodNoSideEffect(context, target, "isTTY", IsTTY);
71+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_NORMAL);
72+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_IO);
73+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_RAW_VT);
7174

7275
Local<Value> func;
7376
if (t->GetFunction(context).ToLocal(&func) &&
@@ -124,9 +127,10 @@ void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
124127
// sequences at all on Windows, such as bracketed paste mode.
125128
// The Node.js readline implementation handles differences between
126129
// these modes.
127-
int err = uv_tty_set_mode(
128-
&wrap->handle_,
129-
args[0]->IsTrue() ? UV_TTY_MODE_RAW_VT : UV_TTY_MODE_NORMAL);
130+
Environment* env = Environment::GetCurrent(args);
131+
int mode;
132+
if (!args[0]->Int32Value(env->context()).To(&mode)) return;
133+
int err = uv_tty_set_mode(&wrap->handle_, static_cast<uv_tty_mode_t>(mode));
130134
args.GetReturnValue().Set(err);
131135
}
132136

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
require('../common');
3+
constassert=require('assert');
4+
const{ spawnSync }=require('child_process');
5+
6+
functionisOnlcrEnabled(){
7+
const{ stdout, stderr, status }=spawnSync('stty',['-a'],{
8+
encoding: 'utf8',
9+
stdio: ['inherit','pipe','pipe'],
10+
});
11+
12+
assert.strictEqual(status,0,stderr);
13+
return/(?:^|[\s;])onlcr(?:[\s;]|$)/.test(stdout);
14+
}
15+
16+
process.stdin.setRawMode(true);
17+
console.log(`raw=${isOnlcrEnabled()}`);
18+
assert.strictEqual(process.stdin.isRaw,true);
19+
assert.strictEqual(process.stdin.rawMode,'raw');
20+
21+
process.stdin.setRawMode(false);
22+
console.log(`normal=${process.stdin.isRaw}`);
23+
assert.strictEqual(process.stdin.rawMode,false);
24+
assert.throws(
25+
()=>process.stdin.setRawMode('raw-vt'),
26+
{
27+
code: 'ERR_INVALID_ARG_VALUE',
28+
name: 'TypeError',
29+
});
30+
assert.strictEqual(process.stdin.rawMode,false);
31+
32+
process.stdin.setRawMode('raw');
33+
console.log(`raw-string=${isOnlcrEnabled()}`);
34+
assert.strictEqual(process.stdin.isRaw,true);
35+
assert.strictEqual(process.stdin.rawMode,'raw');
36+
37+
process.stdin.setRawMode(false);
38+
process.stdin.setRawMode('io');
39+
console.log(`io=${isOnlcrEnabled()}`);
40+
assert.strictEqual(process.stdin.isRaw,true);
41+
assert.strictEqual(process.stdin.rawMode,'io');
42+
43+
process.stdin.setRawMode(false);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
raw=true
2+
normal=false
3+
raw-string=true
4+
io=false

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit cf39500

Browse files
samuel-williams-shopifyaduh95
authored andcommitted
tty: add raw-vt and io raw modes
Signed-off-by: Samuel Williams <samuel.williams@shopify.com> PR-URL: #64140 Refs: #63059 Refs: libuv/libuv#32 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 7e9204f commit cf39500

5 files changed

Lines changed: 108 additions & 12 deletions

File tree

‎doc/api/tty.md‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,18 @@ A `boolean` that is always `true` for `tty.ReadStream` instances.
6969

7070
<!-- YAML
7171
added: v0.7.7
72+
changes:
73+
- version: REPLACEME
74+
pr-url: https://github.com/nodejs/node/pull/64140
75+
description: The `mode` argument supports `'raw'` and `'io'`.
7276
-->
7377

74-
*`mode` {boolean} If `true`, configures the `tty.ReadStream` to operate as a
75-
raw device. If `false`, configures the `tty.ReadStream` to operate in its
76-
default mode. The `readStream.isRaw` property will be set to the resulting
77-
mode.
78+
*`mode` {boolean|string} If `true` or `'raw'`, configures the
79+
`tty.ReadStream` to operate as a raw device. If `'io'`, configures the
80+
`tty.ReadStream` to operate in binary-safe I/O mode. If `false`, configures
81+
the `tty.ReadStream` to operate in its default mode. The `readStream.isRaw`
82+
property will be set to whether the stream is in raw mode, and the
83+
`readStream.rawMode` property will be set to the resulting mode.
7884
* Returns: {this} The read stream instance.
7985

8086
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
9197
(for passing into `new tty.ReadStream()`), be sure to use a read/write flag
9298
such as `'r+'`.
9399

100+
When in binary-safe I/O mode, terminal output processing is also disabled.
101+
This corresponds to libuv's `UV_TTY_MODE_IO` mode and is not supported on
102+
Windows.
103+
104+
### `readStream.rawMode`
105+
106+
<!-- YAML
107+
added: REPLACEME
108+
-->
109+
110+
* {boolean|string}
111+
112+
The current raw mode for the `tty.ReadStream`. This is `false` when the stream
113+
is in its default mode, `'raw'` when raw input mode is enabled, and `'io'` when
114+
binary-safe I/O mode is enabled.
115+
94116
## Class: `tty.WriteStream`
95117

96118
<!-- YAML

‎lib/tty.js‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,17 @@ const {
2727
}=primordials;
2828

2929
constnet=require('net');
30-
const{TTY, isTTY }=internalBinding('tty_wrap');
30+
const{
31+
TTY,
32+
UV_TTY_MODE_IO,
33+
UV_TTY_MODE_NORMAL,
34+
UV_TTY_MODE_RAW_VT,
35+
isTTY,
36+
}=internalBinding('tty_wrap');
3137
const{
3238
ErrnoException,
3339
codes: {
40+
ERR_INVALID_ARG_VALUE,
3441
ERR_INVALID_FD,
3542
ERR_TTY_INIT_FAILED,
3643
},
@@ -68,20 +75,36 @@ function ReadStream(fd, options) {
6875
});
6976

7077
this.isRaw=false;
78+
this.rawMode=false;
7179
this.isTTY=true;
7280
}
7381

7482
ObjectSetPrototypeOf(ReadStream.prototype,net.Socket.prototype);
7583
ObjectSetPrototypeOf(ReadStream,net.Socket);
7684

77-
ReadStream.prototype.setRawMode=function(flag){
78-
flag=!!flag;
79-
consterr=this._handle?.setRawMode(flag);
85+
ReadStream.prototype.setRawMode=function(mode){
86+
letrawMode;
87+
if(mode==='io'||mode==='raw'){
88+
rawMode=mode;
89+
}elseif(typeofmode==='string'){
90+
thrownewERR_INVALID_ARG_VALUE(
91+
'mode',mode,"must be true, false, 'raw', or 'io'");
92+
}else{
93+
rawMode=mode ? 'raw' : false;
94+
}
95+
letttyMode=UV_TTY_MODE_NORMAL;
96+
if(rawMode==='io'){
97+
ttyMode=UV_TTY_MODE_IO;
98+
}elseif(rawMode==='raw'){
99+
ttyMode=UV_TTY_MODE_RAW_VT;
100+
}
101+
consterr=this._handle?.setRawMode(ttyMode);
80102
if(err){
81103
this.emit('error',newErrnoException(err,'setRawMode'));
82104
returnthis;
83105
}
84-
this.isRaw=flag;
106+
this.isRaw=rawMode!==false;
107+
this.rawMode=rawMode;
85108
returnthis;
86109
};
87110

‎src/tty_wrap.cc‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ void TTYWrap::Initialize(Local<Object> target,
6868
SetProtoMethod(isolate, t, "setRawMode", SetRawMode);
6969

7070
SetMethodNoSideEffect(context, target, "isTTY", IsTTY);
71+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_NORMAL);
72+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_IO);
73+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_RAW_VT);
7174

7275
Local<Value> func;
7376
if (t->GetFunction(context).ToLocal(&func) &&
@@ -124,9 +127,10 @@ void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
124127
// sequences at all on Windows, such as bracketed paste mode.
125128
// The Node.js readline implementation handles differences between
126129
// these modes.
127-
int err = uv_tty_set_mode(
128-
&wrap->handle_,
129-
args[0]->IsTrue() ? UV_TTY_MODE_RAW_VT : UV_TTY_MODE_NORMAL);
130+
Environment* env = Environment::GetCurrent(args);
131+
int mode;
132+
if (!args[0]->Int32Value(env->context()).To(&mode)) return;
133+
int err = uv_tty_set_mode(&wrap->handle_, static_cast<uv_tty_mode_t>(mode));
130134
args.GetReturnValue().Set(err);
131135
}
132136

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
require('../common');
3+
constassert=require('assert');
4+
const{ spawnSync }=require('child_process');
5+
6+
functionisOnlcrEnabled(){
7+
const{ stdout, stderr, status }=spawnSync('stty',['-a'],{
8+
encoding: 'utf8',
9+
stdio: ['inherit','pipe','pipe'],
10+
});
11+
12+
assert.strictEqual(status,0,stderr);
13+
return/(?:^|[\s;])onlcr(?:[\s;]|$)/.test(stdout);
14+
}
15+
16+
process.stdin.setRawMode(true);
17+
console.log(`raw=${isOnlcrEnabled()}`);
18+
assert.strictEqual(process.stdin.isRaw,true);
19+
assert.strictEqual(process.stdin.rawMode,'raw');
20+
21+
process.stdin.setRawMode(false);
22+
console.log(`normal=${process.stdin.isRaw}`);
23+
assert.strictEqual(process.stdin.rawMode,false);
24+
assert.throws(
25+
()=>process.stdin.setRawMode('raw-vt'),
26+
{
27+
code: 'ERR_INVALID_ARG_VALUE',
28+
name: 'TypeError',
29+
});
30+
assert.strictEqual(process.stdin.rawMode,false);
31+
32+
process.stdin.setRawMode('raw');
33+
console.log(`raw-string=${isOnlcrEnabled()}`);
34+
assert.strictEqual(process.stdin.isRaw,true);
35+
assert.strictEqual(process.stdin.rawMode,'raw');
36+
37+
process.stdin.setRawMode(false);
38+
process.stdin.setRawMode('io');
39+
console.log(`io=${isOnlcrEnabled()}`);
40+
assert.strictEqual(process.stdin.isRaw,true);
41+
assert.strictEqual(process.stdin.rawMode,'io');
42+
43+
process.stdin.setRawMode(false);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
raw=true
2+
normal=false
3+
raw-string=true
4+
io=false

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit cf39500

Browse files
samuel-williams-shopifyaduh95
authored andcommitted
tty: add raw-vt and io raw modes
Signed-off-by: Samuel Williams <samuel.williams@shopify.com> PR-URL: #64140 Refs: #63059 Refs: libuv/libuv#32 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 7e9204f commit cf39500

5 files changed

Lines changed: 108 additions & 12 deletions

File tree

‎doc/api/tty.md‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,18 @@ A `boolean` that is always `true` for `tty.ReadStream` instances.
6969

7070
<!-- YAML
7171
added: v0.7.7
72+
changes:
73+
- version: REPLACEME
74+
pr-url: https://github.com/nodejs/node/pull/64140
75+
description: The `mode` argument supports `'raw'` and `'io'`.
7276
-->
7377

74-
*`mode` {boolean} If `true`, configures the `tty.ReadStream` to operate as a
75-
raw device. If `false`, configures the `tty.ReadStream` to operate in its
76-
default mode. The `readStream.isRaw` property will be set to the resulting
77-
mode.
78+
*`mode` {boolean|string} If `true` or `'raw'`, configures the
79+
`tty.ReadStream` to operate as a raw device. If `'io'`, configures the
80+
`tty.ReadStream` to operate in binary-safe I/O mode. If `false`, configures
81+
the `tty.ReadStream` to operate in its default mode. The `readStream.isRaw`
82+
property will be set to whether the stream is in raw mode, and the
83+
`readStream.rawMode` property will be set to the resulting mode.
7884
* Returns: {this} The read stream instance.
7985

8086
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
9197
(for passing into `new tty.ReadStream()`), be sure to use a read/write flag
9298
such as `'r+'`.
9399

100+
When in binary-safe I/O mode, terminal output processing is also disabled.
101+
This corresponds to libuv's `UV_TTY_MODE_IO` mode and is not supported on
102+
Windows.
103+
104+
### `readStream.rawMode`
105+
106+
<!-- YAML
107+
added: REPLACEME
108+
-->
109+
110+
* {boolean|string}
111+
112+
The current raw mode for the `tty.ReadStream`. This is `false` when the stream
113+
is in its default mode, `'raw'` when raw input mode is enabled, and `'io'` when
114+
binary-safe I/O mode is enabled.
115+
94116
## Class: `tty.WriteStream`
95117

96118
<!-- YAML

‎lib/tty.js‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,17 @@ const {
2727
}=primordials;
2828

2929
constnet=require('net');
30-
const{TTY, isTTY }=internalBinding('tty_wrap');
30+
const{
31+
TTY,
32+
UV_TTY_MODE_IO,
33+
UV_TTY_MODE_NORMAL,
34+
UV_TTY_MODE_RAW_VT,
35+
isTTY,
36+
}=internalBinding('tty_wrap');
3137
const{
3238
ErrnoException,
3339
codes: {
40+
ERR_INVALID_ARG_VALUE,
3441
ERR_INVALID_FD,
3542
ERR_TTY_INIT_FAILED,
3643
},
@@ -68,20 +75,36 @@ function ReadStream(fd, options) {
6875
});
6976

7077
this.isRaw=false;
78+
this.rawMode=false;
7179
this.isTTY=true;
7280
}
7381

7482
ObjectSetPrototypeOf(ReadStream.prototype,net.Socket.prototype);
7583
ObjectSetPrototypeOf(ReadStream,net.Socket);
7684

77-
ReadStream.prototype.setRawMode=function(flag){
78-
flag=!!flag;
79-
consterr=this._handle?.setRawMode(flag);
85+
ReadStream.prototype.setRawMode=function(mode){
86+
letrawMode;
87+
if(mode==='io'||mode==='raw'){
88+
rawMode=mode;
89+
}elseif(typeofmode==='string'){
90+
thrownewERR_INVALID_ARG_VALUE(
91+
'mode',mode,"must be true, false, 'raw', or 'io'");
92+
}else{
93+
rawMode=mode ? 'raw' : false;
94+
}
95+
letttyMode=UV_TTY_MODE_NORMAL;
96+
if(rawMode==='io'){
97+
ttyMode=UV_TTY_MODE_IO;
98+
}elseif(rawMode==='raw'){
99+
ttyMode=UV_TTY_MODE_RAW_VT;
100+
}
101+
consterr=this._handle?.setRawMode(ttyMode);
80102
if(err){
81103
this.emit('error',newErrnoException(err,'setRawMode'));
82104
returnthis;
83105
}
84-
this.isRaw=flag;
106+
this.isRaw=rawMode!==false;
107+
this.rawMode=rawMode;
85108
returnthis;
86109
};
87110

‎src/tty_wrap.cc‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ void TTYWrap::Initialize(Local<Object> target,
6868
SetProtoMethod(isolate, t, "setRawMode", SetRawMode);
6969

7070
SetMethodNoSideEffect(context, target, "isTTY", IsTTY);
71+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_NORMAL);
72+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_IO);
73+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_RAW_VT);
7174

7275
Local<Value> func;
7376
if (t->GetFunction(context).ToLocal(&func) &&
@@ -124,9 +127,10 @@ void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
124127
// sequences at all on Windows, such as bracketed paste mode.
125128
// The Node.js readline implementation handles differences between
126129
// these modes.
127-
int err = uv_tty_set_mode(
128-
&wrap->handle_,
129-
args[0]->IsTrue() ? UV_TTY_MODE_RAW_VT : UV_TTY_MODE_NORMAL);
130+
Environment* env = Environment::GetCurrent(args);
131+
int mode;
132+
if (!args[0]->Int32Value(env->context()).To(&mode)) return;
133+
int err = uv_tty_set_mode(&wrap->handle_, static_cast<uv_tty_mode_t>(mode));
130134
args.GetReturnValue().Set(err);
131135
}
132136

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
require('../common');
3+
constassert=require('assert');
4+
const{ spawnSync }=require('child_process');
5+
6+
functionisOnlcrEnabled(){
7+
const{ stdout, stderr, status }=spawnSync('stty',['-a'],{
8+
encoding: 'utf8',
9+
stdio: ['inherit','pipe','pipe'],
10+
});
11+
12+
assert.strictEqual(status,0,stderr);
13+
return/(?:^|[\s;])onlcr(?:[\s;]|$)/.test(stdout);
14+
}
15+
16+
process.stdin.setRawMode(true);
17+
console.log(`raw=${isOnlcrEnabled()}`);
18+
assert.strictEqual(process.stdin.isRaw,true);
19+
assert.strictEqual(process.stdin.rawMode,'raw');
20+
21+
process.stdin.setRawMode(false);
22+
console.log(`normal=${process.stdin.isRaw}`);
23+
assert.strictEqual(process.stdin.rawMode,false);
24+
assert.throws(
25+
()=>process.stdin.setRawMode('raw-vt'),
26+
{
27+
code: 'ERR_INVALID_ARG_VALUE',
28+
name: 'TypeError',
29+
});
30+
assert.strictEqual(process.stdin.rawMode,false);
31+
32+
process.stdin.setRawMode('raw');
33+
console.log(`raw-string=${isOnlcrEnabled()}`);
34+
assert.strictEqual(process.stdin.isRaw,true);
35+
assert.strictEqual(process.stdin.rawMode,'raw');
36+
37+
process.stdin.setRawMode(false);
38+
process.stdin.setRawMode('io');
39+
console.log(`io=${isOnlcrEnabled()}`);
40+
assert.strictEqual(process.stdin.isRaw,true);
41+
assert.strictEqual(process.stdin.rawMode,'io');
42+
43+
process.stdin.setRawMode(false);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
raw=true
2+
normal=false
3+
raw-string=true
4+
io=false

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit cf39500

Browse files
samuel-williams-shopifyaduh95
authored andcommitted
tty: add raw-vt and io raw modes
Signed-off-by: Samuel Williams <samuel.williams@shopify.com> PR-URL: #64140 Refs: #63059 Refs: libuv/libuv#32 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 7e9204f commit cf39500

5 files changed

Lines changed: 108 additions & 12 deletions

File tree

‎doc/api/tty.md‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,18 @@ A `boolean` that is always `true` for `tty.ReadStream` instances.
6969

7070
<!-- YAML
7171
added: v0.7.7
72+
changes:
73+
- version: REPLACEME
74+
pr-url: https://github.com/nodejs/node/pull/64140
75+
description: The `mode` argument supports `'raw'` and `'io'`.
7276
-->
7377

74-
*`mode` {boolean} If `true`, configures the `tty.ReadStream` to operate as a
75-
raw device. If `false`, configures the `tty.ReadStream` to operate in its
76-
default mode. The `readStream.isRaw` property will be set to the resulting
77-
mode.
78+
*`mode` {boolean|string} If `true` or `'raw'`, configures the
79+
`tty.ReadStream` to operate as a raw device. If `'io'`, configures the
80+
`tty.ReadStream` to operate in binary-safe I/O mode. If `false`, configures
81+
the `tty.ReadStream` to operate in its default mode. The `readStream.isRaw`
82+
property will be set to whether the stream is in raw mode, and the
83+
`readStream.rawMode` property will be set to the resulting mode.
7884
* Returns: {this} The read stream instance.
7985

8086
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
9197
(for passing into `new tty.ReadStream()`), be sure to use a read/write flag
9298
such as `'r+'`.
9399

100+
When in binary-safe I/O mode, terminal output processing is also disabled.
101+
This corresponds to libuv's `UV_TTY_MODE_IO` mode and is not supported on
102+
Windows.
103+
104+
### `readStream.rawMode`
105+
106+
<!-- YAML
107+
added: REPLACEME
108+
-->
109+
110+
* {boolean|string}
111+
112+
The current raw mode for the `tty.ReadStream`. This is `false` when the stream
113+
is in its default mode, `'raw'` when raw input mode is enabled, and `'io'` when
114+
binary-safe I/O mode is enabled.
115+
94116
## Class: `tty.WriteStream`
95117

96118
<!-- YAML

‎lib/tty.js‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,17 @@ const {
2727
}=primordials;
2828

2929
constnet=require('net');
30-
const{TTY, isTTY }=internalBinding('tty_wrap');
30+
const{
31+
TTY,
32+
UV_TTY_MODE_IO,
33+
UV_TTY_MODE_NORMAL,
34+
UV_TTY_MODE_RAW_VT,
35+
isTTY,
36+
}=internalBinding('tty_wrap');
3137
const{
3238
ErrnoException,
3339
codes: {
40+
ERR_INVALID_ARG_VALUE,
3441
ERR_INVALID_FD,
3542
ERR_TTY_INIT_FAILED,
3643
},
@@ -68,20 +75,36 @@ function ReadStream(fd, options) {
6875
});
6976

7077
this.isRaw=false;
78+
this.rawMode=false;
7179
this.isTTY=true;
7280
}
7381

7482
ObjectSetPrototypeOf(ReadStream.prototype,net.Socket.prototype);
7583
ObjectSetPrototypeOf(ReadStream,net.Socket);
7684

77-
ReadStream.prototype.setRawMode=function(flag){
78-
flag=!!flag;
79-
consterr=this._handle?.setRawMode(flag);
85+
ReadStream.prototype.setRawMode=function(mode){
86+
letrawMode;
87+
if(mode==='io'||mode==='raw'){
88+
rawMode=mode;
89+
}elseif(typeofmode==='string'){
90+
thrownewERR_INVALID_ARG_VALUE(
91+
'mode',mode,"must be true, false, 'raw', or 'io'");
92+
}else{
93+
rawMode=mode ? 'raw' : false;
94+
}
95+
letttyMode=UV_TTY_MODE_NORMAL;
96+
if(rawMode==='io'){
97+
ttyMode=UV_TTY_MODE_IO;
98+
}elseif(rawMode==='raw'){
99+
ttyMode=UV_TTY_MODE_RAW_VT;
100+
}
101+
consterr=this._handle?.setRawMode(ttyMode);
80102
if(err){
81103
this.emit('error',newErrnoException(err,'setRawMode'));
82104
returnthis;
83105
}
84-
this.isRaw=flag;
106+
this.isRaw=rawMode!==false;
107+
this.rawMode=rawMode;
85108
returnthis;
86109
};
87110

‎src/tty_wrap.cc‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ void TTYWrap::Initialize(Local<Object> target,
6868
SetProtoMethod(isolate, t, "setRawMode", SetRawMode);
6969

7070
SetMethodNoSideEffect(context, target, "isTTY", IsTTY);
71+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_NORMAL);
72+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_IO);
73+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_RAW_VT);
7174

7275
Local<Value> func;
7376
if (t->GetFunction(context).ToLocal(&func) &&
@@ -124,9 +127,10 @@ void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
124127
// sequences at all on Windows, such as bracketed paste mode.
125128
// The Node.js readline implementation handles differences between
126129
// these modes.
127-
int err = uv_tty_set_mode(
128-
&wrap->handle_,
129-
args[0]->IsTrue() ? UV_TTY_MODE_RAW_VT : UV_TTY_MODE_NORMAL);
130+
Environment* env = Environment::GetCurrent(args);
131+
int mode;
132+
if (!args[0]->Int32Value(env->context()).To(&mode)) return;
133+
int err = uv_tty_set_mode(&wrap->handle_, static_cast<uv_tty_mode_t>(mode));
130134
args.GetReturnValue().Set(err);
131135
}
132136

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
require('../common');
3+
constassert=require('assert');
4+
const{ spawnSync }=require('child_process');
5+
6+
functionisOnlcrEnabled(){
7+
const{ stdout, stderr, status }=spawnSync('stty',['-a'],{
8+
encoding: 'utf8',
9+
stdio: ['inherit','pipe','pipe'],
10+
});
11+
12+
assert.strictEqual(status,0,stderr);
13+
return/(?:^|[\s;])onlcr(?:[\s;]|$)/.test(stdout);
14+
}
15+
16+
process.stdin.setRawMode(true);
17+
console.log(`raw=${isOnlcrEnabled()}`);
18+
assert.strictEqual(process.stdin.isRaw,true);
19+
assert.strictEqual(process.stdin.rawMode,'raw');
20+
21+
process.stdin.setRawMode(false);
22+
console.log(`normal=${process.stdin.isRaw}`);
23+
assert.strictEqual(process.stdin.rawMode,false);
24+
assert.throws(
25+
()=>process.stdin.setRawMode('raw-vt'),
26+
{
27+
code: 'ERR_INVALID_ARG_VALUE',
28+
name: 'TypeError',
29+
});
30+
assert.strictEqual(process.stdin.rawMode,false);
31+
32+
process.stdin.setRawMode('raw');
33+
console.log(`raw-string=${isOnlcrEnabled()}`);
34+
assert.strictEqual(process.stdin.isRaw,true);
35+
assert.strictEqual(process.stdin.rawMode,'raw');
36+
37+
process.stdin.setRawMode(false);
38+
process.stdin.setRawMode('io');
39+
console.log(`io=${isOnlcrEnabled()}`);
40+
assert.strictEqual(process.stdin.isRaw,true);
41+
assert.strictEqual(process.stdin.rawMode,'io');
42+
43+
process.stdin.setRawMode(false);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
raw=true
2+
normal=false
3+
raw-string=true
4+
io=false

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit cf39500

Browse files
samuel-williams-shopifyaduh95
authored andcommitted
tty: add raw-vt and io raw modes
Signed-off-by: Samuel Williams <samuel.williams@shopify.com> PR-URL: #64140 Refs: #63059 Refs: libuv/libuv#32 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 7e9204f commit cf39500

5 files changed

Lines changed: 108 additions & 12 deletions

File tree

‎doc/api/tty.md‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,18 @@ A `boolean` that is always `true` for `tty.ReadStream` instances.
6969

7070
<!-- YAML
7171
added: v0.7.7
72+
changes:
73+
- version: REPLACEME
74+
pr-url: https://github.com/nodejs/node/pull/64140
75+
description: The `mode` argument supports `'raw'` and `'io'`.
7276
-->
7377

74-
*`mode` {boolean} If `true`, configures the `tty.ReadStream` to operate as a
75-
raw device. If `false`, configures the `tty.ReadStream` to operate in its
76-
default mode. The `readStream.isRaw` property will be set to the resulting
77-
mode.
78+
*`mode` {boolean|string} If `true` or `'raw'`, configures the
79+
`tty.ReadStream` to operate as a raw device. If `'io'`, configures the
80+
`tty.ReadStream` to operate in binary-safe I/O mode. If `false`, configures
81+
the `tty.ReadStream` to operate in its default mode. The `readStream.isRaw`
82+
property will be set to whether the stream is in raw mode, and the
83+
`readStream.rawMode` property will be set to the resulting mode.
7884
* Returns: {this} The read stream instance.
7985

8086
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
9197
(for passing into `new tty.ReadStream()`), be sure to use a read/write flag
9298
such as `'r+'`.
9399

100+
When in binary-safe I/O mode, terminal output processing is also disabled.
101+
This corresponds to libuv's `UV_TTY_MODE_IO` mode and is not supported on
102+
Windows.
103+
104+
### `readStream.rawMode`
105+
106+
<!-- YAML
107+
added: REPLACEME
108+
-->
109+
110+
* {boolean|string}
111+
112+
The current raw mode for the `tty.ReadStream`. This is `false` when the stream
113+
is in its default mode, `'raw'` when raw input mode is enabled, and `'io'` when
114+
binary-safe I/O mode is enabled.
115+
94116
## Class: `tty.WriteStream`
95117

96118
<!-- YAML

‎lib/tty.js‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,17 @@ const {
2727
}=primordials;
2828

2929
constnet=require('net');
30-
const{TTY, isTTY }=internalBinding('tty_wrap');
30+
const{
31+
TTY,
32+
UV_TTY_MODE_IO,
33+
UV_TTY_MODE_NORMAL,
34+
UV_TTY_MODE_RAW_VT,
35+
isTTY,
36+
}=internalBinding('tty_wrap');
3137
const{
3238
ErrnoException,
3339
codes: {
40+
ERR_INVALID_ARG_VALUE,
3441
ERR_INVALID_FD,
3542
ERR_TTY_INIT_FAILED,
3643
},
@@ -68,20 +75,36 @@ function ReadStream(fd, options) {
6875
});
6976

7077
this.isRaw=false;
78+
this.rawMode=false;
7179
this.isTTY=true;
7280
}
7381

7482
ObjectSetPrototypeOf(ReadStream.prototype,net.Socket.prototype);
7583
ObjectSetPrototypeOf(ReadStream,net.Socket);
7684

77-
ReadStream.prototype.setRawMode=function(flag){
78-
flag=!!flag;
79-
consterr=this._handle?.setRawMode(flag);
85+
ReadStream.prototype.setRawMode=function(mode){
86+
letrawMode;
87+
if(mode==='io'||mode==='raw'){
88+
rawMode=mode;
89+
}elseif(typeofmode==='string'){
90+
thrownewERR_INVALID_ARG_VALUE(
91+
'mode',mode,"must be true, false, 'raw', or 'io'");
92+
}else{
93+
rawMode=mode ? 'raw' : false;
94+
}
95+
letttyMode=UV_TTY_MODE_NORMAL;
96+
if(rawMode==='io'){
97+
ttyMode=UV_TTY_MODE_IO;
98+
}elseif(rawMode==='raw'){
99+
ttyMode=UV_TTY_MODE_RAW_VT;
100+
}
101+
consterr=this._handle?.setRawMode(ttyMode);
80102
if(err){
81103
this.emit('error',newErrnoException(err,'setRawMode'));
82104
returnthis;
83105
}
84-
this.isRaw=flag;
106+
this.isRaw=rawMode!==false;
107+
this.rawMode=rawMode;
85108
returnthis;
86109
};
87110

‎src/tty_wrap.cc‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ void TTYWrap::Initialize(Local<Object> target,
6868
SetProtoMethod(isolate, t, "setRawMode", SetRawMode);
6969

7070
SetMethodNoSideEffect(context, target, "isTTY", IsTTY);
71+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_NORMAL);
72+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_IO);
73+
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_RAW_VT);
7174

7275
Local<Value> func;
7376
if (t->GetFunction(context).ToLocal(&func) &&
@@ -124,9 +127,10 @@ void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
124127
// sequences at all on Windows, such as bracketed paste mode.
125128
// The Node.js readline implementation handles differences between
126129
// these modes.
127-
int err = uv_tty_set_mode(
128-
&wrap->handle_,
129-
args[0]->IsTrue() ? UV_TTY_MODE_RAW_VT : UV_TTY_MODE_NORMAL);
130+
Environment* env = Environment::GetCurrent(args);
131+
int mode;
132+
if (!args[0]->Int32Value(env->context()).To(&mode)) return;
133+
int err = uv_tty_set_mode(&wrap->handle_, static_cast<uv_tty_mode_t>(mode));
130134
args.GetReturnValue().Set(err);
131135
}
132136

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
require('../common');
3+
constassert=require('assert');
4+
const{ spawnSync }=require('child_process');
5+
6+
functionisOnlcrEnabled(){
7+
const{ stdout, stderr, status }=spawnSync('stty',['-a'],{
8+
encoding: 'utf8',
9+
stdio: ['inherit','pipe','pipe'],
10+
});
11+
12+
assert.strictEqual(status,0,stderr);
13+
return/(?:^|[\s;])onlcr(?:[\s;]|$)/.test(stdout);
14+
}
15+
16+
process.stdin.setRawMode(true);
17+
console.log(`raw=${isOnlcrEnabled()}`);
18+
assert.strictEqual(process.stdin.isRaw,true);
19+
assert.strictEqual(process.stdin.rawMode,'raw');
20+
21+
process.stdin.setRawMode(false);
22+
console.log(`normal=${process.stdin.isRaw}`);
23+
assert.strictEqual(process.stdin.rawMode,false);
24+
assert.throws(
25+
()=>process.stdin.setRawMode('raw-vt'),
26+
{
27+
code: 'ERR_INVALID_ARG_VALUE',
28+
name: 'TypeError',
29+
});
30+
assert.strictEqual(process.stdin.rawMode,false);
31+
32+
process.stdin.setRawMode('raw');
33+
console.log(`raw-string=${isOnlcrEnabled()}`);
34+
assert.strictEqual(process.stdin.isRaw,true);
35+
assert.strictEqual(process.stdin.rawMode,'raw');
36+
37+
process.stdin.setRawMode(false);
38+
process.stdin.setRawMode('io');
39+
console.log(`io=${isOnlcrEnabled()}`);
40+
assert.strictEqual(process.stdin.isRaw,true);
41+
assert.strictEqual(process.stdin.rawMode,'io');
42+
43+
process.stdin.setRawMode(false);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
raw=true
2+
normal=false
3+
raw-string=true
4+
io=false

0 commit comments

Comments
 (0)