Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add level option #4

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
Expand Down
29 changes: 28 additions & 1 deletion lib/Plack/Middleware/Zstandard.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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]->@*];
Expand Down Expand Up @@ -73,4 +79,25 @@ package Plack::Middleware::Zstandard {
This middleware encodes the body of the response using Zstandard, based on the C<Accept-Encoding>
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<Compress::Stream::Zstd>.

=back

=head1 SEE ALSO

=over 4

=item L<Plack::Middleware::Deflater>

=item L<Compress::Stream::Zstd>

=back

=cut
1 change: 1 addition & 0 deletions t/00_diag.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ $modules{$_} = $_ for qw(
Plack::Builder
Plack::Middleware
Plack::Util
Plack::Util::Accessor
Ref::Util
Test2::Tools::HTTP
Test2::V0
Expand Down
76 changes: 76 additions & 0 deletions t/plack_middleware_zstandard.t
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Loading