Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js: How to detect an empty stdin stream?

Tags:

I have a node.js script/server that reads some input from stdin when its launched. However, sometimes there's no data to be passed in. This is troublesome because it seems like in this case neither the data nor end events are called. How can I detect when this is the case in the node.js code?

I'd like to avoid having to append special "end" characters at the end of the input, so as not to inconvenience the client. The associated code is below:

  var newHTML = '';
  var gfm = spawn(__dirname + '/node_modules/docter/bin/github-flavored-markdown.rb');
  process.stdin.on('data', function(chunk){
    gfm.stdin.write(chunk);
  });
  process.stdin.on('end', function(){
    gfm.stdin.end();
  });
  gfm.stdout.on('data', function(data) {
    newHTML += data;
  });
  gfm.on('exit',function(ecode){
    socket.emit('newContent', newHTML);
  });
  process.stdin.resume();
like image 211
Suan Avatar asked Feb 10 '12 17:02

Suan


People also ask

How do I check if a variable is empty in node JS?

You could check if errorMsg. length > 0 show the error?

How check node JSON is empty?

Object. keys(myObj). length === 0; As there is need to just check if Object is empty it will be better to directly call a native method Object.

How do I check Nodejs server status?

To check the node server running by logging in to the system In windows you can simply go to the Task Manager and check for node in the application list. If it is there then it is running in the machine.

Is process stdout a stream in node JS?

If you've been using Node. js for a while, you've definitely run into streams. HTTP connections are streams, open files are streams; stdin, stdout, and stderr are all streams as well.


2 Answers

An empty or no STDIN stream is detected by the end event from process.stdin.

This simple script stdin.js demonstrates that:

process.stdin.on( 'data', function(data) { console.log( data ) } );
process.stdin.on( 'end', function() { console.log( 'EOF' ) } );

Different scenarios:

$ echo test | node stdin.js
<Buffer 74 65 73 74 0a>
EOF
$ echo -n | node stdin.js
EOF
$ node stdin.js < /dev/null
EOF
$

This script pipe.js demonstrates that using pipe to a spawned child process works very well:

var spawn = require('child_process').spawn;
var cat = spawn( '/bin/cat' );
cat.stdout.on( 'data', function(data) { console.log( data ) } );
cat.stdout.on( 'end', function() { console.log( 'EOF' ) } );

process.stdin.pipe(cat.stdin);

As expected:

$ echo test | node pipe.js
<Buffer 74 65 73 74 0a>
EOF
$ echo -n | node pipe.js
EOF
$ node pipe.js < /dev/null
EOF
$
like image 64
Øystein Steimler Avatar answered Oct 12 '22 17:10

Øystein Steimler


I believe what may be happening is, you are not giving a stdin steam at all.

Øystein Steimler's example shows you feeding /dev/null into your app:

node pipe.js < /dev/null

However, never addressed when you do not stream stdin to the app at all. Just running node pipe.js will not exit because it is still waiting for stdin.

You can test this yourself with other unix programs, for example cat

Try running this:

cat < /dev/null

Now try just running:

cat

It does not exit because it is waiting for stdin. You can type into the terminal and send to the program by pressing enter. It will still not exit (and wait for more input) until it receives the EOF which you can do with ctrl+d

like image 25
JD Isaacks Avatar answered Oct 12 '22 16:10

JD Isaacks