From 85cc7f5649cd610f08bae544d5a889a463607431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Arboleda?= Date: Fri, 18 Oct 2024 01:33:51 -0500 Subject: [PATCH] feat: add support for ETag option in res.sendFile MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- History.md | 1 + lib/response.js | 3 +++ test/res.sendFile.js | 13 +++++++++++++ 3 files changed, 17 insertions(+) diff --git a/History.md b/History.md index 5c7da2ad81..021315c5ab 100644 --- a/History.md +++ b/History.md @@ -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 diff --git a/lib/response.js b/lib/response.js index 38f11e9237..b1dfcb2335 100644 --- a/lib/response.js +++ b/lib/response.js @@ -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 diff --git a/test/res.sendFile.js b/test/res.sendFile.js index 505f8d114d..63ad5558b5 100644 --- a/test/res.sendFile.js +++ b/test/res.sendFile.js @@ -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'));