Skip to content

Support incremental compression and decompression #26

@davidmhammond

Description

@davidmhammond

Although the brotli extension supports streaming via stream wrappers, it is currently missing the ability to compress/decompress incrementally. This feature is needed in order to achieve certain streaming workflows that cannot be accomplished with the existing functionality.

This would involve adding at least four new functions: brotli_compress_init(), brotli_compress_add(), brotli_uncompress_init(), and brotli_uncompress_add(). These functions should be analogous to the zlib extension's deflate_init(), deflate_add(), inflate_init(), and inflate_add(), respectively, including flags to control flush behavior (at a minimum, equivalents of ZLIB_SYNC_FLUSH and ZLIB_FINISH).

Example of when incremental compression is needed: You're using a response model such as PSR-7, the page output is generated in a streaming manner via a generator, and the response object contains headers indicating brotli compression. In this case, as you pull data from the response stream, that data should come out already brotli-compressed (this stream may or may not represent the final output to the browser). Currently, this can only be accomplished by compressing the entire stream contents at once (which means you don't get a true streaming pipeline), or by an elaborate hack whereby reading the stream writes to a brotli stream wrapper that is wrapped around a custom stream wrapper that writes to a buffer which the first stream then reads from. Support for incremental compression would make this much simpler and less hacky.

Example of when incremental decompression is needed: You're using a stream filter framework where each filter operates on a chunk of data at a time, and you want to create a filter that performs a brotli decompression of the input. With incremental decompression, the filter object could call brotli_uncompress_init() for the first chunk, and return brotli_uncompress_add() for each chunk in the stream, passing the BROTLI_FINISH flag on the final chunk. Without incremental decompression, there's really no way to accomplish brotli decompression in this kind of filter system without just building a giant string and decompressing it all on the final chunk (which, again, means you don't get a true streaming pipeline).

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions