dream icon indicating copy to clipboard operation
dream copied to clipboard

Compress/gzip dream responses [evaluate dream-encoding library]

Open mooreryan opened this issue 3 years ago • 6 comments

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 avatar Jun 09 '21 20:06 mooreryan

@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.

aantron avatar Jun 10 '21 02:06 aantron

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.

mooreryan avatar Jun 10 '21 05:06 mooreryan

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. :)

aantron avatar Jun 10 '21 09:06 aantron

Another README to write, I promise I'll get to them soon @aantron 😄

tmattio avatar Jun 10 '21 09:06 tmattio

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.

aantron avatar Jul 08 '21 05:07 aantron

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-crunched 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...

cemerick avatar Mar 17 '22 15:03 cemerick