-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathregistry.go
More file actions
179 lines (153 loc) · 4.85 KB
/
Copy pathregistry.go
File metadata and controls
179 lines (153 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package purl
import (
"errors"
"net/url"
"regexp"
"strings"
"sync"
)
// ErrNoRegistryConfig is returned when a PURL type has no registry configuration.
var ErrNoRegistryConfig = errors.New("no registry configuration for this type")
// ErrNoMatch is returned when a URL doesn't match the reverse regex.
var ErrNoMatch = errors.New("URL does not match any known registry pattern")
// regexCache caches compiled regular expressions by pattern.
var regexCache sync.Map
// RegistryURL returns the human-readable registry URL for the package.
// For example, pkg:npm/lodash returns "https://www.npmjs.com/package/lodash".
func (p *PURL) RegistryURL() (string, error) {
cfg := TypeInfo(p.Type)
if cfg == nil || cfg.RegistryConfig == nil {
return "", ErrNoRegistryConfig
}
return expandTemplate(cfg.RegistryConfig, p.Namespace, p.Name, "")
}
// RegistryURLWithVersion returns the registry URL including version.
// Falls back to RegistryURL if version URLs aren't supported.
func (p *PURL) RegistryURLWithVersion() (string, error) {
if p.Version == "" {
return p.RegistryURL()
}
cfg := TypeInfo(p.Type)
if cfg == nil || cfg.RegistryConfig == nil {
return "", ErrNoRegistryConfig
}
return expandTemplate(cfg.RegistryConfig, p.Namespace, p.Name, p.Version)
}
// expandTemplate expands a URI template with the given components.
func expandTemplate(rc *RegistryConfig, namespace, name, version string) (string, error) {
var template string
hasNamespace := namespace != ""
// Select the appropriate template
if version != "" && rc.Components.VersionInURL {
switch {
case hasNamespace && rc.URITemplateWithVersion != "":
template = rc.URITemplateWithVersion
case !hasNamespace && rc.URITemplateWithVersionNoNS != "":
template = rc.URITemplateWithVersionNoNS
case rc.URITemplateWithVersion != "":
template = rc.URITemplateWithVersion
}
}
if template == "" {
switch {
case hasNamespace:
template = rc.URITemplate
case rc.URITemplateNoNamespace != "":
template = rc.URITemplateNoNamespace
default:
template = rc.URITemplate
}
}
if template == "" {
return "", ErrNoRegistryConfig
}
// Handle namespace prefix (e.g., @ for npm)
displayNamespace := namespace
if rc.Components.NamespacePrefix != "" && namespace != "" {
// Add prefix if not already present
if !strings.HasPrefix(namespace, rc.Components.NamespacePrefix) {
displayNamespace = rc.Components.NamespacePrefix + namespace
}
}
// Expand template variables
result := template
result = strings.ReplaceAll(result, "{namespace}", url.PathEscape(displayNamespace))
result = strings.ReplaceAll(result, "{name}", url.PathEscape(name))
result = strings.ReplaceAll(result, "{version}", url.PathEscape(version))
return result, nil
}
// ParseRegistryURL attempts to parse a registry URL into a PURL.
// It tries all known types to find a match.
func ParseRegistryURL(url string) (*PURL, error) {
for _, t := range KnownTypes() {
p, err := ParseRegistryURLWithType(url, t)
if err == nil {
return p, nil
}
}
return nil, ErrNoMatch
}
// ParseRegistryURLWithType parses a registry URL using a specific PURL type.
func ParseRegistryURLWithType(url, purlType string) (*PURL, error) {
cfg := TypeInfo(purlType)
if cfg == nil || cfg.RegistryConfig == nil || cfg.RegistryConfig.ReverseRegex == "" {
return nil, ErrNoRegistryConfig
}
re, err := getOrCompileRegex(cfg.RegistryConfig.ReverseRegex)
if err != nil {
return nil, err
}
matches := re.FindStringSubmatch(url)
if matches == nil {
return nil, ErrNoMatch
}
var namespace, name, version string
// Parse matches based on component configuration
if cfg.RegistryConfig.Components.Namespace {
if cfg.RegistryConfig.Components.NamespaceRequired {
// Namespace is required: matches[1]=namespace, matches[2]=name, matches[3]=version (if present)
if len(matches) > 1 {
namespace = matches[1]
}
if len(matches) > 2 { //nolint:mnd
name = matches[2]
}
if len(matches) > 3 { //nolint:mnd
version = matches[3]
}
} else {
// Namespace is optional: matches[1]=namespace (maybe empty), matches[2]=name
if len(matches) > 2 { //nolint:mnd
namespace = matches[1]
name = matches[2]
}
if len(matches) > 3 { //nolint:mnd
version = matches[3]
}
}
} else {
// No namespace: matches[1]=name, matches[2]=version (if present)
if len(matches) > 1 {
name = matches[1]
}
if len(matches) > 2 { //nolint:mnd
version = matches[2]
}
}
if name == "" {
return nil, ErrNoMatch
}
return New(purlType, namespace, name, version, nil), nil
}
// getOrCompileRegex returns a cached compiled regex or compiles and caches it.
func getOrCompileRegex(pattern string) (*regexp.Regexp, error) {
if cached, ok := regexCache.Load(pattern); ok {
return cached.(*regexp.Regexp), nil
}
re, err := regexp.Compile(pattern)
if err != nil {
return nil, err
}
regexCache.Store(pattern, re)
return re, nil
}