res.sendfile() using sys.pump() when >= "stream threshold"
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent e037ff6 · Browse files · 3 files·+174 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+174 added−6 removed
- tests:+156 added−0 removed
- other:+18 added−6 removed
Provenance
- Commit
18c7f16- Tree
2b7f66a- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
e037ff6
3 files changed+174 added−6 removed
modified:lib/express/response.js+18 added-6 removed
10 unmodified lines11 12 var fs = require('fs'),13 http = require('http'),14 path = require('path'),15+ pump = require('sys').pump,16 utils = require('connect/utils'),17 mime = require('connect/utils').mime,18 Buffer = require('buffer').Buffer;19 107 unmodified lines127 * @api public128 */129 130 http.ServerResponse.prototype.sendfile = function(path, fn){130- var self = this;131+ var self = this,132+ streamThreshold = this.app.set('stream threshold') || 32 * 1024;133 134 function error(err) {135 delete self.headers['Content-Disposition'];136 if (fn) {4 unmodified lines141 }142 143 fs.stat(path, function(err, stat){144 if (err) return error(err);143- fs.readFile(path, function(err, buf){144- if (err) return error(err);145+ if (stat.size >= streamThreshold) {146+ var stream = fs.createReadStream(path);147 self.contentType(path);148+ self.header('Content-Length', stat.size);149+ self.writeHead(200, self.headers);150+ pump(stream, self, function(err){151+ fn && fn(err, path);152+ });153+ } else {154+ fs.readFile(path, function(err, buf){155+ if (err) return error(err);156+ self.contentType(path);146- self.send(buf);147- fn && fn(null, path);148- });157+ self.send(buf);158+ fn && fn(null, path);159+ });160+ }161 });162 };163 164 /**172 unmodified linesadded:test/fixtures/large.json+151 added
1+{2+ "foo":"bar",3+ "foo":"bar",4+ "foo":"bar",5+ "foo":"bar",6+ "foo":"bar",7+ "foo":"bar",8+ "foo":"bar",9+ "foo":"bar",10+ "foo":"bar",11+ "foo":"bar",12+ "foo":"bar",13+ "foo":"bar",14+ "foo":"bar",15+ "foo":"bar",16+ "foo":"bar",17+ "foo":"bar",18+ "foo":"bar",19+ "foo":"bar",20+ "foo":"bar",21+ "foo":"bar",22+ "foo":"bar",23+ "foo":"bar",24+ "foo":"bar",25+ "foo":"bar",26+ "foo":"bar",27+ "foo":"bar",28+ "foo":"bar",29+ "foo":"bar",30+ "foo":"bar",31+ "foo":"bar",32+ "foo":"bar",33+ "foo":"bar",34+ "foo":"bar",35+ "foo":"bar",36+ "foo":"bar",37+ "foo":"bar",38+ "foo":"bar",39+ "foo":"bar",40+ "foo":"bar",41+ "foo":"bar",42+ "foo":"bar",43+ "foo":"bar",44+ "foo":"bar",45+ "foo":"bar",46+ "foo":"bar",47+ "foo":"bar",48+ "foo":"bar",49+ "foo":"bar",50+ "foo":"bar",51+ "foo":"bar",52+ "foo":"bar",53+ "foo":"bar",54+ "foo":"bar",55+ "foo":"bar",56+ "foo":"bar",57+ "foo":"bar",58+ "foo":"bar",59+ "foo":"bar",60+ "foo":"bar",61+ "foo":"bar",62+ "foo":"bar",63+ "foo":"bar",64+ "foo":"bar",65+ "foo":"bar",66+ "foo":"bar",67+ "foo":"bar",68+ "foo":"bar",69+ "foo":"bar",70+ "foo":"bar",71+ "foo":"bar",72+ "foo":"bar",73+ "foo":"bar",74+ "foo":"bar",75+ "foo":"bar",76+ "foo":"bar",77+ "foo":"bar",78+ "foo":"bar",79+ "foo":"bar",80+ "foo":"bar",81+ "foo":"bar",82+ "foo":"bar",83+ "foo":"bar",84+ "foo":"bar",85+ "foo":"bar",86+ "foo":"bar",87+ "foo":"bar",88+ "foo":"bar",89+ "foo":"bar",90+ "foo":"bar",91+ "foo":"bar",92+ "foo":"bar",93+ "foo":"bar",94+ "foo":"bar",95+ "foo":"bar",96+ "foo":"bar",97+ "foo":"bar",98+ "foo":"bar",99+ "foo":"bar",100+ "foo":"bar",101+ "foo":"bar",102+ "foo":"bar",103+ "foo":"bar",104+ "foo":"bar",105+ "foo":"bar",106+ "foo":"bar",107+ "foo":"bar",108+ "foo":"bar",109+ "foo":"bar",110+ "foo":"bar",111+ "foo":"bar",112+ "foo":"bar",113+ "foo":"bar",114+ "foo":"bar",115+ "foo":"bar",116+ "foo":"bar",117+ "foo":"bar",118+ "foo":"bar",119+ "foo":"bar",120+ "foo":"bar",121+ "foo":"bar",122+ "foo":"bar",123+ "foo":"bar",124+ "foo":"bar",125+ "foo":"bar",126+ "foo":"bar",127+ "foo":"bar",128+ "foo":"bar",129+ "foo":"bar",130+ "foo":"bar",131+ "foo":"bar",132+ "foo":"bar",133+ "foo":"bar",134+ "foo":"bar",135+ "foo":"bar",136+ "foo":"bar",137+ "foo":"bar",138+ "foo":"bar",139+ "foo":"bar",140+ "foo":"bar",141+ "foo":"bar",142+ "foo":"bar",143+ "foo":"bar",144+ "foo":"bar",145+ "foo":"bar",146+ "foo":"bar",147+ "foo":"bar",148+ "foo":"bar",149+ "foo":"bar",150+ "foo":"bar"151+}modified:test/response.test.js+5 added
183 unmodified lines184 185 'test #sendfile()': function(assert){186 var app = express.createServer();187 188+ app.set('stream threshold', 2 * 1024)189+190 app.get('/*', function(req, res, next){191 var file = req.params[0],192 filePath = __dirname + '/fixtures/' + file;193 res.sendfile(filePath, function(err, path){15 unmodified lines209 { body: 'Internal Server Error', status: 500 });210 assert.response(app,211 { url: '/partials' },212 { body: 'Internal Server Error', status: 500 });213+ assert.response(app,214+ { url: '/large.json' },215+ { status: 200, headers: { 'Content-Length': 2535, 'Content-Type': 'application/json' }});216 },217 218 'test #download()': function(assert){219 var app = express.createServer();84 unmodified lines