Created
October 20, 2014 19:49
-
-
Save omochi/52249b59d5f27dfd4d41 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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