How to close a thread that no longer has data to send to node.js?

I use node.js and read the input from the serial port, opening the file / dev / tty, I send the command and read the result of the command, and I want to close the stream after I read and analyze all the data. I know that I have finished reading data with a data marker and ending it. I found that as soon as I closed the thread, my program would not exit.

The following is an example of what I see, but uses / dev / random to generate data slowly (assuming your system does little). I find that the process will end as soon as the device generates data after the stream closes.

var util = require('util'),
    PassThrough = require('stream').PassThrough,
    fs = require('fs');

// If the system is not doing enough to fill the entropy pool
// /dev/random will not return much data.  Feed the entropy pool with :
//  ssh <host> 'cat /dev/urandom' > /dev/urandom
var readStream = fs.createReadStream('/dev/random');
var pt = new PassThrough();

pt.on('data', function (data) {
    console.log(data)
    console.log('closing');
    readStream.close();  //expect the process to terminate immediately
});

readStream.pipe(pt);

Update: 1

, pty node. 2 pty , node, createReadStream.

var fs = require('fs');
var rs = fs.createReadStream('/dev/pts/1'); // a pty that is allocated in another terminal by my user
//wait just a second, don't copy and paste everything at once
process.exit(0);

node . 10.28.

+4
2

readStream.close(), 

readStream.pause().

node, , stream isaacs, :

var Readable = require('stream').Readable;
var myReader = new Readable().wrap(readStream);

myReader readStream.

! , .

+1

/dev/random, 'data' , , .

, , , . .

, :

pt.on('data', function (data) {
  console.log(data)
  console.log('closing');

  pt.removeAllListeners('data');
  readStream.close();
});
-1

All Articles