Skip to content
Myr

Added second callback support for res.download() connection errors

Before adoption: this commit came in with the history the project adopted, so no landing records it.

since you can no longer respond, it will be helpful to have separate callbacks

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 1abb674 · Browse files · 1 file+19 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+19 added−10 removed

  • other:+19 added−10 removed

1 file+19 added−10 removed

modified:lib/response.js+19 added-10 removed
128 unmodified lines
129 * @api public130 */131132res.sendfile = function(path, options, fn){133 var next = this.req.next;134 options = options || {};135136 // support function as second arg137 if ('function' == typeof options) {
2 unmodified lines
140 }141142 options.path = path;143 options.callback = fn;143 send(this.req, this, this.req.next, options);144 send(this.req, this, next, options);145};146147/**148 * Set _Content-Type_ response header passed through `mime.lookup()`.
34 unmodified lines
183};184185/**186 * Transfer the file at the given `path`, with optional 186 * `filename` as an attachment and optional callback `fn(err)`.187 * `filename` as an attachment and optional callback `fn(err)`,188 * and optional `fn2(err)` which is invoked when an error has189 * occurred after headers have been sent.190 *191 * @param {String} path192 * @param {String|Function} filename or fn193 * @param {Function} fn191 * @return {Type}194 * @param {Function} fn2195 * @api public196 */197195res.download = function(path, filename, fn){198res.download = function(path, filename, fn, fn2){199 var self = this;200201 // support callback as second arg202 if ('function' == typeof filename) {203 fn2 = fn;204 fn = filename;205 filename = null;206 }207208 // transfer the file209 this.attachment(filename || path).sendfile(path, function(err){206 // TODO: remove this _header hack when node is fixed207 if (err && !self._header) self.removeHeader('Content-Disposition');208 if (fn) return fn(err);210 var sentHeader = self._header;211 if (err) {210 self.req.next('ENOENT' == err.code211 ? null212 : err);212 if (!sentHeader) self.removeHeader('Content-Disposition');213 if (sentHeader) {214 if (fn2) fn2(err);215 } else if (fn) {216 fn(err);217 } else {218 self.req.next(err);219 }220 } else if (fn) {221 fn();222 }223 });224};225
190 unmodified lines