@@ -9,45 +9,45 @@ export function encodePackageName(name: string): string {
99 return encodeURIComponent ( name )
1010}
1111
12- const WORKSPACE_PREFIX = 'workspace:'
13- const CATALOG_PREFIX = 'catalog:'
14- const NPM_PREFIX = 'npm:'
15- const JSR_PREFIX = 'jsr:'
16- const URL_PREFIXES = [ 'http://' , 'https://' , 'git://' , 'git+' ]
12+ export type VersionProtocol = 'workspace' | 'catalog' | 'npm' | 'jsr' | null
1713
18- export type VersionProtocol = 'npm' | null
14+ const KNOWN_PROTOCOLS = new Set < VersionProtocol > ( [ 'workspace' , 'catalog' , 'npm' , 'jsr' ] )
15+ const URL_PREFIXES = [ 'http://' , 'https://' , 'git://' , 'git+' ]
16+ const UNSUPPORTED_PROTOCOLS = new Set < VersionProtocol > ( [ 'workspace' , 'catalog' , 'jsr' ] )
1917
2018export interface ParsedVersion {
2119 protocol : VersionProtocol
2220 prefix : '' | '^' | '~'
2321 semver : string
2422}
2523
24+ export function isSupportedProtocol ( protocol : VersionProtocol ) : boolean {
25+ return ! UNSUPPORTED_PROTOCOLS . has ( protocol )
26+ }
27+
2628export function formatVersion ( parsed : ParsedVersion ) : string {
2729 const protocol = parsed . protocol ? `${ parsed . protocol } :` : ''
2830 return `${ protocol } ${ parsed . prefix } ${ parsed . semver } `
2931}
3032
3133export function parseVersion ( rawVersion : string ) : ParsedVersion | null {
32- // Skip special protocols that aren't standard npm versions
33- if (
34- [
35- WORKSPACE_PREFIX ,
36- CATALOG_PREFIX ,
37- JSR_PREFIX ,
38- ...URL_PREFIXES ,
39- ] . some ( ( p ) => rawVersion . startsWith ( p ) )
40- ) {
34+ rawVersion = rawVersion . trim ( )
35+ // Skip URL-based versions
36+ if ( URL_PREFIXES . some ( ( p ) => rawVersion . startsWith ( p ) ) )
4137 return null
42- }
4338
4439 let protocol : VersionProtocol = null
4540 let versionStr = rawVersion
4641
47- // Handle npm: protocol (e.g., npm:^1.0.0)
48- if ( rawVersion . startsWith ( NPM_PREFIX ) ) {
49- protocol = 'npm'
50- versionStr = rawVersion . slice ( 4 /* NPM_PREFIX.length */ )
42+ // Parse protocol if present (e.g., npm:^1.0.0 -> protocol: 'npm')
43+ const colonIndex = rawVersion . indexOf ( ':' )
44+ if ( colonIndex !== - 1 ) {
45+ protocol = rawVersion . slice ( 0 , colonIndex ) as VersionProtocol
46+
47+ if ( ! KNOWN_PROTOCOLS . has ( protocol ) )
48+ return null
49+
50+ versionStr = rawVersion . slice ( colonIndex + 1 )
5151 }
5252
5353 const firstChar = versionStr [ 0 ]
0 commit comments