blib/lib/WebDyne/Cloudflare/R2.pm

filter: Statementstmt 97.3% 146 / 150 Branchbran 71.6% 43 / 60 Conditioncond 50.0% 31 / 62 MC/DCmcdc 21.8% 7 / 32 Subroutinesub 100% 30 / 30 Podpod 0.0% 0 / 15 totaltotal 73.6% 257 / 349 CC 52 SCAR 21.0
CC
52
Cov
81%
new
21.5
object
12.7
blob
7.1
CRAP 8.2
« lib/WebDyne/Cloudflare/DurableObject/Context.pm ↑ Summary blib/lib/WebDyne/Cloudflare/Hyperdrive/Statement.pm »
1 package WebDyne::Cloudflare::R2;
2  
3 3 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Encode qw(decode FB_CROAK);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use WebDyne::Cloudflare ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use MIME::Base64 qw(decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use WebDyne::Cloudflare::R2::Blob;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use WebDyne::Cloudflare::R2::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use WebDyne::Cloudflare::R2::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 our $VERSION='0.001';
18 3 our $HOST_CALL;
19  
20 3 ▶use constant EXTENSION_NAME => 'webdyne.cloudflare.r2';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use constant PROTOCOL_VERSION => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 my $json_or=JSON::PP->new()->canonical()->allow_nonref();
24  
25 8 ▶sub new {
Subroutine new: called CC=16 SCAR=30.7
Pod undocumented
26 8 my ($class, %opt)=@_;
27 8 my $scope_hr=$opt{'scope'};
28 8 ▶ die "WebDyne::Cloudflare::R2 requires a PAGI scope hash\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $scope_hr eq "HASH" 0 8 8
29 unless ref($scope_hr) eq 'HASH';
30 my $extension_hr=(ref($scope_hr->{'extensions'}) eq 'HASH')
31 8 ▶ ? $scope_hr->{'extensions'}{EXTENSION_NAME()}
Branch: 1 branch
BranchTrueFalseTotal
ref $scope_hr->{'extensions'} eq 'HASH' ? : 6 2 8
32 : undef;
33 8 ▶ die "PAGI scope has no " . EXTENSION_NAME() . " capability\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $extension_hr eq "HASH" 3 5 8
34 unless ref($extension_hr) eq 'HASH';
35 die "Unsupported R2 capability protocol\n"
36 unless (defined($extension_hr->{'version'})
37 &&!ref($extension_hr->{'version'})
38 5 ▶ &&($extension_hr->{'version'} eq PROTOCOL_VERSION()));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $extension_hr->{"version"} and not ref $extension_hr->{"version"} and $extension_hr->{"version"} eq 1 1 4 5
Condition50%3/6
defined $extension_hr->{"version"} and not ref $extension_hr->{"version"} and $extension_hr->{"version"} eq 1
!ll&&!rl&&r
014
defined $extension_hr->{"version"} and not ref $extension_hr->{"version"}
!ll&&!rl&&r
005
MC/DC33%1/3
defined $extension_hr->{"version"} and not ref $extension_hr->{"version"} and $extension_hr->{"version"} eq 1
defined $extension_hr->{"version"} not ref $extension_hr->{"version"} $extension_hr->{"version"} eq 1
Truth table50%2/4
A and not(B) and C
A= defined $extension_hr->{"version"}B= not ref $extension_hr->{"version"}C= $extension_hr->{"version"} eq 1
ABCresult
0XX0
10X0
1100
1111
39 die "Invalid R2 capability token\n"
40 unless (defined($extension_hr->{'capability'})
41 &&!ref($extension_hr->{'capability'})
42 4 ▶ &&length($extension_hr->{'capability'}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $extension_hr->{"capability"} and not ref $extension_hr->{"capability"} and length $extension_hr->{"capability"} 0 4 4
Condition33%2/6
defined $extension_hr->{"capability"} and not ref $extension_hr->{"capability"} and length $extension_hr->{"capability"}
!ll&&!rl&&r
004
defined $extension_hr->{"capability"} and not ref $extension_hr->{"capability"}
!ll&&!rl&&r
004
MC/DC0%0/3
defined $extension_hr->{"capability"} and not ref $extension_hr->{"capability"} and length $extension_hr->{"capability"}
defined $extension_hr->{"capability"} not ref $extension_hr->{"capability"} length $extension_hr->{"capability"}
Truth table25%1/4
A and not(B) and C
A= defined $extension_hr->{"capability"}B= not ref $extension_hr->{"capability"}C= length $extension_hr->{"capability"}
ABCresult
0XX0
10X0
1100
1111
43  
44 4 ▶ my $binding=(defined($opt{'binding'}) ? $opt{'binding'} : 'R2');
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'binding'} ? : 3 1 4
45 4 ▶ die "Invalid R2 binding name '$binding'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $binding and $binding =~ /\A[A-Z_][A-Z0-9_]*\z/u 0 4 4
Condition33%1/3
not ref $binding and $binding =~ /\A[A-Z_][A-Z0-9_]*\z/u
!ll&&!rl&&r
004
MC/DC0%0/2
not ref $binding and $binding =~ /\A[A-Z_][A-Z0-9_]*\z/u
not ref $binding $binding =~ /\A[A-Z_][A-Z0-9_]*\z/u
Truth table33%1/3
not(A) and B
A= not ref $bindingB= $binding =~ /\A[A-Z_][A-Z0-9_]*\z/u
ABresult
0X0
100
111
46 unless (!ref($binding)&&($binding=~/\A[A-Z_][A-Z0-9_]*\z/));
47 4 my $bindings_ar=$extension_hr->{'bindings'};
48 4 ▶ die "Invalid R2 capability binding list\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $bindings_ar eq "ARRAY" 1 3 4
49 unless (ref($bindings_ar) eq 'ARRAY');
50 3 ▶ my %binding=map { $_ => 1 } grep { defined($_)&&!ref($_) } @{$bindings_ar};
Condition33%1/3
defined $_ && !ref($_)
!ll&&!rl&&r
003
MC/DC0%0/2
defined $_ && !ref($_)
defined $_ !ref($_)
Truth table33%1/3
A && not(B)
A= defined $_B= !ref($_)
ABresult
0X0
100
111
51 die "R2 binding '$binding' is not available to this request\n"
52 3 ▶ unless $binding{$binding};
Branch: 1 branch
BranchTrueFalseTotal
unless $binding{$binding} 0 3 3
53  
54 my $self=bless({
55 binding => $binding,
56 3 capability => $extension_hr->{'capability'},
57 }, $class);
58 3 return $self;
59 }
60  
61  
62 1 ▶sub binding { return shift()->{'binding'}; }
Subroutine binding: called CC=1 SCAR=0.0
Pod undocumented
63  
64  
65 23 ▶sub blob {
Subroutine blob: called CC=7 SCAR=19.5
Pod undocumented
66 # Accept function, class and object calls without discarding payload bytes.
67 #
68 23 ▶ shift() if ((@_>1)&&(defined($_[0])
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 and (defined $_[0] and builtin::blessed($_[0]) || !ref($_[0]) && $_[0] eq __PACKAGE__) 15 8 23
Condition83%10/12
@_ > 1 and (defined $_[0] and builtin::blessed($_[0]) || !ref($_[0]) && $_[0] eq __PACKAGE__)
!ll&&!rl&&r
7115
defined $_[0] and builtin::blessed($_[0]) || !ref($_[0]) && $_[0] eq __PACKAGE__
!ll&&!rl&&r
0115
builtin::blessed($_[0]) or not ref $_[0] and $_[0] eq __PACKAGE__
l!l&&r!l&&!r
4111
not ref $_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
0111
MC/DC60%3/5
@_ > 1 and (defined $_[0] and builtin::blessed($_[0]) || !ref($_[0]) && $_[0] eq __PACKAGE__)
@_ > 1 defined $_[0] builtin::blessed($_[0]) not ref $_[0] $_[0] eq __PACKAGE__
Truth table66%4/6
A and B and C or not(D) and E
A= @_ > 1B= defined $_[0]C= builtin::blessed($_[0])D= not ref $_[0]E= $_[0] eq __PACKAGE__
ABCDEresult
0XXXX0
10XXX0
1100X0
110100
111XX1
110111
69 &&(blessed($_[0])||(!ref($_[0])&&($_[0] eq __PACKAGE__)))));
70 23 ▶ die "R2 blob requires exactly one byte string\n" unless (@_==1);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 1 22 23
71 22 return WebDyne::Cloudflare::R2::Blob->new($_[0]);
72 }
73  
74  
75 11 ▶sub key {
Subroutine key: called CC=4 SCAR=17.1
Pod undocumented
76 11 my ($key)=@_;
77 11 ▶ die "R2 key must be a non-empty scalar\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and not ref $key and length $key 0 11 11
Condition33%2/6
defined $key and not ref $key and length $key
!ll&&!rl&&r
0011
defined $key and not ref $key
!ll&&!rl&&r
0011
MC/DC0%0/3
defined $key and not ref $key and length $key
defined $key not ref $key length $key
Truth table25%1/4
A and not(B) and C
A= defined $keyB= not ref $keyC= length $key
ABCresult
0XX0
10X0
1100
1111
78 unless (defined($key)&&!ref($key)&&length($key));
79 11 return $key;
80 }
81  
82  
83 9 ▶sub known_options {
Subroutine known_options: called CC=2 SCAR=7.0
Pod undocumented
84 9 my ($operation, $allowed_ar, %opt)=@_;
85 9 my %allowed=map { $_ => 1 } @{$allowed_ar};
86 9 my @unknown=sort grep { !$allowed{$_} } keys %opt;
87 9 ▶ die "Unknown R2 $operation option: $unknown[0]\n" if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 9 9
88 9 return %opt;
89 }
90  
91  
92 3 ▶sub encode_text {
Subroutine encode_text: called CC=5 SCAR=17.2
Pod undocumented
93 3 my ($value_ref)=@_;
94 3 ▶ die "R2 object must be a scalar or R2 blob\n" if (!defined($value_ref)||ref($value_ref));
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value_ref or ref $value_ref 1 2 3
Condition66%2/3
not defined $value_ref or ref $value_ref
l!l&&r!l&&!r
012
MC/DC50%1/2
not defined $value_ref or ref $value_ref
not defined $value_ref ref $value_ref
Truth table66%2/3
not(A) or B
A= not defined $value_refB= ref $value_ref
ABresult
1X1
011
000
95 2 ▶ return decode('UTF-8', $value_ref, FB_CROAK)
Branch: 1 branch
BranchTrueFalseTotal
if not utf8::is_utf8($value_ref) and $value_ref =~ /[\x80-\xff]/u 0 2 2
Condition33%1/3
not utf8::is_utf8($value_ref) and $value_ref =~ /[\x80-\xff]/u
!ll&&!rl&&r
020
MC/DC0%0/2
not utf8::is_utf8($value_ref) and $value_ref =~ /[\x80-\xff]/u
not utf8::is_utf8($value_ref) $value_ref =~ /[\x80-\xff]/u
Truth table33%1/3
not(A) and B
A= not utf8::is_utf8($value_ref)B= $value_ref =~ /[\x80-\xff]/u
ABresult
0X0
100
111
96 if (!utf8::is_utf8($value_ref)&&($value_ref=~/[\x80-\xff]/));
97 2 return "$value_ref";
98 }
99  
100  
101 6 ▶sub object {
Subroutine object: called CC=9 SCAR=25.4
Pod undocumented
102 6 my ($value_hr)=@_;
103 6 ▶ return undef unless defined($value_hr);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value_hr 2 4 6
104 4 ▶ die "R2 host returned invalid object metadata\n" unless ref($value_hr) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value_hr eq "HASH" 0 4 4
105 4 ▶ if (exists($value_hr->{'body'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $value_hr->{"body"}) 1 3 4
106 1 my $body_hr=$value_hr->{'body'};
107 die "R2 host returned an invalid object body\n"
108 unless ((ref($body_hr) eq 'HASH')
109 &&defined($body_hr->{'type'})&&($body_hr->{'type'} eq 'bytes')
110 1 ▶ &&defined($body_hr->{'base64'})&&!ref($body_hr->{'base64'}));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body_hr eq "HASH" and defined $body_hr->{"type"} and $body_hr->{"type"} eq "bytes" and defined $body_hr->{"base64"} and not ref $body_hr->{"base64"} 0 1 1
Condition33%4/12
ref $body_hr eq "HASH" and defined $body_hr->{"type"} and $body_hr->{"type"} eq "bytes" and defined $body_hr->{"base64"} and not ref $body_hr->{"base64"}
!ll&&!rl&&r
001
ref $body_hr eq "HASH" and defined $body_hr->{"type"} and $body_hr->{"type"} eq "bytes" and defined $body_hr->{"base64"}
!ll&&!rl&&r
001
ref $body_hr eq "HASH" and defined $body_hr->{"type"} and $body_hr->{"type"} eq "bytes"
!ll&&!rl&&r
001
ref $body_hr eq "HASH" and defined $body_hr->{"type"}
!ll&&!rl&&r
001
MC/DC0%0/5
ref $body_hr eq "HASH" and defined $body_hr->{"type"} and $body_hr->{"type"} eq "bytes" and defined $body_hr->{"base64"} and not ref $body_hr->{"base64"}
ref $body_hr eq "HASH" defined $body_hr->{"type"} $body_hr->{"type"} eq "bytes" defined $body_hr->{"base64"} not ref $body_hr->{"base64"}
Truth table16%1/6
A and B and C and D and not(E)
A= ref $body_hr eq "HASH"B= defined $body_hr->{"type"}C= $body_hr->{"type"} eq "bytes"D= defined $body_hr->{"base64"}E= not ref $body_hr->{"base64"}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
111 1 $value_hr->{'body'}=decode_base64($body_hr->{'base64'});
112 }
113 4 return WebDyne::Cloudflare::R2::Object->new($value_hr);
114 }
115  
116  
117 4 ▶async sub get {
Subroutine get: called CC=2 SCAR=6.9
Pod undocumented
118 4 my ($self, $key, %opt)=@_;
119 4 %opt=known_options('get', [qw(range)], %opt);
120 return object(await $self->execute(
121 operation => 'get',
122 key => key($key),
123 4 ▶ (exists($opt{'range'}) ? (range => $opt{'range'}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
exists $opt{'range'} ? : 1 3 4
124 ));
125 3 }
126  
127  
128 1 ▶async sub head {
Subroutine head: called CC=1 SCAR=0.0
Pod undocumented
129 1 my ($self, $key)=@_;
130 1 return object(await $self->execute(operation => 'head', key => key($key)));
131 3 }
132  
133  
134 4 ▶async sub put {
Subroutine put: called CC=3 SCAR=11.0
Pod undocumented
135 4 my ($self, $key, $value_ref, %opt)=@_;
136 4 %opt=known_options('put', [qw(http_metadata custom_metadata storage_class)], %opt);
137 4 ▶ my $encoded_ref=(blessed($value_ref)&&$value_ref->isa('WebDyne::Cloudflare::R2::Blob'))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($value_ref) && $value_ref->isa('WebDyne::Cloudflare::R2::Blob') ? : 1 3 4
Condition66%2/3
builtin::blessed($value_ref) and $value_ref->isa("WebDyne::Cloudflare::R2::Blob")
!ll&&!rl&&r
301
MC/DC50%1/2
builtin::blessed($value_ref) and $value_ref->isa("WebDyne::Cloudflare::R2::Blob")
builtin::blessed($value_ref) $value_ref->isa("WebDyne::Cloudflare::R2::Blob")
Truth table66%2/3
A and B
A= builtin::blessed($value_ref)B= $value_ref->isa("WebDyne::Cloudflare::R2::Blob")
ABresult
0X0
100
111
138 ? $value_ref->wire_value()
139 : encode_text($value_ref);
140 3 return object(await $self->execute(
141 operation => 'put',
142 key => key($key),
143 value => $encoded_ref,
144 %opt,
145 ));
146 3 }
147  
148  
149 1 ▶async sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod undocumented
150 1 my ($self, $key)=@_;
151 1 return await $self->execute(operation => 'delete', key => key($key));
152 3 }
153  
154  
155 1 ▶async sub delete_many {
Subroutine delete_many: called CC=3 SCAR=11.2
Pod undocumented
156 1 my ($self, @keys)=@_;
157 1 ▶ die "R2 delete_many requires at least one key\n" unless @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 0 1 1
158 1 ▶ die "R2 delete_many accepts at most 1000 keys\n" if @keys > 1000;
Branch: 1 branch
BranchTrueFalseTotal
if @keys > 1000 0 1 1
159 1 return await $self->execute(operation => 'delete', keys => [map { key($_) } @keys]);
160 3 }
161  
162  
163 1 ▶async sub list {
Subroutine list: called CC=3 SCAR=11.1
Pod undocumented
164 1 my ($self, %opt)=@_;
165 1 %opt=known_options('list', [qw(prefix cursor delimiter limit include)], %opt);
166 1 my $result_hr=await $self->execute(operation => 'list', %opt);
167 1 ▶ die "R2 host returned an invalid list response\n" unless ref($result_hr) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $result_hr eq "HASH" 0 1 1
168 1 ▶ $result_hr->{'objects'}=[map { object($_) } @{(defined($result_hr->{'objects'}) ? $result_hr->{'objects'} : [])}];
Branch: 1 branch
BranchTrueFalseTotal
defined $result_hr->{'objects'} ? : 1 0 1
169 1 return $result_hr;
170 3 }
171  
172  
173 10 ▶sub call_host {
Subroutine call_host: called CC=3 SCAR=14.8
Pod undocumented
174 10 my ($wire)=@_;
175 10 ▶ return $HOST_CALL->($wire) if $HOST_CALL;
Branch: 1 branch
BranchTrueFalseTotal
if $WebDyne::Cloudflare::R2::HOST_CALL 10 0 10
176 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
177 0 my $host_call_cr=*{'WebDyne::Cloudflare::R2::Host::call'}{'CODE'};
178 0 ▶ die "R2 host adapter is not registered in this runtime\n" unless $host_call_cr;
Branch: 1 branch
BranchTrueFalseTotal
unless $host_call_cr 0 0 0
179 0 return $host_call_cr->($wire);
180 }
181  
182  
183 11 ▶async sub execute {
Subroutine execute: called CC=6 SCAR=18.0
Pod undocumented
184 11 my ($self, %request)=@_;
185 my $wire_hr={
186 version => PROTOCOL_VERSION,
187 capability => $self->{'capability'},
188 11 binding => $self->{'binding'},
189 %request,
190 };
191 11 my $response_wire=call_host($json_or->encode(WebDyne::Cloudflare::json_value($wire_hr)));
192 10 my $response_hr=eval { $json_or->decode($response_wire) };
193 10 ▶ if ((ref($response_hr) ne 'HASH')||!exists($response_hr->{'ok'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $response_hr ne "HASH" or not exists $response_hr->{"ok"}) 1 9 10
Condition66%2/3
ref $response_hr ne "HASH" or not exists $response_hr->{"ok"}
l!l&&r!l&&!r
019
MC/DC50%1/2
ref $response_hr ne "HASH" or not exists $response_hr->{"ok"}
ref $response_hr ne "HASH" not exists $response_hr->{"ok"}
Truth table66%2/3
A or not(B)
A= ref $response_hr ne "HASH"B= not exists $response_hr->{"ok"}
ABresult
1X1
011
000
194 1 ▶ my $detail=$@||'host returned an invalid response';
Condition50%1/2
$@ || 'host returned an invalid response'
l!l
01
MC/DC0%0/1
$@ || 'host returned an invalid response'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
195 1 die WebDyne::Cloudflare::R2::Error->new(name => 'R2_PROTOCOL_ERROR', message => $detail);
196 }
197 9 ▶ unless ($response_hr->{'ok'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response_hr->{"ok"}) 1 8 9
198 1 ▶ my $error_hr=ref($response_hr->{'error'}) eq 'HASH' ? $response_hr->{'error'} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $response_hr->{'error'} eq 'HASH' ? : 1 0 1
199 1 die WebDyne::Cloudflare::R2::Error->new(%{$error_hr});
200 }
201 8 return $response_hr->{'result'};
202 3 }
203  
204 3 1;
« lib/WebDyne/Cloudflare/DurableObject/Context.pm ↑ Summary blib/lib/WebDyne/Cloudflare/Hyperdrive/Statement.pm »