Unknown method process.openStdin ()

I am trying to pass grep results to a nodejs script. I found that I should get data from process.stdin.

I also found several ways to work with stdin. But they are different, and I can not find all the information about it. I know four ways (the first 3 starts 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) It looks like the first, but with an 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 read, calling stdin.resume() changes stdin to the "old type". Therefore, if we did not call a "resume" - we can use a "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 the readline module. This is very useful if grep results are on multiple lines and I don’t need to split the resulting data myself. But for a long time I could not understand why all the information is transmitted directly to stdout. Then I discovered that we can pass an empty object instead of process.stdout while creating an interface and the data will not be output to the channel.

 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 option. Use another "split" section - it allows you to read from the stream and divide the data into chuks by the specified character ( \r?\n by default). I used it to work with a socket, and as soon as stdin is also a 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 searched every page on Google, but did not find any documentation for this feature!

In addition, during the search, I found that the official documentation for nodejs is ugly - it is not mentioned, since which version methods are available, there is no detailed description of many objects / methods, there are no user comments. And this method ( openStdin ) exists and works, but is not described anywhere! WTF ???

+5
source share
1 answer

While writing the question, I found the answer :)

In the source code for nodejs:

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

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

+5
source

Source: https://habr.com/ru/post/1236456/


All Articles