res.download() and res.sendfile() using connect now
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 db8b288 · Browse files · 3 files·+114 added−17 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+114 added−17 removed
- tests:+81 added−11 removed
- other:+33 added−6 removed
Provenance
- Commit
9b1cc75- Tree
2030bd5- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
db8b288
3 files changed+114 added−17 removed
modified:lib/response.js+33 added-6 removed
119 unmodified lines120 * - `maxAge` defaulting to 0121 * - `root` root directory for relative filenames122 *123 * @param {String} path124- * @param {Object} options124+ * @param {Object|Function} options or fn125+ * @param {Function} fn126 * @api public127 */128 128-res.sendfile = function(path, options){129+res.sendfile = function(path, options, fn){130 options = options || {};131+132+ // support function as second arg133+ if ('function' == typeof options) {134+ fn = options;135+ options = {};136+ }137+138 options.path = path;139+ options.callback = fn;140 send(this.req, this, this.req.next, options);141 };142 143 /**35 unmodified lines179 };180 181 /**182 * Transfer the file at the given `path`, with optional 174- * `filename` as an attachment.183+ * `filename` as an attachment and optional callback `fn(err)`.184 *185 * @param {String} path177- * @param {String} filename186+ * @param {String|Function} filename or fn187+ * @param {Function} fn188 * @return {Type}189 * @api public190 */191 182-res.download = function(path, filename){183- this.attachment(filename || path).sendfile(path);192+res.download = function(path, filename, fn){193+ var self = this;194+195+ // support callback as second arg196+ if ('function' == typeof filename) {197+ fn = filename;198+ filename = null;199+ }200+201+ // transfer the file202+ this.attachment(filename || path).sendfile(path, function(err){203+ if (err) self.removeHeader('Content-Disposition');204+ if (fn) return fn(err);205+ if (err) {206+ self.req.next('ENOENT' == err.code207+ ? null208+ : err);209+ }210+ });211 };212 213 /**214 * Set or get response header `name` with optional `val`.138 unmodified linesadded:test/fixtures/pets.json+1 added
1["tobi", "loki", "jane"]No newline at end of filemodified:test/response.test.js+80 added-11 removed
185 unmodified lines186 res.header('X-Foo', 'bar');187 res.redirect('blog');188 });189 190-190 assert.response(app,191 { url: '/html', headers: { Accept: 'text/html,text/plain' }},192 { body: '<p>Moved Temporarily. Redirecting to <a href="http://google.com">http://google.com</a></p>' });193 assert.response(app,58 unmodified lines252 assert.response(app,253 { url: '/large.json' },254 { status: 200, headers: { 'Content-Type': 'application/json' }});255 },256+ 257+ 'test #sendfile(path, callback)': function(beforeExit){258+ var app = express.createServer()259+ , calls = 0;260+ 261+ app.get('/*', function(req, res, next){262+ var file = req.params[0]263+ , path = __dirname + '/fixtures/' + file;264+ res.sendfile(path, function(err){265+ if (err) return res.send('got an error');266+ ++calls;267+ });268+ });269+270+ assert.response(app,271+ { url: '/forum' },272+ { body: 'got an error' });273+274+ assert.response(app,275+ { url: '/does-not-exist' },276+ { body: 'got an error' });277+278+ assert.response(app,279+ { url: '/large.json' },280+ { headers: { 'Accept-Ranges': 'bytes' }});281+282+ beforeExit(function(){283+ calls.should.equal(1);284+ });285+ },286+ 287+ 'test #sendfile(path, options, callback)': function(beforeExit){288+ var app = express.createServer()289+ , calls = 0;290+ 291+ app.get('/*', function(req, res, next){292+ var file = req.params[0]293+ , path = __dirname + '/fixtures/' + file;294+ res.sendfile(path, { maxAge: 3600000 }, function(err){295+ if (err) return res.send('got an error');296+ ++calls;297+ });298+ });299+300+ assert.response(app,301+ { url: '/does-not-exist' },302+ { body: 'got an error' });303+304+ assert.response(app,305+ { url: '/large.json' },306+ { headers: {307+ 'Accept-Ranges': 'bytes'308+ , 'Cache-Control': 'public max-age=3600'309+ }});310+311+ beforeExit(function(){312+ calls.should.equal(1);313+ });314+ },315 316 'test #sendfile() Accept-Ranges': function(){317 var app = express.createServer();318 52 unmodified lines371 { url: '/large.json', headers: { 'Range': 'bytes=500-10' }},372 { body: 'Requested Range Not Satisfiable', status: 416 });373 },374 317- 'test #download()': function(){318- var app = express.createServer();375+ 'test #download()': function(beforeExit){376+ var app = express.createServer()377+ , calls = 0;378 379 app.get('/json', function(req, res, next){321- var filePath = __dirname + '/fixtures/user.json';322- res.download(filePath, 'account.json');380+ var path = __dirname + '/fixtures/user.json';381+ res.download(path, 'account.json');382 });324- 383+384+ app.get('/callback', function(req, res, next){385+ res.download(__dirname + '/fixtures/pets.json', function(err){386+ ++calls;387+ });388+ });389+390 app.get('/*', function(req, res, next){391 res.download(__dirname + '/fixtures/' + req.params[0]);392 });328- 329- app.error(function(err, req, res){330- res.send(404);331- })332- 393+394 assert.response(app,395+ { url: '/callback' },396+ { body: '["tobi", "loki", "jane"]' });397+398+ assert.response(app,399 { url: '/user.json' },400 { body: '{"name":"tj"}', status: 200, headers: {401 'Content-Type': 'application/json',402 'Content-Disposition': 'attachment; filename="user.json"'11 unmodified lines414 { url: '/missing' },415 function(res){416 assert.equal(null, res.headers['content-disposition']);417 });418+419+ beforeExit(function(){420+ calls.should.equal(1);421+ });422 },423 424 'test #cookie()': function(){425 var app = express.createServer();43 unmodified lines