Expand description
Compression algorithms behind one Algorithm trait: Brotli,
Deflate, Gzip, Lz4, Snappy, Xz and Zstd, and the
choice of one from a name or an HTTP Accept-Encoding header
(Compression::from_accept_encoding).
Every algorithm writes the bytes PHP’s extension writes at the same level, on
the same C library (zlib, C brotli, libzstd, LZ4) or a port of it (snappy),
so data compressed by either runtime is read by the other, and responses
are identical. Each one can also write into a caller’s buffer (Sink), so
the output is never copied.
use utopia_compression::{Algorithm, Compression};
let algorithm = Compression::from_accept_encoding("gzip;q=0.8, br", &[] as &[&str]).unwrap();
assert_eq!(algorithm.content_encoding(), "br");
let body = algorithm.compress(b"Hello, Hello, Hello")?;
assert_eq!(algorithm.decompress(&body)?, b"Hello, Hello, Hello");use Utopia\Compression\Compression;
$algorithm = Compression::fromAcceptEncoding('gzip;q=0.8, br');
echo $algorithm->getContentEncoding(), "\n"; // br
$body = $algorithm->compress('Hello, Hello, Hello');
echo $algorithm->decompress($body), "\n"; // Hello, Hello, HelloCompressing is CPU work that grows with the input: in async code, run
large inputs on the blocking pool (utopia_async::spawn_blocking) so they
do not hold up a runtime thread.
§Cargo features
Each algorithm that needs a codec library is behind a feature, as each
needs an extension in PHP: brotli, lz4, zlib (Deflate and GZIP),
zstd (all on by default) and xz (off). Without its feature an
algorithm reports Algorithm::supported false and fails with
Error::Unsupported, like PHP without the extension. Snappy is pure
Rust and always available.
PHP: ports utopia-php/compression.
| PHP | Rust |
|---|---|
Utopia\Compression\Compression (abstract class) | Algorithm (trait), and Compression for the built-in algorithms |
Compression::NONE, IDENTITY, BROTLI, … | Compression::NONE, Compression::IDENTITY, Compression::BROTLI, … |
Compression::fromName() | Compression::from_name |
Compression::fromAcceptEncoding() | Compression::from_accept_encoding, Compression::from_accept_encoding_with (a name => bool map) |
getName(), getContentEncoding() | Algorithm::name, Algorithm::content_encoding |
compress(), decompress() | Algorithm::compress, Algorithm::decompress |
isSupported() (static) | is_supported() on each algorithm, and Algorithm::supported |
getLevel(), setLevel() | level(), set_level() on Brotli, Lz4 and Zstd |
new LZ4($level), new Zstd($level) | Lz4::with_level, Zstd::with_level |
Brotli::useGenericMode(), useTextMode(), useFontMode() | Brotli::use_generic_mode, Brotli::use_text_mode, Brotli::use_font_mode |
Algorithms\Brotli, Deflate, GZIP, LZ4, Snappy, XZ, Zstd | Brotli, Deflate, Gzip, Lz4, Snappy, Xz, Zstd |
\InvalidArgumentException, a warning and "", \Error | Error::InvalidArgument, Error::Codec, Error::Unsupported |
PHP: where an extension emits a warning and returns false (invalid
data, a level the codec refuses), the PHP method returns an empty string;
Rust returns Error::Codec with the warning’s text.
Structs§
- Allocator
- The allocator a codec’s working state (zstd’s contexts, brotli’s encoder and decoder) comes
from during one call, in the C libraries’ own shape:
alloc(opaque, size)andfree(opaque, address). A block must be aligned asmalloc()aligns one. - Brotli
- Brotli (RFC 7932):
Content-Encoding: br. - Deflate
- Raw deflate (RFC 1951) at level 6:
Content-Encoding: deflate. - Gzip
- Gzip (RFC 1952) at zlib’s default level:
Content-Encoding: gzip. - Lz4
- LZ4 blocks with ext-lz4’s framing: the uncompressed size as a 4-byte little-endian prefix, then one LZ4 block.
- Snappy
- Snappy raw format: the uncompressed length as a varint, then literals and copies.
- Xz
- XZ: LZMA2 at preset 5 in an
.xzcontainer with a CRC64 check. - Zstd
- Zstandard (RFC 8878):
Content-Encoding: zstd.
Enums§
- Compression
- A built-in algorithm, as chosen by name or from an
Accept-Encodingheader. - Error
- Why compressing, decompressing or configuring an algorithm failed.
- Mode
- The kind of data Brotli tunes its encoding for.