11import * as z from 'zod'
22
3- // the basic purl type, containing ecosystem , namespace, name, and version.
4- // other than ecosystem , all fields are nullable. this is for maximum flexibility
3+ // the basic purl type, containing type , namespace, name, and version.
4+ // other than type , all fields are nullable. this is for maximum flexibility
55// at the cost of strict adherence to the package-url spec.
66export const PurlSchema = z . object ( {
77 type : z . string ( ) ,
@@ -14,7 +14,7 @@ export const PurlSchema = z.object({
1414
1515export type PackageURL = z . infer < typeof PurlSchema >
1616
17- const PURL_ECOSYSTEM = / p k g : ( [ a - z A - Z 0 - 9 - _ ] + ) \/ .* /
17+ const PURL_TYPE = / p k g : ( [ a - z A - Z 0 - 9 - _ ] + ) \/ .* /
1818
1919export function parsePURL ( purl : string ) : PackageURL {
2020 const result : PackageURL = {
@@ -26,19 +26,19 @@ export function parsePURL(purl: string): PackageURL {
2626 error : null
2727 }
2828 if ( ! purl . startsWith ( 'pkg:' ) ) {
29- result . error = 'purl must start with "pkg:"'
29+ result . error = 'package-url must start with "pkg:"'
3030 return result
3131 }
32- const ecosystem = purl . match ( PURL_ECOSYSTEM )
33- if ( ecosystem === null ) {
34- result . error = 'purl must contain an ecosystem '
32+ const type = purl . match ( PURL_TYPE )
33+ if ( ! type ) {
34+ result . error = 'package-url must contain a type '
3535 return result
3636 }
37- result . type = ecosystem [ 1 ]
37+ result . type = type [ 1 ]
3838 const parts = purl . split ( '/' )
3939 // the first 'part' should be 'pkg:ecosystem'
40- if ( parts . length < 2 || parts [ 1 ] . length === 0 ) {
41- result . error = 'purl must contain a namespace or name'
40+ if ( parts . length < 2 || ! parts [ 1 ] ) {
41+ result . error = 'package-url must contain a namespace or name'
4242 return result
4343 }
4444 let namePlusRest : string
@@ -49,13 +49,17 @@ export function parsePURL(purl: string): PackageURL {
4949 namePlusRest = parts [ 2 ]
5050 }
5151 const name = namePlusRest . match ( / ( [ ^ @ # ? ] + ) [ @ # ? ] ? .* / )
52- if ( name === null ) {
52+ if ( ! result . namespace && ! name ) {
53+ result . error = 'package-url must contain a namespace or name'
54+ return result
55+ }
56+ if ( ! name ) {
5357 // we're done here
5458 return result
5559 }
5660 result . name = decodeURIComponent ( name [ 1 ] )
5761 const version = namePlusRest . match ( / @ ( [ ^ # ? ] + ) [ # ? ] ? .* / )
58- if ( version === null ) {
62+ if ( ! version ) {
5963 return result
6064 }
6165 result . version = decodeURIComponent ( version [ 1 ] )
0 commit comments