Skip to content

Instantly share code, notes, and snippets.

@omochi
Created October 20, 2014 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omochi/52249b59d5f27dfd4d41 to your computer and use it in GitHub Desktop.
Save omochi/52249b59d5f27dfd4d41 to your computer and use it in GitHub Desktop.
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