File Coverage

blib/lib/Plack/Middleware/BufferedStreaming.pm
Criterion Covered Total %
statement 39 39 100.0
branch 5 6 83.3
condition 1 3 33.3
subroutine 9 9 100.0
pod 1 1 100.0
total 55 58 94.8


line stmt bran cond sub pod time code
1             package Plack::Middleware::BufferedStreaming;
2 39     39   196 use strict;
  39         77  
  39         947  
3 39     39   156 no warnings;
  39         40  
  39         1238  
4 39     39   157 use Carp;
  39         39  
  39         2084  
5 39     39   157 use Plack::Util;
  39         78  
  39         843  
6 39     39   158 use Plack::Util::Accessor qw(force);
  39         78  
  39         197  
7 39     39   195 use Scalar::Util qw(weaken);
  39         78  
  39         1490  
8 39     39   156 use parent qw(Plack::Middleware);
  39         78  
  39         158  
9              
10             sub call {
11 39     39 1 1233 my ( $self, $env ) = @_;
12              
13 39         679 my $caller_supports_streaming = $env->{'psgi.streaming'};
14 39         354 $env->{'psgi.streaming'} = Plack::Util::TRUE;
15              
16 39         1443 my $res = $self->app->($env);
17 38 50 33     616 return $res if $caller_supports_streaming && !$self->force;
18              
19 38 100       928 if ( ref($res) eq 'CODE' ) {
20 4         9 my $ret;
21              
22             $res->(sub {
23 4     4   46 my $write = shift;
24              
25 4 100       27 if ( @$write == 2 ) {
26 2         4 my @body;
27              
28 2         5 $ret = [ @$write, \@body ];
29              
30             return Plack::Util::inline_object(
31 4         19 write => sub { push @body, $_[0] },
32             close => sub { },
33 2         61 );
34             } else {
35 2         10 $ret = $write;
36 2         5 return;
37             }
38 4         60 });
39              
40 4         42 return $ret;
41             } else {
42 34         343 return $res;
43             }
44             }
45              
46             1;
47              
48             __END__