core

package
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned when a requested package/release does not
	// exist on the mirror. Note: Package probes report a missing package as
	// a result (PackageResult.Found == false), not as this error.
	ErrNotFound = errors.New("not found")
	// ErrTimeout is returned when a probe exceeded its context deadline.
	ErrTimeout = errors.New("timeout")
	// ErrInvalidMirror is returned when a URL is not a usable mirror of the
	// expected kind (wrong scheme, not reachable, or not behaving like the
	// expected ecosystem).
	ErrInvalidMirror = errors.New("invalid mirror")
	// ErrUnreachable is returned when the mirror host could not be reached
	// at all (DNS failure, connection refused, network down).
	ErrUnreachable = errors.New("unreachable")
	// ErrNotImplemented is returned for mirror types that have no backend
	// yet (yum, dnf, nuget) or options the backend does not support.
	ErrNotImplemented = errors.New("not implemented")
)

Sentinel errors. Callers should test with errors.Is, not by type-asserting the concrete wrappers below.

Functions

func AsError

func AsError(err error) error

AsError wraps ctx errors (deadline exceeded, canceled) so that both errors.Is(err, mirava.ErrTimeout) and errors.Is(err, context.DeadlineExceeded) hold.

func SpeedRating

func SpeedRating(mbps float64) string

SpeedRating maps MB/s to a coarse label.

Types

type Config

type Config struct {
	HTTPClient *http.Client
	Logger     *slog.Logger
	Store      cache.Store
}

Config is the shared wiring handed to every backend: HTTP client, logger, cache store. Backends must not outlive the process config; they read it once at construction.

func (Config) Client

func (c Config) Client() *http.Client

Client returns the configured client or the shared default.

func (Config) Log

func (c Config) Log() *slog.Logger

Log returns the configured logger or a discard logger.

type DownloadResult

type DownloadResult struct {
	Envelope
	URL      string            `json:"url"`
	Headers  map[string]string `json:"headers,omitempty"`
	Size     int64             `json:"size,omitempty"`
	SHA256   string            `json:"sha256,omitempty"`
	Kind     string            `json:"kind,omitempty"` // blob, tarball, archive, ...
	Filename string            `json:"filename,omitempty"`
}

DownloadResult is a direct download URL plus everything a client needs to fetch it. Some ecosystems (Docker) require headers on the blob request.

type Duration

type Duration time.Duration

Duration is time.Duration with JSON string encoding ("150ms") instead of a bare integer, so website clients can read it directly.

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() ([]byte, error)

func (Duration) String

func (d Duration) String() string

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(b []byte) error

type Envelope

type Envelope struct {
	Type      MirrorType   `json:"type"`
	Mirror    string       `json:"mirror"`
	CheckedAt time.Time    `json:"checked_at"`
	Latency   Duration     `json:"latency"`
	OK        bool         `json:"ok"`
	Detail    ResultDetail `json:"detail,omitempty"`
}

Envelope is embedded in every result so consumers get common metadata without type switches.

type MirrorType

type MirrorType string

MirrorType identifies an ecosystem. Values match the string names used across mirava's CLI, website and API.

const (
	TypeApt      MirrorType = "apt"      // Debian/Ubuntu .deb repositories
	TypeYum      MirrorType = "yum"      // RHEL-family RPM repositories (unimplemented)
	TypeDnf      MirrorType = "dnf"      // DNF-compatible RPM mirrors (unimplemented)
	TypePacman   MirrorType = "pacman"   // Arch Linux mirrors
	TypeNpm      MirrorType = "npm"      // npm registry mirrors
	TypeGo       MirrorType = "go"       // Go module proxy / sumdb mirrors
	TypeCargo    MirrorType = "cargo"    // crates.io sparse index mirrors
	TypePypi     MirrorType = "pypi"     // PEP 503 simple index mirrors
	TypeNuget    MirrorType = "nuget"    // NuGet V3 feeds (unimplemented)
	TypeDocker   MirrorType = "docker"   // OCI / Docker registry mirrors
	TypeComposer MirrorType = "composer" // Composer / Packagist mirrors
)

Supported mirror ecosystems.

func (MirrorType) Implemented

func (t MirrorType) Implemented() bool

Implemented reports whether the mirror type has a backend in this module.

type PackageQuery

type PackageQuery struct {
	Name    string
	Version string // optional; exact version or ecosystem constraint
	Options any
}

PackageQuery names what to look up. Options carries an ecosystem-specific struct, e.g. *apt.QueryOptions{Suite, Component, Arch} — see each backend package for its type.

type PackageResult

type PackageResult struct {
	Envelope
	Found        bool     `json:"found"`
	Name         string   `json:"name"`
	Version      string   `json:"version,omitempty"`
	Detail       any      `json:"detail,omitempty"` // ecosystem payload, e.g. apt.PackageDetail
	CheckedPaths []string `json:"checked_paths,omitempty"`
	FoundPath    string   `json:"found_path,omitempty"`
}

PackageResult reports whether a package exists, with its metadata.

type ParseError

type ParseError struct {
	URL  string
	Kind string // e.g. "json", "gzip", "index"
	Err  error
}

ParseError reports a response body that could not be decoded.

func (*ParseError) Error

func (e *ParseError) Error() string

func (*ParseError) Unwrap

func (e *ParseError) Unwrap() error

type Prober

type Prober interface {
	// Status checks that the mirror is reachable and behaves like the
	// expected ecosystem.
	Status(ctx context.Context, mirror string) (StatusResult, error)
	// Speed measures download throughput from the mirror.
	Speed(ctx context.Context, mirror string, o SpeedOptions) (SpeedResult, error)
	// Package looks up a package on the mirror. A missing package is a
	// result (Found == false), not an error.
	Package(ctx context.Context, mirror string, q PackageQuery) (PackageResult, error)
	// Resolve returns a direct download URL for the package, issuing any
	// intermediate requests required (index lookups, registry tokens).
	Resolve(ctx context.Context, mirror string, q PackageQuery) (DownloadResult, error)
}

Prober is the uniform contract every backend implements. One interface, four probes, ecosystem-specific details carried in Options payloads.

type Progress

type Progress struct {
	Bytes    int64
	Total    int64 // 0 when unknown
	Elapsed  time.Duration
	SpeedMBs float64
}

Progress reports incremental speed-test state.

type RequestError

type RequestError struct {
	URL        string
	StatusCode int // 0 when the request did not complete
	Err        error
}

RequestError reports a failed HTTP request. Err wraps either a transport error (network layer) or nothing when the failure is status-based.

func (*RequestError) Error

func (e *RequestError) Error() string

func (*RequestError) Unwrap

func (e *RequestError) Unwrap() error

type ResultDetail

type ResultDetail interface {
	// contains filtered or unexported methods
}

ResultDetail is implemented by backend-specific payloads so a result can be re-expanded by consumers that know the ecosystem.

type SpeedError

type SpeedError struct {
	URL     string
	Bytes   int64
	Elapsed time.Duration
	Err     error
}

SpeedError reports a speed measurement that produced no usable sample.

func (*SpeedError) Error

func (e *SpeedError) Error() string

func (*SpeedError) Unwrap

func (e *SpeedError) Unwrap() error

type SpeedOptions

type SpeedOptions struct {
	// Duration bounds the download window. Zero uses the backend default
	// (10s). The overall deadline still comes from ctx.
	Duration time.Duration
	// MinBytes is the smallest sample considered valid; shorter downloads
	// yield an error rather than an inflated speed.
	MinBytes int64
	// TestFile overrides the backend's default test file (path relative to
	// the mirror URL). Empty uses the backend default.
	TestFile string
	// OnProgress receives incremental progress at most every ~200ms.
	OnProgress func(Progress)
	// Package, when non-empty, measures speed by downloading a file for
	// that specific package instead of the backend's generic test file.
	Package string
}

SpeedOptions configures a speed probe.

type SpeedResult

type SpeedResult struct {
	Envelope
	SpeedMBps     float64 `json:"speed_mbps"`
	Rating        string  `json:"rating"` // Slow / Average / Good / Excellent
	Bytes         int64   `json:"bytes"`
	DownloadedMB  float64 `json:"downloaded_mb"`
	DurationSec   float64 `json:"duration_sec"`
	TestURL       string  `json:"test_url"`
	ContentLength int64   `json:"content_length"`
}

SpeedResult reports measured throughput.

type StatusError

type StatusError struct {
	URL        string
	StatusCode int
}

StatusError reports an unexpected HTTP status on a completed response.

func (*StatusError) Error

func (e *StatusError) Error() string

func (*StatusError) Unwrap

func (e *StatusError) Unwrap() error

type StatusResult

type StatusResult struct {
	Envelope
	// WorkingPath is the first URL that answered successfully.
	WorkingPath string   `json:"working_path,omitempty"`
	StatusCode  int      `json:"status_code,omitempty"`
	Message     string   `json:"message,omitempty"`
	TestedPaths []string `json:"tested_paths"`
}

StatusResult reports mirror reachability.

type ValidationError

type ValidationError struct {
	Field   string
	Message string
}

ValidationError reports bad caller input.

func (*ValidationError) Error

func (e *ValidationError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL