| 1 | package PAGI::Test::WebSocket; | ||
| 2 | $PAGI::Test::WebSocket::VERSION = '0.002001'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Future::AsyncAwait; | ||
| 6 | use Future; | ||
| 7 | use Carp qw(croak); | ||
| 8 | |||
| 9 | |||
| 10 | sub new { | ||
| 11 | my ($class, %args) = @_; | ||
| 12 | |||
| 13 | croak "app is required" unless $args{app}; | ||
| 14 | croak "scope is required" unless $args{scope}; | ||
| 15 | |||
| 16 | return bless { | ||
| 17 | app => $args{app}, | ||
| 18 | scope => $args{scope}, | ||
| 19 | send_queue => [], # Messages from test -> app | ||
| 20 | recv_queue => [], # Messages from app -> test | ||
| 21 | closed => 0, | ||
| 22 | accepted => 0, | ||
| 23 | close_code => undef, | ||
| 24 | close_reason => '', | ||
| 25 | _pending_receives => [], # Pending receive futures | ||
| 26 | }, $class; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub _start { | ||
| 30 | my ($self) = @_; | ||
| 31 | |||
| 32 | # Create receive coderef for the app | ||
| 33 | my $receive = async sub { | ||
| 34 | # First call returns websocket.connect | ||
| 35 | if (!$self->{_connect_sent}) { | ||
| 36 | $self->{_connect_sent} = 1; | ||
| 37 | return { type => 'websocket.connect' }; | ||
| 38 | } | ||
| 39 | |||
| 40 | # Return queued message if available | ||
| 41 | if (@{$self->{send_queue}}) { | ||
| 42 | return shift @{$self->{send_queue}}; | ||
| 43 | } | ||
| 44 | |||
| 45 | # Return disconnect if closed | ||
| 46 | if ($self->{closed}) { | ||
| 47 | return { type => 'websocket.disconnect', code => $self->{close_code} // 1000 }; | ||
| 48 | } | ||
| 49 | |||
| 50 | # Create a future that will be resolved when data arrives | ||
| 51 | my $future = Future->new; | ||
| 52 | push @{$self->{_pending_receives}}, $future; | ||
| 53 | return await $future; | ||
| 54 | }; | ||
| 55 | |||
| 56 | # Create send coderef for the app | ||
| 57 | my $send = async sub { | ||
| 58 | my ($event) = @_; | ||
| 59 | |||
| 60 | if ($event->{type} eq 'websocket.accept') { | ||
| 61 | $self->{accepted} = 1; | ||
| 62 | } | ||
| 63 | elsif ($event->{type} eq 'websocket.send') { | ||
| 64 | push @{$self->{recv_queue}}, $event; | ||
| 65 | } | ||
| 66 | elsif ($event->{type} eq 'websocket.close') { | ||
| 67 | $self->{closed} = 1; | ||
| 68 | $self->{close_code} = $event->{code} // 1000; | ||
| 69 | $self->{close_reason} = $event->{reason} // ''; | ||
| 70 | } | ||
| 71 | |||
| 72 | return; | ||
| 73 | }; | ||
| 74 | |||
| 75 | # Start the app future but don't block on it | ||
| 76 | $self->{app_future} = $self->{app}->($self->{scope}, $receive, $send); | ||
| 77 | |||
| 78 | # Wait for acceptance (the first two awaits in the app should complete immediately) | ||
| 79 | # This is a bit hacky but works: we need to let the app run until it accepts | ||
| 80 | $self->_pump_app; | ||
| 81 | |||
| 82 | croak "WebSocket connection not accepted" unless $self->{accepted}; | ||
| 83 | |||
| 84 | return $self; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _pump_app { | ||
| 88 | my ($self) = @_; | ||
| 89 | |||
| 90 | # This pumps the app future by checking if it's waiting on a receive | ||
| 91 | # If there are pending receives and we have data, resolve them | ||
| 92 | while (@{$self->{_pending_receives}} && @{$self->{send_queue}}) { | ||
| 93 | my $future = shift @{$self->{_pending_receives}}; | ||
| 94 | my $event = shift @{$self->{send_queue}}; | ||
| 95 | $future->done($event); | ||
| 96 | } | ||
| 97 | |||
| 98 | # If closed and there are pending receives, resolve them with disconnect | ||
| 99 | if ($self->{closed} && @{$self->{_pending_receives}}) { | ||
| 100 | while (my $future = shift @{$self->{_pending_receives}}) { | ||
| 101 | $future->done({ type => 'websocket.disconnect', code => $self->{close_code} // 1000 }); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | sub send_text { | ||
| 107 | my ($self, $text) = @_; | ||
| 108 | |||
| 109 | croak "Cannot send on closed WebSocket" if $self->{closed}; | ||
| 110 | |||
| 111 | push @{$self->{send_queue}}, { | ||
| 112 | type => 'websocket.receive', | ||
| 113 | text => $text, | ||
| 114 | }; | ||
| 115 | |||
| 116 | # Pump the app to process this message | ||
| 117 | $self->_pump_app; | ||
| 118 | |||
| 119 | return $self; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub send_bytes { | ||
| 123 | my ($self, $bytes) = @_; | ||
| 124 | |||
| 125 | croak "Cannot send on closed WebSocket" if $self->{closed}; | ||
| 126 | |||
| 127 | push @{$self->{send_queue}}, { | ||
| 128 | type => 'websocket.receive', | ||
| 129 | bytes => $bytes, | ||
| 130 | }; | ||
| 131 | |||
| 132 | # Pump the app to process this message | ||
| 133 | $self->_pump_app; | ||
| 134 | |||
| 135 | return $self; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub send_json { | ||
| 139 | my ($self, $data) = @_; | ||
| 140 | |||
| 141 | require JSON::MaybeXS; | ||
| 142 | my $text = JSON::MaybeXS::encode_json($data); | ||
| 143 | |||
| 144 | return $self->send_text($text); | ||
| 145 | } | ||
| 146 | |||
| 147 | sub receive_text { | ||
| 148 | my ($self, $timeout) = @_; | ||
| 149 | $timeout //= 5; | ||
| 150 | |||
| 151 | # Check if we have a text message already waiting | ||
| 152 | for my $i (0 .. $#{$self->{recv_queue}}) { | ||
| 153 | my $event = $self->{recv_queue}[$i]; | ||
| 154 | if ($event->{type} eq 'websocket.send' && exists $event->{text}) { | ||
| 155 | splice @{$self->{recv_queue}}, $i, 1; | ||
| 156 | return $event->{text}; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | # Check if connection closed | ||
| 161 | return undef if $self->{closed}; | ||
| 162 | |||
| 163 | # No message available yet | ||
| 164 | croak "Timeout waiting for WebSocket text message"; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub receive_bytes { | ||
| 168 | my ($self, $timeout) = @_; | ||
| 169 | $timeout //= 5; | ||
| 170 | |||
| 171 | # Check if we have a bytes message waiting | ||
| 172 | for my $i (0 .. $#{$self->{recv_queue}}) { | ||
| 173 | my $event = $self->{recv_queue}[$i]; | ||
| 174 | if ($event->{type} eq 'websocket.send' && exists $event->{bytes}) { | ||
| 175 | splice @{$self->{recv_queue}}, $i, 1; | ||
| 176 | return $event->{bytes}; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | # Check if connection closed | ||
| 181 | return undef if $self->{closed}; | ||
| 182 | |||
| 183 | # No message available yet | ||
| 184 | croak "Timeout waiting for WebSocket bytes message"; | ||
| 185 | } | ||
| 186 | |||
| 187 | sub receive_json { | ||
| 188 | my ($self, $timeout) = @_; | ||
| 189 | |||
| 190 | my $text = $self->receive_text($timeout); | ||
| 191 | return undef unless defined $text; | ||
| 192 | |||
| 193 | require JSON::MaybeXS; | ||
| 194 | return JSON::MaybeXS::decode_json($text); | ||
| 195 | } | ||
| 196 | |||
| 197 | sub close { | ||
| 198 | my ($self, $code, $reason) = @_; | ||
| 199 | |||
| 200 | return if $self->{closed}; | ||
| 201 | |||
| 202 | $code //= 1000; | ||
| 203 | $reason //= ''; | ||
| 204 | |||
| 205 | $self->{closed} = 1; | ||
| 206 | $self->{close_code} = $code; | ||
| 207 | $self->{close_reason} = $reason; | ||
| 208 | |||
| 209 | # Push disconnect event | ||
| 210 | push @{$self->{send_queue}}, { | ||
| 211 | type => 'websocket.disconnect', | ||
| 212 | code => $code, | ||
| 213 | reason => $reason, | ||
| 214 | }; | ||
| 215 | |||
| 216 | # Pump the app to let it process the disconnect | ||
| 217 | $self->_pump_app; | ||
| 218 | |||
| 219 | return $self; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub close_code { | ||
| 223 | my ($self) = @_; | ||
| 224 | return $self->{close_code}; | ||
| 225 | } | ||
| 226 | |||
| 227 | sub close_reason { | ||
| 228 | my ($self) = @_; | ||
| 229 | return $self->{close_reason}; | ||
| 230 | } | ||
| 231 | |||
| 232 | sub is_closed { | ||
| 233 | my ($self) = @_; | ||
| 234 | return $self->{closed}; | ||
| 235 | } | ||
| 236 | |||
| 237 | 1; | ||
| 238 | |||
| 239 | __END__ |