-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create Starlette compression Middleware on top of Cramjam #1
Conversation
Fails tests for multiple reasons. Locally the cc @kylebarron |
Status: streaming response works for GZIP but not for the other compression 🤷♂️ I have no idea what's going on 😭 |
@vincentsarago ie >>> import brotli
>>> out1 = brotli.compress(b'foo')
>>> out2 = brotli.compress(b'bar')
>>> brotli.decompress(out1 + out2)
b'foo' # Only decodes the first stream in the buffer
>>> import gzip
>>> out1 = gzip.compress(b'foo')
>>> out2 = gzip.compress(b'bar')
>>> gzip.decompress(out1 + out2)
b'foobar' # gzip happily does both streams
>>>
# cramjam should expose functionality similar to brotli.Compressor:
>>> compressor = brotli.Compressor()
>>> compressor.compress(b'foo')
b''
>>> compressor.compress(b'bar')
b''
>>> out = compressor.finish()
>>> brotli.decompress(out)
b'foobar'
>>> I'll start work in cramjam to support exporting In the mean time, you could use >>> buf = cramjam.Buffer()
>>> buf.write(b'foo')
3
>>> buf.write(b'bar')
3
>>> buf.seek(0)
0
>>> out = cramjam.brotli.compress(buf)
>>> out
cramjam.Buffer(len=10)
>>> bytes(cramjam.brotli.decompress(out))
b'foobar'
>>> I don't exactly have a lot of time to add this stuff, but there is some time. 🙂 |
🤯 Thanks for jumping on this @milesgranger, it's really appreciated 🙏 I also don't have time right now but those are precious information 👼 |
sorry I'm just re-reading this @milesgranger. You are saying that Brotli and Deflate DO NOT support multiple streams? so even if you make it work in cramjam it won't be good enough for the project because we need to make sure our |
🤔 it's interesting because streaming response works with brotli-asgi, it just calls a |
Exactly right. You'll notice that cramjam doesn't expose any object with flush/finish methods. These tie off/finish the end of a stream. In cramjam's case, the entire buffer is expected, or to put it another way, it is stateless; the whole buffer is expected and treated as the output stream. Implementations like brotli have both I worked on it a bit last night and will likely be finished within the week. |
Thanks now I understand :D |
I've merged in and made a pre-release for cramjam with a
I'll update docs later, but for now this is an example: >>> import cramjam
>>> compressor = cramjam.brotli.Compressor()
>>> compressor.compress(b'foo')
3
>>> compressor.compress(b'bar')
3
>>> compressed = compressor.finish()
>>> bytes(compressed)
b'\x8b\x02\x80foobar\x03'
>>> decompressed = cramjam.brotli.decompress(compressed)
>>> bytes(decompressed)
b'foobar'
>>> |
that's awesome @milesgranger if we look at https://github.com/fullonic/brotli-asgi/blob/deb8a927b2328132b56f514b897f2aa8e80e716a/brotli_asgi/__init__.py#L144-L146 and https://github.com/fullonic/brotli-asgi/blob/deb8a927b2328132b56f514b897f2aa8e80e716a/brotli_asgi/__init__.py#L156-L158 it seems that the Again, ASGI streaming response are not something I master and I'm quite confused but if we look at the brotli asgi I think what it does is
I absolutely don't know what the |
No worries, let's see if we can get this squared away. I don't expose >>> import brotli
>>> compressor = brotli.Compressor()
>>> compressor.compress(b'foo')
b''
>>> compressor.compress(b'bar')
b''
>>> compressor.flush()
b'\x8b\x02\x80foobar'
>>> out = compressor.finish()
>>> out
b'\x03' Meanwhile, in the Rust implementation, the flush method returns a ie. >>> import cramjam
>>> compressor = cramjam.brotli.Compressor()
>>> compressor.compress(b'foo')
3
>>> compressor.compress(b'bar')
3
>>> out = compressor.finish()
>>> bytes(out)
b'\x8b\x02\x80foobar\x03' Note! It seems brotli does exactly this as well, should you just call >>> compressor = brotli.Compressor()
>>> compressor.compress(b'foo')
b''
>>> compressor.compress(b'bar')
b''
>>> compressor.finish()
b'\x8b\x02\x80foobar\x03'
>>> In your case, it seems you'd just not need to call |
I took a closer look and see that you would need access to what is currently compressed at arbitrary points, and |
❤️ no worries @milesgranger and thanks for your time I really appreciate 🙏 |
@milesgranger thanks for milesgranger/cramjam#68 🙏 |
My pleasure, also see #2 and see if that helps. :-) |
🤦 I didn't get the notification for #2 Thanks a lot 😄 |
Use cramjam Compressor
No description provided.