Skip to content

Commit

Permalink
Refactored static file serving.
Browse files Browse the repository at this point in the history
  • Loading branch information
cskr committed Jul 19, 2010
1 parent 5ea5e3e commit c797b12
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions lib/renderer.js
Expand Up @@ -299,10 +299,7 @@ function sendStatic(staticFile, stats, ctx) {
if(ctx.status == 206) {
streamRange(staticFile, range[0], range[1], ctx);
} else {
var rs = fs.createReadStream(staticFile);
sys.pump(rs, ctx.response, function() {
ctx.response.end();
});
streamRange(staticFile, 0, stats.size - 1, ctx);
}
} else {
ctx.response.end();
Expand Down Expand Up @@ -331,12 +328,12 @@ function streamRange(staticFile, start, end, ctx) {
var currentPosition = start;
var bufferSize = 4 * 1024;
function reader() {
var buf = new Buffer(bufferSize);
if(currentPosition < end) {
var readLength = Math.min(end - currentPosition + 1, bufferSize);
var buf = new Buffer(readLength);
fs.read(fd, buf, 0, readLength, currentPosition, function(err, bytesRead) {
if(!err) {
ctx.response.write(buf.slice(0, bytesRead), 'binary');
ctx.response.write(buf, 'binary');
currentPosition += bytesRead;
reader();
}
Expand Down

0 comments on commit c797b12

Please sign in to comment.