express icon indicating copy to clipboard operation
express copied to clipboard

custom ETag function isn't working for SendFile

Open kokushkin opened this issue 2 years ago • 2 comments

I set the custom function according to the http://expressjs.com/en/api.html#etag.options.table

app.set('etag', function (body, encoding) {
  return generateHash(body, encoding) // consider the function is defined
})

Despite on that it's not working and the best way I could influence etag value is to call sendFile with etag : true or false but this just turns on and off the standard etag (also not documented) , not the custom one.

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

My use case is that I want to track responses of specific files. So I wanted to make a "strong hash" that I can be 100% sure would unambiguously represent the file.

kokushkin avatar Nov 09 '21 12:11 kokushkin

Hi @kokushkin thank you for bringing this up. Yes, the current etag setting does not affect files, as calculating an etag for a file is noy always trivial. For example, a file can be multiple gigabytes in size and it is not practical to hold the entire contents of a file in memory in order to invoke the etag setting.

You can currently calcualte your etag and set the etag header using res.header and it will not be replaced by the framework.

We can determine what kind of API would be needed otherwise in order to assist in calculating the etag for a file. What do yoi currently do to calculate the etag of these files, may I ask?

dougwilson avatar Nov 15 '21 00:11 dougwilson

Well, I've solved my problem by just calculated it's sha256 digest using "crypto" and writing the result into the log. But initially I tried to use "morgan" together with the standard ETag functionality of "express". I think I even tried to make it explicitly with res.header and it didn't work out (morgan was enable to access etag for some reason) but I'm not sure, I might try it again and give you feedback). So, the initial thought was to hook into the process and say I wanna get absolutely unique digest for some types of files, let's say *.jpg files no matter where they are in the routes and I couldn't do it so I have to explore the routes.

kokushkin avatar Nov 22 '21 06:11 kokushkin