Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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