Brotli Compression

Not sure if this should be posted here or in Sparkle, but as the middleware is added to the XDataServer component, maybe here makes the most sense.

I'd like to see a new compression option for Brotli. This is something that I think is now supported by the majority of browsers and offers better compression than gzip. I've been experimenting with it a bit using the YuBrotli code to compress files containing JSON. Using gzip, the files were about half the size. With Brotli (max compression) they are about a third of the size, so a solid improvement.

In the Actorious demo app I've been working away on in the TMS Blog, all this JSON is created in advance, so compressing it can take as long as it likes, even though I think at normal settings it is still on-par time-wise with gzip. In the app, I just return a TStream with a file and set the "Content-Encoding" to "br" and it makes it all the way back to the browser which does the decompression. Which is fantastic, by the way, and the compression middleware that does the gzip currently doesn't even get in the way.

So it is currently already working with Brotli in this fashion, but for dynamic content it would be nicer to have this as an explicit compression option and also to not have to encode the files manually.

One of the problems here is that the library you mentioned is commercial, so it cannot be used for official Sparkle. For dynamic content you can already do it, just checking the Accept-Encoding header and processing like you do. Or, copy/paste the compress middleware source code and create your own that supports Brotli.

Of course, it would be ideal that Sparkle provides this built-in, but as I mentioned, we would have to implement the Brotli algorithm ourselves, which is a kind of reinventing the wheel. But something to be considered, sure. Maybe add it as a feature request so we can track for the future?

Sorry, I wasn't implying that'd you'd use their library directly. There are other commercial Delphi vendors that offer Brotli compression support as well, just that this one happens to be a little more accessible and can be used in XData projects as-is pretty well. Might be useful to others.

I'll add it as a feature request. And maybe have a look at directly implementing it as you've suggested. I got it working almost by accident - I had been looking around for a way to decompress Brotli directly in the client (as there is no way to use the browser's decoder) until I did a :man_facepalming: and realized that if I just set the encoding, the browser would do the decompression automatically.

1 Like