Skip to content

Commit

Permalink
support for streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
plicease committed May 10, 2024
1 parent cfa4fdd commit 6c58c8f
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 27 deletions.
20 changes: 13 additions & 7 deletions lib/Plack/Middleware/Zstandard.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package Plack::Middleware::Zstandard {

use parent qw( Plack::Middleware );
use Plack::Util ();
use Ref::Util qw( is_arrayref );
use Ref::Util qw( is_plain_arrayref );
use Compress::Stream::Zstd::Compressor ();

sub prepare_app ($self) {
Expand Down Expand Up @@ -36,16 +36,22 @@ package Plack::Middleware::Zstandard {

my $compressor = Compress::Stream::Zstd::Compressor->new;

if($res->[2] && is_arrayref $res->[2]) {
my @buf = grep length, map { $compressor->compress($_) } grep defined, $res->[2]->@*;
if($res->[2] && is_plain_arrayref $res->[2]) {
$res->[2] = [grep length, map { $compressor->compress($_) } grep defined, $res->[2]->@*];
my $end = $compressor->end;
push @buf, $end if length $end;
$res->[2] = \@buf;
push $res->[2]->@*, $end if length $end;
return undef;
} else {
return sub ($chunk) {
# TODO: what about end?
return $compressor->compress($chunk);
if(defined $chunk) {
return $compressor->compress($chunk);
} elsif(defined $compressor) {
my $end = $compressor->end;
undef $compressor;
return $end;
} else {
return undef;
}
};
}
});
Expand Down
166 changes: 146 additions & 20 deletions t/plack_middleware_zstandard.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,158 @@ use Compress::Stream::Zstd::Decompressor;

subtest 'basic' => sub {

my @res;
our @res;

my $app = psgi_app_guard builder {
enable 'Zstandard';
sub { return \@res };
};

my $content = 'Hello World';
@res = ( 200, [ 'Content-Type' => 'text/plain', 'Content-Length' => length($content) ], [ $content ] );

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',
);
subtest 'short string' => sub {

my $content = 'Hello World';
local @res = ( 200, [ 'Content-Type' => 'text/plain', 'Content-Length' => length($content) ], [ $content ] );

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',
);

};

subtest 'short string as array' => sub {

my @content = ('Hello', undef, ' ', 'World');
local @res = ( 200, [ 'Content-Type' => 'text/plain', 'Content-Length' => length(join '', @content) ], [ @content ] );

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',
);

};

subtest 'no content status' => sub {

local @res = ( 304, [], [''] );

req(
GET('/', 'Accept-Encoding' => 'zstd'),
res {
code 304;
header 'Content-Encoding' => DNE();
header 'Vary', DNE();
call content => '';
},
);

note_debug();

};

subtest 'Cache-Control: no-transform' => sub {

my $content = 'Hello World';
local @res = (
200, [
'Content-Type' => 'text/plain',
'Content-Length' => length($content),
'Cache-Control' => 'no-transform',
], [
$content
]
);

req(
GET('/', 'Accept-Encoding' => 'zstd'),
res {
code 200;
header 'Content-Encoding' => DNE();
header 'Vary', DNE();
header 'Content-Length' => length($content);
call content => 'Hello World';
},
);

note_debug();

};

subtest 'No accept' => sub {

my $content = 'Hello World';
local @res = (
200, [
'Content-Type' => 'text/plain',
'Content-Length' => length($content),
], [
$content
]
);

req(
GET('/'),
res {
code 200;
header 'Content-Encoding' => DNE();
header 'Vary', 'Accept-Encoding';
header 'Content-Length' => length($content);
call content => 'Hello World';
},
);

note_debug();

};

subtest 'stream' => sub {

my $content = 'Hello World';
open my $fh, '<', \$content;
local @res = ( 200, [ 'Content-Type' => 'text/plain' ], $fh );

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',
);

};

};

Expand Down

0 comments on commit 6c58c8f

Please sign in to comment.