Core\Compress::Codec
Names the compressed format a program reads or writes.
Core\Compress::Codec is declared in the class Core\Compress. Every Core\Compress method that compresses or decompresses takes it as an argument.
Every Core\Compress call says which format it means, and says it as a case rather than as part of a member's name. There are five cases. Gzip, Zlib and Deflate are the same compressed stream under three different wrappers: a gzip header and footer, a short zlib header, or nothing around it at all. Brotli and Zstd are different algorithms rather than different wrappers, and both are common on the web today. Every format a browser asks for has a case here, although the deflate a browser names is the zlib wrapper rather than the bare stream.
Good to know: a compressed frame carries no label saying which case wrote it, and nothing sniffs one. A program that stores or sends a frame keeps the format beside it, and reading a frame under the wrong case is refused rather than guessed at. The examples show the three wrappers over one stream, turn a Content-Encoding header into a case, and pick the format a client said it could read.
Examples
Five formats and three wrappers
<?nvs// Gzip, Zlib and Deflate contain the same compressed data. Gzip and Zlib add a// few bytes around it. This program compresses one text with each of them and// prints the difference in size.
class Format{ public static function name(Core\Compress::Codec $codec): string { return match ($codec) { Core\Compress::Codec::Gzip => 'Gzip', Core\Compress::Codec::Zlib => 'Zlib', Core\Compress::Codec::Deflate => 'Deflate', Core\Compress::Codec::Brotli => 'Brotli', Core\Compress::Codec::Zstd => 'Zstd', }; }}
string $text = Core\Str::repeat("the quick brown fox jumps over the lazy dog\n", 16);bytes $payload = $text as bytes;
uint $bare = Core\Bytes::length(Core\Compress::compress($payload, Core\Compress::Codec::Deflate));uint $gzip = Core\Bytes::length(Core\Compress::compress($payload, Core\Compress::Codec::Gzip));uint $zlib = Core\Bytes::length(Core\Compress::compress($payload, Core\Compress::Codec::Zlib));
echo "Deflate is the stream with nothing around it\n";echo 'Gzip is that same stream plus ', $gzip - $bare, " octets\n";echo 'Zlib is that same stream plus ', $zlib - $bare, " octets\n";
// Brotli and Zstd are different compression algorithms. With each of the five// codecs, `decompress` returns exactly the original bytes.array<Core\Compress::Codec> $codecs = [ Core\Compress::Codec::Gzip, Core\Compress::Codec::Zlib, Core\Compress::Codec::Deflate, Core\Compress::Codec::Brotli, Core\Compress::Codec::Zstd,];
foreach ($codecs as Core\Compress::Codec $codec) { bytes $frame = Core\Compress::compress($payload, $codec); echo Format::name($codec), ': ', Core\Hash::equals(Core\Compress::decompress($frame, $codec), $payload) ? 'reads back' : 'differs', "\n";}Deflate is the stream with nothing around itGzip is that same stream plus 18 octetsZlib is that same stream plus 6 octetsGzip: reads backZlib: reads backDeflate: reads backBrotli: reads backZstd: reads backThe name a header uses
<?nvs// A compressed response says which format its body is in, in one header. The// name the header uses is not always the name of the case: `deflate` there// means the zlib wrapper rather than the bare stream. A name nothing answers// is refused here, while it is still a name, rather than guessed at later.
string $message = "{\"order\": 1042, \"total\": \"18.40\"}";array<string> $headers = ['gzip', 'deflate', 'br', 'zstd', 'exi'];
foreach ($headers as string $header) { ?Core\Compress::Codec $codec = match ($header) { 'gzip' => Core\Compress::Codec::Gzip, 'deflate' => Core\Compress::Codec::Zlib, 'br' => Core\Compress::Codec::Brotli, 'zstd' => Core\Compress::Codec::Zstd, default => null, };
if ($codec == null) { echo 'Content-Encoding: ', $header, " -> no case reads that\n"; } else { // The header picked the case, and the case is what reads the body. // Nothing here looks at the frame to decide. bytes $body = Core\Compress::compress($message, $codec); echo 'Content-Encoding: ', $header, ' -> ', Core\Bytes::length(Core\Compress::decompress($body, $codec)), " octets of JSON\n"; }}Content-Encoding: gzip -> 33 octets of JSONContent-Encoding: deflate -> 33 octets of JSONContent-Encoding: br -> 33 octets of JSONContent-Encoding: zstd -> 33 octets of JSONContent-Encoding: exi -> no case reads thatSending a page in the format the client asked for
<?nvs// A client lists the formats it can read. The server picks the best format that// it can also write. It sends the format name in `Content-Encoding`, because the// compressed bytes do not contain it.
class Response{ // The server decides the order: Zstd, then Brotli, then Gzip. Almost every // client can read Gzip, so it is the last choice. public static function pick(string $accept): ?Core\Compress::Codec { if (Core\Str::contains($accept, 'zstd')) { return Core\Compress::Codec::Zstd; } if (Core\Str::contains($accept, 'br')) { return Core\Compress::Codec::Brotli; } if (Core\Str::contains($accept, 'gzip')) { return Core\Compress::Codec::Gzip; } return null; }
// `header` returns the `Content-Encoding` name. In HTTP, `deflate` means the // Zlib format. Plain `Deflate` has no name, so the result is "". public static function header(Core\Compress::Codec $codec): string { return match ($codec) { Core\Compress::Codec::Gzip => 'gzip', Core\Compress::Codec::Zlib => 'deflate', Core\Compress::Codec::Deflate => '', Core\Compress::Codec::Brotli => 'br', Core\Compress::Codec::Zstd => 'zstd', }; }}
bytes $page = Core\Str::repeat("<li>order 1042 shipped</li>\n", 40) as bytes;
array<string> $clients = [ 'gzip, deflate, br, zstd', 'gzip, deflate', 'identity',];
foreach ($clients as string $accept) { ?Core\Compress::Codec $codec = Response::pick($accept);
// If the client accepts none of these formats, the server sends the page // without compression. if ($codec == null) { echo 'accepts ', $accept, " -> sent as it is\n"; } else { bytes $frame = Core\Compress::compress($page, $codec); echo 'accepts ', $accept, ' -> Content-Encoding: ', Response::header($codec), ', ', Core\Bytes::length($frame), ' octets instead of ', Core\Bytes::length($page), "\n"; }}accepts gzip, deflate, br, zstd -> Content-Encoding: zstd, 81 octets instead of 1120accepts gzip, deflate -> Content-Encoding: gzip, 68 octets instead of 1120accepts identity -> sent as it is