blib/lib/PAGI/Endpoint/WebSocket.pm

filter: Statementstmt 84.4% 49 / 58 Branchbran 64.2% 9 / 14 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 85.7% 12 / 14 Podpod 60.0% 3 / 5 totaltotal 78.7% 74 / 94 CC 9 SCAR 13.4
CC
9
Cov
80%
handle
8.7
__ANON__
3.0
__ANON__
2.0
CRAP 3.8
« lib/PAGI/Middleware/RequestId.pm ↑ Summary blib/lib/PAGI/Session.pm »
1 package PAGI::Endpoint::WebSocket;
2 5 $PAGI::Endpoint::WebSocket::VERSION = '0.002002';
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Factory class method - override in subclass for customization
10 7 ▶sub context_class { 'PAGI::Context' }
Subroutine context_class: called CC=1 SCAR=0.0
Pod documented
11  
12 # Encoding: 'text', 'bytes', or 'json'
13 2 ▶sub encoding { 'text' }
Subroutine encoding: called CC=1 SCAR=0.0
Pod documented
14  
15 5 ▶sub to_app {
Subroutine to_app: called CC=1 SCAR=0.0
Pod documented
16 5 my ($class) = @_;
17 5 my $context_class = $class->context_class;
18  
19 3 ▶ return async sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
20 3 my ($scope, $receive, $send) = @_;
21  
22 3 ▶ my $type = $scope->{type} // '';
Condition50%1/2
$scope->{"type"} // ""
l!l
30
MC/DC0%0/1
$scope->{"type"} // ""
$scope->{"type"}
Truth table50%1/2
A
A= $scope->{"type"}
Aresult
11
00
23 3 ▶ croak "Expected websocket scope, got '$type'" unless $type eq 'websocket';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "websocket" 0 3 3
24  
25 3 require PAGI::Context;
26 3 my $endpoint = $class->new;
27 3 my $ctx = $context_class->new($scope, $receive, $send);
28  
29 3 await $endpoint->handle($ctx);
30 5 };
31 }
32  
33 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
34 4 my ($class, %args) = @_;
35 4 return bless \%args, $class;
36 }
37  
38 3 ▶async sub handle {
Subroutine handle: called CC=7 SCAR=21.6
Pod undocumented
39 3 my ($self, $ctx) = @_;
40 3 my $ws = $ctx->websocket;
41  
42 # Call on_connect if defined
43 3 ▶ if ($self->can('on_connect')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('on_connect')) { } 3 0 3
44 3 await $self->on_connect($ctx);
45 } else {
46 # Default: accept the connection
47 0 await $ws->accept;
48 }
49  
50 # Register disconnect callback
51 3 ▶ if ($self->can('on_disconnect')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("on_disconnect")) 1 2 3
52 1 ▶ $ws->on_close(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
53 1 my ($code, $reason) = @_;
54 1 $self->on_disconnect($ctx, $code, $reason);
55 1 });
56 }
57  
58 # Handle messages based on encoding
59 3 eval {
60 3 ▶ if ($self->can('on_receive')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('on_receive')) { } 1 2 3
61 1 my $encoding = $self->encoding;
62  
63 1 ▶ if ($encoding eq 'json') {
Branch: 2 branches
BranchTrueFalseTotal
if ($encoding eq 'json') { } 0 1 1
elsif ($encoding eq 'bytes') { } 0 1 1
64 0 ▶ await $ws->each_json(async sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 0 my ($data) = @_;
66 0 await $self->on_receive($ctx, $data);
67 0 });
68 } elsif ($encoding eq 'bytes') {
69 0 ▶ await $ws->each_bytes(async sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 0 my ($data) = @_;
71 0 await $self->on_receive($ctx, $data);
72 0 });
73 } else {
74 # Default: text
75 2 ▶ await $ws->each_text(async sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
76 2 my ($data) = @_;
77 2 await $self->on_receive($ctx, $data);
78 1 });
79 }
80 } else {
81 # No on_receive, just wait for disconnect
82 2 await $ws->run;
83 }
84 };
85 3 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
86 5 }
87  
88 5 1;
89  
90 __END__
« lib/PAGI/Middleware/RequestId.pm ↑ Summary blib/lib/PAGI/Session.pm »