1

I expect the code below to create a Readline Interface with a readable stream from a file with a list of ~1000 URLs, start streaming from the input stream (logging a line to the console), and then pause:

var readline = require('readline');
var fs = require('fs');

var rlinterface = readline.createInterface({
    input: fs.createReadStream('urls.txt'),
    output: null,
    terminal: false
});

rlinterface.on('pause', function() {
    console.log('Readline paused.');
});

rlinterface.on('line', function(line){
    console.log(line);
    rlinterface.pause();
}); 

The listener for the pause event notifies me that the stream should be paused, but all of the lines in the file from the input stream are logged to the console, suggesting that the stream never paused. Even if the stream doesn't pause immediately after the first line (maybe a few extra lines need to be captured in a buffer), I don't expect all of the lines from the file from the input stream to be streamed before the 'pause'. So what's going on?

Community
  • 1
  • 1
celeritas
  • 2,191
  • 1
  • 17
  • 28

1 Answers1

3

What's probably happening is that the entire file is being read in at once and by the time you pause(), there's nothing left to read. Specifically, the default highWaterMark for fs.ReadStream is 64KB, so the Readable stream could easily consume your entire 17KB file in the first read because it tries to keep the internal buffer full up to highWaterMark.

mscdex
  • 104,356
  • 15
  • 192
  • 153
  • From NodeJS docs: Note that `rl.pause()` doesn't immediately pause the stream of events. Several events may be emitted after calling pause, including line. – Onur Yıldırım Oct 13 '15 at 00:36
  • @OnurYıldırım That's not relevant in this particular case though. – mscdex Oct 13 '15 at 02:18