feat: compress Static Files with zstdUnverified
98a3c8f parent: 772ff4d modified
.github/workflows/ci.yml +3 -1 | @@ -27,6 +27,8 @@ jobs: | ||
| 27 | 27 | with: |
| 28 | 28 | php-version: ${{ matrix.php }} |
| 29 | 29 | coverage: none |
| 30 | + # Covers the zstd and brotli compression of Static Files, skipped without them. | |
| 31 | + extensions: zstd, brotli | |
| 30 | 32 | tools: flex |
| 31 | 33 | |
| 32 | 34 | - name: Validate composer.json |
| @@ -55,7 +57,7 @@ jobs: | ||
| 55 | 57 | with: |
| 56 | 58 | php-version: '8.5' |
| 57 | 59 | coverage: none |
| 58 | - extensions: zip | |
| 60 | + extensions: zip, zstd | |
| 59 | 61 | |
| 60 | 62 | - name: Install with the runtime of this checkout |
| 61 | 63 | working-directory: example |
| @@ -27,6 +27,8 @@ jobs: | |||
| 27 | with: | 27 | with: |
| 28 | php-version: ${{ matrix.php }} | 28 | php-version: ${{ matrix.php }} |
| 29 | coverage: none | 29 | coverage: none |
| 30 | + # Covers the zstd and brotli compression of Static Files, skipped without them. | ||
| 31 | + extensions: zstd, brotli | ||
| 30 | tools: flex | 32 | tools: flex |
| 31 | 33 | ||
| 32 | - name: Validate composer.json | 34 | - name: Validate composer.json |
| @@ -55,7 +57,7 @@ jobs: | |||
| 55 | with: | 57 | with: |
| 56 | php-version: '8.5' | 58 | php-version: '8.5' |
| 57 | coverage: none | 59 | coverage: none |
| 58 | - extensions: zip | 60 | + extensions: zip, zstd |
| 59 | 61 | ||
| 60 | - name: Install with the runtime of this checkout | 62 | - name: Install with the runtime of this checkout |
| 61 | working-directory: example | 63 | working-directory: example |
modified
README.md +3 -3 | @@ -319,7 +319,7 @@ Long-lived caching of digested files is what spares most of them. | ||
| 319 | 319 | |
| 320 | 320 | A file is sent compressed when the client accepts it, in this order: |
| 321 | 321 | |
| 322 | -1. a precompressed variant shipped next to it, `app.css.br` then `app.css.gz`, written by any build step; | |
| 322 | +1. a precompressed variant shipped next to it, `app.css.br`, then `app.css.zst`, then `app.css.gz`, written by any build step; | |
| 323 | 323 | 2. otherwise, when the `static_compression` runtime option is set, the file compressed by the runtime. |
| 324 | 324 | |
| 325 | 325 | ```json |
| @@ -332,8 +332,8 @@ A file is sent compressed when the client accepts it, in this order: | ||
| 332 | 332 | } |
| 333 | 333 | ``` |
| 334 | 334 | |
| 335 | -`static_compression` is `false` by default, `"gzip"`, or `"br"`, which needs the `brotli` extension: any other value, or `"br"` without the extension, answers every Invocation with a 500 until fixed. | |
| 336 | -The Scaleway `php85` runtime does not load `brotli`, so use `"gzip"` there, and ship `.br` variants built beforehand for brotli. | |
| 335 | +`static_compression` is `false` by default, `"gzip"`, `"zstd"` or `"br"`, each needing its extension (`zlib`, `zstd`, `brotli`): any other value, or one whose extension is not loaded, answers every Invocation with a 500 until fixed. | |
| 336 | +The Scaleway `php85` runtime loads `zlib` and `zstd` but not `brotli`: use `"zstd"` or `"gzip"` there, and ship `.br` variants built beforehand for brotli. | |
| 337 | 337 | The runtime only compresses textual files (`text/*`, JavaScript, JSON, XML, SVG, WebAssembly) of at least 1 KiB, and keeps the results in memory for the Warm Process, up to 32 MiB. |
| 338 | 338 | |
| 339 | 339 | The order of preference is the runtime's: only a `q=0` in `Accept-Encoding` refuses an encoding. |
| @@ -319,7 +319,7 @@ Long-lived caching of digested files is what spares most of them. | |||
| 319 | 319 | ||
| 320 | A file is sent compressed when the client accepts it, in this order: | 320 | A file is sent compressed when the client accepts it, in this order: |
| 321 | 321 | ||
| 322 | -1. a precompressed variant shipped next to it, `app.css.br` then `app.css.gz`, written by any build step; | 322 | +1. a precompressed variant shipped next to it, `app.css.br`, then `app.css.zst`, then `app.css.gz`, written by any build step; |
| 323 | 2. otherwise, when the `static_compression` runtime option is set, the file compressed by the runtime. | 323 | 2. otherwise, when the `static_compression` runtime option is set, the file compressed by the runtime. |
| 324 | 324 | ||
| 325 | ```json | 325 | ```json |
| @@ -332,8 +332,8 @@ A file is sent compressed when the client accepts it, in this order: | |||
| 332 | } | 332 | } |
| 333 | ``` | 333 | ``` |
| 334 | 334 | ||
| 335 | -`static_compression` is `false` by default, `"gzip"`, or `"br"`, which needs the `brotli` extension: any other value, or `"br"` without the extension, answers every Invocation with a 500 until fixed. | 335 | +`static_compression` is `false` by default, `"gzip"`, `"zstd"` or `"br"`, each needing its extension (`zlib`, `zstd`, `brotli`): any other value, or one whose extension is not loaded, answers every Invocation with a 500 until fixed. |
| 336 | -The Scaleway `php85` runtime does not load `brotli`, so use `"gzip"` there, and ship `.br` variants built beforehand for brotli. | 336 | +The Scaleway `php85` runtime loads `zlib` and `zstd` but not `brotli`: use `"zstd"` or `"gzip"` there, and ship `.br` variants built beforehand for brotli. |
| 337 | The runtime only compresses textual files (`text/*`, JavaScript, JSON, XML, SVG, WebAssembly) of at least 1 KiB, and keeps the results in memory for the Warm Process, up to 32 MiB. | 337 | The runtime only compresses textual files (`text/*`, JavaScript, JSON, XML, SVG, WebAssembly) of at least 1 KiB, and keeps the results in memory for the Warm Process, up to 32 MiB. |
| 338 | 338 | ||
| 339 | The order of preference is the runtime's: only a `q=0` in `Accept-Encoding` refuses an encoding. | 339 | The order of preference is the runtime's: only a `q=0` in `Accept-Encoding` refuses an encoding. |
modified
example/.gitignore +1 -0 | @@ -7,3 +7,4 @@ | ||
| 7 | 7 | # Precompressed Static File variants are shipped on purpose, whatever a global gitignore says. |
| 8 | 8 | !/public/**/*.gz |
| 9 | 9 | !/public/**/*.br |
| 10 | +!/public/**/*.zst | |
| @@ -7,3 +7,4 @@ | |||
| 7 | # Precompressed Static File variants are shipped on purpose, whatever a global gitignore says. | 7 | # Precompressed Static File variants are shipped on purpose, whatever a global gitignore says. |
| 8 | !/public/**/*.gz | 8 | !/public/**/*.gz |
| 9 | !/public/**/*.br | 9 | !/public/**/*.br |
| 10 | +!/public/**/*.zst | ||
modified
example/README.md +1 -1 | @@ -18,7 +18,7 @@ The files of `public/` are [Static Files](../README.md#static-files), served by | ||
| 18 | 18 | |
| 19 | 19 | | File | What it shows | |
| 20 | 20 | | --- | --- | |
| 21 | -| `/css/app.css` | A stylesheet, revalidated with its `ETag`; with `Accept-Encoding: gzip`, its precompressed `app.css.gz` | | |
| 21 | +| `/css/app.css` | A stylesheet, revalidated with its `ETag`; with `Accept-Encoding: zstd` or `gzip`, its precompressed `app.css.zst` or `app.css.gz` | | |
| 22 | 22 | | `/js/greeting.js` | A JavaScript module, as `text/javascript` | |
| 23 | 23 | | `/images/logo.png` | A binary file, base64-encoded in the Function Response | |
| 24 | 24 | | `/assets/app-Ex4mPl3.css` | A digested file as AssetMapper writes one, listed in `assets/manifest.json`, so cached for good | |
| @@ -18,7 +18,7 @@ The files of `public/` are [Static Files](../README.md#static-files), served by | |||
| 18 | 18 | ||
| 19 | | File | What it shows | | 19 | | File | What it shows | |
| 20 | | --- | --- | | 20 | | --- | --- | |
| 21 | -| `/css/app.css` | A stylesheet, revalidated with its `ETag`; with `Accept-Encoding: gzip`, its precompressed `app.css.gz` | | 21 | +| `/css/app.css` | A stylesheet, revalidated with its `ETag`; with `Accept-Encoding: zstd` or `gzip`, its precompressed `app.css.zst` or `app.css.gz` | |
| 22 | | `/js/greeting.js` | A JavaScript module, as `text/javascript` | | 22 | | `/js/greeting.js` | A JavaScript module, as `text/javascript` | |
| 23 | | `/images/logo.png` | A binary file, base64-encoded in the Function Response | | 23 | | `/images/logo.png` | A binary file, base64-encoded in the Function Response | |
| 24 | | `/assets/app-Ex4mPl3.css` | A digested file as AssetMapper writes one, listed in `assets/manifest.json`, so cached for good | | 24 | | `/assets/app-Ex4mPl3.css` | A digested file as AssetMapper writes one, listed in `assets/manifest.json`, so cached for good | |
modified
example/e2e/run.sh +9 -0 | @@ -148,6 +148,15 @@ expect "precompressed variant encoding" "$(header gzip content-encoding)" gzip | ||
| 148 | 148 | expect "precompressed variant varies" "$(header gzip vary)" Accept-Encoding |
| 149 | 149 | expect "precompressed variant body" "$(php -r 'echo hash("sha256", (string) gzdecode(file_get_contents($argv[1])));' "$tmp/gzip.body")" "$(sha256 "$(dirname "$0")/../public/css/app.css")" |
| 150 | 150 | |
| 151 | +# zstd is preferred to gzip, and Scaleway's php85 can compress with it too. | |
| 152 | +status=$(request zstd -H 'Accept-Encoding: gzip, zstd' "$base_url/css/app.css") | |
| 153 | +expect "zstd variant preferred to gzip" "$(header zstd content-encoding)" zstd | |
| 154 | +if php -r 'exit(function_exists("zstd_uncompress") ? 0 : 1);'; then | |
| 155 | + expect "zstd variant body" "$(php -r 'echo hash("sha256", (string) zstd_uncompress(file_get_contents($argv[1])));' "$tmp/zstd.body")" "$(sha256 "$(dirname "$0")/../public/css/app.css")" | |
| 156 | +else | |
| 157 | + echo "skip zstd variant body: the local php lacks the zstd extension" | |
| 158 | +fi | |
| 159 | + | |
| 151 | 160 | status=$(request js "$base_url/js/greeting.js") |
| 152 | 161 | expect "JavaScript module content type" "$status $(header js content-type)" "200 text/javascript" |
| 153 | 162 | status=$(request png "$base_url/images/logo.png") |
| @@ -148,6 +148,15 @@ expect "precompressed variant encoding" "$(header gzip content-encoding)" gzip | |||
| 148 | expect "precompressed variant varies" "$(header gzip vary)" Accept-Encoding | 148 | expect "precompressed variant varies" "$(header gzip vary)" Accept-Encoding |
| 149 | expect "precompressed variant body" "$(php -r 'echo hash("sha256", (string) gzdecode(file_get_contents($argv[1])));' "$tmp/gzip.body")" "$(sha256 "$(dirname "$0")/../public/css/app.css")" | 149 | expect "precompressed variant body" "$(php -r 'echo hash("sha256", (string) gzdecode(file_get_contents($argv[1])));' "$tmp/gzip.body")" "$(sha256 "$(dirname "$0")/../public/css/app.css")" |
| 150 | 150 | ||
| 151 | +# zstd is preferred to gzip, and Scaleway's php85 can compress with it too. | ||
| 152 | +status=$(request zstd -H 'Accept-Encoding: gzip, zstd' "$base_url/css/app.css") | ||
| 153 | +expect "zstd variant preferred to gzip" "$(header zstd content-encoding)" zstd | ||
| 154 | +if php -r 'exit(function_exists("zstd_uncompress") ? 0 : 1);'; then | ||
| 155 | + expect "zstd variant body" "$(php -r 'echo hash("sha256", (string) zstd_uncompress(file_get_contents($argv[1])));' "$tmp/zstd.body")" "$(sha256 "$(dirname "$0")/../public/css/app.css")" | ||
| 156 | +else | ||
| 157 | + echo "skip zstd variant body: the local php lacks the zstd extension" | ||
| 158 | +fi | ||
| 159 | + | ||
| 151 | status=$(request js "$base_url/js/greeting.js") | 160 | status=$(request js "$base_url/js/greeting.js") |
| 152 | expect "JavaScript module content type" "$status $(header js content-type)" "200 text/javascript" | 161 | expect "JavaScript module content type" "$status $(header js content-type)" "200 text/javascript" |
| 153 | status=$(request png "$base_url/images/logo.png") | 162 | status=$(request png "$base_url/images/logo.png") |
added
example/public/css/app.css.zst +0 -0 | new file mode 100644 | ||
| Binary files /dev/null and b/example/public/css/app.css.zst differ | ||
| new file mode 100644 | |||
| Binary files /dev/null and b/example/public/css/app.css.zst differ | Binary files /dev/null and b/example/public/css/app.css.zst differ | ||
modified
src/ScalewayRuntime.php +1 -1 | @@ -27,7 +27,7 @@ use Symfony\Component\Runtime\SymfonyRuntime; | ||
| 27 | 27 | * - "front_controller": the Front Controller path, relative to "project_dir"; defaults to "public/index.php"; |
| 28 | 28 | * - "host": the public host of the function, as no Host header reaches it; defaults to the SCALEWAY_HOST |
| 29 | 29 | * environment variable, then to "localhost"; |
| 30 | - * - "static_compression": false (the default), "gzip" or "br", the encoding Static Files of a textual type are | |
| 30 | + * - "static_compression": false (the default), "gzip", "br" or "zstd", the encoding Static Files of a textual type are | |
| 31 | 31 | * compressed with on the fly when they have no precompressed variant. |
| 32 | 32 | * |
| 33 | 33 | * Unlike SymfonyRuntime, it defaults the env to prod when the process environment sets none, whatever .env files say. |
| @@ -27,7 +27,7 @@ use Symfony\Component\Runtime\SymfonyRuntime; | |||
| 27 | * - "front_controller": the Front Controller path, relative to "project_dir"; defaults to "public/index.php"; | 27 | * - "front_controller": the Front Controller path, relative to "project_dir"; defaults to "public/index.php"; |
| 28 | * - "host": the public host of the function, as no Host header reaches it; defaults to the SCALEWAY_HOST | 28 | * - "host": the public host of the function, as no Host header reaches it; defaults to the SCALEWAY_HOST |
| 29 | * environment variable, then to "localhost"; | 29 | * environment variable, then to "localhost"; |
| 30 | - * - "static_compression": false (the default), "gzip" or "br", the encoding Static Files of a textual type are | 30 | + * - "static_compression": false (the default), "gzip", "br" or "zstd", the encoding Static Files of a textual type are |
| 31 | * compressed with on the fly when they have no precompressed variant. | 31 | * compressed with on the fly when they have no precompressed variant. |
| 32 | * | 32 | * |
| 33 | * Unlike SymfonyRuntime, it defaults the env to prod when the process environment sets none, whatever .env files say. | 33 | * Unlike SymfonyRuntime, it defaults the env to prod when the process environment sets none, whatever .env files say. |
modified
src/StaticFiles.php +12 -14 | @@ -18,10 +18,10 @@ use Symfony\Component\Mime\MimeTypes; | ||
| 18 | 18 | final class StaticFiles |
| 19 | 19 | { |
| 20 | 20 | /** Encodings of the precompressed variants, in order of preference, with their file extension. */ |
| 21 | - public const array PRECOMPRESSED = ['br' => '.br', 'gzip' => '.gz']; | |
| 21 | + public const array PRECOMPRESSED = ['br' => '.br', 'zstd' => '.zst', 'gzip' => '.gz']; | |
| 22 | 22 | |
| 23 | - /** The values of the "static_compression" runtime option, but false. */ | |
| 24 | - public const array COMPRESSIONS = ['gzip', 'br']; | |
| 23 | + /** The values of the "static_compression" runtime option, but false, with the extension and function each needs. */ | |
| 24 | + public const array COMPRESSIONS = ['gzip' => ['zlib', 'gzencode'], 'br' => ['brotli', 'brotli_compress'], 'zstd' => ['zstd', 'zstd_compress']]; | |
| 25 | 25 | |
| 26 | 26 | // The platform enforces no limit, but larger responses intermittently time out (docs/probe-results.md#size-and-encodings). |
| 27 | 27 | public const int MAX_RESPONSE_SIZE = 6 * 1024 * 1024; |
| @@ -49,9 +49,9 @@ final class StaticFiles | ||
| 49 | 49 | private array $manifests = []; |
| 50 | 50 | |
| 51 | 51 | /** |
| 52 | - * @param string $webRoot the directory holding the Static Files | |
| 53 | - * @param 'gzip'|'br'|null $compression the encoding to compress textual files with on the fly, if any | |
| 54 | - * @param resource $errorLog | |
| 52 | + * @param string $webRoot the directory holding the Static Files | |
| 53 | + * @param 'gzip'|'br'|'zstd'|null $compression the encoding to compress textual files with on the fly, if any | |
| 54 | + * @param resource $errorLog | |
| 55 | 55 | */ |
| 56 | 56 | public function __construct( |
| 57 | 57 | private readonly string $webRoot, |
| @@ -69,15 +69,13 @@ final class StaticFiles | ||
| 69 | 69 | if (false === $compression || null === $compression) { |
| 70 | 70 | return new self($webRoot, null, $errorLog); |
| 71 | 71 | } |
| 72 | - if (!\in_array($compression, self::COMPRESSIONS, true)) { | |
| 73 | - throw new \InvalidArgumentException(\sprintf('The "static_compression" runtime option must be false, "gzip" or "br", "%s" given.', get_debug_type($compression))); | |
| 72 | + if (!\is_string($compression) || !isset(self::COMPRESSIONS[$compression])) { | |
| 73 | + throw new \InvalidArgumentException(\sprintf('The "static_compression" runtime option must be false, "gzip", "br" or "zstd", "%s" given.', get_debug_type($compression))); | |
| 74 | 74 | } |
| 75 | 75 | // Falling back to another encoding would hide that the function serves something else than what was asked. |
| 76 | - if ('br' === $compression && !\function_exists('brotli_compress')) { | |
| 77 | - throw new \LogicException('The "static_compression" runtime option is "br", but the brotli extension is not loaded.'); | |
| 78 | - } | |
| 79 | - if ('gzip' === $compression && !\function_exists('gzencode')) { | |
| 80 | - throw new \LogicException('The "static_compression" runtime option is "gzip", but the zlib extension is not loaded.'); | |
| 76 | + [$extension, $function] = self::COMPRESSIONS[$compression]; | |
| 77 | + if (!\function_exists($function)) { | |
| 78 | + throw new \LogicException(\sprintf('The "static_compression" runtime option is "%s", but the %s extension is not loaded.', $compression, $extension)); | |
| 81 | 79 | } |
| 82 | 80 | |
| 83 | 81 | return new self($webRoot, $compression, $errorLog); |
| @@ -255,7 +253,7 @@ final class StaticFiles | ||
| 255 | 253 | } |
| 256 | 254 | |
| 257 | 255 | $content = (string) file_get_contents($file); |
| 258 | - $body = (string) ('br' === $encoding ? brotli_compress($content) : gzencode($content)); | |
| 256 | + $body = (string) (self::COMPRESSIONS[$encoding][1])($content); | |
| 259 | 257 | // Past the cap, files are compressed again for each Invocation rather than growing the Warm Process. |
| 260 | 258 | if ($this->compressedSize + \strlen($body) <= self::COMPRESSION_CACHE_SIZE) { |
| 261 | 259 | $this->compressed[$key] = $body; |
| @@ -18,10 +18,10 @@ use Symfony\Component\Mime\MimeTypes; | |||
| 18 | final class StaticFiles | 18 | final class StaticFiles |
| 19 | { | 19 | { |
| 20 | /** Encodings of the precompressed variants, in order of preference, with their file extension. */ | 20 | /** Encodings of the precompressed variants, in order of preference, with their file extension. */ |
| 21 | - public const array PRECOMPRESSED = ['br' => '.br', 'gzip' => '.gz']; | 21 | + public const array PRECOMPRESSED = ['br' => '.br', 'zstd' => '.zst', 'gzip' => '.gz']; |
| 22 | 22 | ||
| 23 | - /** The values of the "static_compression" runtime option, but false. */ | 23 | + /** The values of the "static_compression" runtime option, but false, with the extension and function each needs. */ |
| 24 | - public const array COMPRESSIONS = ['gzip', 'br']; | 24 | + public const array COMPRESSIONS = ['gzip' => ['zlib', 'gzencode'], 'br' => ['brotli', 'brotli_compress'], 'zstd' => ['zstd', 'zstd_compress']]; |
| 25 | 25 | ||
| 26 | // The platform enforces no limit, but larger responses intermittently time out (docs/probe-results.md#size-and-encodings). | 26 | // The platform enforces no limit, but larger responses intermittently time out (docs/probe-results.md#size-and-encodings). |
| 27 | public const int MAX_RESPONSE_SIZE = 6 * 1024 * 1024; | 27 | public const int MAX_RESPONSE_SIZE = 6 * 1024 * 1024; |
| @@ -49,9 +49,9 @@ final class StaticFiles | |||
| 49 | private array $manifests = []; | 49 | private array $manifests = []; |
| 50 | 50 | ||
| 51 | /** | 51 | /** |
| 52 | - * @param string $webRoot the directory holding the Static Files | 52 | + * @param string $webRoot the directory holding the Static Files |
| 53 | - * @param 'gzip'|'br'|null $compression the encoding to compress textual files with on the fly, if any | 53 | + * @param 'gzip'|'br'|'zstd'|null $compression the encoding to compress textual files with on the fly, if any |
| 54 | - * @param resource $errorLog | 54 | + * @param resource $errorLog |
| 55 | */ | 55 | */ |
| 56 | public function __construct( | 56 | public function __construct( |
| 57 | private readonly string $webRoot, | 57 | private readonly string $webRoot, |
| @@ -69,15 +69,13 @@ final class StaticFiles | |||
| 69 | if (false === $compression || null === $compression) { | 69 | if (false === $compression || null === $compression) { |
| 70 | return new self($webRoot, null, $errorLog); | 70 | return new self($webRoot, null, $errorLog); |
| 71 | } | 71 | } |
| 72 | - if (!\in_array($compression, self::COMPRESSIONS, true)) { | 72 | + if (!\is_string($compression) || !isset(self::COMPRESSIONS[$compression])) { |
| 73 | - throw new \InvalidArgumentException(\sprintf('The "static_compression" runtime option must be false, "gzip" or "br", "%s" given.', get_debug_type($compression))); | 73 | + throw new \InvalidArgumentException(\sprintf('The "static_compression" runtime option must be false, "gzip", "br" or "zstd", "%s" given.', get_debug_type($compression))); |
| 74 | } | 74 | } |
| 75 | // Falling back to another encoding would hide that the function serves something else than what was asked. | 75 | // Falling back to another encoding would hide that the function serves something else than what was asked. |
| 76 | - if ('br' === $compression && !\function_exists('brotli_compress')) { | 76 | + [$extension, $function] = self::COMPRESSIONS[$compression]; |
| 77 | - throw new \LogicException('The "static_compression" runtime option is "br", but the brotli extension is not loaded.'); | 77 | + if (!\function_exists($function)) { |
| 78 | - } | 78 | + throw new \LogicException(\sprintf('The "static_compression" runtime option is "%s", but the %s extension is not loaded.', $compression, $extension)); |
| 79 | - if ('gzip' === $compression && !\function_exists('gzencode')) { | ||
| 80 | - throw new \LogicException('The "static_compression" runtime option is "gzip", but the zlib extension is not loaded.'); | ||
| 81 | } | 79 | } |
| 82 | 80 | ||
| 83 | return new self($webRoot, $compression, $errorLog); | 81 | return new self($webRoot, $compression, $errorLog); |
| @@ -255,7 +253,7 @@ final class StaticFiles | |||
| 255 | } | 253 | } |
| 256 | 254 | ||
| 257 | $content = (string) file_get_contents($file); | 255 | $content = (string) file_get_contents($file); |
| 258 | - $body = (string) ('br' === $encoding ? brotli_compress($content) : gzencode($content)); | 256 | + $body = (string) (self::COMPRESSIONS[$encoding][1])($content); |
| 259 | // Past the cap, files are compressed again for each Invocation rather than growing the Warm Process. | 257 | // Past the cap, files are compressed again for each Invocation rather than growing the Warm Process. |
| 260 | if ($this->compressedSize + \strlen($body) <= self::COMPRESSION_CACHE_SIZE) { | 258 | if ($this->compressedSize + \strlen($body) <= self::COMPRESSION_CACHE_SIZE) { |
| 261 | $this->compressed[$key] = $body; | 259 | $this->compressed[$key] = $body; |
modified
tests/HandlerTest.php +1 -1 | @@ -120,7 +120,7 @@ final class HandlerTest extends TestCase | ||
| 120 | 120 | |
| 121 | 121 | self::assertSame(500, $handler->invoke(['path' => '/static.txt'], [])['statusCode']); |
| 122 | 122 | self::assertSame(500, $handler->invoke(['path' => '/'], [])['statusCode']); |
| 123 | - self::assertStringContainsString('The "static_compression" runtime option must be false, "gzip" or "br"', $this->errorLog()); | |
| 123 | + self::assertStringContainsString('The "static_compression" runtime option must be false, "gzip", "br" or "zstd"', $this->errorLog()); | |
| 124 | 124 | self::assertSame(0, RecordingKernel::$builds); |
| 125 | 125 | } |
| 126 | 126 | |
| @@ -120,7 +120,7 @@ final class HandlerTest extends TestCase | |||
| 120 | 120 | ||
| 121 | self::assertSame(500, $handler->invoke(['path' => '/static.txt'], [])['statusCode']); | 121 | self::assertSame(500, $handler->invoke(['path' => '/static.txt'], [])['statusCode']); |
| 122 | self::assertSame(500, $handler->invoke(['path' => '/'], [])['statusCode']); | 122 | self::assertSame(500, $handler->invoke(['path' => '/'], [])['statusCode']); |
| 123 | - self::assertStringContainsString('The "static_compression" runtime option must be false, "gzip" or "br"', $this->errorLog()); | 123 | + self::assertStringContainsString('The "static_compression" runtime option must be false, "gzip", "br" or "zstd"', $this->errorLog()); |
| 124 | self::assertSame(0, RecordingKernel::$builds); | 124 | self::assertSame(0, RecordingKernel::$builds); |
| 125 | } | 125 | } |
| 126 | 126 | ||
modified
tests/StaticFilesTest.php +29 -2 | @@ -184,9 +184,11 @@ final class StaticFilesTest extends TestCase | ||
| 184 | 184 | yield 'gzip only' => ['gzip', 'gzip']; |
| 185 | 185 | yield 'both, brotli preferred whatever the weights' => ['gzip;q=1.0, br;q=0.1', 'br']; |
| 186 | 186 | yield 'brotli refused' => ['gzip, br;q=0', 'gzip']; |
| 187 | + yield 'zstd preferred to gzip' => ['gzip, zstd', 'zstd']; | |
| 188 | + yield 'brotli preferred to zstd' => ['zstd, br', 'br']; | |
| 187 | 189 | yield 'wildcard' => ['*', 'br']; |
| 188 | - yield 'wildcard but brotli' => ['*, br;q=0', 'gzip']; | |
| 189 | - yield 'everything refused' => ['br;q=0, gzip;q=0', null]; | |
| 190 | + yield 'wildcard but brotli' => ['*, br;q=0', 'zstd']; | |
| 191 | + yield 'everything refused' => ['br;q=0, zstd;q=0, gzip;q=0', null]; | |
| 190 | 192 | yield 'case insensitive' => ['GZIP', 'gzip']; |
| 191 | 193 | } |
| 192 | 194 | |
| @@ -194,6 +196,7 @@ final class StaticFilesTest extends TestCase | ||
| 194 | 196 | public function testServesThePreferredPrecompressedVariant(string $acceptEncoding, ?string $expected): void |
| 195 | 197 | { |
| 196 | 198 | $this->write('app.css.br', 'brotli bytes'); |
| 199 | + $this->write('app.css.zst', "\x28\xb5\x2f\xfd zstd bytes"); | |
| 197 | 200 | $this->write('app.css.gz', "\x1f\x8b gzip bytes"); |
| 198 | 201 | |
| 199 | 202 | $response = $this->serve('/app.css', headers: ['Accept-Encoding' => $acceptEncoding]); |
| @@ -204,6 +207,7 @@ final class StaticFilesTest extends TestCase | ||
| 204 | 207 | $body = true === ($response['isBase64Encoded'] ?? false) ? base64_decode($response['body'], true) : $response['body']; |
| 205 | 208 | self::assertSame(match ($expected) { |
| 206 | 209 | 'br' => 'brotli bytes', |
| 210 | + 'zstd' => "\x28\xb5\x2f\xfd zstd bytes", | |
| 207 | 211 | 'gzip' => "\x1f\x8b gzip bytes", |
| 208 | 212 | default => 'body { color: red; }', |
| 209 | 213 | }, $body); |
| @@ -338,6 +342,29 @@ final class StaticFilesTest extends TestCase | ||
| 338 | 342 | StaticFiles::create($this->root, 'br', $this->errorLog); |
| 339 | 343 | } |
| 340 | 344 | |
| 345 | + public function testZstdCompressionNeedsTheZstdExtension(): void | |
| 346 | + { | |
| 347 | + if (\function_exists('zstd_compress')) { | |
| 348 | + self::markTestSkipped('The zstd extension is loaded.'); | |
| 349 | + } | |
| 350 | + | |
| 351 | + $this->expectExceptionMessage('the zstd extension is not loaded'); | |
| 352 | + | |
| 353 | + StaticFiles::create($this->root, 'zstd', $this->errorLog); | |
| 354 | + } | |
| 355 | + | |
| 356 | + #[RequiresPhpExtension('zstd')] | |
| 357 | + public function testCompressesWithZstd(): void | |
| 358 | + { | |
| 359 | + $css = str_repeat('body { color: red; }', 100); | |
| 360 | + $this->write('large.css', $css); | |
| 361 | + | |
| 362 | + $response = $this->serve('/large.css', headers: ['Accept-Encoding' => 'gzip, zstd'], compression: 'zstd'); | |
| 363 | + | |
| 364 | + self::assertSame('zstd', $response['headers']['Content-Encoding'] ?? null); | |
| 365 | + self::assertSame($css, zstd_uncompress((string) base64_decode($response['body'], true))); | |
| 366 | + } | |
| 367 | + | |
| 341 | 368 | #[RequiresPhpExtension('brotli')] |
| 342 | 369 | public function testCompressesWithBrotli(): void |
| 343 | 370 | { |
| @@ -184,9 +184,11 @@ final class StaticFilesTest extends TestCase | |||
| 184 | yield 'gzip only' => ['gzip', 'gzip']; | 184 | yield 'gzip only' => ['gzip', 'gzip']; |
| 185 | yield 'both, brotli preferred whatever the weights' => ['gzip;q=1.0, br;q=0.1', 'br']; | 185 | yield 'both, brotli preferred whatever the weights' => ['gzip;q=1.0, br;q=0.1', 'br']; |
| 186 | yield 'brotli refused' => ['gzip, br;q=0', 'gzip']; | 186 | yield 'brotli refused' => ['gzip, br;q=0', 'gzip']; |
| 187 | + yield 'zstd preferred to gzip' => ['gzip, zstd', 'zstd']; | ||
| 188 | + yield 'brotli preferred to zstd' => ['zstd, br', 'br']; | ||
| 187 | yield 'wildcard' => ['*', 'br']; | 189 | yield 'wildcard' => ['*', 'br']; |
| 188 | - yield 'wildcard but brotli' => ['*, br;q=0', 'gzip']; | 190 | + yield 'wildcard but brotli' => ['*, br;q=0', 'zstd']; |
| 189 | - yield 'everything refused' => ['br;q=0, gzip;q=0', null]; | 191 | + yield 'everything refused' => ['br;q=0, zstd;q=0, gzip;q=0', null]; |
| 190 | yield 'case insensitive' => ['GZIP', 'gzip']; | 192 | yield 'case insensitive' => ['GZIP', 'gzip']; |
| 191 | } | 193 | } |
| 192 | 194 | ||
| @@ -194,6 +196,7 @@ final class StaticFilesTest extends TestCase | |||
| 194 | public function testServesThePreferredPrecompressedVariant(string $acceptEncoding, ?string $expected): void | 196 | public function testServesThePreferredPrecompressedVariant(string $acceptEncoding, ?string $expected): void |
| 195 | { | 197 | { |
| 196 | $this->write('app.css.br', 'brotli bytes'); | 198 | $this->write('app.css.br', 'brotli bytes'); |
| 199 | + $this->write('app.css.zst', "\x28\xb5\x2f\xfd zstd bytes"); | ||
| 197 | $this->write('app.css.gz', "\x1f\x8b gzip bytes"); | 200 | $this->write('app.css.gz', "\x1f\x8b gzip bytes"); |
| 198 | 201 | ||
| 199 | $response = $this->serve('/app.css', headers: ['Accept-Encoding' => $acceptEncoding]); | 202 | $response = $this->serve('/app.css', headers: ['Accept-Encoding' => $acceptEncoding]); |
| @@ -204,6 +207,7 @@ final class StaticFilesTest extends TestCase | |||
| 204 | $body = true === ($response['isBase64Encoded'] ?? false) ? base64_decode($response['body'], true) : $response['body']; | 207 | $body = true === ($response['isBase64Encoded'] ?? false) ? base64_decode($response['body'], true) : $response['body']; |
| 205 | self::assertSame(match ($expected) { | 208 | self::assertSame(match ($expected) { |
| 206 | 'br' => 'brotli bytes', | 209 | 'br' => 'brotli bytes', |
| 210 | + 'zstd' => "\x28\xb5\x2f\xfd zstd bytes", | ||
| 207 | 'gzip' => "\x1f\x8b gzip bytes", | 211 | 'gzip' => "\x1f\x8b gzip bytes", |
| 208 | default => 'body { color: red; }', | 212 | default => 'body { color: red; }', |
| 209 | }, $body); | 213 | }, $body); |
| @@ -338,6 +342,29 @@ final class StaticFilesTest extends TestCase | |||
| 338 | StaticFiles::create($this->root, 'br', $this->errorLog); | 342 | StaticFiles::create($this->root, 'br', $this->errorLog); |
| 339 | } | 343 | } |
| 340 | 344 | ||
| 345 | + public function testZstdCompressionNeedsTheZstdExtension(): void | ||
| 346 | + { | ||
| 347 | + if (\function_exists('zstd_compress')) { | ||
| 348 | + self::markTestSkipped('The zstd extension is loaded.'); | ||
| 349 | + } | ||
| 350 | + | ||
| 351 | + $this->expectExceptionMessage('the zstd extension is not loaded'); | ||
| 352 | + | ||
| 353 | + StaticFiles::create($this->root, 'zstd', $this->errorLog); | ||
| 354 | + } | ||
| 355 | + | ||
| 356 | + #[RequiresPhpExtension('zstd')] | ||
| 357 | + public function testCompressesWithZstd(): void | ||
| 358 | + { | ||
| 359 | + $css = str_repeat('body { color: red; }', 100); | ||
| 360 | + $this->write('large.css', $css); | ||
| 361 | + | ||
| 362 | + $response = $this->serve('/large.css', headers: ['Accept-Encoding' => 'gzip, zstd'], compression: 'zstd'); | ||
| 363 | + | ||
| 364 | + self::assertSame('zstd', $response['headers']['Content-Encoding'] ?? null); | ||
| 365 | + self::assertSame($css, zstd_uncompress((string) base64_decode($response['body'], true))); | ||
| 366 | + } | ||
| 367 | + | ||
| 341 | #[RequiresPhpExtension('brotli')] | 368 | #[RequiresPhpExtension('brotli')] |
| 342 | public function testCompressesWithBrotli(): void | 369 | public function testCompressesWithBrotli(): void |
| 343 | { | 370 | { |