crates

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: Apache-2.0 Imports: 16 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

Overview

Package crates is the Source for crates.io. It lists the versions of a crate, yanked ones included.

Two endpoints

The versions and their yank flags come from the sparse index, GET {index}/{prefix}/{name} (https://index.crates.io), a newline-delimited JSON file with one line per published version. The index is a static, CDN-served file with an ETag, so a poll that finds nothing new is one cheap 304. A yank or an un-yank rewrites the line of the version, which changes the ETag, so yank transitions are detected like new releases.

The index carries no timestamps. When (and only when) the index document has changed, and an API is configured, List reads created_at from the paginated GET {api}/api/v1/crates/{name}/versions?per_page=100 (https://crates.io), following meta.next_page for at most maxPages pages. That endpoint is used rather than the crate record because the record of a very large crate is itself above netguard's response cap. A 304 on the index costs no API request. If an API call fails, List fails and the fetch state is not advanced, so the next poll retries both. If the API does not yet list some version that the index has (the two are not updated atomically), List returns the versions it has, those with a zero PublishedAt, but hands back the incoming fetch state unchanged, so the next poll refetches both and fills the times in.

The public index uses the public crates.io API. A custom index (a mirror or an alternative registry) has no API by default, since a mirror rarely serves crates.io's API: its versions have a zero PublishedAt unless WithAPIURL names one.

Size cap

The index file of a very large crate (swc_core: about 16 MiB) is above netguard's global 10 MiB response cap. netguard gives index.crates.io a built-in 64 MiB cap; a custom index host keeps the global cap unless the operator sets HostLimit.MaxBodyBytes for it. A crate over its host's cap fails on every poll with an error wrapping netguard.ErrBodyTooLarge, and the state is not advanced.

Names

crates.io treats names case-insensitively but "-" and "_" as different characters ("serde_json" is not "serde-json"), so Normalize only lower-cases and the coalescing key keeps "-" and "_" apart. Alternative registries may allow a leading "_", which is rejected here.

Crawler policy

crates.io asks crawlers to send a User-Agent that identifies them with a way to get in touch, and to make at most one request per second to crates.io. netguard's default User-Agent is "angelia/<version> (+https://github.com/angelia-dev/angelia)", which satisfies the first rule; operators who run Angelia at scale should set netguard.Config.UserAgent to something with their own contact. The second is netguard.DefaultHostLimits: crates.io (the API) is limited to 1 request per second. index.crates.io is the CDN meant for bulk reads and is not covered by the crawler policy; it uses netguard's fallback limit.

Errors and ordering

A crate that does not exist is a 404 from the index, returned as an error wrapping *netguard.StatusError, never as an empty result. Versions are sorted newest first by PublishedAt; versions without one, and versions with equal ones, follow in reverse index order (the index is append-only, oldest first).

What a Version carries

Raw is the "vers" of the index line. Yanked is its "yanked" flag. PublishedAt is the API's created_at. Prerelease is left nil: crates.io has no such flag and the core derives it from the version. Metadata has "checksum" (the index's cksum), and "rust_version" and "license" when known.

Index

Constants

View Source
const (
	// Kind is the registry key of this source.
	Kind = "crates"
	// DefaultIndexURL is the public sparse index.
	DefaultIndexURL = "https://index.crates.io"
	// DefaultAPIURL is the public crates.io API.
	DefaultAPIURL = "https://crates.io"
	// DefaultPageURL is the prefix of crate pages on crates.io.
	DefaultPageURL = "https://crates.io/crates"
)

Variables

This section is empty.

Functions

func Normalize

func Normalize(name string) string

Normalize returns the canonical form of a crate name, the one used in the index path and in the target coalescing key: lower case. "-" and "_" are not folded, they are different characters on crates.io.

Types

type Option

type Option func(*Source)

Option configures a Source.

func WithAPIURL

func WithAPIURL(u string) Option

WithAPIURL sets the base URL of the crates.io API, used for created_at. The default is DefaultAPIURL when the index is the public one, and otherwise none: no API request is made and PublishedAt stays zero.

func WithPageURL

func WithPageURL(prefix string) Option

WithPageURL sets the prefix of the release page URL, to which "/{name}/{version}" is appended. Without it, URL is set only for the public registry.

type Source

type Source struct {
	// contains filtered or unexported fields
}

Source implements source.Source for crates.io.

func New

func New(indexURL string, hc *http.Client, opts ...Option) *Source

New returns a crates Source reading the sparse index at indexURL (DefaultIndexURL if empty) with hc, normally the HTTPClient of a netguard client.

func (*Source) Canonical

func (s *Source) Canonical(t source.Target) (source.Target, error)

Canonical implements source.Canonicalizer: the "name" coordinate is lower-cased, so "Serde" and "serde" share one target. crates.io does not allow two crates whose names differ only in case and resolves either spelling to the same crate; the sparse index files are named by the lower-case name (https://doc.rust-lang.org/cargo/reference/registry-index.html#index-files). crates.io does not allow two names that differ only in "-"/"_" either, but the sparse index resolves only the separator spelling the crate was published with, so folding them would need I/O: they are kept apart. See ADR 0021.

func (*Source) Kind

func (*Source) Kind() string

Kind implements source.Source.

func (*Source) List

List implements source.Source.

func (*Source) Validate

func (*Source) Validate(t source.Target) error

Validate implements source.Source. The only coordinate is "name", the crate name. It does no I/O.

Jump to

Keyboard shortcuts

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