@@ -13,6 +13,7 @@ import (
1313 "github.com/git-pkgs/proxy/internal/database"
1414 "github.com/git-pkgs/proxy/internal/metrics"
1515 "github.com/git-pkgs/proxy/internal/storage"
16+ "github.com/git-pkgs/purl"
1617 "github.com/git-pkgs/registries/fetch"
1718)
1819
@@ -50,8 +51,8 @@ type CacheResult struct {
5051
5152// GetOrFetchArtifact retrieves an artifact from cache or fetches from upstream.
5253func (p * Proxy ) GetOrFetchArtifact (ctx context.Context , ecosystem , name , version , filename string ) (* CacheResult , error ) {
53- pkgPURL := fmt . Sprintf ( "pkg:%s/%s" , ecosystem , name )
54- versionPURL := fmt . Sprintf ( "pkg:%s/%s@%s" , ecosystem , name , version )
54+ pkgPURL := purl . MakePURLString ( ecosystem , name , "" )
55+ versionPURL := purl . MakePURLString ( ecosystem , name , version )
5556
5657 if cached , err := p .checkCache (ctx , pkgPURL , versionPURL , filename ); err != nil {
5758 return nil , err
@@ -101,8 +102,9 @@ func (p *Proxy) checkCache(ctx context.Context, pkgPURL, versionPURL, filename s
101102 _ = p .DB .RecordArtifactHit (versionPURL , filename )
102103
103104 // Extract ecosystem from pkgPURL for metrics
104- ecosystem := extractEcosystem (pkgPURL )
105- metrics .RecordCacheHit (ecosystem )
105+ if p , err := purl .Parse (pkgPURL ); err == nil {
106+ metrics .RecordCacheHit (purl .PURLTypeToEcosystem (p .Type ))
107+ }
106108
107109 return & CacheResult {
108110 Reader : reader ,
@@ -251,8 +253,8 @@ func JSONError(w http.ResponseWriter, status int, message string) {
251253// GetOrFetchArtifactFromURL retrieves an artifact from cache or fetches from a specific URL.
252254// This is useful for registries where download URLs are determined from metadata.
253255func (p * Proxy ) GetOrFetchArtifactFromURL (ctx context.Context , ecosystem , name , version , filename , downloadURL string ) (* CacheResult , error ) {
254- pkgPURL := fmt . Sprintf ( "pkg:%s/%s" , ecosystem , name )
255- versionPURL := fmt . Sprintf ( "pkg:%s/%s@%s" , ecosystem , name , version )
256+ pkgPURL := purl . MakePURLString ( ecosystem , name , "" )
257+ versionPURL := purl . MakePURLString ( ecosystem , name , version )
256258
257259 if cached , err := p .checkCache (ctx , pkgPURL , versionPURL , filename ); err != nil {
258260 return nil , err
@@ -297,28 +299,3 @@ func (p *Proxy) fetchAndCacheFromURL(ctx context.Context, ecosystem, name, versi
297299 }, nil
298300}
299301
300- // extractEcosystem extracts the ecosystem from a package PURL.
301- // PURL format: pkg:ecosystem/name[@version]
302- func extractEcosystem (purl string ) string {
303- if len (purl ) < 5 || ! startsWith (purl , "pkg:" ) {
304- return "unknown"
305- }
306- rest := purl [4 :] // Skip "pkg:"
307- if idx := indexOf (rest , "/" ); idx != - 1 {
308- return rest [:idx ]
309- }
310- return "unknown"
311- }
312-
313- func startsWith (s , prefix string ) bool {
314- return len (s ) >= len (prefix ) && s [:len (prefix )] == prefix
315- }
316-
317- func indexOf (s , substr string ) int {
318- for i := 0 ; i + len (substr ) <= len (s ); i ++ {
319- if s [i :i + len (substr )] == substr {
320- return i
321- }
322- }
323- return - 1
324- }
0 commit comments