change res.sendfile() to use send() module
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 7a5041b · Browse files · 9 files·+108 added−78 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+108 added−78 removed
- tests:+45 added−6 removed
- other:+63 added−72 removed
Provenance
- Commit
ab61837- Tree
325c0da- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
7a5041b
9 files changed+108 added−78 removed
modified:examples/auth/app.js+10 added-5 removed
6 unmodified lines78910111213111213141516171819202122232024252627282930313228333435363789 unmodified lines6 unmodified lines , hash = require('./pass').hash;
var app = module.exports = express();
app.use(express.bodyParser());app.use(express.cookieParser('shhhh, very secret'));app.use(express.session());// config
app.set('view engine', 'ejs');app.set('views', __dirname + '/views');
// middleware
app.use(express.bodyParser());app.use(express.cookieParser('shhhh, very secret'));app.use(express.session());
// Session-persisted message middleware
app.locals.use(function(req,res){app.use(function(req, res, next){ var err = req.session.error , msg = req.session.success; delete req.session.error; delete req.session.success; res.locals.message = ''; if (err) res.locals.message = '<p class="msg error">' + err + '</p>'; if (msg) res.locals.message = '<p class="msg success">' + msg + '</p>'; next();})});
// dummy database
var users = {89 unmodified linesmodified:examples/expose-data-to-client/index.js-8 removed
43 unmodified lines444546474849505152535455484950519 unmodified lines43 unmodified lines req.user = new User('Tobi'); next();});
// if you wanted to _always_ expose// the user you might do something like this:/*app.locals.use(function(req, res){ if (req.user) res.locals.expose.user = req.user;})*/
app.get('/', function(req, res){ res.redirect('/user');});
9 unmodified linesmodified:examples/mvc/index.js+18 added-17 removed
23 unmodified lines24 sess.messages.push(msg);25 return this;26 };27 28+// log29+if (!module.parent) app.use(express.logger('dev'));30+31+// serve static files32+app.use(express.static(__dirname + '/public'));33+34+// session support35+app.use(express.cookieParser('some secret here'));36+app.use(express.session());37+38+// parse request bodies (req.body)39+app.use(express.bodyParser());40+41+// support _method (PUT in forms etc)42+app.use(express.methodOverride());43+44 // expose the "messages" local variable when views are rendered29-app.locals.use(function(req, res){45+app.use(function(req, res, next){46 var msgs = req.session.messages || [];47 48 // expose "messages" local variable49 res.locals.messages = msgs;10 unmodified lines60 61 // empty or "flush" the messages so they62 // don't build up63 req.session.messages = [];64+ next();65 });66 50-// log51-if (!module.parent) app.use(express.logger('dev'));52-53-// serve static files54-app.use(express.static(__dirname + '/public'));55-56-// session support57-app.use(express.cookieParser('some secret here'));58-app.use(express.session());59-60-// parse request bodies (req.body)61-app.use(express.bodyParser());62-63-// support _method (PUT in forms etc)64-app.use(express.methodOverride());65-67 // load controllers68 require('./lib/boot')(app, { verbose: !module.parent });69 70 // assume "not found" in the error msgs23 unmodified linesmodified:examples/view-locals/index.js-23 removed
86 unmodified lines87 88 function users2(req, res, next) {89 User.all(function(err, users){90 if (err) return next(err);91- // this would not be ideal for *this*91 res.locals.users = users.filter(ferrets);92 next();93 })94 }6 unmodified lines101 res.render('user', { title: 'Users' });102 });103 104 106-107-108-// let's assume we wanted to load the users109-// and count for every res.render() call, we110-// could use app.locals.use() for this. These111-// are callbacks which run in parallel ONLY112-// when res.render() is invoked. If no views113-// are rendered, there is no overhead.114-115-// This may be ideal if you want to load auxiliary116-// user information, but only for templates. Note117-// that (req, res) are available to you, so you may118-// access req.session.user etc.119-120-// Keep in mind these execute in *parallel*, so these121-// callbacks should not depend on each other, this122-// also makes them slightly more efficient than123-// using middleware which execute sequentially124-125-app.locals.use(count2);126-app.locals.use(users2);127-105 app.get('/locals', function(req, res){106 res.render('user', { title: 'Users' });107 });108 2 unmodified linesmodified:lib/middleware.js+1 added-1 removed
24 unmodified lines25 26 req.__proto__ = app.request;27 res.__proto__ = app.response;28 29- res.locals = utils.locals(res);29+ res.locals = res.locals || utils.locals(res);30 31 next();32 }33 };modified:lib/response.js+33 added-18 removed
10 unmodified lines11 , normalizeTypes = require('./utils').normalizeTypes12 , statusCodes = http.STATUS_CODES13 , send = connect.static.send14 , cookie = require('cookie')15+ , send = require('send')16 , mime = connect.mime17 , basename = path.basename18 , extname = path.extname19 , join = path.join;151 unmodified lines171 res.sendfile = function(path, options, fn){172 var self = this173 , req = self.req174 , next = this.req.next174- , options = options || {};175+ , options = options || {}176+ , done;177 178 // support function as second arg179 if ('function' == typeof options) {180 fn = options;181 options = {};182 }183 182- // callback183- options.callback = function(err){184- if (err) {185- // cast ENOENT186- if ('ENOENT' == err.code) err = utils.error(404);184+ // socket errors185+ req.socket.on('error', error);186 188- // ditch content-disposition to prevent funky responses189- if (!self.headerSent) self.removeHeader('Content-Disposition');187+ // errors188+ function error(err) {189+ if (done) return;190+ done = true;191 191- // woot! callback available192- if (fn) return fn(err);192+ // clean up193+ req.socket.removeListener('error', error);194+ if (!self.headerSent) self.removeHeader('Content-Disposition');195 194- // lost in limbo if there's no callback195- if (self.headerSent) return;196+ // callback available197+ if (fn) return fn(err);198 197- return req.next(err);198- }199+ // list in limbo if there's no callback200+ if (self.headerSent) return;201 200- fn && fn();201- };202+ // delegate203+ next(err);204+ }205 206+ // streaming207+ function stream() {208+ if (done) return;209+ req.socket.removeListener('error', error);210+ if (fn) self.on('finish', fn);211+ }212+213 // transfer204- options.path = encodeURIComponent(path);205- send(this.req, this, next, options);214+ var file = send(req, path);215+ if (options.root) file.root(options.root);216+ file.maxage(options.maxAge || 0);217+ file.on('error', error);218+ file.on('directory', next);219+ file.on('stream', stream);220+ file.pipe(this);221 };222 223 /**224 * Transfer the file at the given `path` as an attachment.378 unmodified linesmodified:test/res.locals.js+22 added-3 removed
15 unmodified lines16 });17 18 request(app)19 .get('/')20- .end(function(res){21- done();22- })20+ .expect(200, done);21 })22 })23+24+ it('should work when mounted', function(done){25+ var app = express();26+ var blog = express();27+28+ app.use(blog);29+30+ blog.use(function(req, res, next){31+ res.locals.foo = 'bar';32+ next();33+ });34+35+ app.use(function(req, res){36+ res.locals.foo.should.equal('bar');37+ res.end();38+ });39+40+ request(app)41+ .get('/')42+ .expect(200, done);43+ })44 })modified:test/res.sendfile.js+23 added-3 removed
9 unmodified lines10 11 app.use(function(req, res){12 res.sendfile('test/fixtures/user.html', function(err){13 assert(!err);14+ req.socket.listeners('error').should.have.length(1); // node's original handler15 done();16 });17 });18 21 unmodified lines40 , calls = 0;41 42 app.use(function(req, res){43 res.sendfile('test/fixtures/nope.html', function(err){43- assert(!res.headerSent);44 ++calls;45+ assert(!res.headerSent);46 res.send(err.message);47 });48 });49 50 request(app)51 .get('/')52 .end(function(err, res){52- calls.should.equal(1);53- res.text.should.equal('Not Found');53+ assert(1 == calls, 'called too many times');54+ res.text.should.equal("ENOENT, stat 'test/fixtures/nope.html'");55 res.statusCode.should.equal(200);56 done();57 });58 })28 unmodified lines87 .get('/')88 .expect('Forbidden')89 .expect(200, done);90 })91+92+ it('should invoke the callback on socket error', function(done){93+ var app = express()94+ , calls = 0;95+96+ app.use(function(req, res){97+ res.sendfile('test/fixtures/user.html', function(err){98+ assert(!res.headerSent);99+ req.socket.listeners('error').should.have.length(1); // node's original handler100+ done();101+ });102+103+ req.socket.emit('error', new Error('broken!'));104+ });105+106+ request(app)107+ .get('/')108+ .end(function(){});109+ })110 })111 112 describe('.sendfile(path)', function(){113 describe('with an absolute path', function(){129 unmodified linesmodified:package.json+1 added
16 unmodified lines17 "mkdirp": "0.3.3",18 "cookie": "0.0.3",19 "fresh": "0.1.0",20 "methods": "0.0.1",21+ "send": "0.0.2",22 "debug": "*"23 },24 "devDependencies": {25 "ejs": "*",21 unmodified lines