| 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"
$args{"history"} // 0
$args{"headers"} // []
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"}
Truth table50%1/2
A
A= $args{"history"}
Truth table50%1/2
A
A= $args{"headers"}
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$channel //= undef
$channel
Truth table50%1/2
A
A= $channel
| |||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | 1 | ▶ | if ($channel) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | return 0 unless $channels{$channel}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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}
MC/DC0%0/1
$channels{$channel} //= {"clients", {}, "history", [], "history_size", $history_size}
$channels{$channel}
Truth table50%1/2
A
A= $channels{$channel}
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$history_size and defined $last_event_id
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
| |||||||||||||||||||||||||||||||||
| 150 | 0 | my $found = 0; | |||||||||||||||||||||||||||||||
| 151 | 0 | for my $event (@{$channels{$channel}{history}}) { | |||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | if ($found) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||
| 159 | 0 | $found = 1; | |||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | 1 | ▶ | $on_connect->($scope, $channel) if $on_connect; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | # Wait for disconnect | ||||||||||||||||||||||||||||||||
| 167 | 1 | ▶ | while (!$client_data->{closed}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 168 | 1 | my $event = await $receive->(); | |||||||||||||||||||||||||||||||
| 169 | 1 | ▶ | if ($event->{type} eq 'http.disconnect') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||
| 178 | 1 | ▶ | $on_close->($scope, $channel) if $on_close; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | # End response | ||||||||||||||||||||||||||||||||
| 181 | 1 | ▶ | unless ($client_data->{closed}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 197 | 0 | $data .= "event: $event->{event}\n"; | |||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | if ($event->{id}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 200 | 0 | $data .= "id: $event->{id}\n"; | |||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | my $content = $event->{data} // ''; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$event->{"data"} // ""
MC/DC0%0/1
$event->{"data"} // ""
$event->{"data"}
Truth table0%0/2
A
A= $event->{"data"}
| |||||||||||||||||||||||||||||||||
| 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"} // []
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
| |||||||||||||||||||||||||||||||||
| 215 | 0 | ▶ | if (lc($h->[0]) eq 'last-event-id') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 216 | 0 | return $h->[1]; | |||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||
| 219 | 1 | return undef; | |||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||
| 222 | 1 | 1; | |||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||
| 224 | __END__ | ||||||||||||||||||||||||||||||||