Created
August 12, 2010 08:41
-
-
Save ollym/520588 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 file = 'path/to/a/file', | |
stats = fs.statSync(file); | |
var bufferSize = 4 * 1024 * 1024; | |
options = { | |
from: 0, | |
to: bufferSize, | |
bufferSize: bufferSize | |
} | |
function flush(options) { | |
fs.createReadStream(file, options) | |
.on('data', function(buffer) { | |
res.write(buffer); | |
}); | |
} | |
req.connection.on('drain', function() { | |
options.from += bufferSize; | |
options.to += bufferSize; | |
if (options.from > stats.size) { | |
res.end(); | |
return; | |
} | |
if (options.to > stats.size) { | |
options.to = undefined; | |
options.bufferSize = (stats.size - options.from); | |
} | |
flush(options); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment