Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown method process.openStdin()

I'm trying to pipe grep results into nodejs script. I've found, that I should receive data from process.stdin.

Also I've found several ways to work with stdin. But they are different and I can't find all information about it. I know four ways (first 3 start with var data = ""):

1) Most popular in search results

process.stdin.resume();
process.stdin.setEncoding( 'utf8' );
process.stdin.on('data', function(chunk) { data += chunk; });
process.stdin.on('end', function() { console.log('data: ' + data); });

2) Looks like the first one, but with unknown function process.openStdin()

var stdin = process.openStdin();
stdin.on('data', function(chunk) { data += chunk; });
stdin.on('end', function() { console.log('data: ' + data); });

3) In the documentation I've read that calling stdin.resume() changes stdin to 'old type'. So if we didn't called 'resume' - we can use 'readable' event

process.stdin.setEncoding('utf8');
process.stdin.on('readable', function() { data += process.stdin.read(); });
process.stdin.on('end', function() { console.log('data: ' + data); });

4) Use module readline. It is very usefull as long as grep results are in mutiple lines and there I don't need to split received data by myself. But for a long time i couldn't understand why all information is piped to stdout directly. Then i i've found that we can pass empty object instead of process.stdout while creating interface and data wouldn't piped to output.

var readline = require('readline'),
//rl = readline.createInterface(process.stdin, process.stdout);
rl = readline.createInterface(process.stdin, {});
rl.on('line', function(data) { console.log('line: ' + data); });

5) My own variant. Use another module 'split' - it allows to read from stream and devide data into chuks by specified symbol (\r?\n by default). I used it to work with socket and as soon as stdin is also readable stream - we can use it here.

var split = require('split');
process.stdin.setEncoding('utf8');
process.stdin.pipe(split()).on('data', function(data) { console.log('line: ' + data); });

My question is "What is process.openStdin();????"

I've searched every page in google, but didn't found any documentation on this function!

Also while searching I've discovered, that official documentation for nodejs is ugly - not mentioned since which version methods are available, no detailed description on many objects/methods, no user comments. And this method (openStdin) - exists and works, but nowhere discribed! WTF???

like image 590
porfirion Avatar asked Nov 20 '15 16:11

porfirion


People also ask

How do I end a node js process?

Method 1: Using the Ctrl+C key I hope everyone knows this shortcut to exit any Node. js process from outside. This shortcut can be used to stop any running process by hitting this command on terminal.

What is process Hrtime?

The process. hrtime() method to measure code execution time which returns array which include current high-resolution real time in a [seconds, nanoseconds].

What is process CWD?

process. cwd() returns the current working directory, i.e. the directory from which you invoked the node command. __dirname returns the directory name of the directory containing the JavaScript source code file.

What is process exit in JavaScript?

The process. exit() method is used to end the process which is running at the same time with an exit code in NodeJS. Syntax: process.exit( code )


1 Answers

While writing the question I've found the answer :)

It is in source code of nodejs:

process.openStdin = function() {
  process.stdin.resume();
  return process.stdin;
};

But I wonder, why is it not described in documentation? If it is a function for private use only, why is it used by many people, who wrote about working with stdin?

like image 152
porfirion Avatar answered Sep 23 '22 15:09

porfirion