lib/WebDyne/Cloudflare/DurableObject.pm

filter: Statementstmt 93.2% 110 / 118 Branchbran 73.0% 38 / 52 Conditioncond 40.3% 27 / 67 MC/DCmcdc 12.5% 4 / 32 Subroutinesub 90.9% 20 / 22 Podpod 0.0% 0 / 11 totaltotal 65.8% 199 / 302 CC 50 SCAR 25.1
CC
50
Cov
74%
decode_value
21.2
bytes
21.2
capability
19.1
CRAP 12.4
« blib/lib/WebDyne/Cloudflare/Hyperdrive/Codec.pm ↑ Summary blib/lib/WebDyne/Cloudflare/SecretsStore.pm »
1 package WebDyne::Cloudflare::DurableObject;
2  
3 1 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util qw(blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use MIME::Base64 qw(encode_base64 decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use WebDyne::Cloudflare ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use WebDyne::Cloudflare::DurableObject::Bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use WebDyne::Cloudflare::DurableObject::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION='0.001';
15 1 our $HOST_CALL;
16 1 my $json_or=JSON::PP->new()->canonical()->allow_nonref();
17  
18 3 ▶sub capability {
Subroutine capability: called CC=9 SCAR=29.5
Pod undocumented
19 3 my ($scope_hr)=@_;
20 3 ▶ die "Durable Object requires a scope hash\n" unless (ref($scope_hr) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $scope_hr eq "HASH" 0 3 3
21 3 my $extension_hr=$scope_hr->{'extensions'}{'webdyne.cloudflare.durable_object'};
22 die "No valid Durable Object capability in scope\n"
23 unless ((ref($extension_hr) eq 'HASH')&&($extension_hr->{'version'}||0)==1
24 &&defined($extension_hr->{'capability'})&&!ref($extension_hr->{'capability'})
25 3 ▶ &&length($extension_hr->{'capability'})&&(ref($extension_hr->{'bindings'}) eq 'ARRAY'));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $extension_hr eq "HASH" and ($extension_hr->{"version"} || 0) == 1 and defined $extension_hr->{"capability"} and not ref $extension_hr->{"capability"} and length $extension_hr->{"capability"} and ref $extension_hr->{"bindings"} eq "ARRAY" 0 3 3
Condition35%6/17
ref $extension_hr eq "HASH" and ($extension_hr->{"version"} || 0) == 1 and defined $extension_hr->{"capability"} and not ref $extension_hr->{"capability"} and length $extension_hr->{"capability"} and ref $extension_hr->{"bindings"} eq "ARRAY"
!ll&&!rl&&r
003
ref $extension_hr eq "HASH" and ($extension_hr->{"version"} || 0) == 1 and defined $extension_hr->{"capability"} and not ref $extension_hr->{"capability"} and length $extension_hr->{"capability"}
!ll&&!rl&&r
003
ref $extension_hr eq "HASH" and ($extension_hr->{"version"} || 0) == 1 and defined $extension_hr->{"capability"} and not ref $extension_hr->{"capability"}
!ll&&!rl&&r
003
ref $extension_hr eq "HASH" and ($extension_hr->{"version"} || 0) == 1 and defined $extension_hr->{"capability"}
!ll&&!rl&&r
003
ref $extension_hr eq "HASH" and ($extension_hr->{"version"} || 0) == 1
!ll&&!rl&&r
003
$extension_hr->{'version'} || 0
l!l
30
MC/DC0%0/6
ref $extension_hr eq "HASH" and ($extension_hr->{"version"} || 0) == 1 and defined $extension_hr->{"capability"} and not ref $extension_hr->{"capability"} and length $extension_hr->{"capability"} and ref $extension_hr->{"bindings"} eq "ARRAY"
ref $extension_hr eq "HASH" $extension_hr->{'version'} defined $extension_hr->{"capability"} not ref $extension_hr->{"capability"} length $extension_hr->{"capability"} ref $extension_hr->{"bindings"} eq "ARRAY"
Truth table14%1/7
A and B and C and not(D) and E and F
A= ref $extension_hr eq "HASH"B= $extension_hr->{'version'}C= defined $extension_hr->{"capability"}D= not ref $extension_hr->{"capability"}E= length $extension_hr->{"capability"}F= ref $extension_hr->{"bindings"} eq "ARRAY"
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
26 3 return $extension_hr;
27 }
28  
29  
30 2 ▶sub new {
Subroutine new: called CC=4 SCAR=14.2
Pod undocumented
31 2 my ($class, %opt)=@_;
32 2 my $extension_hr=capability($opt{'scope'});
33 2 my $binding=$opt{'binding'};
34 die "Durable Object requires an available binding\n"
35 unless (defined($binding)&&!ref($binding)
36 2 ▶ &&grep { $_ eq $binding } @{$extension_hr->{'bindings'}});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $binding and not ref $binding and grep {$_ eq $binding;} @{$$extension_hr{"bindings"};} 1 1 2
Condition50%3/6
defined $binding and not ref $binding and grep {$_ eq $binding;} @{$$extension_hr{"bindings"};}
!ll&&!rl&&r
011
defined $binding and not ref $binding
!ll&&!rl&&r
002
MC/DC33%1/3
defined $binding and not ref $binding and grep {$_ eq $binding;} @{$$extension_hr{"bindings"};}
defined $binding not ref $binding grep {$_ eq $binding;} @{$$extension_hr{"bindings"};}
Truth table50%2/4
A and not(B) and C
A= defined $bindingB= not ref $bindingC= grep {$_ eq $binding;} @{$$extension_hr{"bindings"};}
ABCresult
0XX0
10X0
1100
1111
37 1 return bless({capability => $extension_hr->{'capability'}, binding => $binding}, $class);
38 }
39  
40  
41 1 ▶async sub get_by_name {
Subroutine get_by_name: called CC=1 SCAR=0.0
Pod undocumented
42 1 my ($self, $name)=@_;
43 1 my $id=await $self->execute(operation => 'resolve', name => $name);
44 1 return bless({%{$self}, id => $id}, ref($self));
45 1 }
46  
47  
48 0 ▶async sub get_by_id {
Subroutine get_by_id: not called CC=1 SCAR=6.9
Pod undocumented
49 0 my ($self, $id)=@_;
50 0 $id=await $self->execute(operation => 'validate_id', id => $id);
51 0 return bless({%{$self}, id => $id}, ref($self));
52 1 }
53  
54  
55 1 ▶sub id { return shift()->{'id'}; }
Subroutine id: called CC=1 SCAR=0.0
Pod undocumented
56 0 ▶sub binding { return shift()->{'binding'}; }
Subroutine binding: not called CC=1 SCAR=6.9
Pod undocumented
57  
58  
59 2 ▶async sub call {
Subroutine call: called CC=2 SCAR=7.0
Pod undocumented
60 2 my ($self, $method, @args)=@_;
61 2 ▶ die "Select a Durable Object before calling a method\n" unless $self->{'id'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"id"} 0 2 2
62 2 return decode_value(await $self->execute(operation => 'call', id => $self->{'id'},
63 method => $method, args => encode_value(\@args)));
64 1 }
65  
66  
67 2 ▶sub bytes {
Subroutine bytes: called CC=6 SCAR=30.5
Pod undocumented
68 2 ▶ shift() if ((@_==2)&&(blessed($_[0])||(!ref($_[0])&&defined($_[0])&&($_[0] eq __PACKAGE__))));
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 and builtin::blessed($_[0]) || !ref($_[0]) && defined $_[0] && $_[0] eq __PACKAGE__ 0 2 2
Condition8%1/12
@_ == 2 and builtin::blessed($_[0]) || !ref($_[0]) && defined $_[0] && $_[0] eq __PACKAGE__
!ll&&!rl&&r
200
builtin::blessed($_[0]) or not ref $_[0] and defined $_[0] and $_[0] eq __PACKAGE__
l!l&&r!l&&!r
000
not ref $_[0] and defined $_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
000
not ref $_[0] and defined $_[0]
!ll&&!rl&&r
000
MC/DC0%0/5
@_ == 2 and builtin::blessed($_[0]) || !ref($_[0]) && defined $_[0] && $_[0] eq __PACKAGE__
@_ == 2 builtin::blessed($_[0]) not ref $_[0] defined $_[0] $_[0] eq __PACKAGE__
Truth table16%1/6
A and B or not(C) and D and E
A= @_ == 2B= builtin::blessed($_[0])C= not ref $_[0]D= defined $_[0]E= $_[0] eq __PACKAGE__
ABCDEresult
0XXXX0
100XX0
1010X0
101100
11XXX1
101111
69 2 return WebDyne::Cloudflare::DurableObject::Bytes->new(@_);
70 }
71  
72  
73 17 ▶sub encode_value {
Subroutine encode_value: called CC=12 SCAR=25.0
Pod undocumented
74 17 my ($value_ref, $seen_hr, $depth)=@_;
75 17 ▶ $depth=0 unless defined($depth);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $depth 5 12 17
76 17 ▶ die "Durable Object value nesting exceeds 64\n" if ($depth>64);
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 64 0 17 17
77 17 ▶ return WebDyne::Cloudflare::json_value($value_ref) unless ref($value_ref);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value_ref 7 10 17
78 10 ▶ return $value_ref if JSON::PP::is_bool($value_ref);
Branch: 1 branch
BranchTrueFalseTotal
if JSON::PP::is_bool($value_ref) 0 10 10
79 10 ▶ if (blessed($value_ref)&&$value_ref->isa('WebDyne::Cloudflare::DurableObject::Bytes')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value_ref) and $value_ref->isa("WebDyne::Cloudflare::DurableObject::Bytes")) 1 9 10
Condition66%2/3
builtin::blessed($value_ref) and $value_ref->isa("WebDyne::Cloudflare::DurableObject::Bytes")
!ll&&!rl&&r
901
MC/DC50%1/2
builtin::blessed($value_ref) and $value_ref->isa("WebDyne::Cloudflare::DurableObject::Bytes")
builtin::blessed($value_ref) $value_ref->isa("WebDyne::Cloudflare::DurableObject::Bytes")
Truth table66%2/3
A and B
A= builtin::blessed($value_ref)B= $value_ref->isa("WebDyne::Cloudflare::DurableObject::Bytes")
ABresult
0X0
100
111
80 1 return {t => 'bytes', v => encode_base64($value_ref->value(), '')};
81 }
82 9 ▶ die "Durable Object values must be arrays, hashes or explicit bytes\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value_ref eq "HASH" or ref $value_ref eq "ARRAY" 0 9 9
Condition66%2/3
ref $value_ref eq "HASH" or ref $value_ref eq "ARRAY"
l!l&&r!l&&!r
540
MC/DC0%0/2
ref $value_ref eq "HASH" or ref $value_ref eq "ARRAY"
ref $value_ref eq "HASH" ref $value_ref eq "ARRAY"
Truth table66%2/3
A or B
A= ref $value_ref eq "HASH"B= ref $value_ref eq "ARRAY"
ABresult
1X1
011
000
83 unless ((ref($value_ref) eq 'HASH')||(ref($value_ref) eq 'ARRAY'));
84 9 ▶ $seen_hr={} unless $seen_hr;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen_hr 5 4 9
85 9 my $address=refaddr($value_ref);
86 9 ▶ die "Cyclic Durable Object value\n" if $seen_hr->{$address};
Branch: 1 branch
BranchTrueFalseTotal
if $seen_hr->{$address} 1 8 9
87 8 local $seen_hr->{$address}=1;
88 8 ▶ return {t => 'array', v => [map { encode_value($_, $seen_hr, $depth+1) } @{$value_ref}]}
Branch: 1 branch
BranchTrueFalseTotal
if ref $value_ref eq "ARRAY" 4 4 8
89 if (ref($value_ref) eq 'ARRAY');
90 4 return {t => 'hash', v => {map { $_ => encode_value($value_ref->{$_}, $seen_hr, $depth+1) } keys(%{$value_ref})}};
91 }
92  
93  
94 14 ▶sub decode_value {
Subroutine decode_value: called CC=16 SCAR=30.5
Pod undocumented
95 14 my ($value_ref, $depth)=@_;
96 14 ▶ $depth=0 unless defined($depth);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $depth 3 11 14
97 14 ▶ die "Durable Object value nesting exceeds 64\n" if ($depth>64);
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 64 0 14 14
98 14 ▶ return $value_ref unless ref($value_ref);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value_ref 7 7 14
99 7 ▶ return $value_ref if JSON::PP::is_bool($value_ref);
Branch: 1 branch
BranchTrueFalseTotal
if JSON::PP::is_bool($value_ref) 0 7 7
100 die "Invalid Durable Object value envelope\n"
101 7 ▶ unless ((ref($value_ref) eq 'HASH')&&(keys(%{$value_ref})==2)&&defined($value_ref->{'t'}));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value_ref eq "HASH" and keys %{$value_ref;} == 2 and defined $value_ref->{"t"} 0 7 7
Condition33%2/6
ref $value_ref eq "HASH" and keys %{$value_ref;} == 2 and defined $value_ref->{"t"}
!ll&&!rl&&r
007
ref $value_ref eq "HASH" and keys %{$value_ref;} == 2
!ll&&!rl&&r
007
MC/DC0%0/3
ref $value_ref eq "HASH" and keys %{$value_ref;} == 2 and defined $value_ref->{"t"}
ref $value_ref eq "HASH" keys %{$value_ref;} == 2 defined $value_ref->{"t"}
Truth table25%1/4
A and B and C
A= ref $value_ref eq "HASH"B= keys %{$value_ref;} == 2C= defined $value_ref->{"t"}
ABCresult
0XX0
10X0
1100
1111
102 7 my $type=$value_ref->{'t'};
103 7 my $item_ref=$value_ref->{'v'};
104 7 ▶ if (($type eq 'bytes')&&defined($item_ref)&&!ref($item_ref)
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "bytes" and defined $item_ref and not ref $item_ref and $item_ref =~ m[\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z]u) 1 6 7
Condition66%6/9
$type eq "bytes" and defined $item_ref and not ref $item_ref and $item_ref =~ m[\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z]u
!ll&&!rl&&r
601
$type eq "bytes" and defined $item_ref and not ref $item_ref
!ll&&!rl&&r
601
$type eq "bytes" and defined $item_ref
!ll&&!rl&&r
601
MC/DC25%1/4
$type eq "bytes" and defined $item_ref and not ref $item_ref and $item_ref =~ m[\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z]u
$type eq "bytes" defined $item_ref not ref $item_ref $item_ref =~ m[\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z]u
Truth table40%2/5
A and B and not(C) and D
A= $type eq "bytes"B= defined $item_refC= not ref $item_refD= $item_ref =~ m[\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z]u
ABCDresult
0XXX0
10XX0
110X0
11100
11111
105 &&($item_ref=~/\A(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?\z/)) {
106 1 return bytes(decode_base64($item_ref));
107 }
108 6 ▶ return [map { decode_value($_, $depth+1) } @{$item_ref}]
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "array" and ref $item_ref eq "ARRAY" 3 3 6
Condition66%2/3
$type eq "array" and ref $item_ref eq "ARRAY"
!ll&&!rl&&r
303
MC/DC50%1/2
$type eq "array" and ref $item_ref eq "ARRAY"
$type eq "array" ref $item_ref eq "ARRAY"
Truth table66%2/3
A and B
A= $type eq "array"B= ref $item_ref eq "ARRAY"
ABresult
0X0
100
111
109 if (($type eq 'array')&&(ref($item_ref) eq 'ARRAY'));
110 3 ▶ return {map { $_ => decode_value($item_ref->{$_}, $depth+1) } keys(%{$item_ref})}
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hash" and ref $item_ref eq "HASH" 3 0 3
Condition33%1/3
$type eq "hash" and ref $item_ref eq "HASH"
!ll&&!rl&&r
003
MC/DC0%0/2
$type eq "hash" and ref $item_ref eq "HASH"
$type eq "hash" ref $item_ref eq "HASH"
Truth table33%1/3
A and B
A= $type eq "hash"B= ref $item_ref eq "HASH"
ABresult
0X0
100
111
111 if (($type eq 'hash')&&(ref($item_ref) eq 'HASH'));
112 0 die "Invalid Durable Object value envelope\n";
113 }
114  
115  
116 4 ▶async sub execute {
Subroutine execute: called CC=7 SCAR=20.5
Pod undocumented
117 4 my ($self, %request)=@_;
118 my $wire=$json_or->encode(WebDyne::Cloudflare::json_value({
119 4 version => 1, capability => $self->{'capability'}, binding => $self->{'binding'}, %request,
120 }));
121 4 ▶ die "Durable Object request exceeds limit\n" if (length($wire)>1048576);
Branch: 1 branch
BranchTrueFalseTotal
if length $wire > 1048576 0 4 4
122 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
123 0 ▶ my $host_cr=$HOST_CALL||*{'WebDyne::Cloudflare::DurableObject::Host::call'}{'CODE'};
Condition50%1/2
$WebDyne::Cloudflare::DurableObject::HOST_CALL || *{'WebDyne::Cloudflare::DurableObject::Host::call';}{'CODE'}
l!l
40
MC/DC0%0/1
$WebDyne::Cloudflare::DurableObject::HOST_CALL || *{'WebDyne::Cloudflare::DurableObject::Host::call';}{'CODE'}
$WebDyne::Cloudflare::DurableObject::HOST_CALL
Truth table50%1/2
A
A= $WebDyne::Cloudflare::DurableObject::HOST_CALL
Aresult
11
00
124 4 ▶ die "Durable Object host adapter is not registered\n" unless $host_cr;
Branch: 1 branch
BranchTrueFalseTotal
unless $host_cr 0 4 4
125 4 my $response_hr=$json_or->decode($host_cr->($wire));
126 4 ▶ die "Invalid Durable Object host response\n" unless ((ref($response_hr) eq 'HASH')&&exists($response_hr->{'ok'}));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $response_hr eq "HASH" and exists $response_hr->{"ok"} 0 4 4
Condition33%1/3
ref $response_hr eq "HASH" and exists $response_hr->{"ok"}
!ll&&!rl&&r
004
MC/DC0%0/2
ref $response_hr eq "HASH" and exists $response_hr->{"ok"}
ref $response_hr eq "HASH" exists $response_hr->{"ok"}
Truth table33%1/3
A and B
A= ref $response_hr eq "HASH"B= exists $response_hr->{"ok"}
ABresult
0X0
100
111
127 4 ▶ die WebDyne::Cloudflare::DurableObject::Error->new(%{$response_hr->{'error'}}) unless $response_hr->{'ok'};
Branch: 1 branch
BranchTrueFalseTotal
unless $response_hr->{"ok"} 1 3 4
128 3 return $response_hr->{'result'};
129 1 }
130  
131 1 1;
« blib/lib/WebDyne/Cloudflare/Hyperdrive/Codec.pm ↑ Summary blib/lib/WebDyne/Cloudflare/SecretsStore.pm »