| 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
| |||||||||||||||||||||||||||||
| 16 | 8 | ▶ | my @comment = defined $event->{comment} ? split($SPLIT_RE, $event->{comment}) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 8 | my @parts; | |||||||||||||||||||||||||||
| 19 | 8 | ▶ | if (@comment) { push @parts, ": $_" for @comment } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | else { | ||||||||||||||||||||||||||||
| 21 | 6 | ▶ | push @parts, "event: $event->{type}" if defined $event->{type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 6 | push @parts, "data: $_" for @data; | |||||||||||||||||||||||||||
| 23 | 6 | ▶ | push @parts, "id: $event->{id}" if defined $event->{id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Skip lines with encoding errors | ||||||||||||||||||||||||||||
| 36 | 24 | ▶ | next unless defined(my $lines = decode 'UTF-8', $1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # Skip comments | ||||||||||||||||||||||||||||
| 39 | 24 | ▶ | next if $lines =~ /^\s*:/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition50%1/2
$1 // "message"
MC/DC0%0/1
$1 // "message"
$1
Truth table50%1/2
A
A= $1
| |||||||||||||||||||||||||||||
| 44 | 28 | ▶ | elsif ($line =~ /^data(?::\s*(.*))?$/) { $event->{text} .= ($first++ ? "\n" : '') . ($1 // '') } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$1 // ""
MC/DC100%1/1
$1 // ""
$1
Truth table100%2/2
A
A= $1
| |||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||