Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,12 @@ res.sendFile = function sendFile(path, options, callback) {
// create file stream
var pathname = encodeURI(path);

// wire application etag option to send
opts.etag = this.app.enabled('etag');
// wire application etag option to send, as a default only: every other
// option is passed along to send untouched, and overriding this one made
// send's documented `etag` option unreachable through res.sendFile
Comment on lines +401 to +403

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that expanding this comment is necessary. I know that LLMs love comments, but not every line of code needs 3 lines of comments to explain what it does.

if (opts.etag === undefined) {
opts.etag = this.app.enabled('etag');
}
Comment on lines +404 to +406

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (opts.etag === undefined) {
opts.etag = this.app.enabled('etag');
}
opts.etag ??= this.app.enabled('etag');

This would slightly change logic (overwrite undefined and null), but why not write it the easier way (boolean is expected anyway). Nullish coalescing assignment was designed for such use case.

var file = send(req, pathname, opts);

// transfer
Expand Down
22 changes: 22 additions & 0 deletions test/res.sendFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@ describe('res', function(){
}
});

it('should let an explicit etag option turn the ETag off', function (done) {
var app = createApp(path.resolve(fixtures, 'name.txt'), { etag: false });

request(app)
.get('/')
.expect(handleHeaders)
.expect(200, done);

function handleHeaders (res) {
assert(res.headers.etag === undefined);
}
});

it('should let an explicit etag option turn the ETag on', function (done) {
var app = createApp(path.resolve(fixtures, 'name.txt'), { etag: true }).disable('etag');

request(app)
.get('/')
.expect('ETag', /^(?:W\/)?"[^"]+"$/)
.expect(200, done);
});

it('should 404 for directory', function (done) {
var app = createApp(path.resolve(fixtures, 'blog'));

Expand Down