celeritas
celeritas

Reputation: 2281

Node.js Readline Interface 'pause' event does not pause input stream

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?

Upvotes: 1

Views: 1213

Answers (1)

mscdex
mscdex

Reputation: 106736

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.

Upvotes: 3

Related Questions