Created
November 11, 2013 22:47
-
-
Save aarsilv/7421949 to your computer and use it in GitHub Desktop.
Simple illustration of bug in pausing stream being parsed by node csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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