libbrotli: Brotli Compression for Racket
| (require libbrotli) | package: libbrotli |
Racket bindings for Google’s Brotli compression library. Provides one-shot compression/decompression of byte strings, a streaming output port for incremental compression, and a streaming input port for incremental decompression.
All compression and decompression functions accept an optional #:dictionary parameter for LZ77 prefix dictionaries. If a dictionary is used for compression, the same dictionary must be used for decompression. Dictionary data must not exceed 16 MiB.
1 One-Shot Compression
procedure
(brotli-compress src [ quality #:window window #:mode mode #:lgblock lgblock #:dictionary dictionary]) → bytes? src : bytes? quality : quality/c = BROTLI_DEFAULT_QUALITY window : window/c = BROTLI_DEFAULT_WINDOW mode : mode/c = BROTLI_MODE_GENERIC lgblock : lgblock/c = 0 dictionary : bytes? = #""
procedure
(brotli-compress! src dst [ quality #:window window #:mode mode #:lgblock lgblock #:dictionary dictionary]) → exact-nonnegative-integer? src : bytes? dst : bytes? quality : quality/c = BROTLI_DEFAULT_QUALITY window : window/c = BROTLI_DEFAULT_WINDOW mode : mode/c = BROTLI_MODE_GENERIC lgblock : lgblock/c = 0 dictionary : bytes? = #""
2 One-Shot Decompression
procedure
(brotli-decompress src [ max-decompressed-size #:dictionary dictionary]) → bytes? src : bytes? max-decompressed-size : (or/c #f exact-positive-integer?) = #f dictionary : bytes? = #""
procedure
(brotli-decompress! src dst [ #:dictionary dictionary]) → exact-nonnegative-integer? src : bytes? dst : bytes? dictionary : bytes? = #""
3 Streaming Output Port
procedure
(open-brotli-output out [ #:quality quality #:window window #:mode mode #:lgblock lgblock #:dictionary dictionary #:close? close? #:name name]) → output-port? out : output-port? quality : quality/c = 6 window : window/c = BROTLI_DEFAULT_WINDOW mode : mode/c = BROTLI_MODE_GENERIC lgblock : lgblock/c = 0 dictionary : bytes? = #"" close? : boolean? = #t name : symbol? = 'brotli-output
Calling flush-output on the returned port issues a Brotli FLUSH operation, ensuring the receiver can decode all data written so far. This is essential for streaming protocols like SSE.
Closing the returned port finalises the Brotli stream. If close? is #t (the default), the underlying port out is also closed; otherwise it is left open.
4 Streaming Input Port
procedure
(open-brotli-input in [ #:dictionary dictionary #:close? close? #:name name]) → input-port? in : input-port? dictionary : bytes? = #"" close? : boolean? = #t name : symbol? = 'brotli-input
Reading from the returned port pulls compressed bytes from in, decompresses them incrementally, and returns the decompressed data. This allows decompression of arbitrarily large streams without loading the entire compressed input into memory.
Closing the returned port destroys the decoder state. If close? is #t (the default), the underlying port in is also closed; otherwise it is left open.
Raises exn:fail? if the compressed data is corrupt or the stream is truncated.
5 Contracts
value
value
value
value
6 Constants
6.1 Defaults
value
value
6.2 Quality Range
value
value
6.3 Window Range
value
value
6.4 Compression Modes
value
BROTLI_MODE_GENERIC : mode/c = 0
value
BROTLI_MODE_TEXT : mode/c = 1
value
BROTLI_MODE_FONT : mode/c = 2