Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SendStream.prototype.stream = function(path, options){
// TODO: this is all lame, refactor meeee
var finished = false;
var self = this;
var res = this.res;
var req = this.req;
console.log("SendStream [" + path + "] stream, res.socket [" +res.socket+"]");
// pipe
var stream = fs.createReadStream(path, options);
this.emit('stream', stream);
stream.pipe(res);
res.on("close", function onResClose(){
console.log("SendStream [" + path + "] resp closed, finished = ["+finished+"]");
if(finished){ return; }
res.end();
destroy(stream);
finished = true;
});
res.on("finish", function onResFinish(){
console.log("SendStream [" + path + "] resp finished, finished = ["+finished+"]");
if(finished){ return; }
destroy(stream);
finished = true;
});
stream.on("error", function onerror(err){
console.log("SendStream [" + path + "] stream error [" +err+"], finished = ["+finished+"]");
if(finished){ return; }
self.onStatError(err);
destroy(stream);
finished = true;
});
stream.on("end", function onend(){
console.log("SendStream [" + path + "] stream end, finished = ["+finished+"]");
self.emit('end');
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.