lib/Lingy/nREPL.pm

filter: Statementstmt 26.6% 49 / 184 Branchbran 0.0% 0 / 66 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 42.8% 15 / 35 Podpod 0.0% 0 / 12 totaltotal 21.2% 65 / 306 CC 39 SCAR 33.5
CC
39
Cov
20%
stop
110.0
_bencode
56.0
new
30.0
CRAP 28.6
« lib/Lingy/Printer.pm ↑ Summary blib/lib/Lingy/Common.pm »
1 2 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Lingy::nREPL;
3  
4 2 ▶use Lingy;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Lingy::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Bencode;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use YAML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use IO::All;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use XXX;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use constant default_log_file => '.nrepl-log';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use constant port_file => $ENV{LINGY_NREPL_PORT_FILE} // '.nrepl-port';
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{"LINGY_NREPL_PORT_FILE"} // ".nrepl-port"
l!l
02
MC/DC0%0/1
$ENV{"LINGY_NREPL_PORT_FILE"} // ".nrepl-port"
$ENV{"LINGY_NREPL_PORT_FILE"}
Truth table50%1/2
A
A= $ENV{"LINGY_NREPL_PORT_FILE"}
Aresult
11
00
18  
19 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod undocumented
20 0 my ($class, %args) = @_;
21  
22 0 srand;
23 0 my $port = int(rand(10000)) + 40000;
24  
25 0 ▶ my $socket = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
my $socket = "IO::Socket::INET"->new("LocalPort", $port, "Proto", "tcp", "Listen", 4096, "Reuse", 1) or die "Can't create socket: $IO::Socket::errstr" 0 0 0
26 LocalPort => $port,
27 Proto => 'tcp',
28 Listen => SOMAXCONN,
29 Reuse => 1,
30 ) or die "Can't create socket: $IO::Socket::errstr";
31  
32 0 $socket->autoflush;
33  
34 0 my $log;
35 0 ▶ if (my $log_file = $ENV{LINGY_NREPL_LOG}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $log_file = $ENV{"LINGY_NREPL_LOG"}) 0 0 0
36 0 ▶ $log_file = default_log_file if $log_file eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $log_file eq 1 0 0 0
37 0 $log = io($log_file);
38 0 ▶ $log->absolute unless $log->is_stdio;
Branch: 1 branch
BranchTrueFalseTotal
unless $log->is_stdio 0 0 0
39 }
40  
41 0 my $self = bless {
42 port => $port,
43 socket => $socket,
44 runtime => Lingy->new,
45 clients => {},
46 sessions => {},
47 yaml => YAML::PP->new(header => 0),
48 log => $log,
49 }, $class;
50  
51 0 return $self;
52 }
53  
54 #------------------------------------------------------------------------------
55 # nREPL server op codes handlers:
56 #------------------------------------------------------------------------------
57 0 ▶sub op_eval {
Subroutine op_eval: not called CC=4 SCAR=30.0
Pod undocumented
58 0 my ($self) = @_;
59  
60 0 ▶ if (my $file = $self->{request}{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $self->{"request"}{"file"}) 0 0 0
61 0 RT->env->set('*file*', STRING->new($file));
62 }
63  
64 0 eval {
65 0 local *STDOUT;
66 tie *STDOUT, 'StreamedOutput',
67 0 ▶ sub { $self->send_response(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 0 'out';
69  
70 0 local *STDERR;
71 tie *STDERR, 'StreamedOutput',
72 0 ▶ sub { $self->send_response(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 'err';
74  
75 0 my $code = $self->{request}->{code};
76 0 ▶ if (my @results = $self->{runtime}->reps($code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@results) = $self->{"runtime"}->reps($code)) 0 0 0
77 0 $self->send_response({
78 value => $results[-1],
79 ns => RT->current_ns_name
80 });
81 }
82 };
83 0 ▶ $self->send_response({err => $@}) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
84  
85 0 $self->send_response({status => 'done'});
86 }
87  
88 0 ▶sub op_clone {
Subroutine op_clone: not called CC=2 SCAR=17.9
Pod undocumented
89 0 my ($self) = @_;
90  
91 my $session_to_clone = exists $self->{request}{session}
92 ? $self->{request}{session}
93 0 ▶ : 'default';
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'request'}{'session'} ? : 0 0 0
94  
95 0 my $session_id = Data::UUID->new->create_str();
96  
97 0 my %cloned_session = %{ $self->{sessions}->{$session_to_clone} };
98 0 $self->{sessions}->{$session_id} = \%cloned_session;
99  
100 0 $self->send_response({
101 'new-session' => $session_id,
102 status => 'done',
103 });
104 }
105  
106 0 ▶sub op_describe {
Subroutine op_describe: not called CC=1 SCAR=6.9
Pod undocumented
107 0 my ($self) = @_;
108  
109 0 my %ops = map {($_ => +{})}
110 qw(eval clone describe close);
111  
112 0 $self->send_response({
113 ops => { %ops },
114 status => 'done',
115 });
116 }
117  
118 0 ▶sub op_close {
Subroutine op_close: not called CC=3 SCAR=24.8
Pod undocumented
119 0 my ($self) = @_;
120  
121  
122 my $session_to_close = $self->{request}{session} or
123 0 ▶ return $self->send_response({
Branch: 1 branch
BranchTrueFalseTotal
my $session_to_close = $self->{"request"}{"session"} or return $self->send_response({"status", "error", "error", "No session specified to close"}) 0 0 0
124 status => 'error',
125 error => "No session specified to close",
126 });
127  
128 0 ▶ $self->{sessions}{$session_to_close} or
Branch: 1 branch
BranchTrueFalseTotal
$self->{"sessions"}{$session_to_close} or return $self->send_response({"status", "error", "error", "No such session: '${session_to_close}'"}) 0 0 0
129 return $self->send_response({
130 status => 'error',
131 error => "No such session: '$session_to_close'",
132 });
133  
134 0 delete $self->{sessions}{$session_to_close};
135  
136 0 $self->send_response({status => 'done'});
137 }
138  
139 #------------------------------------------------------------------------------
140 # Starting and stopping server:
141 #------------------------------------------------------------------------------
142 0 ▶sub start {
Subroutine start: not called CC=3 SCAR=24.8
Pod undocumented
143 0 my ($self) = @_;
144  
145 0 $self->{sessions}{default} = {};
146  
147 0 my $port = $self->{port};
148  
149 0 io(port_file)->print($port);
150  
151 0 print "nREPL server started on port $port " .
152 "on host 127.0.0.1 - nrepl://127.0.0.1:$port\n";
153  
154 0 ▶ if (my $log = $self->{log}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $log = $self->{"log"}) 0 0 0
155 0 ▶ print "Log file: $log\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $log->is_stdio 0 0 0
156 unless $log->is_stdio;
157 }
158  
159 $self->log({
160 0 '===' => 'START',
161 'url' => "nrepl://127.0.0.1:$port",
162 });
163  
164 0 $self->{select} = IO::Select->new($self->{socket});
165  
166 0 ▶ $SIG{INT} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
167 0 $self->log({
168 '===' => 'INTERUPT',
169 });
170 0 $self->stop;
171 0 exit 0;
172 0 };
173  
174 0 return $self;
175 }
176  
177 0 ▶sub run {
Subroutine run: not called CC=5 SCAR=34.0
Pod undocumented
178 0 my ($self) = @_;
179  
180 0 my $select = $self->{select};
181 0 my $client = 0;
182  
183 0 while (1) {
184 0 foreach my $socket ($select->can_read(0.01)) {
185 0 delete @{$self}{qw( conn request )};
186  
187 0 ▶ if ($socket == $self->{socket}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($socket == $self->{"socket"}) 0 0 0
188 0 my $connection = $self->{socket}->accept;
189 0 $self->{clients}->{$connection} = ++$client;
190 0 $select->add($connection);
191 0 $self->log({
192 '===' => 'CONNECT',
193 client => $client,
194 });
195 0 next;
196 }
197  
198 0 ▶ my ($request, $buffer, $length) =
Branch: 1 branch
BranchTrueFalseTotal
() = $self->next_request($socket, $client) or next 0 0 0
199 $self->next_request($socket, $client)
200 or next;
201  
202 0 my $op = $request->{op};
203 0 my $client_id = $self->{clients}->{$socket};
204  
205 0 $self->log({
206 '-->' => ":op $op, :client $client_id",
207 buffer => "$length: $buffer",
208 request => $request,
209 });
210  
211 0 my $handler = "op_$op";
212 0 ▶ if ($self->can($handler)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($handler)) { } 0 0 0
213 0 @{$self}{qw( conn request )} = (
214 $socket,
215 $request,
216 );
217 0 $self->$handler;
218  
219 } else {
220 0 $self->log({
221 '???' => $op,
222 client => $client_id,
223 error => "Unsupported op: '$op'",
224 });
225 }
226 }
227 }
228 }
229  
230 0 ▶sub stop {
Subroutine stop: not called CC=10 SCAR=47.0
Pod undocumented
231 0 my ($self) = @_;
232  
233 0 ▶ return unless defined $self->{select};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"select"} 0 0 0
234  
235 0 ▶ if (-e port_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e ".nrepl-port") 0 0 0
236 0 ▶ unlink port_file
Branch: 1 branch
BranchTrueFalseTotal
unlink ".nrepl-port" or warn qq[Could not unlink '${\".nrepl-port";}' file: $!] 0 0 0
237 0 or warn "Could not unlink '${\ port_file}' file: $!";
238 }
239  
240 $self->log({
241 0 '===' => 'STOP',
242 'url' => "nrepl://127.0.0.1:$self->{port}",
243 });
244  
245 0 foreach my $client ($self->{select}->handles) {
246 0 ▶ if ($client != $self->{socket}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client != $self->{"socket"}) 0 0 0
247 0 $self->{select}->remove($client);
248 0 ▶ shutdown($client, 2)
Branch: 1 branch
BranchTrueFalseTotal
shutdown $client, 2 or warn "Couldn't properly shut down a client connection: $!" 0 0 0
249 or warn "Couldn't properly shut down a client connection: $!";
250 0 ▶ close $client
Branch: 1 branch
BranchTrueFalseTotal
close $client or warn "Couldn't close a client connection: $!" 0 0 0
251 or warn "Couldn't close a client connection: $!";
252 }
253 }
254  
255 0 $self->{select}->remove($self->{socket});
256  
257 0 ▶ if ($self->{socket}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"socket"}) 0 0 0
258 close $self->{socket}
259 0 ▶ or warn "Couldn't close the server socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $self->{"socket"} or warn "Couldn't close the server socket: $!" 0 0 0
260 0 $self->{socket} = undef;
261 }
262  
263 0 $self->{select} = undef;
264 }
265  
266 0 ▶sub next_request {
Subroutine next_request: not called CC=3 SCAR=24.8
Pod undocumented
267 0 my ($self, $socket, $client) = @_;
268 0 my $buffer;
269 0 ▶ my $length = sysread($socket, $buffer, 65535)
Branch: 1 branch
BranchTrueFalseTotal
my $length = sysread($socket, $buffer, 65535) or return $self->close_socket($socket, $client) 0 0 0
270 or return $self->close_socket($socket, $client);
271 0 my $request;
272 0 eval {
273 0 $request = Bencode::bdecode($buffer, 1);
274 };
275 0 ▶ die "Error decoding request buffer:\n$buffer\n$@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
276 0 return ($request, $buffer, $length);
277 }
278  
279 0 ▶sub close_socket {
Subroutine close_socket: not called CC=1 SCAR=6.9
Pod undocumented
280 0 my ($self, $socket, $client) = @_;
281 # Connection closed by client
282 0 my $client_id = $self->{clients}->{$socket};
283 0 delete $self->{clients}->{$socket};
284 0 $self->{select}->remove($socket);
285 0 close($socket);
286 0 $self->log({
287 '===' => 'CLOSED',
288 client => $client,
289 });
290 0 return;
291 }
292  
293 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
294 0 my ($self) = @_;
295 0 $self->stop;
296 }
297  
298 #------------------------------------------------------------------------------
299 # nREPL server response methods:
300 #------------------------------------------------------------------------------
301  
302 0 ▶sub send_response {
Subroutine send_response: not called CC=2 SCAR=17.9
Pod undocumented
303 0 my ($self, $data) = @_;
304 my ($conn, $request) =
305 0 @{$self}{qw(conn request)};
306  
307 my $response = {
308 id => $request->{id},
309 0 ▶ $request->{session} ? (session => $request->{session}) : (),
Branch: 1 branch
BranchTrueFalseTotal
$request->{'session'} ? : 0 0 0
310 %$data,
311 };
312  
313 0 print $conn Bencode::bencode($response);
314  
315 0 $self->log({
316 '<--' => ":op $request->{op}, :client $self->{clients}{$conn}",
317 response => $response,
318 });
319  
320 0 return;
321 }
322  
323 #------------------------------------------------------------------------------
324 # Logging
325 #------------------------------------------------------------------------------
326  
327 0 ▶sub log {
Subroutine log: not called CC=3 SCAR=24.8
Pod undocumented
328 0 my ( $self, $data ) = @_;
329 0 ▶ my $log = $self->{log} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $log = $self->{"log"} or return 0 0 0
330 0 my $yaml = $self->{yaml}->dump_string($data);
331 0 $log->print( $yaml . "\n" );
332 0 ▶ $log->autoflush unless $log->is_stdio;
Branch: 1 branch
BranchTrueFalseTotal
unless $log->is_stdio 0 0 0
333 }
334  
335 #------------------------------------------------------------------------------
336 # Tied class to capture stdio during Lingy evals
337 #------------------------------------------------------------------------------
338 {
339 package StreamedOutput;
340  
341 0 ▶ sub TIEHANDLE {
Subroutine TIEHANDLE: not called CC=1 SCAR=6.9
342 0 my ($class, $send_response, $output_type) = @_;
343 0 bless {
344 send_response => $send_response,
345 output_type => $output_type,
346 }, $class;
347 }
348  
349 0 ▶ sub PRINT {
Subroutine PRINT: not called CC=2 SCAR=17.9
350 0 my ( $self, @args ) = @_;
351 0 ▶ my $output_type = $self->{output_type} // 'out';
Condition0%0/2
$self->{"output_type"} // "out"
l!l
00
MC/DC0%0/1
$self->{"output_type"} // "out"
$self->{"output_type"}
Truth table0%0/2
A
A= $self->{"output_type"}
Aresult
11
00
352 0 $self->{send_response}->({$output_type => join('', @args)});
353 }
354  
355 0 ▶ sub PRINTF {
Subroutine PRINTF: not called CC=1 SCAR=6.9
356 0 my ( $self, $format, @args ) = @_;
357 0 $self->PRINT(sprintf $format, @args);
358 }
359 }
360  
361 #------------------------------------------------------------------------------
362 # Hot patch Bencode to encode numbers as strings
363 #------------------------------------------------------------------------------
364 {
365 2 package Bencode;
366 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
367 2 our ( $DEBUG, $do_lenient_decode, $max_depth, $undef_encoding );
368 0 ▶ sub _bencode {
Subroutine _bencode: not called CC=7 SCAR=40.3
369 map
370 +( ( not defined ) ? ( $undef_encoding or croak 'unhandled data type' )
371 #: ( not ref ) ? ( m/\A (?: 0 | -? [1-9] \d* ) \z/x ? 'i' . $_ . 'e' : length . ':' . $_ )
372 : ( not ref ) ? length . ':' . $_
373 : ( 'SCALAR' eq ref ) ? ( length $$_ ) . ':' . $$_ # escape hatch -- use this to avoid num/str heuristics
374 : ( 'ARRAY' eq ref ) ? 'l' . ( join '', _bencode @$_ ) . 'e'
375 0 ▶ : ( 'HASH' eq ref ) ? 'd' . do { my @k = sort keys %$_; join '', map +( length $k[0] ) . ':' . ( shift @k ) . $_, _bencode @$_{ @k } } . 'e'
Branch: 5 branches
BranchTrueFalseTotal
!defined($_) ? : 0 0 0
!ref($_) ? : 0 0 0
'SCALAR' eq ref $_ ? : 0 0 0
'ARRAY' eq ref $_ ? : 0 0 0
'HASH' eq ref $_ ? : 0 0 0
Condition0%0/2
$Bencode::undef_encoding or croak("unhandled data type")
l!l
00
MC/DC0%0/1
$Bencode::undef_encoding or croak("unhandled data type")
$Bencode::undef_encoding
Truth table0%0/2
A
A= $Bencode::undef_encoding
Aresult
11
00
376 : croak 'unhandled data type'
377 ), @_
378 }
379 }
380  
381 2 1;
« lib/Lingy/Printer.pm ↑ Summary blib/lib/Lingy/Common.pm »