mirror

package
v0.23.5 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: Apache-2.0 Imports: 19 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 mirror downloads the files of GitHub releases from github.com or from the family's mirror, https://mirror.actnow.dev/github, which answers GitHub's paths for the releases the apps need (spec §9.6):

/<owner>/<repo>/releases/latest                    GitHub: a redirect to …/releases/tag/<tag>;
                                                   the mirror: JSON {"tag": "<tag>"}
/<owner>/<repo>/releases/download/<tag>/<asset>    the file

GitHub's release files reach many users in China at a few KB/s; the mirror (Cloudflare R2) at a few MB/s. Neither is trusted: the caller checks what it gets as it always did (a signature, a pinned SHA-256), and a file that fails its check is fetched from the next source.

Which sources: Sources maps the family's download source setting (network.hfEndpoints): automatic → both, raced; "official" → github.com only; "mirror" → the mirror only.

c := &mirror.Client{Sources: mirror.Sources(hfEndpoints), HTTP: client}
tag, _, err := c.Latest(ctx, "yt-dlp/yt-dlp", validTag)
file, from, err := c.File(ctx, "/yt-dlp/yt-dlp/releases/download/"+tag+"/yt-dlp_macos.zip",
	mirror.FileOptions{Dir: dir, SHA256: sum})

Index

Constants

View Source
const (
	// GitHub is github.com.
	GitHub = "https://github.com"
	// ActNow is the family's mirror of the releases the apps download.
	ActNow = "https://mirror.actnow.dev/github"
	// EnvSources, when set, replaces the sources: base URLs separated by
	// spaces (development and tests).
	EnvSources = "ACTNOW_GITHUB_SOURCES"
)

The sources.

View Source
const (
	DefaultLead       = 256 << 10
	DefaultStall      = 20 * time.Second
	DefaultMinRate    = 32 << 10
	DefaultRateWindow = 30 * time.Second
)

Defaults of the Client's tuning.

Variables

View Source
var (
	ErrStalled = errors.New("the download stalled")
	ErrTooSlow = errors.New("the download is too slow")
)

Why a source is left.

View Source
var (
	// TextSourceAuto: the automatic choice (the default).
	TextSourceAuto = family.Text{EN: "Automatic (the fastest of the official sites and mirrors)", ZH: "自动(官方站点和镜像中最快的)"}
	// TextSourceOfficial: "official".
	TextSourceOfficial = family.Text{EN: "Official sites only (huggingface.co, github.com)", ZH: "仅官方站点(huggingface.co、github.com)"}
	// TextSourceMirror: "mirror".
	TextSourceMirror = family.Text{EN: "Mirrors only (hf-mirror.com, mirror.actnow.dev)", ZH: "仅镜像(hf-mirror.com、mirror.actnow.dev)"}
	// TextSourceNote: under the setting.
	TextSourceNote = family.Text{
		EN: "Models come from Hugging Face, ffmpeg and yt-dlp from GitHub. Every download is checked (signature or checksum), so a mirror cannot change what you get.",
		ZH: "模型来自 Hugging Face,ffmpeg 和 yt-dlp 来自 GitHub。所有下载都会校验(签名或校验和),镜像无法改动你拿到的文件。",
	}
)

Reference wordings of the download source setting (network.hfEndpoints, spec §9.6), which covers models (Hugging Face) and tools (GitHub), for the apps' catalogues. Apps whose downloads are only models keep naming Hugging Face hosts only.

View Source
var ErrChecksum = errors.New("checksum mismatch")

ErrChecksum: what a source served does not match the digest.

Functions

func Hosts

func Hosts(sources []string) []string

Hosts are the origins of the sources (for a connection test).

func Sources

func Sources(hfEndpoints string) []string

Sources are the sources for a value of network.hfEndpoints (spec §9.6): "official" → github.com; "mirror" → the ActNow mirror; anything else ("", "auto", URLs of Hugging Face mirrors) → both. $ACTNOW_GITHUB_SOURCES replaces them.

func Try

