| 1 | 6 | package At::Protocol::NSID v1.8.0 { # https://github.com/bluesky-social/atproto/blob/main/packages/syntax/src/nsid.ts | |||||||||||||||||||||||||||
| 2 | 6 | ▶ | use v5.42; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | no warnings qw[experimental::builtin experimental::try]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use At::Error qw[register throw]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use feature 'try'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use parent -norequire => 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | use overload | ||||||||||||||||||||||||||||
| 8 | 6 | ▶ | '""' => sub ( $s, $u, $q ) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | join '.', @$s; | |||||||||||||||||||||||||||
| 10 | 6 | }; | |||||||||||||||||||||||||||
| 11 | 6 | our %EXPORT_TAGS = ( all => [ our @EXPORT_OK = qw[parse create isValid ensureValidNsid ensureValidNsidRegex] ] ); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | #~ Grammar: | ||||||||||||||||||||||||||||
| 14 | #~ alpha = "a" / "b" / "c" / "d" / "e" / "f" / "g" / "h" / "i" / "j" / "k" / "l" / "m" / "n" / "o" / "p" / "q" / "r" / "s" / "t" / "u" / "v" / "w" / "x" / "y" / "z" / "A" / "B" / "C" / "D" / "E" / "F" / "G" / "H" / "I" / "J" / "K" / "L" / "M" / "N" / "O" / "P" / "Q" / "R" / "S" / "T" / "U" / "V" / "W" / "X" / "Y" / "Z" | ||||||||||||||||||||||||||||
| 15 | #~ number = "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" / "0" | ||||||||||||||||||||||||||||
| 16 | #~ delim = "." | ||||||||||||||||||||||||||||
| 17 | #~ segment = alpha *( alpha / number / "-" ) | ||||||||||||||||||||||||||||
| 18 | #~ authority = segment *( delim segment ) | ||||||||||||||||||||||||||||
| 19 | #~ name = alpha *( alpha / number ) | ||||||||||||||||||||||||||||
| 20 | #~ nsid = authority delim name | ||||||||||||||||||||||||||||
| 21 | 12 | ▶ | sub new( $class, $nsid ) { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 12 | ensureValidNsid($nsid); | |||||||||||||||||||||||||||
| 23 | 12 | bless [ split /\./, $nsid, -1 ], $class; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 12 | ▶ | sub parse($nsid) { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 12 | __PACKAGE__->new($nsid); | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 6 | ▶ | sub create( $authority, $name ) { | ||||||||||||||||||||||||||
|
Subroutine
create: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 6 | parse join '.', reverse( split( /\./, $authority, -1 ) ), $name; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 4 | ▶ | sub authority($s) { join '.', reverse splice( @$s, 0, -1 ); } | ||||||||||||||||||||||||||
|
Subroutine
authority: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 4 | ▶ | sub name($s) { | ||||||||||||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 4 | @$s[-1]; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub isValid($nsid) { | ||||||||||||||||||||||||||
|
Subroutine
isValid: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 0 | try { | |||||||||||||||||||||||||||
| 41 | 0 | parse($nsid); | |||||||||||||||||||||||||||
| 42 | 0 | return 1; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 0 | catch ($err) { return 0; } | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | #~ Human readable constraints on NSID: | ||||||||||||||||||||||||||||
| 48 | #~ - a valid domain in reversed notation | ||||||||||||||||||||||||||||
| 49 | #~ - followed by an additional period-separated name, which is camel-case letters | ||||||||||||||||||||||||||||
| 50 | 217 | ▶ | sub ensureValidNsid($nsid) { | ||||||||||||||||||||||||||
|
Subroutine
ensureValidNsid: called CC=12 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # check that all chars are boring ASCII | ||||||||||||||||||||||||||||
| 53 | 217 | ▶ | throw InvalidNsidError('Disallowed characters in NSID (ASCII letters, digits, dashes, periods only)') unless $nsid =~ /^[a-zA-Z0-9.-]*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | # | ||||||||||||||||||||||||||||
| 55 | 179 | ▶ | throw InvalidNsidError('NSID is too long (317 chars max)') if length $nsid > 253 + 1 + 63; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 177 | my @labels = split /\./, $nsid, -1; # negative length, ftw | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # | ||||||||||||||||||||||||||||
| 59 | 177 | ▶ | throw InvalidNsidError('NSID needs at least three parts') if scalar @labels < 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | # | ||||||||||||||||||||||||||||
| 61 | 167 | for my $i ( 0 .. $#labels ) { | |||||||||||||||||||||||||||
| 62 | 662 | my $l = $labels[$i]; | |||||||||||||||||||||||||||
| 63 | 662 | ▶ | throw InvalidNsidError('NSID parts can not be empty') unless length $l; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 658 | ▶ | throw InvalidNsidError('NSID part too long (max 63 chars)') if length $l > 63; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 654 | ▶ | throw InvalidNsidError('NSID parts can not start or end with hyphen') if $l =~ /^-|-$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 650 | ▶ | throw InvalidNsidError('NSID first part may not start with a digit') if $i == 0 && $l =~ /^[0-9]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i == 0 and $l =~ /^[0-9]/u
MC/DC100%2/2
$i == 0 and $l =~ /^[0-9]/u
$i == 0
$l =~ /^[0-9]/u
Truth table100%3/3
A and B
A= $i == 0B= $l =~ /^[0-9]/u
| |||||||||||||||||||||||||||||
| 67 | 646 | ▶ | throw InvalidNsidError('NSID name part must be letters or digits') if $i == $#labels && $l !~ /^[a-zA-Z][a-zA-Z0-9]*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i == $#labels and not $l =~ /^[a-zA-Z][a-zA-Z0-9]*$/u
MC/DC100%2/2
$i == $#labels and not $l =~ /^[a-zA-Z][a-zA-Z0-9]*$/u
$i == $#labels
not $l =~ /^[a-zA-Z][a-zA-Z0-9]*$/u
Truth table100%3/3
A and not(B)
A= $i == $#labelsB= not $l =~ /^[a-zA-Z][a-zA-Z0-9]*$/u
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 146 | 1; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 173 | ▶ | sub ensureValidNsidRegex ($nsid) { | ||||||||||||||||||||||||||
|
Subroutine
ensureValidNsidRegex: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | #~ simple regex to enforce most constraints via just regex and length. | ||||||||||||||||||||||||||||
| 75 | #~ hand wrote this regex based on above constraints | ||||||||||||||||||||||||||||
| 76 | 173 | ▶ | throw InvalidNsidError(q[NSID didn't validate via regex]) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | unless $nsid =~ /^[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(\.[a-zA-Z][a-zA-Z0-9]{0,62})$/; | ||||||||||||||||||||||||||||
| 78 | 116 | ▶ | throw InvalidNsidError('NSID is too long (317 chars max)') if length $nsid > 253 + 1 + 63; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 114 | 1; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | # | ||||||||||||||||||||||||||||
| 82 | 6 | register 'InvalidNsidError', 1; | |||||||||||||||||||||||||||
| 83 | }; | ||||||||||||||||||||||||||||
| 84 | 6 | 1; | |||||||||||||||||||||||||||
| 85 | __END__ | ||||||||||||||||||||||||||||