feat: add support for ETag option in res.sendFile (#6073)

This patch introduces the ability to control the ETag generation
through the `res.sendFile` function. Specifically, the ETag option
is wired to the application's configuration, allowing it to be
enabled or disabled based on the app's settings.

Fixes: https://github.com/expressjs/express/issues/2294

Signed-off-by: Juan José Arboleda <soyjuanarbol@gmail.com>
This commit is contained in:
Juan José 2025-02-13 14:39:31 -05:00 committed by GitHub
parent d2de128a32
commit 327af123a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 0 deletions

View File

@ -10,6 +10,7 @@ unreleased
* refactor: prefix built-in node module imports
* Remove unused `depd` dependency
* Add support for `Uint8Array` in `res.send`
* Add support for ETag option in res.sendFile
* deps: debug@^4.4.0
* deps: body-parser@^2.1.0
* deps: router@^2.1.0

View File

@ -389,6 +389,9 @@ 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');
var file = send(req, pathname, opts);
// transfer

View File

@ -78,6 +78,19 @@ describe('res', function(){
});
});
it('should disable the ETag function if requested', function (done) {
var app = createApp(path.resolve(fixtures, 'name.txt')).disable('etag');
request(app)
.get('/')
.expect(handleHeaders)
.expect(200, done);
function handleHeaders (res) {
assert(res.headers.etag === undefined);
}
});
it('should 404 for directory', function (done) {
var app = createApp(path.resolve(fixtures, 'blog'));