func Try[T any](ctx context.Context, c *Client, fn func(ctx context.Context, base string) (T, error)) (T, string, error)

Try runs fn for every source at once and returns the first result without an error and its source; the other calls are cancelled. With one source fn runs alone. When every call fails the errors are joined, each prefixed by its source. Use it for what is checked as a whole from one source (a checksum file and its signature).

Types

type ChecksumError

type ChecksumError struct{ URL, Got string }

ChecksumError is a download that did not match its digest.

func (*ChecksumError) Error

func (e *ChecksumError) Error() string

func (*ChecksumError) Is

func (e *ChecksumError) Is(target error) bool

type Client

type Client struct {
	// Sources are the base URLs, in order of preference when they tie
	// (nil: Sources("")).
	Sources []string
	// HTTP is the client requests go through, asked for each download so
	// that a changed proxy applies (nil, or returning nil:
	// http.DefaultClient).
	HTTP func() *http.Client
	// Logf, when set, is told which source served what and why one was
	// left.
	Logf func(format string, args ...any)

	// Lead is how many bytes of a file decide the race between sources
	// (0: DefaultLead).
	Lead int64
	// Stall: a source that sends nothing for this long is left for the
	// next one (0: DefaultStall).
	Stall time.Duration
	// MinRate: a source slower than this many bytes per second over
	// RateWindow is left for the next one (0: DefaultMinRate). Neither
	// applies to the last source left.
	MinRate    int64
	RateWindow time.Duration
}

Client downloads from the sources. The zero Client uses both sources and http.DefaultClient. Safe for concurrent use.

func (*Client) Bytes

func (c *Client) Bytes(ctx context.Context, p string, limit int64, check func([]byte) error) ([]byte, string, error)

Bytes downloads p (at most limit bytes) from the first source that serves it and check accepts (nil: any).

func (*Client) File

func (c *Client) File(ctx context.Context, p string, o FileOptions) (string, string, error)

File downloads p (a path under the sources, "/owner/repo/releases/ download/tag/asset") into a temporary file in o.Dir and checks its SHA-256; it returns the file (the caller renames or removes it) and the source it came from.

Every source starts at once; the first to deliver Lead bytes (or the whole file) wins and the others stop. When the winner fails, stalls (Stall), falls below MinRate over RateWindow, or serves a file that does not match, the sources left race again from the start; a source left for stalling or crawling races once more, on a new connection (the last attempt is never left for being slow). A failure of every source returns their errors joined (errors.Is(err, ErrChecksum) when one served a wrong file).

func (*Client) Latest

func (c *Client) Latest(ctx context.Context, repo string, ok func(tag string) bool) (string, string, error)

Latest is the tag of repo's ("owner/name") latest release, asked of every source at once: the first tag that ok accepts (nil: any) wins. GitHub answers /releases/latest with a redirect to …/releases/tag/<tag>; the ActNow mirror, files served by R2, which cannot redirect, with JSON {"tag": "<tag>"}. Either answer is read from any source.

func (*Client) Small

func (c *Client) Small(ctx context.Context, base, p string, limit int64) ([]byte, error)

Small downloads base+p, at most limit bytes, from one source.

type FileOptions

type FileOptions struct {
	// Dir is where the temporary file is made ("": os.TempDir()); on
	// the same volume as where it goes, it can be renamed into place.
	Dir string
	// SHA256 is the file's digest (hex): what is downloaded must match.
	SHA256 string
	// Size is the file's size when known, for progress before a server
	// says it (0: the server's).
	Size int64
	// Limit is the most bytes accepted (0: 4 GiB).
	Limit int64
	// Progress, when set, gets bytes done and the total (0 when unknown).
	// Done can go back when a source is left for another.
	Progress func(done, total int64)
}

FileOptions are File's.

type StatusError

type StatusError struct {
	URL    string
	Status string
	Code   int
}

StatusError is a source's answer other than 200 (or a redirect where one is expected).

func (*StatusError) Error

func (e *StatusError) Error() string

Jump to

Keyboard shortcuts

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