lib/PAGI/App/SSE/Pubsub.pm

filter: Statementstmt 61.1% 63 / 103 Branchbran 27.5% 11 / 40 Conditioncond 35.0% 7 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 91.6% 11 / 12 Podpod 60.0% 3 / 5 totaltotal 49.7% 95 / 191 CC 35 SCAR 31.0
CC
35
Cov
50%
__ANON__
36.8
publish
31.3
_format_event
30.0
CRAP 22.1
« blib/lib/PAGI/Middleware/ReverseProxy.pm ↑ Summary blib/lib/PAGI/Middleware/Lint.pm »
1 package PAGI::App::SSE::Pubsub;
2 1 $PAGI::App::SSE::Pubsub::VERSION = '0.002002';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 PAGI::App::SSE::Pubsub - Pub/sub Server-Sent Events
11  
12 =head1 SYNOPSIS
13  
14 use PAGI::App::SSE::Pubsub;
15  
16 my $app = PAGI::App::SSE::Pubsub->new->to_app;
17  
18 # From elsewhere, publish events
19 PAGI::App::SSE::Pubsub->publish('news', { data => 'Hello!' });
20  
21 =cut
22  
23 # Shared state
24 1 my %channels; # channel => { clients => { id => { send => cb, scope => scope } } }
25 1 my $next_id = 1;
26  
27 1 ▶sub new {
Subroutine new: called CC=4 SCAR=15.8
Pod undocumented
28 1 my ($class, %args) = @_;
29  
30 return bless {
31 channel => $args{channel} // 'default',
32 retry => $args{retry},
33 on_connect => $args{on_connect},
34 on_close => $args{on_close},
35 history => $args{history} // 0,
36 1 ▶ headers => $args{headers} // [],
Condition50%3/6
$args{"channel"} // "default"
l!l
10
$args{"history"} // 0
l!l
01
$args{"headers"} // []
l!l
01
MC/DC0%0/1
$args{"channel"} // "default"
$args{"channel"}
MC/DC0%0/1
$args{"history"} // 0
$args{"history"}
MC/DC0%0/1
$args{"headers"} // []
$args{"headers"}
Truth table50%1/2
A
A= $args{"channel"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"history"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"headers"}
Aresult
11
00
37 }, $class;
38 }
39  
40 # Class method to publish events
41 1 ▶sub publish {
Subroutine publish: called CC=6 SCAR=34.4
Pod documented
42 1 my ($class, $channel, $event) = @_;
43  
44 1 ▶ return unless $channels{$channel};
Branch: 1 branch
BranchTrueFalseTotal
unless $channels{$channel} 1 0 1
45  
46 0 my $data = _format_event($event);
47 0 my $clients = $channels{$channel}{clients};
48  
49 0 for my $id (keys %$clients) {
50 0 my $client = $clients->{$id};
51 0 eval {
52 0 $client->{send}->({
53 type => 'http.response.body',
54 body => $data,
55 more => 1,
56 });
57 };
58 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
59 0 $client->{closed} = 1;
60 0 delete $clients->{$id};
61 }
62 }
63  
64 # Store in history if enabled
65 0 ▶ if ($channels{$channel}{history_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($channels{$channel}{"history_size"}) 0 0 0
66 0 push @{$channels{$channel}{history}}, $event;
67 0 my $max = $channels{$channel}{history_size};
68 0 ▶ if (@{$channels{$channel}{history}} > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$channels{$channel}{"history"};} > $max) 0 0 0
69 0 shift @{$channels{$channel}{history}};
70 }
71 }
72 }
73  
74 # Class method to get client count
75 1 ▶sub client_count {
Subroutine client_count: called CC=5 SCAR=20.9
Pod documented
76 1 my ($class, $channel) = @_;
77 1 ▶ $channel //= undef;
Condition50%1/2
$channel //= undef
l!l
01
MC/DC0%0/1
$channel //= undef
$channel
Truth table50%1/2
A
A= $channel
Aresult
11
00
78  
79 1 ▶ if ($channel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($channel) 0 1 1
80 0 ▶ return 0 unless $channels{$channel};
Branch: 1 branch
BranchTrueFalseTotal
unless $channels{$channel} 0 0 0
81 0 return scalar keys %{$channels{$channel}{clients}};
82 }
83 1 my $total = 0;
84 0 $total += scalar keys %{$_->{clients}} for values %channels;
85 1 return $total;
86 }
87  
88 # Class method to list channels
89 1 ▶sub list_channels {
Subroutine list_channels: called CC=1 SCAR=0.0
Pod documented
90 1 my ($class) = @_;
91  
92 1 return keys %channels;
93 }
94  
95 1 ▶sub to_app {
Subroutine to_app: called CC=1 SCAR=0.5
Pod undocumented
96 1 my ($self) = @_;
97  
98 1 my $channel = $self->{channel};
99 1 my $retry = $self->{retry};
100 1 my $on_connect = $self->{on_connect};
101 1 my $on_close = $self->{on_close};
102 1 my $history_size = $self->{history};
103 1 my $extra_headers = $self->{headers};
104  
105 1 ▶ return async sub {
Subroutine __ANON__: called CC=16 SCAR=36.1
106 1 my ($scope, $receive, $send) = @_;
107 1 ▶ die "Unsupported scope type: $scope->{type}" if $scope->{type} ne 'http';
Branch: 1 branch
BranchTrueFalseTotal
if $scope->{"type"} ne "http" 0 1 1
108  
109 # Build headers
110 1 my @headers = (
111 ['content-type', 'text/event-stream'],
112 ['cache-control', 'no-cache'],
113 ['connection', 'keep-alive'],
114 @$extra_headers,
115 );
116  
117 1 await $send->({
118 type => 'http.response.start',
119 status => 200,
120 headers => \@headers,
121 });
122  
123 # Initialize channel
124 1 ▶ $channels{$channel} //= {
Condition50%1/2
$channels{$channel} //= {"clients", {}, "history", [], "history_size", $history_size}
l!l
01
MC/DC0%0/1
$channels{$channel} //= {"clients", {}, "history", [], "history_size", $history_size}
$channels{$channel}
Truth table50%1/2
A
A= $channels{$channel}
Aresult
11
00
125 clients => {},
126 history => [],
127 history_size => $history_size,
128 };
129  
130 1 my $client_id = $next_id++;
131 1 my $client_data = {
132 send => $send,
133 scope => $scope,
134 closed => 0,
135 };
136 1 $channels{$channel}{clients}{$client_id} = $client_data;
137  
138 # Send retry hint
139 1 ▶ if (defined $retry) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $retry) 0 1 1
140 0 await $send->({
141 type => 'http.response.body',
142 body => "retry: $retry\n\n",
143 more => 1,
144 });
145 }
146  
147 # Send history if requested
148 1 my $last_event_id = _get_last_event_id($scope);
149 1 ▶ if ($history_size && defined $last_event_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($history_size and defined $last_event_id) 0 1 1
Condition33%1/3
$history_size and defined $last_event_id
!ll&&!rl&&r
100
MC/DC0%0/2
$history_size and defined $last_event_id
$history_size defined $last_event_id
Truth table33%1/3
A and B
A= $history_sizeB= defined $last_event_id
ABresult
0X0
100
111
150 0 my $found = 0;
151 0 for my $event (@{$channels{$channel}{history}}) {
152 0 ▶ if ($found) {
Branch: 2 branches
BranchTrueFalseTotal
if ($found) { } 0 0 0
elsif ($event->{'id'} and $event->{'id'} eq $last_event_id) { } 0 0 0
153 0 await $send->({
154 type => 'http.response.body',
155 body => _format_event($event),
156 more => 1,
157 });
158 ▶ } elsif ($event->{id} && $event->{id} eq $last_event_id) {
Condition0%0/3
$event->{"id"} and $event->{"id"} eq $last_event_id
!ll&&!rl&&r
000
MC/DC0%0/2
$event->{"id"} and $event->{"id"} eq $last_event_id
$event->{"id"} $event->{"id"} eq $last_event_id
Truth table0%0/3
A and B
A= $event->{"id"}B= $event->{"id"} eq $last_event_id
ABresult
0X0
100
111
159 0 $found = 1;
160 }
161 }
162 }
163  
164 1 ▶ $on_connect->($scope, $channel) if $on_connect;
Branch: 1 branch
BranchTrueFalseTotal
if $on_connect 0 1 1
165  
166 # Wait for disconnect
167 1 ▶ while (!$client_data->{closed}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($client_data->{"closed"}) 1 0 1
168 1 my $event = await $receive->();
169 1 ▶ if ($event->{type} eq 'http.disconnect') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"type"} eq "http.disconnect") 1 0 1
170 1 last;
171 }
172 }
173  
174 # Cleanup
175 1 delete $channels{$channel}{clients}{$client_id};
176 1 ▶ delete $channels{$channel} if !keys %{$channels{$channel}{clients}};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$channels{$channel}{"clients"};} 1 0 1
177  
178 1 ▶ $on_close->($scope, $channel) if $on_close;
Branch: 1 branch
BranchTrueFalseTotal
if $on_close 0 1 1
179  
180 # End response
181 1 ▶ unless ($client_data->{closed}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($client_data->{"closed"}) 1 0 1
182 1 await $send->({
183 type => 'http.response.body',
184 body => '',
185 more => 0,
186 });
187 }
188 1 };
189 }
190  
191 0 ▶sub _format_event {
Subroutine _format_event: not called CC=5 SCAR=34.0
192 0 my ($event) = @_;
193  
194 0 my $data = '';
195  
196 0 ▶ if ($event->{event}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"event"}) 0 0 0
197 0 $data .= "event: $event->{event}\n";
198 }
199 0 ▶ if ($event->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"id"}) 0 0 0
200 0 $data .= "id: $event->{id}\n";
201 }
202  
203 0 ▶ my $content = $event->{data} // '';
Condition0%0/2
$event->{"data"} // ""
l!l
00
MC/DC0%0/1
$event->{"data"} // ""
$event->{"data"}
Truth table0%0/2
A
A= $event->{"data"}
Aresult
11
00
204 0 for my $line (split /\n/, $content) {
205 0 $data .= "data: $line\n";
206 }
207  
208 0 return "$data\n";
209 }
210  
211 1 ▶sub _get_last_event_id {
Subroutine _get_last_event_id: called CC=4 SCAR=17.9
212 1 my ($scope) = @_;
213  
214 1 ▶ for my $h (@{$scope->{headers} // []}) {
Condition50%1/2
$scope->{"headers"} // []
l!l
10
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
Aresult
11
00
215 0 ▶ if (lc($h->[0]) eq 'last-event-id') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $h->[0] eq "last-event-id") 0 0 0
216 0 return $h->[1];
217 }
218 }
219 1 return undef;
220 }
221  
222 1 1;
223  
224 __END__
« blib/lib/PAGI/Middleware/ReverseProxy.pm ↑ Summary blib/lib/PAGI/Middleware/Lint.pm »