Skip to content

Instantly share code, notes, and snippets.

@Marak
Created October 28, 2010 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Marak/652260 to your computer and use it in GitHub Desktop.
Save Marak/652260 to your computer and use it in GitHub Desktop.
this.Server.prototype.stream = function (pathname, files, buffer, res, callback) {
(function streamFile(files, offset) {
var file = files.shift();
if (file) {
// Stream the file to the client
fs.createReadStream(path.join(pathname || '.', file), {
flags: 'r',
encoding: 'binary',
mode: 0666,
bufferSize: 4096
}).addListener('data', function (chunk) {
buffer.write(chunk, offset, 0);
res.write (chunk, 'binary');
offset += chunk.length;
}).addListener('close', function () {
streamFile(files, offset);
}).addListener('error', function (err) {
callback(err);
sys.error(err);
});
} else {
res.end();
callback(null, buffer, offset);
}
})(files.slice(0), 0);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment