Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,15 @@ info, _ := resolver.Resolve(ctx, "npm", "lodash", "4.17.21")
// info.URL = "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz"
```

`WithBaseURLs` points those patterns at a mirror instead of the public host. Ecosystems left out of the map keep their defaults:

```go
resolver := fetch.NewResolver(fetch.WithBaseURLs(map[string]string{
"npm": "https://npm.mycompany.com",
"cargo": "https://crates.mycompany.com/crates",
}))
```

## Private Registries

PURLs with a `repository_url` qualifier automatically use that URL:
Expand Down
66 changes: 54 additions & 12 deletions fetch/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"unicode"

"github.com/git-pkgs/purl"
"github.com/git-pkgs/registries"
"github.com/git-pkgs/registries/client"
)
Expand All @@ -28,21 +29,60 @@ type Registry interface {
URLs() client.URLBuilder
}

// defaultBaseURLs are the public registry hosts used when no registry is
// registered for an ecosystem and WithBaseURLs does not override them.
var defaultBaseURLs = map[string]string{
"npm": "https://registry.npmjs.org",
"cargo": "https://static.crates.io/crates",
"gem": "https://rubygems.org",
"golang": "https://proxy.golang.org",
"hex": "https://repo.hex.pm",
"pub": "https://pub.dev",
"maven": "https://repo1.maven.org/maven2",
"nuget": "https://api.nuget.org",
}

// Resolver determines download URLs for package artifacts.
type Resolver struct {
registries map[string]Registry
baseURLs map[string]string
}

// ResolverOption configures a Resolver.
type ResolverOption func(*Resolver)

// WithBaseURLs overrides the base URL used for ecosystems resolved without a
// registered registry, keyed by ecosystem. Ecosystems not in the map keep
// the public registry host.
func WithBaseURLs(baseURLs map[string]string) ResolverOption {
return func(r *Resolver) {
for ecosystem, baseURL := range baseURLs {
if baseURL != "" {
r.baseURLs[purl.EcosystemToPURLType(ecosystem)] = strings.TrimRight(baseURL, "/")
}
}
}
}

// NewResolver creates a new URL resolver.
func NewResolver() *Resolver {
return &Resolver{
func NewResolver(opts ...ResolverOption) *Resolver {
r := &Resolver{
registries: make(map[string]Registry),
baseURLs: make(map[string]string, len(defaultBaseURLs)),
}
for ecosystem, baseURL := range defaultBaseURLs {
r.baseURLs[ecosystem] = baseURL
}
for _, opt := range opts {
opt(r)
}
return r
}

// RegisterRegistry adds a registry for URL resolution.
// RegisterRegistry adds a registry for URL resolution. Ecosystem names are
// matched by PURL type, so "rubygems" and "gem" are the same ecosystem.
func (r *Resolver) RegisterRegistry(reg Registry) {
r.registries[reg.Ecosystem()] = reg
r.registries[purl.EcosystemToPURLType(reg.Ecosystem())] = reg
}

// ArtifactInfo contains information about a downloadable artifact.
Expand Down Expand Up @@ -73,6 +113,7 @@ func (r *Resolver) ResolveWithOptions(
version string,
options ResolveOptions,
) (*ArtifactInfo, error) {
ecosystem = purl.EcosystemToPURLType(ecosystem)
reg, ok := r.registries[ecosystem]
if !ok {
info, err := r.resolveWithoutRegistry(ecosystem, name, version)
Expand All @@ -96,35 +137,36 @@ func (r *Resolver) ResolveWithOptions(
// when no registry client is configured.
func (r *Resolver) resolveWithoutRegistry(ecosystem, name, version string) (*ArtifactInfo, error) {
var url, filename string
base := r.baseURLs[ecosystem]

switch ecosystem {
case "npm":
shortName := name
if idx := strings.LastIndex(name, "/"); idx >= 0 {
shortName = name[idx+1:]
}
url = fmt.Sprintf("https://registry.npmjs.org/%s/-/%s-%s.tgz", name, shortName, version)
url = fmt.Sprintf("%s/%s/-/%s-%s.tgz", base, name, shortName, version)
filename = fmt.Sprintf("%s-%s.tgz", shortName, version)

case "cargo":
url = fmt.Sprintf("https://static.crates.io/crates/%s/%s-%s.crate", name, name, version)
url = fmt.Sprintf("%s/%s/%s-%s.crate", base, name, name, version)
filename = fmt.Sprintf("%s-%s.crate", name, version)

case "gem":
url = fmt.Sprintf("https://rubygems.org/downloads/%s-%s.gem", name, version)
url = fmt.Sprintf("%s/downloads/%s-%s.gem", base, name, version)
filename = fmt.Sprintf("%s-%s.gem", name, version)

case "golang":
encoded := encodeGoModule(name)
url = fmt.Sprintf("https://proxy.golang.org/%s/@v/%s.zip", encoded, version)
url = fmt.Sprintf("%s/%s/@v/%s.zip", base, encoded, version)
filename = fmt.Sprintf("%s@%s.zip", lastPathComponent(name), version)

case "hex":
url = fmt.Sprintf("https://repo.hex.pm/tarballs/%s-%s.tar", name, version)
url = fmt.Sprintf("%s/tarballs/%s-%s.tar", base, name, version)
filename = fmt.Sprintf("%s-%s.tar", name, version)

case "pub":
url = fmt.Sprintf("https://pub.dev/packages/%s/versions/%s.tar.gz", name, version)
url = fmt.Sprintf("%s/packages/%s/versions/%s.tar.gz", base, name, version)
filename = fmt.Sprintf("%s-%s.tar.gz", name, version)

case "maven":
Expand All @@ -134,13 +176,13 @@ func (r *Resolver) resolveWithoutRegistry(ecosystem, name, version string) (*Art
return nil, fmt.Errorf("invalid maven name format, expected group:artifact")
}
group = strings.ReplaceAll(group, ".", "/")
url = fmt.Sprintf("https://repo1.maven.org/maven2/%s/%s/%s/%s-%s.jar", group, artifact, version, artifact, version)
url = fmt.Sprintf("%s/%s/%s/%s/%s-%s.jar", base, group, artifact, version, artifact, version)
filename = fmt.Sprintf("%s-%s.jar", artifact, version)

case "nuget":
// NuGet package IDs are case-insensitive, use lowercase
lowername := strings.ToLower(name)
url = fmt.Sprintf("https://api.nuget.org/v3-flatcontainer/%s/%s/%s.%s.nupkg", lowername, version, lowername, version)
url = fmt.Sprintf("%s/v3-flatcontainer/%s/%s/%s.%s.nupkg", base, lowername, version, lowername, version)
filename = fmt.Sprintf("%s.%s.nupkg", lowername, version)

default:
Expand Down
114 changes: 114 additions & 0 deletions fetch/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,59 @@ func TestResolveWithoutRegistry(t *testing.T) {
}
}

func TestResolveWithBaseURLs(t *testing.T) {
r := NewResolver(WithBaseURLs(map[string]string{
"npm": "https://npm.internal/",
"cargo": "https://crates.internal/crates",
"gem": "https://gems.internal",
"golang": "https://goproxy.internal",
"hex": "https://hex.internal",
"pub": "https://pub.internal",
"maven": "https://maven.internal/releases",
"nuget": "https://nuget.internal",
}))

tests := []struct {
ecosystem string
name string
version string
wantURL string
}{
{"npm", "@babel/core", "7.23.0", "https://npm.internal/@babel/core/-/core-7.23.0.tgz"},
{"cargo", "serde", "1.0.193", "https://crates.internal/crates/serde/serde-1.0.193.crate"},
{"gem", "rails", "7.1.2", "https://gems.internal/downloads/rails-7.1.2.gem"},
{"golang", "github.com/Azure/azure-sdk-for-go", "v68.0.0", "https://goproxy.internal/github.com/!azure/azure-sdk-for-go/@v/v68.0.0.zip"},
{"hex", "phoenix", "1.7.10", "https://hex.internal/tarballs/phoenix-1.7.10.tar"},
{"pub", "flutter", "3.16.0", "https://pub.internal/packages/flutter/versions/3.16.0.tar.gz"},
{"maven", "com.google.guava:guava", "32.1.3-jre", "https://maven.internal/releases/com/google/guava/guava/32.1.3-jre/guava-32.1.3-jre.jar"},
{"nuget", "Newtonsoft.Json", "13.0.3", "https://nuget.internal/v3-flatcontainer/newtonsoft.json/13.0.3/newtonsoft.json.13.0.3.nupkg"},
}

for _, tt := range tests {
t.Run(tt.ecosystem, func(t *testing.T) {
info, err := r.Resolve(context.Background(), tt.ecosystem, tt.name, tt.version)
if err != nil {
t.Fatalf("Resolve failed: %v", err)
}
if info.URL != tt.wantURL {
t.Errorf("URL = %q, want %q", info.URL, tt.wantURL)
}
})
}
}

func TestResolveWithBaseURLsKeepsDefaultsForOtherEcosystems(t *testing.T) {
r := NewResolver(WithBaseURLs(map[string]string{"npm": "https://npm.internal", "cargo": ""}))

info, err := r.Resolve(context.Background(), "cargo", "serde", "1.0.193")
if err != nil {
t.Fatalf("Resolve failed: %v", err)
}
if want := "https://static.crates.io/crates/serde/serde-1.0.193.crate"; info.URL != want {
t.Errorf("URL = %q, want %q", info.URL, want)
}
}

func TestResolveUnsupportedEcosystem(t *testing.T) {
r := NewResolver()

Expand Down Expand Up @@ -301,3 +354,64 @@ func TestFilenameFromURL(t *testing.T) {
}
}
}

type namedRegistry struct {
fakeRegistry
ecosystem string
}

func (n *namedRegistry) Ecosystem() string { return n.ecosystem }

func TestResolveEcosystemAliases(t *testing.T) {
for _, ecosystem := range []string{"gem", "rubygems", "RubyGems"} {
t.Run(ecosystem, func(t *testing.T) {
info, err := NewResolver().Resolve(context.Background(), ecosystem, "rake", "13.2.1")
if err != nil {
t.Fatalf("Resolve failed: %v", err)
}
if want := "https://rubygems.org/downloads/rake-13.2.1.gem"; info.URL != want {
t.Errorf("URL = %q, want %q", info.URL, want)
}
})
}
}

func TestResolveRegisteredEcosystemAliases(t *testing.T) {
want := "https://repo.packagist.org/dist/monolog/monolog-3.5.0.zip"
for _, tt := range []struct{ registered, requested string }{
{"composer", "packagist"},
{"packagist", "composer"},
{"composer", "composer"},
} {
t.Run(tt.registered+"/"+tt.requested, func(t *testing.T) {
r := NewResolver()
r.RegisterRegistry(&namedRegistry{
ecosystem: tt.registered,
fakeRegistry: fakeRegistry{versions: []registries.Version{{
Number: "3.5.0",
Metadata: map[string]any{"download_url": want},
}}},
})

info, err := r.Resolve(context.Background(), tt.requested, "monolog/monolog", "3.5.0")
if err != nil {
t.Fatalf("Resolve failed: %v", err)
}
if info.URL != want {
t.Errorf("URL = %q, want %q", info.URL, want)
}
})
}
}

func TestWithBaseURLsAcceptsEcosystemAliases(t *testing.T) {
r := NewResolver(WithBaseURLs(map[string]string{"rubygems": "https://gems.internal"}))

info, err := r.Resolve(context.Background(), "gem", "rake", "13.2.1")
if err != nil {
t.Fatalf("Resolve failed: %v", err)
}
if want := "https://gems.internal/downloads/rake-13.2.1.gem"; info.URL != want {
t.Errorf("URL = %q, want %q", info.URL, want)
}
}
Loading