From c797b126ec8d0515e1ec306473c9bf29be0dbc7b Mon Sep 17 00:00:00 2001 From: Chandra Sekar S Date: Mon, 19 Jul 2010 12:39:12 +0530 Subject: [PATCH] Refactored static file serving. --- lib/renderer.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/renderer.js b/lib/renderer.js index edc2043..12760b0 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -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(); @@ -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(); }