Mojo/SSE.pm

filter: Statementstmt 100% 38 / 38 Branchbran 91.6% 22 / 24 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 94.7% 72 / 76 CC 18 SCAR 21.1
CC
18
Cov
95%
parse_event
11.1
build_event
8.0
BEGIN
1.0
CRAP 8.3
« Mojo/Transaction/WebSocket.pm ↑ Summary Mojo/UserAgent/CookieJar.pm »
1 package Mojo::SSE;
2 31 ▶use Mojo::Base -strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 31 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 31 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 31 ▶use Mojo::Util qw(decode encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 31 our @EXPORT_OK = (qw(build_event parse_event));
9  
10 31 my $SPLIT_RE = qr/(?:\x0d\x0a|(?<!\x0d)\x0a|\x0d(?!\x0a))/;
11  
12 8 ▶sub build_event {
Subroutine build_event: called CC=8 SCAR=20.8
Pod documented
13 8 my $event = shift;
14  
15 8 ▶ my @data = defined $event->{text} ? split($SPLIT_RE, $event->{text}) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $event->{'text'} ? : 6 2 8
16 8 ▶ my @comment = defined $event->{comment} ? split($SPLIT_RE, $event->{comment}) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $event->{'comment'} ? : 2 6 8
17  
18 8 my @parts;
19 8 ▶ if (@comment) { push @parts, ": $_" for @comment }
Branch: 1 branch
BranchTrueFalseTotal
if (@comment) { } 2 6 8
20 else {
21 6 ▶ push @parts, "event: $event->{type}" if defined $event->{type};
Branch: 1 branch
BranchTrueFalseTotal
if defined $event->{"type"} 5 1 6
22 6 push @parts, "data: $_" for @data;
23 6 ▶ push @parts, "id: $event->{id}" if defined $event->{id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $event->{"id"} 2 4 6
24 }
25  
26 8 return encode('UTF-8', join("\x0d\x0a", @parts, '', ''));
27 }
28  
29 28 ▶sub parse_event {
Subroutine parse_event: called CC=11 SCAR=24.1
Pod documented
30 28 my $buffer = shift;
31 28 my $event = {id => undef, type => 'message', text => ''};
32  
33 28 ▶ while ($$buffer =~ s/^(.*?)(?:(?:\x0d\x0a|(?<!\x0d)\x0a|\x0d(?!\x0a)){2})//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$buffer =~ s/^(.*?)(?:(?:\x0d\x0a|(?<!\x0d)\x0a|\x0d(?!\x0a)){2})//su) 24 7 31
34  
35 # Skip lines with encoding errors
36 24 ▶ next unless defined(my $lines = decode 'UTF-8', $1);
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $lines = decode("UTF-8", $1)) 0 24 24
37  
38 # Skip comments
39 24 ▶ next if $lines =~ /^\s*:/;
Branch: 1 branch
BranchTrueFalseTotal
if $lines =~ /^\s*:/u 3 21 24
40  
41 21 my $first = 0;
42 21 for my $line (split $SPLIT_RE, $lines) {
43 40 ▶ if ($line =~ /^event(?::\s*(\S.*))?$/) { $event->{type} = $1 // 'message' }
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^event(?::\s*(\S.*))?$/u) { } 8 32 40
elsif ($line =~ /^data(?::\s*(.*))?$/u) { } 28 4 32
elsif ($line =~ /^id(?::\s*(.*))?$/u) { } 4 0 4
Condition50%1/2
$1 // "message"
l!l
80
MC/DC0%0/1
$1 // "message"
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
44 28 ▶ elsif ($line =~ /^data(?::\s*(.*))?$/) { $event->{text} .= ($first++ ? "\n" : '') . ($1 // '') }
Branch: 1 branch
BranchTrueFalseTotal
$first++ ? : 7 21 28
Condition100%2/2
$1 // ""
l!l
253
MC/DC100%1/1
$1 // ""
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
45 4 elsif ($line =~ /^id(?::\s*(.*))?$/) { $event->{id} = $1 }
46 }
47  
48 21 return $event;
49 }
50  
51 7 return undef;
52 }
53  
54 31 1;
55  
56 =encoding utf8
57  
58 =head1 NAME
59  
60 Mojo::SSE - Server-Sent Events
61  
62 =head1 SYNOPSIS
63  
64 use Mojo::SSE qw(build_event parse_event);
65  
66 =head1 DESCRIPTION
67  
68 L<Mojo::SSE> implements the Server-Sent Events protocol. Note that this module is B<EXPERIMENTAL> and may change
69 without warning!
70  
71 =head1 FUNCTIONS
72  
73 L<Mojo::SSE> implements the following functions, which can be imported individually.
74  
75 =head2 build_event
76  
77 my $bytes = build_event $event, $chars;
78  
79 Build Server-Sent Event.
80  
81 =head2 parse_event
82  
83 my $event = parse_event \$bytes;
84  
85 Parse Server-Sent Event. Returns C<undef> if no complete event was found.
86  
87 =head1 SEE ALSO
88  
89 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
90  
91 =cut
« Mojo/Transaction/WebSocket.pm ↑ Summary Mojo/UserAgent/CookieJar.pm »