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 get file descriptor of process.{stdin,stdout,stderr} #2136

Closed
ORESoftware opened this issue Aug 21, 2019 · 5 comments
Closed

how to get file descriptor of process.{stdin,stdout,stderr} #2136

ORESoftware opened this issue Aug 21, 2019 · 5 comments

Comments

@ORESoftware
Copy link

On Node.js 8+, is there a way to get file descriptors for process.stdin, process.stdout, process.stderr in the node.js runtime?

@addaleax
Copy link
Member

Is process.stdin.fd, process.stdout.fd, process.stderr.fd what you’re looking for? They do have fixed values, i.e. 0, 1 and 2, unless you are running code inside a Worker (in which case they are not associated with fds) or the streams have been overwritten with other objects.

@ORESoftware
Copy link
Author

that should do, i think i had this same problem before, typescript typings didnt mention the fd's

@gireeshpunathil
Copy link
Member

closing as answered

HarshithaKP added a commit to HarshithaKP/node that referenced this issue Jan 17, 2020
addaleax pushed a commit to nodejs/node that referenced this issue Jan 20, 2020
Fixes: #28386
Refs: #31292
Refs: nodejs/help#2136

PR-URL: #31395
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
codebytere pushed a commit to nodejs/node that referenced this issue Feb 17, 2020
Fixes: #28386
Refs: #31292
Refs: nodejs/help#2136

PR-URL: #31395
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
codebytere pushed a commit to nodejs/node that referenced this issue Mar 14, 2020
Fixes: #28386
Refs: #31292
Refs: nodejs/help#2136

PR-URL: #31395
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
codebytere pushed a commit to nodejs/node that referenced this issue Mar 17, 2020
Fixes: #28386
Refs: #31292
Refs: nodejs/help#2136

PR-URL: #31395
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
@ORESoftware
Copy link
Author

anyone know how this can be accomplished given a file descriptor perhaps in a Node.js process?
https://unix.stackexchange.com/questions/576954/discover-what-file-stdout-is-being-written-to

@martian17
Copy link

martian17 commented Apr 3, 2023

process.stdin.fd seems to induce EAGAIN error when passed as an argument to fs.readSync.
When passing a literal 0 no error is thrown.
If this is a bug I'll file an issue.
Here is an example code

const fs = require("fs");

const getchar = function(){
    const buffer = Buffer.alloc(1);
    fs.readSync(process.stdin.fd/*0*/, buffer, 0, 1);
    return buffer[0];
};

const readline = function(pmt){
    process.stdout.write(pmt);
    let c;
    const buff = [];
    while((c = getchar()) !== '\n'.charCodeAt(0)){
        buff.push(c);
    }
    return Buffer.from(buff).toString("utf8");
}

const age = readline("your age: ");
console.log(`You are ${age} years old`);
const name = readline("your name: ");
console.log(`Your name is ${name}`);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants