Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to open exist tty using net.Socket ? #37780

Closed
oppai opened this issue Mar 16, 2021 · 6 comments · Fixed by #53567
Closed

How to open exist tty using net.Socket ? #37780

oppai opened this issue Mar 16, 2021 · 6 comments · Fixed by #53567
Labels
doc Issues and PRs related to the documentations. tty Issues and PRs related to the tty subsystem.

Comments

@oppai
Copy link

oppai commented Mar 16, 2021

Hi, there

I want to open exist tty using net.Socket. But I got an error like a below.

I have two questions.
First, how can I open tty using net.Socket module ?
Second, I success to open tty using tty.ReadStream like a them. But I didn't find it in document. It is trial function? (It is not safe to use in production?)

thank you

  • Node.js Version: v14.16.0
  • OS: MacOS / Linux
  • Scope (install, code, runtime, meta, other?): code
  • Module (and version) (if relevant): net.Socket
var fs = require('fs')
var net = require('net')

const fd = fs.openSync('/dev/ttys003', 'r+');
const tty = new net.Socket({fd, readable: true})
tty.on('data', (data) => {
  console.log(data)
})
TypeError [ERR_INVALID_FD_TYPE]: Unsupported fd type: TTY
    at createHandle (net.js:147:9)
    at new Socket (net.js:328:22)
    at Object.<anonymous> (/Users/hiroaki.murayama/progate/progate_ide/v2/src/runtime/sample.js:5:13)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'ERR_INVALID_FD_TYPE'
@RaisinTen
Copy link
Contributor

I don't think opening a socket to a TTY fd is supported yet.
At the bottom of https://nodejs.org/api/net.html#net_class_net_socket:

The newly created socket can be either a TCP socket or a streaming IPC endpoint, depending on what it connect() to.

@addaleax
Copy link
Member

@RaisinTen No, it is supported. I think this might also qualify as a bug in the net module.

In any case, one can use tty.ReadStream(fd) and tty.WriteStream(fd) to create TTY streams (which should be one class but for some reason isn’t), and both of these inherit from net.Socket.

@RaisinTen
Copy link
Contributor

@addaleax The passed fd type gets rejected here:

node/lib/net.js

Lines 140 to 156 in 3bba40a

function createHandle(fd, is_server) {
validateInt32(fd, 'fd', 0);
const type = guessHandleType(fd);
if (type === 'PIPE') {
return new Pipe(
is_server ? PipeConstants.SERVER : PipeConstants.SOCKET
);
}
if (type === 'TCP') {
return new TCP(
is_server ? TCPConstants.SERVER : TCPConstants.SOCKET
);
}
throw new ERR_INVALID_FD_TYPE(type);
}
. So, this can be fixed by adding an option to accept TTY fds. Should we move this issue to the main repo?

@addaleax
Copy link
Member

Yeah, although I think there might also be another issue somewhere for merging the two TTY classes? I think that would be the best solution here.

@oppai
Copy link
Author

oppai commented Mar 17, 2021

Thank you, @addaleax and @RaisinTen !
I understand that net.Socket is not supported for tty fd, and I should use tty module instead of net.Socket.

So, there is no document about accepting fd in tty modules.
https://nodejs.org/api/tty.html#tty_class_tty_readstream

it is missing? or experimental function?

@addaleax
Copy link
Member

I don’t know why the constructors are not documented, but they should be, yes.

@RaisinTen RaisinTen transferred this issue from nodejs/help Mar 17, 2021
@Ayase-252 Ayase-252 added doc Issues and PRs related to the documentations. tty Issues and PRs related to the tty subsystem. labels Mar 31, 2021
Ayase-252 added a commit to Ayase-252/node that referenced this issue Apr 1, 2021
deepak1556 pushed a commit to microsoft/node-pty that referenced this issue Mar 18, 2024
* Remove deprecated API `process.binding`

Originally designed to work with ancient node.js 0.12 and io.js

* Fix node `net.Socket` limitations

nodejs/node#37780
targos pushed a commit that referenced this issue Jul 28, 2024
Co-authored-by: Qingyu Deng <i@ayase-lab.com>
PR-URL: #53567
Fixes: #37780
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
Reviewed-By: Claudio Wunder <cwunder@gnome.org>
marco-ippolito pushed a commit that referenced this issue Aug 19, 2024
Co-authored-by: Qingyu Deng <i@ayase-lab.com>
PR-URL: #53567
Fixes: #37780
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
Reviewed-By: Claudio Wunder <cwunder@gnome.org>
marco-ippolito pushed a commit that referenced this issue Aug 19, 2024
Co-authored-by: Qingyu Deng <i@ayase-lab.com>
PR-URL: #53567
Fixes: #37780
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
Reviewed-By: Claudio Wunder <cwunder@gnome.org>
marco-ippolito pushed a commit that referenced this issue Aug 19, 2024
Co-authored-by: Qingyu Deng <i@ayase-lab.com>
PR-URL: #53567
Fixes: #37780
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
Reviewed-By: Claudio Wunder <cwunder@gnome.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. tty Issues and PRs related to the tty subsystem.
Projects
None yet
4 participants