dream
dream copied to clipboard
Compress/gzip dream responses [evaluate dream-encoding library]
Hey aantron, so I've coded up a little api using dream, and one thing I can't find in the docs mentioned is if it is possible to compress/gzip responses.
Is this possible? I was thinking maybe using the stream response in combination with an ocaml gzip library.
Thanks!
@mooreryan It is possible in principle, but I haven't looked into it in detail, picked a compression library, made any specific suggestions, etc. @tmattio has mentioned that he was interested in making a library of Dream helpers for this. Now that you've asked, I'm also considering making an example about it.
Ah your tip about @tmattio seems correct...I saw this https://github.com/tmattio/dream-encoding and the Dream_encoding.compress
middleware from that repo seems to work just fine for compressing responses.
Ah nice. I'll link to it from this repo once dream-encoding is released, has proper README (as mentioned in the current README), etc. :)
Another README to write, I promise I'll get to them soon @aantron 😄
dream-encoding is now installable from opam (and by esy), and recommended in Dream's README. For usage with large responses, Dream will need to rework the I/O a little bit (relax the types, etc.) to make it easier for middlewares to do stream processing. After that, dream-encoding should be changed internally to do streaming compression (it is ready, just needs to change the Dream functions it is calling once they are ready). So, I want to leave this issue open until that Dream I/O rework, and potentially also until there is an example in Dream that shows dream-encoding.
A side note: it would be really nice if the eventual solution to this problem accounted for caching/preparing the results of compressing static resources (maybe especially those coming out of an ocaml-crunch
ed module), so responses for unchanging content don't need to be gzipped over and over. It might be that the right solution is a general-purpose memoization middleware...