lib/HTTP/Promise/Stream/Brotli.pm

filter: Statementstmt 36.1% 34 / 94 Branchbran 3.1% 1 / 32 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 58.8% 10 / 17 Podpod 100% 6 / 6 totaltotal 32.0% 51 / 159 CC 19 SCAR 35.4
CC
19
Cov
27%
decode
72.0
encode
72.0
decode_bro
6.0
CRAP 34.4
« lib/HTTP/Promise/Stream/LZW.pm ↑ Summary blib/lib/HTTP/Promise/Stream/QuotedPrint.pm »
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Stream/Brotli.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/05/04
7 ## Modified 2023/09/08
8 ## All rights reserved.
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package HTTP::Promise::Stream::Brotli;
15 BEGIN
16 {
17 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶ warnings::register_categories( 'HTTP::Promise' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶ use HTTP::Promise::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶ use parent -norequire, qw( HTTP::Promise::Stream::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶ use vars qw( @EXPORT_OK $VERSION $EXCEPTION_CLASS $BrotliError );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 # use Nice::Try;
24 use constant {
25 3 ENCODE_BUFFER_SIZE => ( 32 * 1024 ),
26 DECODE_BUFFER_SIZE => ( 32 * 1024 ),
27 3 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 our @EXPORT_OK = qw( decode_bro encode_bro );
29 3 our $EXCEPTION_CLASS = 'HTTP::Promise::Exception';
30 3 our $VERSION = 'v0.2.0';
31 };
32  
33 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 sub decode
37 {
38 0 ▶ my $self = shift( @_ );
Subroutine decode: not called CC=8 SCAR=42.8
Pod documented
39 0 my $from = shift( @_ );
40 0 my $to = shift( @_ );
41 0 my $opts = $self->_get_args_as_hash( @_ );
42 0 my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from );
43 0 my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 );
44 0 ▶ return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from_fh and defined $to_fh 0 0 0
Condition0%0/3
defined $from_fh and defined $to_fh
!ll&&!rl&&r
000
MC/DC0%0/2
defined $from_fh and defined $to_fh
defined $from_fh defined $to_fh
Truth table0%0/3
A and B
A= defined $from_fhB= defined $to_fh
ABresult
0X0
100
111
45 0 my( $n, $buff );
46 0 ▶ $self->_load_class( 'IO::Uncompress::Brotli', { no_import => 1 } ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("IO::Uncompress::Brotli", {"no_import", 1}) or return $self->pass_error 0 0 0
47 0 my $c = IO::Uncompress::Brotli->create;
48
49 0 ▶ while( $n = $reader->( $buff, DECODE_BUFFER_SIZE ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($n = &$reader($buff, 32768)) 0 0 0
50 {
51 0 my $decoded = $c->decompress( $buff );
52 # try-catch
53 0 local $@;
54 my $rv = eval
55 0 {
56 0 $writer->( $decoded );
57 };
58 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
59 {
60 0 return( $self->error( "Error decompressing with Brotli: $@" ) );
61 }
62 0 ▶ return( $self->pass_error ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
63 }
64 0 ▶ return( $self->pass_error ) if( !defined( $n ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 0 0
65 0 return( $self );
66 }
67  
68 sub decode_bro
69 {
70 0 ▶ my $s = __PACKAGE__->new;
Subroutine decode_bro: not called CC=2 SCAR=17.9
Pod documented
71 0 my $rv = $s->decode( @_ );
72 0 ▶ if( !defined( $rv ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rv) { } 0 0 0
73 {
74 0 $BrotliError = $s->error;
75 0 return( $s->pass_error );
76 }
77 else
78 {
79 0 undef( $BrotliError );
80 0 return( $rv );
81 }
82 }
83  
84 sub encode
85 {
86 0 ▶ my $self = shift( @_ );
Subroutine encode: not called CC=8 SCAR=42.8
Pod documented
87 0 my $from = shift( @_ );
88 0 my $to = shift( @_ );
89 0 my $opts = $self->_get_args_as_hash( @_ );
90 0 my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from );
91 0 my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 );
92 0 ▶ return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from_fh and defined $to_fh 0 0 0
Condition0%0/3
defined $from_fh and defined $to_fh
!ll&&!rl&&r
000
MC/DC0%0/2
defined $from_fh and defined $to_fh
defined $from_fh defined $to_fh
Truth table0%0/3
A and B
A= defined $from_fhB= defined $to_fh
ABresult
0X0
100
111
93 0 my( $n, $buff );
94 0 ▶ $self->_load_class( 'IO::Compress::Brotli', { no_import => 1 } ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("IO::Compress::Brotli", {"no_import", 1}) or return $self->pass_error 0 0 0
95 0 my $c = IO::Compress::Brotli->create;
96
97 0 ▶ while( $n = $reader->( $buff, ENCODE_BUFFER_SIZE ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($n = &$reader($buff, 32768)) 0 0 0
98 {
99 0 my $encoded = $c->compress( $buff );
100 # try-catch
101 0 local $@;
102 my $rv = eval
103 0 {
104 0 $writer->( $encoded );
105 };
106 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
107 {
108 0 return( $self->error( "Error compressing with Brotli: $@" ) );
109 }
110 0 ▶ return( $self->pass_error ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
111 }
112 0 $c->finish;
113  
114 0 ▶ return( $self->pass_error ) if( !defined( $n ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 0 0
115 0 return( $self );
116 }
117  
118 sub encode_bro
119 {
120 0 ▶ my $s = __PACKAGE__->new;
Subroutine encode_bro: not called CC=2 SCAR=17.9
Pod documented
121 0 my $rv = $s->encode( @_ );
122 0 ▶ if( !defined( $rv ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rv) { } 0 0 0
123 {
124 0 $BrotliError = $s->error;
125 0 return( $s->pass_error );
126 }
127 else
128 {
129 0 undef( $BrotliError );
130 0 return( $rv );
131 }
132 }
133  
134 sub is_decoder_installed
135 {
136 4 ▶ local $@;
Subroutine is_decoder_installed: called CC=2 SCAR=7.0
Pod documented
137 eval
138 4 {
139 4 ▶ local $SIG{__DIE__} = sub{};
Subroutine __ANON__: not called CC=1 SCAR=0.0
140 4 require IO::Uncompress::Brotli;
141 };
142 # eval( 'use IO::Uncompress::Brotli ();' );
143 4 ▶ return( $@ ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 4 4
144 }
145  
146 sub is_encoder_installed
147 {
148 0 ▶ local $@;
Subroutine is_encoder_installed: not called CC=2 SCAR=17.9
Pod documented
149 # eval( 'use IO::Compress::Brotli ();' );
150 eval
151 0 {
152 0 ▶ local $SIG{__DIE__} = sub{};
Subroutine __ANON__: not called CC=1 SCAR=6.9
153 0 require IO::Uncompress::Brotli;
154 };
155 0 ▶ return( $@ ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
156 }
157  
158 # NOTE: sub FREEZE is inherited
159  
160 # NOTE: sub STORABLE_freeze is inherited
161  
162 # NOTE: sub STORABLE_thaw is inherited
163  
164 # NOTE: sub THAW is inherited
165  
166 3 1;
167 # NOTE: POD
168 __END__
169  
170 =encoding utf-8
171  
172 =head1 NAME
173  
174 HTTP::Promise::Stream::Brotli - Stream Encoder for Brotli Encoding
175  
176 =head1 SYNOPSIS
177  
178 use HTTP::Promise::Stream::Brotli;
179 my $s = HTTP::Promise::Stream::Brotli->new ||
180 die( HTTP::Promise::Stream::Brotli->error, "\n" );
181 $s->encode( $input => $output ) ||
182 die( $s->error );
183 $s->decode( $input => $output ) || die( $s->error );
184 HTTP::Promise::Stream::Brotli::encode_bro( $input => $output ) ||
185 die( $HTTP::Promise::Stream::Brotli::BrotliError );
186 HTTP::Promise::Stream::Brotli::decode_bro( $input => $output ) ||
187 die( $HTTP::Promise::Stream::Brotli::BrotliError );
188  
189 =head1 VERSION
190  
191 v0.2.0
192  
193 =head1 DESCRIPTION
194  
195 This implements an encoding and decoding mechanism for Brotli compression using either of the following on input and output:
196  
197 =over 4
198  
199 =item C<filepath>
200  
201 If the parameter is neither a scalar reference nor a file handle, it will be assumed to be a file path.
202  
203 =item C<file handle>
204  
205 This can be a native file handle, or an object oriented one as long as it implements the C<print> or C<write>, and C<read> methods. The C<read> method is expected to return the number of bytes read or C<undef> upon error. The C<print> and C<write> methods are expected to simply return true upon success and C<undef> upon error.
206  
207 Alternatively, those methods can die and those exceptions wil be caught.
208  
209 =item C<scalar reference>
210  
211 This can be a simple scalar reference, or an object scalar reference.
212  
213 =back
214  
215 This module requires L<IO::Compress::Brotli> and L<IO::Uncompress::Brotli> to be installed or it will return an error.
216  
217 Brotli L<is described|https://www.brotli.org/> as "Brotli is a generic-purpose lossless compression algorithm that compresses data using a combination of a modern variant of the LZ77 algorithm, Huffman coding and 2nd order context modeling" that is "similar in speed with deflate" and "supported by L<most web browsers|http://caniuse.com/#search=brotli>, major web servers, and some CDNs". The specification of its Compressed Data Format is defined in L<rfc7932|https://tools.ietf.org/html/rfc7932>.
218  
219 According to L<Mozilla|https://developer.mozilla.org/en-US/docs/Glossary/brotli_compression>, "Brotli provides better compression ratios than gzip and deflate speeds are comparable, but brotli compressing is a slower process than Gzip"
220  
221 =head1 CONSTRUCTOR
222  
223 =head2 new
224  
225 Creates a new L<HTTP::Promise::Stream::Brotli> object and returns it.
226  
227 =head1 METHODS
228  
229 =head2 decode
230  
231 This takes 2 arguments: an input and an output. Each one can be either a file path, a file handle, or a scalar reference.
232  
233 It will decode the Brotli encoded data and write the result into the output.
234  
235 It returns true upon success and sets an L<error|Module::Generic/error> and return C<undef> upon error.
236  
237 =head2 encode
238  
239 This takes 2 arguments: an input and an output. Each one can be either a file path, a file handle, or a scalar reference.
240  
241 It will encode the data into Brotli encoded data and write the result into the output.
242  
243 It returns true upon success and sets an L<error|Module::Generic/error> and return C<undef> upon error.
244  
245 =head1 CLASS FUNCTIONS
246  
247 The following class functions are available and can also be exported, such as:
248  
249 use HTTP::Promise::Stream::Brotli qw( decode_bro encode_bro );
250  
251 =head2 decode_bro
252  
253 This takes the same 2 arguments used in L</decode>: an input and an output. Each one can be either a file path, a file handle, or a scalar reference.
254  
255 It will decode the Brotli encoded data and write the result into the output.
256  
257 It returns true upon success, and upon error, it will set the error in the global variable C<$BrotliError> and return C<undef>
258  
259 my $decoded = HTTP::Promise::Stream::Brotli::decode_bro( $encoded );
260 die( "Something went wrong: $HTTP::Promise::Stream::Brotli::BrotliError\n" if( !defined( $decoded ) );
261 print( "Decoded data is: $decoded\n" );
262  
263 =head2 encode_bro
264  
265 This takes the same 2 arguments used in L</encode>: an input and an output. Each one can be either a file path, a file handle, or a scalar reference.
266  
267 It will encode the data into Brotli encoded data and write the result into the output.
268  
269 It returns true upon success, and upon error, it will set the error in the global variable C<$BrotliError> and return C<undef>
270  
271 my $encoded = HTTP::Promise::Stream::Brotli::encode_bro( $data );
272 die( "Something went wrong: $HTTP::Promise::Stream::Brotli::BrotliError\n" if( !defined( $encoded ) );
273 print( "Encoded data is: $encoded\n" );
274  
275 =head2 is_decoder_installed
276  
277 Returns true if the module L<IO::Uncompress::Brotli> is installed, false otherwise.
278  
279 =head2 is_encoder_installed
280  
281 Returns true if the module L<IO::Compress::Brotli> is installed, false otherwise.
282  
283 =head1 THREAD-SAFETY
284  
285 This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries.
286  
287 =head1 AUTHOR
288  
289 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
290  
291 =head1 SEE ALSO
292  
293 L<IO::Compress::Brotli>, L<IO::Uncompress::Brotli>
294  
295 L<Brotli web page|https://www.brotli.org/>, L<Brotli Github page|https://github.com/google/brotli>
296  
297 L<rfc7932|https://tools.ietf.org/html/rfc7932>
298  
299 L<Wikipedia page|https://en.wikipedia.org/wiki/Brotli>
300  
301 L<caniuse|http://caniuse.com/#search=brotli>
302  
303 L<HTTP::Promise>, L<HTTP::Promise::Request>, L<HTTP::Promise::Response>, L<HTTP::Promise::Message>, L<HTTP::Promise::Entity>, L<HTTP::Promise::Headers>, L<HTTP::Promise::Body>, L<HTTP::Promise::Body::Form>, L<HTTP::Promise::Body::Form::Data>, L<HTTP::Promise::Body::Form::Field>, L<HTTP::Promise::Status>, L<HTTP::Promise::MIME>, L<HTTP::Promise::Parser>, L<HTTP::Promise::IO>, L<HTTP::Promise::Stream>, L<HTTP::Promise::Exception>
304  
305 =head1 COPYRIGHT & LICENSE
306  
307 Copyright(c) 2022 DEGUEST Pte. Ltd.
308  
309 All rights reserved.
310  
311 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
312  
313 =cut
« lib/HTTP/Promise/Stream/LZW.pm ↑ Summary blib/lib/HTTP/Promise/Stream/QuotedPrint.pm »