lib/Langertha/Skeid/SwaggerUI.pm

filter: Statementstmt 88.0% 132 / 150 Branchbran 76.0% 35 / 46 Conditioncond 57.1% 12 / 21 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 82.3% 28 / 34 Podpod 100% 10 / 10 totaltotal 81.0% 222 / 274 CC 36 SCAR 16.4
CC
36
Cov
82%
__ANON__
20.0
__ANON__
9.3
ensure
7.0
CRAP 5.1
« lib/Langertha/Skeid/Protocol/Audio.pm ↑ Summary blib/lib/Langertha/Skeid/KeyBroker.pm »
1 package Langertha::Skeid::SwaggerUI;
2 86 our $VERSION = '0.004';
3 # ABSTRACT: Swagger UI assets, fetched on first use and cached, never shipped
4 86 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 86 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 86 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 86 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 86 ▶use Digest::SHA qw(sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 86 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 86 ▶use Mojo::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 86 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 86 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 86 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 86 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 # swagger-ui-dist 5.17.14 (Apache-2.0). Digests taken from the files cdn.jsdelivr.net serves for
18 # that version; unpkg.com serves the same bytes.
19 86 my $VERSION_PINNED = '5.17.14';
20 86 my $SOURCE = 'https://cdn.jsdelivr.net/npm/swagger-ui-dist@' . $VERSION_PINNED . '/';
21 86 my %FILE = (
22 'swagger-ui.css' => {
23 sha256 => '40170f0ee859d17f92131ba707329a88a070e4f66874d11365e9a77d232f6117',
24 max => 524288,
25 },
26 'swagger-ui-bundle.js' => {
27 sha256 => 'c2e4a9ef08144839ff47c14202063ecfe4e59e70a4e7154a26bd50d880c88ba1',
28 max => 4194304,
29 },
30 );
31  
32  
33 86 has fetcher => (is => 'lazy');
34 503 ▶has digests => (is => 'ro', default => sub { +{ map { $_ => $FILE{$_}{sha256} } keys %FILE } });
Subroutine __ANON__: called CC=1 SCAR=0.0
35 511 ▶has retry_after => (is => 'ro', default => sub { 60 });
Subroutine __ANON__: called CC=1 SCAR=0.0
36 511 ▶has timeout => (is => 'ro', default => sub { 60 });
Subroutine __ANON__: called CC=1 SCAR=0.0
37  
38 86 has _ua => (is => 'lazy');
39 512 ▶has _inflight => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
40 512 ▶has _waiters => (is => 'ro', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
41 86 has _failed_at => (is => 'rw');
42 86 has _failed => (is => 'rw');
43  
44 0 ▶sub _build__ua {
Subroutine _build__ua: not called CC=1 SCAR=6.9
45 0 my $ua = Mojo::UserAgent->new;
46 0 $ua->max_redirects(3);
47 0 $ua->connect_timeout(10);
48 0 $ua->request_timeout(60);
49 0 $ua->inactivity_timeout(30);
50 0 $ua->max_response_size(4194304 + 1024);
51 0 return $ua;
52 }
53  
54 0 ▶sub _build_fetcher {
Subroutine _build_fetcher: not called CC=1 SCAR=6.9
55 0 my ($self) = @_;
56 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
57 0 my ($url, $cb) = @_;
58 0 ▶ $self->_ua->get($url => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
59 0 my ($ua, $tx) = @_;
60 0 my $err = $tx->error;
61 0 ▶ return $cb->(undef, $err->{code} ? 'HTTP ' . $err->{code} : ($err->{message} // 'request failed'))
Branch: 2 branches
BranchTrueFalseTotal
if $err 0 0 0
$err->{'code'} ? : 0 0 0
Condition0%0/2
$err->{"message"} // "request failed"
l!l
00
MC/DC0%0/1
$err->{"message"} // "request failed"
$err->{"message"}
Truth table0%0/2
A
A= $err->{"message"}
Aresult
11
00
62 if $err;
63 0 return $cb->($tx->res->body, undef);
64 0 });
65 0 };
66 }
67  
68  
69 0 ▶sub version { $VERSION_PINNED }
Subroutine version: not called CC=1 SCAR=6.9
Pod documented
70 0 ▶sub source { $SOURCE }
Subroutine source: not called CC=1 SCAR=6.9
Pod documented
71 26 ▶sub file_names { sort keys %FILE }
Subroutine file_names: called CC=1 SCAR=0.0
Pod documented
72  
73  
74 29 ▶sub is_asset { defined $_[1] && exists $FILE{$_[1]} }
Subroutine is_asset: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] and exists $FILE{$_[1]} 29 0 29
75  
76  
77 2 ▶sub default_cache_dir {
Subroutine default_cache_dir: called CC=5 SCAR=17.3
Pod documented
78 2 my ($self) = @_;
79 2 my $leaf = 'swagger-ui-' . $VERSION_PINNED;
80 2 my $xdg = $ENV{XDG_CACHE_HOME};
81 2 ▶ return "$xdg/skeid/$leaf" if defined($xdg) && $xdg =~ m{\A/};
Branch: 1 branch
BranchTrueFalseTotal
if defined $xdg and $xdg =~ m[\A/] 1 1 2
Condition66%2/3
defined $xdg and $xdg =~ m[\A/]
!ll&&!rl&&r
101
MC/DC50%1/2
defined $xdg and $xdg =~ m[\A/]
defined $xdg $xdg =~ m[\A/]
Truth table66%2/3
A and B
A= defined $xdgB= $xdg =~ m[\A/]
ABresult
0X0
100
111
82 1 my $home = $ENV{HOME};
83 1 ▶ return "$home/.cache/skeid/$leaf" if defined($home) && $home =~ m{\A/};
Branch: 1 branch
BranchTrueFalseTotal
if defined $home and $home =~ m[\A/] 1 0 1
Condition33%1/3
defined $home and $home =~ m[\A/]
!ll&&!rl&&r
001
MC/DC0%0/2
defined $home and $home =~ m[\A/]
defined $home $home =~ m[\A/]
Truth table33%1/3
A and B
A= defined $homeB= $home =~ m[\A/]
ABresult
0X0
100
111
84 0 return File::Spec->tmpdir . "/skeid-$</$leaf";
85 }
86  
87  
88 24 ▶sub asset_path {
Subroutine asset_path: called CC=3 SCAR=11.0
Pod documented
89 24 my ($self, $dir, $name) = @_;
90 24 ▶ return unless $self->is_asset($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_asset($name) 0 24 24
91 24 my $path = "$dir/$name";
92 24 ▶ return -f $path ? $path : undef;
Branch: 1 branch
BranchTrueFalseTotal
-f $path ? : 10 14 24
93 }
94  
95  
96 18 ▶sub ready {
Subroutine ready: called CC=3 SCAR=11.0
Pod documented
97 18 my ($self, $dir) = @_;
98 18 for my $name ($self->file_names) {
99 22 ▶ return 0 unless defined $self->asset_path($dir, $name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->asset_path($dir, $name) 14 8 22
100 }
101 4 return 1;
102 }
103  
104  
105 18 ▶sub ensure {
Subroutine ensure: called CC=7 SCAR=19.5
Pod documented
106 18 my ($self, $dir, $cb) = @_;
107 18 ▶ return $cb->(1) if $self->ready($dir);
Branch: 1 branch
BranchTrueFalseTotal
if $self->ready($dir) 4 14 18
108  
109 14 ▶ if (defined $self->_failed_at && Time::HiRes::time() - $self->_failed_at < $self->retry_after) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_failed_at and &Time::HiRes::time() - $self->_failed_at < $self->retry_after) 2 12 14
Condition100%3/3
defined $self->_failed_at and &Time::HiRes::time() - $self->_failed_at < $self->retry_after
!ll&&!rl&&r
1112
MC/DC100%2/2
defined $self->_failed_at and &Time::HiRes::time() - $self->_failed_at < $self->retry_after
defined $self->_failed_at &Time::HiRes::time() - $self->_failed_at < $self->retry_after
Truth table100%3/3
A and B
A= defined $self->_failed_atB= &Time::HiRes::time() - $self->_failed_at < $self->retry_after
ABresult
0X0
100
111
110 2 return $cb->(0, 'backoff', $self->_failed);
111 }
112  
113 12 push @{$self->_waiters}, $cb;
114 12 ▶ return if $self->_inflight;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_inflight 4 8 12
115 8 $self->_inflight(1);
116  
117 8 my %body;
118 8 my @todo = $self->file_names;
119 8 my $done = 0;
120 8 my $finish;
121 1 ▶ my $guard = Mojo::IOLoop->timer($self->timeout => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
122 1 ▶ $finish->(0, 'swagger-ui fetch timed out') if $finish;
Branch: 1 branch
BranchTrueFalseTotal
if $finish 1 0 1
123 8 });
124  
125 8 ▶ $finish = sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
126 8 my ($ok, $message) = @_;
127 8 ▶ return if $done++;
Branch: 1 branch
BranchTrueFalseTotal
if $done++ 0 8 8
128 8 Mojo::IOLoop->remove($guard);
129 8 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 4 4 8
130 4 $ok = eval { $self->_store($dir, \%body); 1 };
131 4 ▶ $message = 'swagger-ui cache directory is not writable' unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 3 4
132 }
133 8 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 3 5 8
134 3 $self->_failed_at(undef);
135 3 $self->_failed(undef);
136 }
137 else {
138 5 $self->_failed_at(Time::HiRes::time());
139 5 $self->_failed($message);
140 }
141 8 $self->_inflight(0);
142 8 my @waiters = splice @{$self->_waiters};
143 8 for my $w (@waiters) {
144 12 ▶ eval { $w->($ok ? (1) : (0, 'fetch', $message)); 1 };
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 7 5 12
145 }
146 8 return;
147 8 };
148  
149 8 for my $name (@todo) {
150 16 my $url = $SOURCE . $name;
151 16 my $called = 0;
152 16 my $ok = eval {
153 14 ▶ $self->fetcher->($url, sub {
Subroutine __ANON__: called CC=9 SCAR=22.3
154 14 my ($content, $error) = @_;
155 14 ▶ return if $called++ || $done;
Branch: 1 branch
BranchTrueFalseTotal
if $called++ or $done 3 11 14
Condition66%2/3
$called++ or $done
l!l&&r!l&&!r
0311
MC/DC50%1/2
$called++ or $done
$called++ $done
Truth table66%2/3
A or B
A= $called++B= $done
ABresult
1X1
011
000
156 11 ▶ if (!defined $content) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 1 10 11
157 1 ▶ return $finish->(0, "swagger-ui fetch failed: $url: " . ($error // 'no body'));
Condition50%1/2
$error // "no body"
l!l
10
MC/DC0%0/1
$error // "no body"
$error
Truth table50%1/2
A
A= $error
Aresult
11
00
158 }
159 10 ▶ if (length($content) > $FILE{$name}{max}
Branch: 1 branch
BranchTrueFalseTotal
if (length $content > $FILE{$name}{"max"} or &sha256_hex($content) ne ($self->digests->{$name} // "")) 2 8 10
Condition60%3/5
length $content > $FILE{$name}{"max"} or &sha256_hex($content) ne ($self->digests->{$name} // "")
l!l&&r!l&&!r
028
$self->digests->{$name} // ""
l!l
100
MC/DC50%1/2
length $content > $FILE{$name}{"max"} or &sha256_hex($content) ne ($self->digests->{$name} // "")
length $content > $FILE{$name}{"max"} &sha256_hex($content) ne ($self->digests->{$name} // "")
MC/DC0%0/1
$self->digests->{$name} // ""
$self->digests->{$name}
Truth table66%2/3
A or B
A= length $content > $FILE{$name}{"max"}B= &sha256_hex($content) ne ($self->digests->{$name} // "")
ABresult
1X1
011
000
Truth table50%1/2
A
A= $self->digests->{$name}
Aresult
11
00
160 || sha256_hex($content) ne ($self->digests->{$name} // '')) {
161 2 return $finish->(0, "swagger-ui fetch failed: $url: content does not match the pinned digest");
162 }
163 8 $body{$name} = $content;
164 8 ▶ $finish->(1) if keys(%body) == @todo;
Branch: 1 branch
BranchTrueFalseTotal
if keys %body == @todo 4 4 8
165 8 return;
166 16 });
167 16 1;
168 };
169 16 ▶ $finish->(0, "swagger-ui fetch failed: $url: request could not be started") unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 16 16
170 }
171 8 return;
172 }
173  
174 # Written under a temporary name and renamed, so a half-written file never answers a request.
175 # The directory is created private to the process's user.
176 4 ▶sub _store {
Subroutine _store: called CC=3 SCAR=11.0
177 4 my ($self, $dir, $body) = @_;
178 4 my $d = Mojo::File->new($dir);
179 4 $d->make_path({mode => 0700});
180 3 for my $name (sort keys %$body) {
181 6 my $tmp = $d->child(".$name.$$.tmp");
182 6 $tmp->spew($body->{$name});
183 6 ▶ rename "$tmp", $d->child($name)->to_string or croak "rename failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
rename "$tmp", $d->child($name)->to_string or croak("rename failed: $!") 6 0 6
184 }
185 3 return;
186 }
187  
188  
189 9 ▶sub index_html {
Subroutine index_html: called CC=1 SCAR=0.0
Pod documented
190 9 return <<'HTML';
191 <!doctype html>
192 <html lang="en">
193 <head>
194 <meta charset="utf-8">
195 <meta name="viewport" content="width=device-width, initial-scale=1">
196 <title>Skeid API</title>
197 <link rel="stylesheet" href="/docs/swagger-ui.css">
198 </head>
199 <body>
200 <div id="swagger-ui"></div>
201 <script src="/docs/swagger-ui-bundle.js"></script>
202 <script src="/docs/init.js"></script>
203 </body>
204 </html>
205 HTML
206 }
207  
208 1 ▶sub init_js {
Subroutine init_js: called CC=1 SCAR=0.0
Pod documented
209 1 return <<'JS';
210 (function () {
211 var key = null;
212 try { key = window.sessionStorage.getItem('skeid_key'); } catch (e) {}
213  
214 function load(attempt) {
215 var headers = key ? { Authorization: 'Bearer ' + key } : {};
216 return fetch('/openapi.json', { headers: headers }).then(function (res) {
217 if (res.status === 401 && attempt < 2) {
218 var given = window.prompt('Skeid API key');
219 if (!given) { throw new Error('An API key is needed to read this API description'); }
220 key = given;
221 try { window.sessionStorage.setItem('skeid_key', key); } catch (e) {}
222 return load(attempt + 1);
223 }
224 if (!res.ok) { throw new Error('GET /openapi.json answered ' + res.status); }
225 return res.json();
226 });
227 }
228  
229 load(0).then(function (spec) {
230 window.ui = window.SwaggerUIBundle({
231 spec: spec,
232 dom_id: '#swagger-ui',
233 deepLinking: true,
234 requestInterceptor: function (req) {
235 if (key && !(req.headers && req.headers.Authorization)) {
236 req.headers = req.headers || {};
237 req.headers.Authorization = 'Bearer ' + key;
238 }
239 return req;
240 }
241 });
242 }).catch(function (err) {
243 document.getElementById('swagger-ui').textContent = String(err.message || err);
244 });
245 })();
246 JS
247 }
248  
249 86 1;
250  
251 __END__
« lib/Langertha/Skeid/Protocol/Audio.pm ↑ Summary blib/lib/Langertha/Skeid/KeyBroker.pm »