Skip to content

Commit

Permalink
refactor: res.download lower complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
criszst committed Jan 28, 2025
1 parent 6233671 commit 904abea
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,31 +417,38 @@ res.sendFile = function sendFile(path, options, callback) {
* to set the attachment and filename.
*
* This method uses `res.sendFile()`.
*
*

Check failure on line 420 in lib/response.js

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed
* Examples:
*

Check failure on line 422 in lib/response.js

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed
* // Basic usage with path
* res.download('/path/to/file.csv');
*

Check failure on line 425 in lib/response.js

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed
* // With filename and callback
* res.download('/path/to/file.csv', 'report.csv', (err) => { ... });
*

Check failure on line 428 in lib/response.js

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed
* // With options (no filename)
* res.download('/path/to/file.csv', { headers: { 'Cache-Control': 'no-cache' } });
*

Check failure on line 431 in lib/response.js

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed
* @param {String} path
* @param {String|Function|Object} [filename]
* @param {Object|Function} [options]
* @param {Function} [callback]
* @public
*/

res.download = function download (path, filename, options, callback) {
var done = callback;
var name = filename;
var opts = options || null

// support function as second or third arg
if (typeof filename === 'function') {
done = filename;
name = null;
opts = null
} else if (typeof options === 'function') {
done = options
opts = null
}
// identify the callback among the [filename, options, callback]
const done = [filename, options, callback].find(arg => typeof arg === 'function');

// support optional filename, where options may be in it's place
if (typeof filename === 'object' &&
(typeof options === 'function' || options === undefined)) {
name = null
opts = filename
}
var name = null;
var opts = {};

// determine if filename is a object or string
if (typeof filename === 'string') name = filename;
else if (typeof filename === 'object') opts = filename;

// if options is a function, set it as callback
if (typeof options === 'object') opts = Object.assign({}, opts, options);

// set Content-Disposition when file is sent
var headers = {
Expand All @@ -450,23 +457,19 @@ res.download = function download (path, filename, options, callback) {

// merge user-provided headers
if (opts && opts.headers) {
var keys = Object.keys(opts.headers)
for (var i = 0; i < keys.length; i++) {
var key = keys[i]
Object.keys(opts.headers).forEach(key => {
if (key.toLowerCase() !== 'content-disposition') {
headers[key] = opts.headers[key]
headers[key] = opts.headers[key];
}
}
});
}

// merge user-provided options
opts = Object.create(opts)
opts.headers = headers

// Resolve the full path for sendFile
var fullPath = !opts.root
? resolve(path)
: path
var fullPath = !opts.root ? resolve(path) : path

// send file
return this.sendFile(fullPath, opts, done)
Expand Down Expand Up @@ -865,7 +868,7 @@ res.vary = function(field){
*
* - `cache` boolean hinting to the engine it should cache
* - `filename` filename of the view being rendered
*
*

Check failure on line 871 in lib/response.js

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed
* @public
*/

Expand Down

0 comments on commit 904abea

Please sign in to comment.