Skip to content

Instantly share code, notes, and snippets.

@ollym
Created August 12, 2010 08:41
Show Gist options
  • Save ollym/520588 to your computer and use it in GitHub Desktop.
Save ollym/520588 to your computer and use it in GitHub Desktop.
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