cargo

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: AGPL-3.0 Imports: 9 Imported by: 1 module, 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

Overview

Package cargo holds the parts of the Cargo sparse registry protocol that proxy and group repositories need: request-path parsing, index file validation and merging, the registry config.json and its download template.

Index

Constants

View Source
const MaxIndexBytes = 64 << 20

MaxIndexBytes bounds one index file read into memory. The largest crates have a few thousand versions, a few MiB of index.

Variables

View Source
var ErrBadPath = errors.New("not a cargo registry path")

ErrBadPath is returned for a path that is not a registry request.

Functions

func DownloadURL

func DownloadURL(dl, crate, version, cksum string) string

DownloadURL expands a registry's "dl" template for one crate version as cargo does: the markers {crate}, {version}, {prefix}, {lowerprefix} and {sha256-checksum} are substituted; without any marker the template is a base URL and "/{crate}/{version}/download" is appended.

func IndexPathFor

func IndexPathFor(name string) string

IndexPathFor returns the path of crate's index file below the index root: "1/<n>", "2/<n>", "3/<c>/<n>" or "<ab>/<cd>/<n>". name must be ASCII.

func MergeIndex

func MergeIndex(docs [][]byte) ([]byte, error)

MergeIndex merges the index files docs of one crate (in member priority order): the union of their versions, the first file that lists a version providing its entry. Entries keep the order they first appear in.

func RenderConfig

func RenderConfig(baseURL, repoName string, public bool) []byte

RenderConfig is the config.json a hosted, proxy or group repository serves: dl and api point at the repository under baseURL, the origin the client reached (absolute: cargo concatenates the dl template and never resolves it against the index URL), and a private repository makes cargo send its token with every request.

func ValidCksum

func ValidCksum(s string) bool

ValidCksum reports whether s is a hex sha256 digest.

func VerifyConfig

func VerifyConfig(spool io.ReadSeeker) error

VerifyConfig reports whether spool holds a registry config.json: a JSON object with a string "dl".

func VerifyIndex

func VerifyIndex(spool io.ReadSeeker) error

VerifyIndex reports whether doc, read from spool, is a well-formed index file; it is the integrity check applied to an upstream answer before it is cached.

Types

type Config

type Config struct {
	DL string `json:"dl"`
}

Config is the part of a registry config.json this package reads.

func ParseConfig

func ParseConfig(r io.Reader) (Config, error)

ParseConfig reads a registry config.json.

type Entry

type Entry struct {
	Vers  string
	Cksum string
	// Pubtime is the entry's "pubtime", the publish time as the index states it
	// (RFC 3339, UTC); "" when the index does not carry one. See Published.
	Pubtime string
	// Line is the raw JSON of the entry, without the newline.
	Line []byte
}

Entry is one line of an index file.

func FindVersion

func FindVersion(doc []byte, vers string) (Entry, bool, error)

FindVersion returns the entry of index doc whose version is exactly vers.

func ParseIndex

func ParseIndex(doc []byte) ([]Entry, error)

ParseIndex parses the newline-delimited JSON of an index file. Every non-blank line must be a JSON object with string "name" and "vers"; an index file without entries is refused.

func (Entry) Published added in v0.16.0

func (e Entry) Published() time.Time

Published is the publish time of the entry's version, the zero time when the index does not state one or states one that does not parse.

type Kind

type Kind int

Kind is the type of a registry request path.

const (
	// KindConfig is config.json (at the registry root or below index/).
	KindConfig Kind = iota
	// KindIndex is a sparse index file: index/<prefix>/<crate>.
	KindIndex
	// KindDownload is api/v1/crates/<crate>/<version>/download.
	KindDownload
)

type Path

type Path struct {
	Kind Kind
	// Crate is the crate name; lowercase for KindIndex.
	Crate string
	// Version is set for KindDownload.
	Version string
	// IndexPath is the path of a KindIndex file below index/, e.g. "se/rd/serde".
	IndexPath string
}

Path is a parsed registry request path.

func ParsePath

func ParsePath(relPath string) (Path, error)

ParsePath classifies relPath, the path below the repository root. Index paths must use the canonical lowercase layout cargo requests.

Jump to

Keyboard shortcuts

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