Your IP : 216.73.216.21


Current Path : /pkg/pilot/1.0/lib/perl5/Mojo/
Upload File :
Current File : //pkg/pilot/1.0/lib/perl5/Mojo/SSE.pm

package Mojo::SSE;
use Mojo::Base -strict;

use Carp       qw(croak);
use Exporter   qw(import);
use Mojo::Util qw(decode encode);

our @EXPORT_OK = (qw(build_event parse_event));

my $SPLIT_RE = qr/(?:\x0d\x0a|(?<!\x0d)\x0a|\x0d(?!\x0a))/;

sub build_event {
  my $event = shift;

  my @data    = defined $event->{text}    ? split($SPLIT_RE, $event->{text})    : ();
  my @comment = defined $event->{comment} ? split($SPLIT_RE, $event->{comment}) : ();

  my @parts;
  if (@comment) { push @parts, ": $_" for @comment }
  else {
    push @parts, "event: $event->{type}" if defined $event->{type};
    push @parts, "data: $_" for @data;
    push @parts, "id: $event->{id}" if defined $event->{id};
  }

  return encode('UTF-8', join("\x0d\x0a", @parts, '', ''));
}

sub parse_event {
  my $buffer = shift;
  my $event  = {id => undef, type => 'message', text => ''};

  while ($$buffer =~ s/^(.*?)(?:(?:\x0d\x0a|(?<!\x0d)\x0a|\x0d(?!\x0a)){2})//s) {

    # Skip lines with encoding errors
    next unless defined(my $lines = decode 'UTF-8', $1);

    # Skip comments
    next if $lines =~ /^\s*:/;

    my $first = 0;
    for my $line (split $SPLIT_RE, $lines) {
      if    ($line =~ /^event(?::\s*(\S.*))?$/) { $event->{type} = $1 // 'message' }
      elsif ($line =~ /^data(?::\s*(.*))?$/)    { $event->{text} .= ($first++ ? "\n" : '') . ($1 // '') }
      elsif ($line =~ /^id(?::\s*(.*))?$/)      { $event->{id} = $1 }
    }

    return $event;
  }

  return undef;
}

1;

=encoding utf8

=head1 NAME

Mojo::SSE - Server-Sent Events

=head1 SYNOPSIS

  use Mojo::SSE qw(build_event parse_event);

=head1 DESCRIPTION

L<Mojo::SSE> implements the Server-Sent Events protocol. Note that this module is B<EXPERIMENTAL> and may change
without warning!

=head1 FUNCTIONS

L<Mojo::SSE> implements the following functions, which can be imported individually.

=head2 build_event

  my $bytes = build_event $event, $chars;

Build Server-Sent Event.

=head2 parse_event

  my $event = parse_event \$bytes;

Parse Server-Sent Event. Returns C<undef> if no complete event was found.

=head1 SEE ALSO

L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.

=cut

https://nalibali.org/post-sitemap.xml 2026-11-20T09:00:00+00:00 https://nalibali.org/page-sitemap.xml 2026-11-18T09:24:11+00:00 https://nalibali.org/stories-sitemap.xml 2026-05-05T06:48:38+00:00 https://nalibali.org/stories-sitemap2.xml 2026-05-05T06:48:38+00:00 https://nalibali.org/audio_stories-sitemap.xml 2026-03-27T06:53:13+00:00 https://nalibali.org/story_cards-sitemap.xml 2025-06-06T10:27:48+00:00 https://nalibali.org/story_seeds-sitemap.xml 2025-05-19T07:35:43+00:00 https://nalibali.org/rhymes-sitemap.xml 2025-06-17T07:43:10+00:00 https://nalibali.org/activity_sheets-sitemap.xml 2025-05-16T09:16:43+00:00 https://nalibali.org/printables-sitemap.xml 2025-05-16T09:28:40+00:00 https://nalibali.org/posters_bookmarks-sitemap.xml 2025-05-16T10:20:22+00:00 https://nalibali.org/team_member-sitemap.xml 2025-05-28T09:40:43+00:00 https://nalibali.org/video_stories-sitemap.xml 2025-06-23T08:48:28+00:00 https://nalibali.org/guides-sitemap.xml 2025-06-17T09:38:41+00:00 https://nalibali.org/story_supplements-sitemap.xml 2025-09-16T12:22:20+00:00 https://nalibali.org/vacancies-sitemap.xml 2026-09-17T07:05:01+00:00 https://nalibali.org/category-sitemap.xml 2026-11-20T09:00:00+00:00 https://nalibali.org/post_tag-sitemap.xml 2026-11-06T00:00:00+00:00 https://nalibali.org/test-sitemap.xml 2025-06-13T09:36:28+00:00 https://nalibali.org/story_categories-sitemap.xml 2026-05-05T06:48:38+00:00 https://nalibali.org/activity_sheets_categories-sitemap.xml 2025-05-16T09:16:43+00:00 https://nalibali.org/printables_categories-sitemap.xml 2025-05-16T09:28:40+00:00 https://nalibali.org/posters_bookmarks_categories-sitemap.xml 2025-05-16T10:20:22+00:00 https://nalibali.org/single_audio-sitemap.xml 2026-03-27T06:53:13+00:00 https://nalibali.org/rhyme_categories-sitemap.xml 2025-06-17T07:43:10+00:00 https://nalibali.org/guide_categories-sitemap.xml 2025-06-17T09:38:41+00:00 https://nalibali.org/author-sitemap.xml 2026-01-22T07:26:07+00:00