deps-app / ring-etag-middleware

Ring middleware to add fast checksum based ETags to responses
MIT License
16 stars 2 forks source link

Doesn't work when serving from uberjar #3

Open whmountains opened 2 years ago

whmountains commented 2 years ago

Files served from an uberjar are instances of JarURLInputStream not File, therefore no etag is calculated.

danielcompton commented 2 years ago

Patches welcome, or I'll get to this eventually.

whmountains commented 2 years ago

I tried to make a patch but got hung up on some limitations of my Java interop knowledge. I went for a temporary fix for now but will be happy to submit a patch if I get one working. Cheers.

uvNikita commented 1 year ago

@danielcompton I might be missing something, but it seems that checksum-file works as-is when passed sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream instance.

It even returned the same checksum value as when the file is passed as a File instance.

So I think might just work if modifying the check here to also accept JarURLInputStream or generic InputStream/FileInputStream:

https://github.com/deps-app/ring-etag-middleware/blob/fb26097130d84a38b941b4a7f2f0ad0bbd95f9e8/src/co/deps/ring_etag_middleware.clj#L67C10-L67C29

danielcompton commented 1 year ago

Great! I'm happy to take a patch for this.

uvNikita commented 9 months ago

For someone looking at it in the future, I didn't manage to figure out a way with JarURLInputStream to stream file contents and calculate checksum at the same time. Specifically what I was missing is to be able to "reset" JarURLInputStream after it has been consumed for checksum calculation and return it in result.

I ended up calculating checksum of all static files (js,css,images) during the uberjar build process and storing the sums in a separate file in resources. Then this file can be used to compare checksums in production.