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   158 use strict;
  39         78  
  39         1059  
3 39     39   155 no warnings;
  39         39  
  39         2516  
4 39     39   308 use Carp;
  39         40  
  39         3103  
5 39     39   308 use Plack::Util;
  39         39  
  39         1389  
6 39     39   239 use Plack::Util::Accessor qw(force);
  39         40  
  39         385  
7 39     39   118 use Scalar::Util qw(weaken);
  39         77  
  39         2544  
8 39     39   155 use parent qw(Plack::Middleware);
  39         77  
  39         200  
9              
10             sub call {
11 39     39 1 1247 my ( $self, $env ) = @_;
12              
13 39         629 my $caller_supports_streaming = $env->{'psgi.streaming'};
14 39         540 $env->{'psgi.streaming'} = Plack::Util::TRUE;
15              
16 39         1610 my $res = $self->app->($env);
17 38 50 33     1307 return $res if $caller_supports_streaming && !$self->force;
18              
19 38 100       21538 if ( ref($res) eq 'CODE' ) {
20 4         40 my $ret;
21              
22             $res->(sub {
23 4     4   51 my $write = shift;
24              
25 4 100       43 if ( @$write == 2 ) {
26 2         3 my @body;
27              
28 2         14 $ret = [ @$write, \@body ];
29              
30             return Plack::Util::inline_object(
31 4         10 write => sub { push @body, $_[0] },
32             close => sub { },
33 2         21492 );
34             } else {
35 2         6 $ret = $write;
36 2         5 return;
37             }
38 4         64 });
39              
40 4         48 return $ret;
41             } else {
42 34         167 return $res;
43             }
44             }
45              
46             1;
47              
48             __END__