File Coverage

blib/lib/Chrome/DevToolsProtocol/Transport/NetAsync.pm
Criterion Covered Total %
statement 24 72 33.3
branch 0 6 0.0
condition 0 5 0.0
subroutine 8 16 50.0
pod 1 5 20.0
total 33 104 31.7


line stmt bran cond sub pod time code
1             package Chrome::DevToolsProtocol::Transport::NetAsync;
2 1     1   6 use strict;
  1         2  
  1         35  
3 1     1   5 use Moo 2;
  1         28  
  1         10  
4 1     1   362 use Filter::signatures;
  1         2  
  1         7  
5 1     1   29 no warnings 'experimental::signatures';
  1         2  
  1         42  
6 1     1   5 use feature 'signatures';
  1         2  
  1         99  
7 1     1   7 use Scalar::Util 'weaken';
  1         2  
  1         64  
8 1     1   1886 use IO::Async::Loop;
  1         25268  
  1         46  
9              
10 1     1   611 use Net::Async::WebSocket::Client;
  1         51126  
  1         657  
11             Net::Async::WebSocket::Client->VERSION(0.12); # fixes some errors with masked frames
12              
13             our $VERSION = '0.70';
14              
15             =head1 NAME
16              
17             Chrome::DevToolsProtocol::Transport::NetAsync - IO::Async backend for Chrome communication
18              
19             =head1 SYNOPSIS
20              
21             my $got_endpoint = Future->done( "ws://..." );
22             my $t = Chrome::DevToolsProtocol::Transport::NetAsync->new;
23             $t->connect( $handler, $got_endpoint, $logger)
24             ->then(sub {
25             my( $connection ) = @_;
26             print "We are connected\n";
27             });
28              
29             =cut
30              
31             has 'type' => (
32             is => 'ro',
33             default => 'websocket'
34             );
35              
36             has 'loop' => (
37             is => 'lazy',
38             default => sub { IO::Async::Loop->new() },
39             );
40              
41             has 'connection' => (
42             is => 'rw',
43             );
44              
45 0     0 0   sub connect( $self, $handler, $got_endpoint, $logger ) {
  0            
  0            
  0            
  0            
  0            
46 0   0 0     $logger ||= sub{};
47 0           weaken $handler;
48              
49 0           my $client;
50 0     0     $got_endpoint->then( sub( $endpoint ) {
  0            
  0            
51             $client = Net::Async::WebSocket::Client->new(
52             # Kick off the continous polling
53             on_frame => sub {
54 0           my( $connection, $message )=@_;
55 0 0         if( $handler ) { # may have gone away already
56 0           $handler->on_response( $connection, $message )
57             };
58             },
59             on_read_eof => sub {
60 0           my( $connection )=@_;
61 0           $logger->('info', "Connection closed");
62             # TODO: should we tell handler?
63             },
64 0           );
65              
66             # Patch unlimited frame size into the client so we can receive large
67             # buffers. This should become an RT ticket against Net::Async::WebSocket::Client
68 0           $client->{framebuffer} = Protocol::WebSocket::Frame->new(
69             max_payload_size => undef
70             );
71              
72 0           $self->loop->add( $client );
73 0   0       $self->{connection} ||= $client;
74              
75 0 0         die "Got an undefined endpoint" unless defined $endpoint;
76 0           $logger->('debug',"Connecting to $endpoint");
77             $client->connect( url => $endpoint, on_connected => sub {
78 0           $logger->('info',"Connected to $endpoint");
79 0           } );
80             })->catch(sub{
81             #require Data::Dumper;
82             #warn "caught";
83             #warn Data::Dumper::Dumper( \@_ );
84 0     0     Future->fail( @_ );
85 0           });
86             }
87              
88 0     0 0   sub send( $self, $message ) {
  0            
  0            
  0            
89 0           $self->connection->send_text_frame( $message )
90             }
91              
92 0     0 0   sub close( $self ) {
  0            
  0            
93 0           my $c = delete $self->{connection};
94 0 0         if( $c) {
95 0           $c->close
96             };
97             }
98              
99 0     0 0   sub future( $self ) {
  0            
  0            
100 0           my $res = $self->loop->new_future;
101 0           return $res
102             }
103              
104             =head2 C<< $transport->sleep( $seconds ) >>
105              
106             $transport->sleep( 10 )->get; # wait for 10 seconds
107              
108             Returns a Future that will be resolved in the number of seconds given.
109              
110             =cut
111              
112 0     0 1   sub sleep( $self, $seconds ) {
  0            
  0            
  0            
113 0           $self->loop->delay_future( after => $seconds )
114             }
115              
116             1;
117              
118             =head1 REPOSITORY
119              
120             The public repository of this module is
121             L<https://github.com/Corion/www-mechanize-chrome>.
122              
123             =head1 SUPPORT
124              
125             The public support forum of this module is L<https://perlmonks.org/>.
126              
127             =head1 BUG TRACKER
128              
129             Please report bugs in this module via the Github bug queue at
130             L<https://github.com/Corion/WWW-Mechanize-Chrome/issues>
131              
132             =head1 AUTHOR
133              
134             Max Maischein C<corion@cpan.org>
135              
136             =head1 COPYRIGHT (c)
137              
138             Copyright 2010-2023 by Max Maischein C<corion@cpan.org>.
139              
140             =head1 LICENSE
141              
142             This module is released under the same terms as Perl itself.
143              
144             =cut