pack

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxPackageSize int64 = 100 * 1024 * 1024  // 100MB
	DefaultCacheTTL             = 7 * 24 * time.Hour // 7 days
)
View Source
const (
	Extension    = ".zip"
	ManifestFile = "manifest.toml"
	LibDir       = "lib"
	DocsDir      = "docs"
)

Variables

View Source
var (
	ErrInvalidPackage  = errors.New("invalid package format")
	ErrMissingManifest = errors.New("missing manifest.toml")
	ErrInvalidManifest = errors.New("invalid manifest format")
	ErrModuleNotFound  = errors.New("module not found in package")
	ErrFetchFailed     = errors.New("failed to fetch package")
)

Functions

func ClearCache

func ClearCache(cacheDir string) error

ClearCache removes all cached packages from cacheDir. If cacheDir is empty, uses the OS default cache directory.

func DefaultCacheDir

func DefaultCacheDir() (string, error)

DefaultCacheDir returns the default cache directory for packages.

func Fetch

func Fetch(source string, insecure bool) ([]byte, error)

Fetch loads bytes from a URL or local path. For URLs, uses the cache with ETag/Last-Modified freshness checks.

func FetchFile

func FetchFile(path string, maxSize ...int64) ([]byte, error)

FetchFile loads from local filesystem.

func FetchWithCache

func FetchWithCache(source string, insecure bool, cacheDir string, maxSize ...int64) ([]byte, error)

FetchWithCache loads bytes from a URL or local path, using cacheDir for remote URLs. If cacheDir is empty, uses the OS default cache directory. An optional #sha256=<hex> fragment on source is stripped before fetching and used to verify the downloaded bytes; a mismatch is a fatal error. maxSize limits download size (0 = use DefaultMaxPackageSize).

func HashBytes

func HashBytes(data []byte) string

HashBytes returns the SHA-256 hex digest of data.

func IsURL

func IsURL(source string) bool

IsURL returns true if source starts with http:// or https://.

func Pack

func Pack(srcDir, dst string, force bool) (string, error)

Pack creates a package from srcDir, writing to dst. Reads manifest.toml from srcDir. Use force to overwrite an existing dst. Returns the SHA-256 hex hash of the written package.

func PruneCache

func PruneCache(cacheDir string, ttl time.Duration) error

PruneCache removes cache entries that have not been accessed within ttl. If cacheDir is empty, uses the OS default cache directory. If ttl is 0, uses DefaultCacheTTL. Each cache entry is a .zip/.meta pair; the .zip mod time tracks last access.

func Unpack

func Unpack(src string, opts UnpackOptions) error

Unpack extracts a package from a local path or URL.

func UnpackRemove

func UnpackRemove(src string, insecure bool, destDir string) error

UnpackRemove removes the files that would be extracted from a package.

Types

type DirDocReader

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

DirDocReader reads docs from an unpacked package directory.

func NewDirDocReader

func NewDirDocReader(dir string) *DirDocReader

NewDirDocReader creates a DocReader for an unpacked package directory.

func (*DirDocReader) ListDocs

func (r *DirDocReader) ListDocs() []string

func (*DirDocReader) Name

func (r *DirDocReader) Name() string

func (*DirDocReader) ReadDoc

func (r *DirDocReader) ReadDoc(name string) ([]byte, error)

type DocReader

type DocReader interface {
	// Name returns a display name for this source.
	Name() string
	// ListDocs returns all doc file paths relative to docs/ (e.g. "guide.md").
	ListDocs() []string
	// ReadDoc reads a doc file by its relative path.
	ReadDoc(name string) ([]byte, error)
}

DocReader provides access to docs/ content from a package source.

type Loader

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

Loader implements libloader.LibraryLoader over a set of packages. Packages are searched in reverse order (last added = highest priority).

func NewLoader

func NewLoader() *Loader

NewLoader creates a new Loader.

func (*Loader) AddFromPath

func (l *Loader) AddFromPath(source string, insecure bool) error

AddFromPath loads a .zip package from a local path or URL. source may include a #sha256=<hex> fragment for integrity verification.

func (*Loader) AddPackage

func (l *Loader) AddPackage(p *Package)

AddPackage adds a package to the loader.

func (*Loader) Description

func (l *Loader) Description() string

Description implements libloader.LibraryLoader.

func (*Loader) GetMainEntry

func (l *Loader) GetMainEntry() (module, function string, found bool)

GetMainEntry returns the main entry point from the last package that defines one. Returns module, function, and whether one was found.

func (*Loader) Load

func (l *Loader) Load(name string) (string, bool, error)

Load implements libloader.LibraryLoader. Searches packages in reverse order (last = highest priority), then fallback.

func (*Loader) SetCacheDir

func (l *Loader) SetCacheDir(dir string)

SetCacheDir overrides the default OS cache directory for remote packages.

func (*Loader) SetFallback

func (l *Loader) SetFallback(fallback libloader.LibraryLoader)

SetFallback sets the fallback loader used when no package provides the module.

type Manifest

type Manifest struct {
	Name        string `toml:"name"`
	Version     string `toml:"version"`
	Description string `toml:"description,omitempty"`
	Main        string `toml:"main,omitempty"` // module.function entry point
}

Manifest describes package metadata.

func ReadManifestFromDir

func ReadManifestFromDir(dir string) (Manifest, error)

ReadManifestFromDir reads manifest.toml from a source directory.

type Package

type Package struct {
	Manifest Manifest
	// contains filtered or unexported fields
}

Package represents a loaded package. All file contents are decompressed into memory at Open time. docs/ entries are intentionally excluded; use ZipDocReader for those.

func Open

func Open(r io.ReaderAt, size int64) (*Package, error)

func OpenFile

func OpenFile(path string) (*Package, error)

OpenFile opens a package from a local file path.

func OpenURL

func OpenURL(url string, insecure bool) (*Package, error)

OpenURL opens a package from a URL.

func (*Package) HasDocs

func (p *Package) HasDocs() bool

HasDocs returns true if the package contains a docs folder. Since docs/ is not loaded into p.files, we track this separately.

func (*Package) List

func (p *Package) List(dir string) []string

List returns file names under a directory prefix within the package.

func (*Package) ReadFile

func (p *Package) ReadFile(name string) ([]byte, error)

ReadFile reads a file from the package by path.

type UnpackOptions

type UnpackOptions struct {
	DestDir  string
	Force    bool
	List     bool
	Insecure bool
}

UnpackOptions configures extraction behaviour.

type ZipDocReader

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

ZipDocReader reads docs from a zip package file.

func NewZipDocReader

func NewZipDocReader(src string, insecure bool) (*ZipDocReader, error)

NewZipDocReader opens a zip and extracts only the docs/ entries.

func (*ZipDocReader) ListDocs

func (r *ZipDocReader) ListDocs() []string

func (*ZipDocReader) Name

func (r *ZipDocReader) Name() string

func (*ZipDocReader) ReadDoc

func (r *ZipDocReader) ReadDoc(name string) ([]byte, error)

Jump to

Keyboard shortcuts

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