diff --git a/README.md b/README.md index 181768d..fca59e0 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,18 @@ builder { This middleware encodes the body of the response using Zstandard, based on the `Accept-Encoding` request header. +# CONFIGURATION + +- level + + Compression level. Should be an integer from 1 to 22. If not provided, then the default will + be chosen by [Compress::Stream::Zstd](https://metacpan.org/pod/Compress::Stream::Zstd). + +# SEE ALSO + +- [Plack::Middleware::Deflater](https://metacpan.org/pod/Plack::Middleware::Deflater) +- [Compress::Stream::Zstd](https://metacpan.org/pod/Compress::Stream::Zstd) + # AUTHOR Graham Ollis diff --git a/lib/Plack/Middleware/Zstandard.pm b/lib/Plack/Middleware/Zstandard.pm index a7fab44..ae2da1e 100644 --- a/lib/Plack/Middleware/Zstandard.pm +++ b/lib/Plack/Middleware/Zstandard.pm @@ -8,10 +8,16 @@ package Plack::Middleware::Zstandard { use parent qw( Plack::Middleware ); use Plack::Util (); + use Plack::Util::Accessor qw( level _constructor_args ); use Ref::Util qw( is_plain_arrayref ); use Compress::Stream::Zstd::Compressor (); sub prepare_app ($self) { + if(defined $self->level) { + $self->_constructor_args([$self->level]); + } else { + $self->_constructor_args([]); + } } sub call ($self, $env) { @@ -37,7 +43,7 @@ package Plack::Middleware::Zstandard { $h->set('Content-Encoding' => 'zstd'); $h->remove('Content-Length'); - my $compressor = Compress::Stream::Zstd::Compressor->new; + my $compressor = Compress::Stream::Zstd::Compressor->new($self->_constructor_args->@*); if($res->[2] && is_plain_arrayref $res->[2]) { $res->[2] = [grep length, map { $compressor->compress($_) } grep defined, $res->[2]->@*]; @@ -73,4 +79,25 @@ package Plack::Middleware::Zstandard { This middleware encodes the body of the response using Zstandard, based on the C request header. +=head1 CONFIGURATION + +=over 4 + +=item level + +Compression level. Should be an integer from 1 to 22. If not provided, then the default will +be chosen by L. + +=back + +=head1 SEE ALSO + +=over 4 + +=item L + +=item L + +=back + =cut diff --git a/t/00_diag.t b/t/00_diag.t index 91a6f5f..22c55b2 100644 --- a/t/00_diag.t +++ b/t/00_diag.t @@ -17,6 +17,7 @@ $modules{$_} = $_ for qw( Plack::Builder Plack::Middleware Plack::Util + Plack::Util::Accessor Ref::Util Test2::Tools::HTTP Test2::V0 diff --git a/t/plack_middleware_zstandard.t b/t/plack_middleware_zstandard.t index ad72b53..e257934 100644 --- a/t/plack_middleware_zstandard.t +++ b/t/plack_middleware_zstandard.t @@ -1,4 +1,5 @@ use Test2::V0 -no_srand => 1; +use experimental qw( signatures ); use Plack::Builder; use Test2::Tools::HTTP qw( :short psgi_app_guard ); use HTTP::Request::Common; @@ -187,6 +188,81 @@ subtest 'basic' => sub { }; +subtest 'level' => sub { + + my @last_new_args; + my $mock = mock 'Compress::Stream::Zstd::Compressor' => ( + before => [ + new => sub ($class, @args) { + @last_new_args = @args; + }, + ], + ); + + subtest 'override level = 22' => sub { + + my $app = psgi_app_guard builder { + enable 'Zstandard', level => 22; + sub { return [ 200, ['Content-Type' => 'text/plain'], ['Hello World']] }; + }; + + req( + GET('/', 'Accept-Encoding' => 'zstd'), + res { + code 200; + content_type 'text/plain'; + header 'Content-Length' => DNE(); + header 'Content-Encoding' => 'zstd'; + header 'Vary', 'Accept-Encoding'; + }, + ); + + is( + decompress(), + 'Hello World', + 'content', + ); + + is( + \@last_new_args, + [22], + 'expected args', + ); + }; + + subtest 'default' => sub { + + my $app = psgi_app_guard builder { + enable 'Zstandard'; + sub { return [ 200, ['Content-Type' => 'text/plain'], ['Hello World']] }; + }; + + req( + GET('/', 'Accept-Encoding' => 'zstd'), + res { + code 200; + content_type 'text/plain'; + header 'Content-Length' => DNE(); + header 'Content-Encoding' => 'zstd'; + header 'Vary', 'Accept-Encoding'; + }, + ); + + is( + decompress(), + 'Hello World', + 'content', + ); + + is( + \@last_new_args, + [], + 'expected args', + ); + }; + +}; + sub note_debug { note $_ for map { $_->as_string} (tx->req, tx->res); }