| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Net::NodeTransformator; |
|
2
|
|
|
|
|
|
|
# ABSTRACT: interface to node transformator |
|
3
|
|
|
|
|
|
|
|
|
4
|
2
|
|
|
2
|
|
199606
|
use strict; |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
114
|
|
|
5
|
2
|
|
|
2
|
|
13
|
use warnings; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
91
|
|
|
6
|
2
|
|
|
2
|
|
3115
|
use AnyEvent; |
|
|
2
|
|
|
|
|
14391
|
|
|
|
2
|
|
|
|
|
70
|
|
|
7
|
2
|
|
|
2
|
|
2417
|
use AnyEvent::Handle; |
|
|
2
|
|
|
|
|
50755
|
|
|
|
2
|
|
|
|
|
58
|
|
|
8
|
2
|
|
|
2
|
|
1851
|
use AnyEvent::Socket; |
|
|
2
|
|
|
|
|
36405
|
|
|
|
2
|
|
|
|
|
334
|
|
|
9
|
2
|
|
|
2
|
|
1610
|
use AnyEvent::Proc 0.101; |
|
|
2
|
|
|
|
|
30679
|
|
|
|
2
|
|
|
|
|
155
|
|
|
10
|
2
|
|
|
2
|
|
29
|
use Try::Tiny; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
166
|
|
|
11
|
2
|
|
|
2
|
|
669
|
use Env::Path; |
|
|
2
|
|
|
|
|
3026
|
|
|
|
2
|
|
|
|
|
23
|
|
|
12
|
2
|
|
|
2
|
|
2400
|
use File::Temp qw(tempdir); |
|
|
2
|
|
|
|
|
42826
|
|
|
|
2
|
|
|
|
|
206
|
|
|
13
|
2
|
|
|
2
|
|
36
|
use POSIX qw(getcwd); |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
21
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = '0.103'; # VERSION |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub new { |
|
19
|
0
|
|
|
0
|
1
|
|
my ($class, $hostport) = @_; |
|
20
|
0
|
0
|
|
|
|
|
if ($hostport !~ m{:}) { |
|
21
|
0
|
0
|
|
|
|
|
if ($hostport =~ m{^\d+$}) { |
|
22
|
0
|
|
|
|
|
|
$hostport = "localhost:$hostport"; |
|
23
|
|
|
|
|
|
|
} else { |
|
24
|
0
|
|
|
|
|
|
$hostport = "unix/:$hostport"; |
|
25
|
|
|
|
|
|
|
} |
|
26
|
|
|
|
|
|
|
} |
|
27
|
0
|
|
|
|
|
|
my ($host, $port) = parse_hostport($hostport); |
|
28
|
0
|
0
|
0
|
|
|
|
if ($host eq 'unix/' and $port !~ m{^/}) { |
|
29
|
0
|
|
|
|
|
|
$port = getcwd.'/'.$port; |
|
30
|
|
|
|
|
|
|
} |
|
31
|
|
|
|
|
|
|
bless { |
|
32
|
0
|
|
0
|
|
|
|
host => $host, |
|
33
|
|
|
|
|
|
|
port => $port, |
|
34
|
|
|
|
|
|
|
} => ref $class || $class; |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub standalone { |
|
39
|
0
|
|
|
0
|
1
|
|
my ($class, $hostport) = @_; |
|
40
|
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
my $bin = 'transformator'; |
|
42
|
0
|
|
|
|
|
|
my ($path) = Env::Path->PATH->Whence($bin); |
|
43
|
|
|
|
|
|
|
|
|
44
|
0
|
0
|
|
|
|
|
unless ($hostport) { |
|
45
|
0
|
|
|
|
|
|
my $tmpdir = tempdir(CLEANUP => 1); |
|
46
|
0
|
|
|
|
|
|
$hostport = "$tmpdir/~sock"; |
|
47
|
|
|
|
|
|
|
} |
|
48
|
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
my $errstr; |
|
50
|
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
my $ok = 0; |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $server = AnyEvent::Proc->new( |
|
54
|
|
|
|
|
|
|
bin => $path, |
|
55
|
|
|
|
|
|
|
args => [ $hostport ], |
|
56
|
|
|
|
|
|
|
rtimeout => 10, |
|
57
|
|
|
|
|
|
|
errstr => \$errstr, |
|
58
|
0
|
|
|
0
|
|
|
on_rtimeout => sub { shift->kill; $ok = 0 }, |
|
|
0
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
); |
|
60
|
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
while (local $_ = $server->readline) { |
|
62
|
0
|
|
|
|
|
|
AE::log debug => $_; |
|
63
|
0
|
0
|
|
|
|
|
if (/server bound/) { |
|
64
|
0
|
|
|
|
|
|
$ok = 1; |
|
65
|
0
|
|
|
|
|
|
last; |
|
66
|
|
|
|
|
|
|
} |
|
67
|
|
|
|
|
|
|
} |
|
68
|
|
|
|
|
|
|
|
|
69
|
0
|
0
|
|
|
|
|
AE::log error => $errstr if $errstr; |
|
70
|
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
$server->stop_rtimeout; |
|
72
|
|
|
|
|
|
|
|
|
73
|
0
|
0
|
|
|
|
|
unless ($ok) { |
|
74
|
0
|
|
|
|
|
|
$server->fire_and_kill(10); |
|
75
|
0
|
|
|
|
|
|
AE::log fatal => 'standalone service not started'; |
|
76
|
0
|
|
|
|
|
|
return undef; |
|
77
|
|
|
|
|
|
|
} else { |
|
78
|
0
|
|
|
|
|
|
my $client = $class->new($hostport); |
|
79
|
0
|
|
|
|
|
|
$client->{_server} = $server; |
|
80
|
0
|
|
|
|
|
|
return $client; |
|
81
|
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub cleanup { |
|
86
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
|
87
|
0
|
0
|
|
|
|
|
if (exists $self->{_server}) { |
|
88
|
0
|
|
|
|
|
|
my $server = delete $self->{_server}; |
|
89
|
0
|
|
|
|
|
|
$server->fire_and_kill(10); |
|
90
|
|
|
|
|
|
|
} else { |
|
91
|
0
|
|
|
|
|
|
AE::log note => "$self->cleanup called when no standalone server active"; |
|
92
|
|
|
|
|
|
|
} |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub transform_cv($%) { |
|
98
|
0
|
|
|
0
|
1
|
|
my ($self, %options) = @_; |
|
99
|
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
my $cv = AE::cv; |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
my $err = sub { |
|
103
|
0
|
|
|
0
|
|
|
$options{on_error}->(@_); |
|
104
|
0
|
|
|
|
|
|
$cv->send(undef); |
|
105
|
0
|
|
|
|
|
|
}; |
|
106
|
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
my $host = $self->{host}; |
|
108
|
0
|
|
|
|
|
|
my $port = $self->{port}; |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
tcp_connect ($host, $port, sub { |
|
111
|
0
|
0
|
|
0
|
|
|
return $err->("Connect to $host:$port failed: $!") unless @_; |
|
112
|
0
|
|
|
|
|
|
my ($fh) = @_; |
|
113
|
0
|
|
|
|
|
|
my $AEH; |
|
114
|
|
|
|
|
|
|
$AEH = AnyEvent::Handle->new( |
|
115
|
|
|
|
|
|
|
fh => $fh, |
|
116
|
|
|
|
|
|
|
on_error => sub { |
|
117
|
0
|
|
|
|
|
|
my ($handle, $fatal, $message) = @_; |
|
118
|
0
|
|
|
|
|
|
$handle->destroy; |
|
119
|
0
|
|
|
|
|
|
$err->("Socket error: $message"); |
|
120
|
|
|
|
|
|
|
}, |
|
121
|
|
|
|
|
|
|
on_eof => sub { |
|
122
|
0
|
|
|
|
|
|
$AEH->destroy; |
|
123
|
|
|
|
|
|
|
}, |
|
124
|
0
|
|
|
|
|
|
); |
|
125
|
|
|
|
|
|
|
$AEH->push_read(cbor => sub { |
|
126
|
0
|
|
|
|
|
|
my $answer = $_[1]; |
|
127
|
0
|
0
|
0
|
|
|
|
if (defined $answer and ref $answer eq 'HASH') { |
|
128
|
0
|
0
|
|
|
|
|
if (exists $answer->{error}) { |
|
|
|
0
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
|
$err->("Service error: ".$answer->{error}); |
|
130
|
|
|
|
|
|
|
} elsif (exists $answer->{result}) { |
|
131
|
0
|
|
|
|
|
|
$cv->send($answer->{result}); |
|
132
|
|
|
|
|
|
|
} else { |
|
133
|
0
|
|
|
|
|
|
$err->("Something is wrong: no result and no error"); |
|
134
|
|
|
|
|
|
|
} |
|
135
|
|
|
|
|
|
|
} else { |
|
136
|
0
|
|
|
|
|
|
$err->("No answer"); |
|
137
|
|
|
|
|
|
|
} |
|
138
|
0
|
|
|
|
|
|
}); |
|
139
|
0
|
|
0
|
|
|
|
$AEH->push_write(cbor => [ $options{engine}, $options{input}, $options{data} || {} ]); |
|
140
|
0
|
|
|
|
|
|
}); |
|
141
|
0
|
|
|
|
|
|
$cv; |
|
142
|
|
|
|
|
|
|
} |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub transform($$$;$) { |
|
146
|
0
|
|
|
0
|
1
|
|
my ($self, $engine, $input, $data) = @_; |
|
147
|
0
|
|
|
|
|
|
my $error; |
|
148
|
|
|
|
|
|
|
my $result = $self->transform_cv( |
|
149
|
0
|
|
|
0
|
|
|
on_error => sub { $error = shift }, |
|
150
|
0
|
|
|
|
|
|
engine => $engine, |
|
151
|
|
|
|
|
|
|
input => $input, |
|
152
|
|
|
|
|
|
|
data => $data, |
|
153
|
|
|
|
|
|
|
)->recv; |
|
154
|
0
|
0
|
0
|
|
|
|
return $result if defined $result and not defined $error and not ref $result; |
|
|
|
|
0
|
|
|
|
|
|
155
|
0
|
0
|
0
|
|
|
|
if (not defined $result and defined $error) { |
|
156
|
0
|
|
|
|
|
|
AE::log error => $error; |
|
157
|
|
|
|
|
|
|
} else { |
|
158
|
0
|
|
|
|
|
|
AE::log error => "Something is wrong: $@"; |
|
159
|
|
|
|
|
|
|
} |
|
160
|
|
|
|
|
|
|
} |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
|
|
163
|
0
|
|
|
0
|
1
|
|
sub jade ($$;$) { shift->transform(jade => @_) } |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
|
|
166
|
0
|
|
|
0
|
1
|
|
sub coffeescript ($$;$) { shift->transform(coffeescript => @_) } |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
|
|
169
|
0
|
|
|
0
|
1
|
|
sub minify_html ($$;$) { shift->transform(minify_html => @_) } |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
|
|
172
|
0
|
|
|
0
|
1
|
|
sub minify_css ($$;$) { shift->transform(minify_css => @_) } |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
|
|
175
|
0
|
|
|
0
|
1
|
|
sub minify_js ($$;$) { shift->transform(minify_js => @_) } |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
1; |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
__END__ |