Skip to content

Instantly share code, notes, and snippets.

@aarsilv
Created November 11, 2013 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aarsilv/7421949 to your computer and use it in GitHub Desktop.
Save aarsilv/7421949 to your computer and use it in GitHub Desktop.
Simple illustration of bug in pausing stream being parsed by node csv
var csv = require('csv');
var fs = require('fs');
var os = require('os');
var path = require('path');
var ENABLE_PAUSING = true;
var testFilePath = path.join(os.tmpDir(),'test.csv');
console.log('Creating test csv temp file');
var testFileContents = '';
var numTestRows = 100000;
var i = 0;
while (i < numTestRows*5) {
testFileContents += [++i, ++i, ++i, ++i, ++i].join(',')+'\n';
}
fs.writeFileSync(testFilePath, testFileContents);
console.log('Reading CSV file');
var stream = fs.createReadStream(testFilePath);
var rowsProcessed = 0;
csv().from.stream(stream)
.on('record', function(row, index) {
if (++rowsProcessed % (Math.floor(numTestRows / 5)) === 0) {
console.log('Processed '+rowsProcessed+' rows so far');
if (ENABLE_PAUSING) {
console.log('pausing stream for 100ms--reading should stop');
if (!stream.paused) {
stream.pause();
setTimeout(function() {
console.log('resuming stream--reading should resume');
stream.resume();
}, 100);
}
}
}
})
.on('end', function() {
console.log('Read '+rowsProcessed+' rows');
})
.on('error', function(error){
console.log('Error: ', error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment