| 1 | package Net::Nostr::Message; | ||
| 2 | |||
| 3 | 1 | ▶ | use strictures 2; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 1 | ▶ | use Net::Nostr::_ConstructorArgs (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | ▶ | use Carp qw(croak); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use JSON (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use Scalar::Util qw(blessed); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 0 | ▶ | use Net::Nostr::Event; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use Net::Nostr::Filter; | ||
| 12 | sub hints { | ||
| 13 | my $self = shift; | ||
| 14 | if (@_) { | ||
| 15 | croak 'hints are only valid for EOSE' unless $self->type eq 'EOSE'; | ||
| 16 | _validate_hints($_[0]); | ||
| 17 | $self->{hints} = [@{$_[0]}]; | ||
| 18 | } | ||
| 19 | return exists $self->{hints} ? [@{$self->{hints}}] : undef; | ||
| 20 | } | ||
| 21 | |||
| 22 | use Class::Tiny qw(type subscription_id event event_id accepted message prefix filters challenge count approximate neg_msg filter neg_limit hints); | ||
| 23 | |||
| 24 | my $JSON = JSON->new->utf8; | ||
| 25 | |||
| 26 | sub _validate_hints { | ||
| 27 | my ($hints) = @_; | ||
| 28 | croak 'hints must be an array of strings' unless ref($hints) eq 'ARRAY'; | ||
| 29 | for my $hint (@$hints) { | ||
| 30 | croak 'each hint must be a string' | ||
| 31 | unless defined($hint) && !ref($hint) && $JSON->encode($hint) =~ /\A"/; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | sub _validate_subscription_id { | ||
| 36 | my ($sub_id) = @_; | ||
| 37 | croak "subscription_id must be a non-empty string" | ||
| 38 | unless defined $sub_id && !ref($sub_id) && length($sub_id) > 0; | ||
| 39 | croak "subscription_id must be at most 64 characters" | ||
| 40 | unless length($sub_id) <= 64; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub _validate_non_negative_int { | ||
| 44 | my ($value, $name) = @_; | ||
| 45 | croak "$name must be a non-negative integer" | ||
| 46 | unless defined $value && !ref($value) && $value =~ /\A[0-9]+\z/; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub _validate_hex_message { | ||
| 50 | my ($value, $name) = @_; | ||
| 51 | croak "$name must be an even-length hex string" | ||
| 52 | unless defined $value && !ref($value) && $value =~ /\A(?:[0-9a-f]{2})+\z/; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub _validate_filters { | ||
| 56 | my ($filters) = @_; | ||
| 57 | for my $f (@$filters) { | ||
| 58 | croak "each filter must be a Net::Nostr::Filter object" | ||
| 59 | unless blessed($f) && $f->isa('Net::Nostr::Filter'); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | sub _extract_prefix { | ||
| 64 | my ($message) = @_; | ||
| 65 | return undef unless defined $message && $message =~ /^([a-z-]+): /; | ||
| 66 | return $1; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub new { | ||
| 70 | my $class = shift; | ||
| 71 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 72 | my %known; @known{Class::Tiny->get_all_attributes_for($class)} = (); | ||
| 73 | my @unknown = grep { !exists $known{$_} } keys %args; | ||
| 74 | croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown; | ||
| 75 | my $self = bless {}, $class; | ||
| 76 | |||
| 77 | croak "type is required" unless defined $args{type}; | ||
| 78 | $self->type($args{type}); | ||
| 79 | |||
| 80 | my $type = $args{type}; | ||
| 81 | croak 'hints are only valid for EOSE' if exists($args{hints}) && $type ne 'EOSE'; | ||
| 82 | |||
| 83 | if ($type eq 'EVENT') { | ||
| 84 | croak "event is required for EVENT message" unless $args{event}; | ||
| 85 | croak "event must be a Net::Nostr::Event object" | ||
| 86 | unless blessed($args{event}) && $args{event}->isa('Net::Nostr::Event'); | ||
| 87 | $self->event($args{event}); | ||
| 88 | $self->subscription_id($args{subscription_id}) if defined $args{subscription_id}; | ||
| 89 | } elsif ($type eq 'REQ') { | ||
| 90 | _validate_subscription_id($args{subscription_id}); | ||
| 91 | $self->subscription_id($args{subscription_id}); | ||
| 92 | croak "req requires at least one filter" | ||
| 93 | unless $args{filters} && @{$args{filters}}; | ||
| 94 | _validate_filters($args{filters}); | ||
| 95 | $self->filters($args{filters}); | ||
| 96 | } elsif ($type eq 'CLOSE') { | ||
| 97 | _validate_subscription_id($args{subscription_id}); | ||
| 98 | $self->subscription_id($args{subscription_id}); | ||
| 99 | } elsif ($type eq 'OK') { | ||
| 100 | croak "event_id is required for OK message" | ||
| 101 | unless defined $args{event_id}; | ||
| 102 | croak "event_id must be 64-char lowercase hex" | ||
| 103 | unless $args{event_id} =~ /\A[0-9a-f]{64}\z/; | ||
| 104 | $self->event_id($args{event_id}); | ||
| 105 | $self->accepted($args{accepted} ? 1 : 0); | ||
| 106 | my $msg = $args{message} // ''; | ||
| 107 | croak "message must be a string" if ref($msg); | ||
| 108 | $self->message($msg); | ||
| 109 | $self->prefix(_extract_prefix($self->message)); | ||
| 110 | } elsif ($type eq 'EOSE') { | ||
| 111 | croak "subscription_id is required for EOSE" | ||
| 112 | unless defined $args{subscription_id}; | ||
| 113 | $self->subscription_id($args{subscription_id}); | ||
| 114 | $self->hints($args{hints}) if exists $args{hints}; | ||
| 115 | } elsif ($type eq 'NOTICE') { | ||
| 116 | croak "message is required for NOTICE" unless defined $args{message}; | ||
| 117 | croak "message must be a string" if ref($args{message}); | ||
| 118 | $self->message($args{message}); | ||
| 119 | } elsif ($type eq 'CLOSED') { | ||
| 120 | croak "subscription_id is required for CLOSED" | ||
| 121 | unless defined $args{subscription_id}; | ||
| 122 | croak "message is required for CLOSED" | ||
| 123 | unless defined $args{message}; | ||
| 124 | croak "message must be a string" if ref($args{message}); | ||
| 125 | $self->subscription_id($args{subscription_id}); | ||
| 126 | $self->message($args{message}); | ||
| 127 | $self->prefix(_extract_prefix($self->message)); | ||
| 128 | } elsif ($type eq 'COUNT') { | ||
| 129 | # Bidirectional: client sends filters, relay sends count | ||
| 130 | _validate_subscription_id($args{subscription_id}); | ||
| 131 | $self->subscription_id($args{subscription_id}); | ||
| 132 | croak "COUNT count and filters are mutually exclusive" | ||
| 133 | if defined $args{count} && $args{filters}; | ||
| 134 | if (defined $args{count}) { | ||
| 135 | _validate_non_negative_int($args{count}, 'count'); | ||
| 136 | $self->count(0 + $args{count}); | ||
| 137 | $self->approximate($args{approximate} ? 1 : 0) if $args{approximate}; | ||
| 138 | } else { | ||
| 139 | croak "COUNT requires at least one filter" | ||
| 140 | unless $args{filters} && @{$args{filters}}; | ||
| 141 | _validate_filters($args{filters}); | ||
| 142 | $self->filters($args{filters}); | ||
| 143 | } | ||
| 144 | } elsif ($type eq 'AUTH') { | ||
| 145 | # Bidirectional: relay sends challenge string, client sends signed event | ||
| 146 | croak "AUTH event and challenge are mutually exclusive" | ||
| 147 | if defined $args{event} && defined $args{challenge}; | ||
| 148 | if (defined $args{event}) { | ||
| 149 | croak "event must be a Net::Nostr::Event object" | ||
| 150 | unless blessed($args{event}) && $args{event}->isa('Net::Nostr::Event'); | ||
| 151 | $self->event($args{event}); | ||
| 152 | } elsif (defined $args{challenge}) { | ||
| 153 | croak "challenge must be a string" if ref($args{challenge}); | ||
| 154 | $self->challenge($args{challenge}); | ||
| 155 | } else { | ||
| 156 | croak "AUTH requires either 'event' or 'challenge'"; | ||
| 157 | } | ||
| 158 | } elsif ($type eq 'NEG-OPEN') { | ||
| 159 | _validate_subscription_id($args{subscription_id}); | ||
| 160 | $self->subscription_id($args{subscription_id}); | ||
| 161 | croak "filter is required for NEG-OPEN" | ||
| 162 | unless $args{filter}; | ||
| 163 | croak "filter must be a Net::Nostr::Filter object" | ||
| 164 | unless blessed($args{filter}) && $args{filter}->isa('Net::Nostr::Filter'); | ||
| 165 | $self->filter($args{filter}); | ||
| 166 | croak "neg_msg is required for NEG-OPEN" | ||
| 167 | unless defined $args{neg_msg}; | ||
| 168 | _validate_hex_message($args{neg_msg}, 'neg_msg'); | ||
| 169 | $self->neg_msg($args{neg_msg}); | ||
| 170 | } elsif ($type eq 'NEG-MSG') { | ||
| 171 | _validate_subscription_id($args{subscription_id}); | ||
| 172 | $self->subscription_id($args{subscription_id}); | ||
| 173 | croak "neg_msg is required for NEG-MSG" | ||
| 174 | unless defined $args{neg_msg}; | ||
| 175 | _validate_hex_message($args{neg_msg}, 'neg_msg'); | ||
| 176 | $self->neg_msg($args{neg_msg}); | ||
| 177 | } elsif ($type eq 'NEG-CLOSE') { | ||
| 178 | _validate_subscription_id($args{subscription_id}); | ||
| 179 | $self->subscription_id($args{subscription_id}); | ||
| 180 | } elsif ($type eq 'NEG-ERR') { | ||
| 181 | croak "subscription_id is required for NEG-ERR" | ||
| 182 | unless defined $args{subscription_id}; | ||
| 183 | croak "message is required for NEG-ERR" | ||
| 184 | unless defined $args{message}; | ||
| 185 | croak "message must be a string" if ref($args{message}); | ||
| 186 | $self->subscription_id($args{subscription_id}); | ||
| 187 | $self->message($args{message}); | ||
| 188 | $self->prefix(_extract_prefix($self->message)); | ||
| 189 | if (defined $args{neg_limit}) { | ||
| 190 | _validate_non_negative_int($args{neg_limit}, 'neg_limit'); | ||
| 191 | $self->neg_limit(0 + $args{neg_limit}); | ||
| 192 | } | ||
| 193 | } else { | ||
| 194 | croak "unknown message type: $type"; | ||
| 195 | } | ||
| 196 | |||
| 197 | return $self; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub serialize { | ||
| 201 | my ($self) = @_; | ||
| 202 | my $type = $self->type; | ||
| 203 | |||
| 204 | if ($type eq 'EVENT') { | ||
| 205 | if (defined $self->subscription_id) { | ||
| 206 | return $JSON->encode(['EVENT', $self->subscription_id, $self->event->to_hash]); | ||
| 207 | } | ||
| 208 | return $JSON->encode(['EVENT', $self->event->to_hash]); | ||
| 209 | } elsif ($type eq 'REQ') { | ||
| 210 | return $JSON->encode(['REQ', $self->subscription_id, map { $_->to_hash } @{$self->filters}]); | ||
| 211 | } elsif ($type eq 'CLOSE') { | ||
| 212 | return $JSON->encode(['CLOSE', $self->subscription_id]); | ||
| 213 | } elsif ($type eq 'OK') { | ||
| 214 | return $JSON->encode(['OK', $self->event_id, $self->accepted ? JSON::true : JSON::false, $self->message]); | ||
| 215 | } elsif ($type eq 'EOSE') { | ||
| 216 | return $JSON->encode(['EOSE', $self->subscription_id, | ||
| 217 | (exists $self->{hints} ? ($self->hints) : ())]); | ||
| 218 | } elsif ($type eq 'NOTICE') { | ||
| 219 | return $JSON->encode(['NOTICE', $self->message]); | ||
| 220 | } elsif ($type eq 'CLOSED') { | ||
| 221 | return $JSON->encode(['CLOSED', $self->subscription_id, $self->message]); | ||
| 222 | } elsif ($type eq 'COUNT') { | ||
| 223 | if (defined $self->count) { | ||
| 224 | my %result = (count => $self->count); | ||
| 225 | $result{approximate} = JSON::true if $self->approximate; | ||
| 226 | return $JSON->encode(['COUNT', $self->subscription_id, \%result]); | ||
| 227 | } | ||
| 228 | return $JSON->encode(['COUNT', $self->subscription_id, map { $_->to_hash } @{$self->filters}]); | ||
| 229 | } elsif ($type eq 'AUTH') { | ||
| 230 | if ($self->event) { | ||
| 231 | return $JSON->encode(['AUTH', $self->event->to_hash]); | ||
| 232 | } else { | ||
| 233 | return $JSON->encode(['AUTH', $self->challenge]); | ||
| 234 | } | ||
| 235 | } elsif ($type eq 'NEG-OPEN') { | ||
| 236 | return $JSON->encode(['NEG-OPEN', $self->subscription_id, $self->filter->to_hash, $self->neg_msg]); | ||
| 237 | } elsif ($type eq 'NEG-MSG') { | ||
| 238 | return $JSON->encode(['NEG-MSG', $self->subscription_id, $self->neg_msg]); | ||
| 239 | } elsif ($type eq 'NEG-CLOSE') { | ||
| 240 | return $JSON->encode(['NEG-CLOSE', $self->subscription_id]); | ||
| 241 | } elsif ($type eq 'NEG-ERR') { | ||
| 242 | my @arr = ('NEG-ERR', $self->subscription_id, $self->message); | ||
| 243 | push @arr, $self->neg_limit if defined $self->neg_limit; | ||
| 244 | return $JSON->encode(\@arr); | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | my %PARSERS = ( | ||
| 249 | EVENT => sub { | ||
| 250 | my ($arr) = @_; | ||
| 251 | # client-to-relay: ["EVENT", {event}] | ||
| 252 | if (@$arr == 2) { | ||
| 253 | croak "EVENT event element must be a JSON object\n" | ||
| 254 | unless ref($arr->[1]) eq 'HASH'; | ||
| 255 | return ( | ||
| 256 | event => Net::Nostr::Event->from_wire($arr->[1]), | ||
| 257 | ); | ||
| 258 | } | ||
| 259 | # relay-to-client: ["EVENT", sub_id, {event}] | ||
| 260 | croak "EVENT message requires 2 or 3 elements\n" unless @$arr == 3; | ||
| 261 | croak "EVENT subscription_id must be a string\n" | ||
| 262 | if ref($arr->[1]); | ||
| 263 | croak "EVENT event element must be a JSON object\n" | ||
| 264 | unless ref($arr->[2]) eq 'HASH'; | ||
| 265 | return ( | ||
| 266 | subscription_id => $arr->[1], | ||
| 267 | event => Net::Nostr::Event->from_wire($arr->[2]), | ||
| 268 | ); | ||
| 269 | }, | ||
| 270 | OK => sub { | ||
| 271 | my ($arr) = @_; | ||
| 272 | croak "OK message requires 4 elements\n" unless @$arr == 4; | ||
| 273 | croak "OK event_id must be 64-char lowercase hex\n" | ||
| 274 | unless defined $arr->[1] && $arr->[1] =~ /\A[0-9a-f]{64}\z/; | ||
| 275 | croak "OK message must be a string\n" | ||
| 276 | if ref($arr->[3]); | ||
| 277 | return ( | ||
| 278 | event_id => $arr->[1], | ||
| 279 | accepted => $arr->[2] ? 1 : 0, | ||
| 280 | message => $arr->[3], | ||
| 281 | ); | ||
| 282 | }, | ||
| 283 | EOSE => sub { | ||
| 284 | my ($arr) = @_; | ||
| 285 | croak "EOSE message requires 2 or 3 elements\n" unless @$arr == 2 || @$arr == 3; | ||
| 286 | croak "EOSE subscription_id must be a string\n" | ||
| 287 | if ref($arr->[1]); | ||
| 288 | return ( | ||
| 289 | subscription_id => $arr->[1], | ||
| 290 | (@$arr == 3 ? (hints => $arr->[2]) : ()), | ||
| 291 | ); | ||
| 292 | }, | ||
| 293 | CLOSED => sub { | ||
| 294 | my ($arr) = @_; | ||
| 295 | croak "CLOSED message requires 3 elements\n" unless @$arr == 3; | ||
| 296 | croak "CLOSED subscription_id must be a string\n" | ||
| 297 | if ref($arr->[1]); | ||
| 298 | croak "CLOSED message must be a string\n" | ||
| 299 | if ref($arr->[2]); | ||
| 300 | return ( | ||
| 301 | subscription_id => $arr->[1], | ||
| 302 | message => $arr->[2], | ||
| 303 | ); | ||
| 304 | }, | ||
| 305 | NOTICE => sub { | ||
| 306 | my ($arr) = @_; | ||
| 307 | croak "NOTICE message requires 2 elements\n" unless @$arr == 2; | ||
| 308 | croak "NOTICE message must be a string\n" | ||
| 309 | if ref($arr->[1]); | ||
| 310 | return ( | ||
| 311 | message => $arr->[1], | ||
| 312 | ); | ||
| 313 | }, | ||
| 314 | REQ => sub { | ||
| 315 | my ($arr) = @_; | ||
| 316 | croak "REQ message requires at least 3 elements\n" unless @$arr >= 3; | ||
| 317 | my @filters; | ||
| 318 | for my $i (2 .. $#$arr) { | ||
| 319 | croak "REQ filter element must be a JSON object\n" | ||
| 320 | unless ref($arr->[$i]) eq 'HASH'; | ||
| 321 | push @filters, Net::Nostr::Filter->new(%{$arr->[$i]}); | ||
| 322 | } | ||
| 323 | return ( | ||
| 324 | subscription_id => $arr->[1], | ||
| 325 | filters => \@filters, | ||
| 326 | ); | ||
| 327 | }, | ||
| 328 | CLOSE => sub { | ||
| 329 | my ($arr) = @_; | ||
| 330 | croak "CLOSE message requires 2 elements\n" unless @$arr == 2; | ||
| 331 | return ( | ||
| 332 | subscription_id => $arr->[1], | ||
| 333 | ); | ||
| 334 | }, | ||
| 335 | COUNT => sub { | ||
| 336 | my ($arr) = @_; | ||
| 337 | croak "COUNT message requires at least 3 elements\n" unless @$arr >= 3; | ||
| 338 | # Relay-to-client: ["COUNT", sub_id, {"count": N}] | ||
| 339 | if (ref($arr->[2]) eq 'HASH' && exists $arr->[2]{count}) { | ||
| 340 | _validate_non_negative_int($arr->[2]{count}, 'count'); | ||
| 341 | return ( | ||
| 342 | subscription_id => $arr->[1], | ||
| 343 | count => $arr->[2]{count}, | ||
| 344 | ($arr->[2]{approximate} ? (approximate => 1) : ()), | ||
| 345 | ); | ||
| 346 | } | ||
| 347 | # Client-to-relay: ["COUNT", sub_id, {filter}...] | ||
| 348 | my @filters; | ||
| 349 | for my $i (2 .. $#$arr) { | ||
| 350 | croak "COUNT filter element must be a JSON object\n" | ||
| 351 | unless ref($arr->[$i]) eq 'HASH'; | ||
| 352 | push @filters, Net::Nostr::Filter->new(%{$arr->[$i]}); | ||
| 353 | } | ||
| 354 | return ( | ||
| 355 | subscription_id => $arr->[1], | ||
| 356 | filters => \@filters, | ||
| 357 | ); | ||
| 358 | }, | ||
| 359 | AUTH => sub { | ||
| 360 | my ($arr) = @_; | ||
| 361 | croak "AUTH message requires 2 elements\n" unless @$arr == 2; | ||
| 362 | # If second element is a hash, it's a signed event from client | ||
| 363 | if (ref($arr->[1]) eq 'HASH') { | ||
| 364 | return ( | ||
| 365 | event => Net::Nostr::Event->from_wire($arr->[1]), | ||
| 366 | ); | ||
| 367 | } | ||
| 368 | # Otherwise it's a challenge string from relay | ||
| 369 | croak "AUTH challenge must be a string\n" | ||
| 370 | if ref($arr->[1]); | ||
| 371 | return ( | ||
| 372 | challenge => $arr->[1], | ||
| 373 | ); | ||
| 374 | }, | ||
| 375 | 'NEG-OPEN' => sub { | ||
| 376 | my ($arr) = @_; | ||
| 377 | croak "NEG-OPEN message requires 4 elements\n" unless @$arr == 4; | ||
| 378 | croak "NEG-OPEN subscription_id must be a string\n" | ||
| 379 | if ref($arr->[1]); | ||
| 380 | croak "NEG-OPEN filter must be a JSON object\n" | ||
| 381 | unless ref($arr->[2]) eq 'HASH'; | ||
| 382 | croak "NEG-OPEN neg_msg must be an even-length hex string\n" | ||
| 383 | unless defined $arr->[3] && !ref($arr->[3]) && $arr->[3] =~ /\A(?:[0-9a-f]{2})+\z/; | ||
| 384 | return ( | ||
| 385 | subscription_id => $arr->[1], | ||
| 386 | filter => Net::Nostr::Filter->new(%{$arr->[2]}), | ||
| 387 | neg_msg => $arr->[3], | ||
| 388 | ); | ||
| 389 | }, | ||
| 390 | 'NEG-MSG' => sub { | ||
| 391 | my ($arr) = @_; | ||
| 392 | croak "NEG-MSG message requires 3 elements\n" unless @$arr == 3; | ||
| 393 | croak "NEG-MSG subscription_id must be a string\n" | ||
| 394 | if ref($arr->[1]); | ||
| 395 | croak "NEG-MSG neg_msg must be an even-length hex string\n" | ||
| 396 | unless defined $arr->[2] && !ref($arr->[2]) && $arr->[2] =~ /\A(?:[0-9a-f]{2})+\z/; | ||
| 397 | return ( | ||
| 398 | subscription_id => $arr->[1], | ||
| 399 | neg_msg => $arr->[2], | ||
| 400 | ); | ||
| 401 | }, | ||
| 402 | 'NEG-CLOSE' => sub { | ||
| 403 | my ($arr) = @_; | ||
| 404 | croak "NEG-CLOSE message requires 2 elements\n" unless @$arr == 2; | ||
| 405 | croak "NEG-CLOSE subscription_id must be a string\n" | ||
| 406 | if ref($arr->[1]); | ||
| 407 | return ( | ||
| 408 | subscription_id => $arr->[1], | ||
| 409 | ); | ||
| 410 | }, | ||
| 411 | 'NEG-ERR' => sub { | ||
| 412 | my ($arr) = @_; | ||
| 413 | croak "NEG-ERR message requires 3 or 4 elements\n" unless @$arr == 3 || @$arr == 4; | ||
| 414 | croak "NEG-ERR subscription_id must be a string\n" | ||
| 415 | if ref($arr->[1]); | ||
| 416 | croak "NEG-ERR message must be a string\n" | ||
| 417 | if ref($arr->[2]); | ||
| 418 | if (@$arr == 4) { | ||
| 419 | _validate_non_negative_int($arr->[3], 'neg_limit'); | ||
| 420 | } | ||
| 421 | return ( | ||
| 422 | subscription_id => $arr->[1], | ||
| 423 | message => $arr->[2], | ||
| 424 | (@$arr == 4 ? (neg_limit => $arr->[3]) : ()), | ||
| 425 | ); | ||
| 426 | }, | ||
| 427 | ); | ||
| 428 | |||
| 429 | sub parse { | ||
| 430 | my ($class, $raw) = @_; | ||
| 431 | croak "message JSON must be UTF-8 encoded bytes, not decoded characters\n" | ||
| 432 | if defined $raw && !ref($raw) && $raw =~ /[^\x00-\xFF]/; | ||
| 433 | my $arr = eval { $JSON->decode($raw) }; | ||
| 434 | croak "invalid JSON: $@\n" if $@; | ||
| 435 | croak "message must be a JSON array\n" unless ref($arr) eq 'ARRAY'; | ||
| 436 | croak "message array must not be empty\n" unless @$arr; | ||
| 437 | |||
| 438 | my $type = $arr->[0]; | ||
| 439 | croak "unknown message type: $type\n" unless $PARSERS{$type}; | ||
| 440 | |||
| 441 | my %fields = $PARSERS{$type}->($arr); | ||
| 442 | return $class->new(type => $type, %fields); | ||
| 443 | } | ||
| 444 | |||
| 445 | 1; | ||
| 446 | |||
| 447 | __END__ | ||