assets

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package assets provides functionality for downloading and self-hosting external CDN assets.

Overview

This package manages external JavaScript and CSS dependencies that are typically loaded from CDNs (like jsdelivr, unpkg, etc.). It provides:

  • A registry of known CDN assets with integrity hashes
  • Download functionality with caching
  • Integrity verification using SHA-256/384/512 hashes
  • Copy to output directory for self-hosting

Asset Registry

The registry defines all supported external assets with their CDN URLs, local paths, and optional SRI (Subresource Integrity) hashes:

asset := assets.GetAsset("glightbox-js")
// URL: https://cdn.jsdelivr.net/npm/glightbox@3.3.0/dist/js/glightbox.min.js
// LocalPath: glightbox/glightbox.min.js

Configuration

Asset handling is controlled by the Assets config in markata-go.toml:

[markata-go.assets]
mode = "self-hosted"  # "cdn", "self-hosted", or "auto"
cache_dir = ".markata/assets-cache"
verify_integrity = true

CLI Commands

The assets subcommand provides management tools:

markata-go assets download   # Download all CDN assets to cache
markata-go assets list       # Show status of all assets
markata-go assets clean      # Remove cached assets

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAssetNotFound     = errors.New("asset not found in registry")
	ErrIntegrityMismatch = errors.New("asset integrity check failed")
	ErrDownloadFailed    = errors.New("asset download failed")
)

Common errors for asset downloading.

Functions

func AssetGroups

func AssetGroups() map[string][]Asset

AssetGroups returns assets grouped by their library name. For example: {"glightbox": [...], "htmx": [...], ...}

func AssetNames

func AssetNames() []string

AssetNames returns the names of all registered assets.

Types

type Asset

type Asset struct {
	// Name is a unique identifier for the asset (e.g., "glightbox-js")
	Name string

	// URL is the full CDN URL for the asset
	URL string

	// LocalPath is the path relative to the vendor output directory
	// e.g., "glightbox/glightbox.min.js"
	LocalPath string

	// Integrity is the SRI hash (e.g., "sha384-...")
	// Empty string means no integrity verification
	Integrity string

	// Version is the asset version
	Version string

	// Type is the asset type: "js", "css", or "other"
	Type string
}

Asset represents an external CDN asset that can be self-hosted.

func GetAsset

func GetAsset(name string) *Asset

GetAsset returns an asset by name, or nil if not found.

func GetAssetsByType

func GetAssetsByType(assetType string) []Asset

GetAssetsByType returns all assets of a given type.

func Registry

func Registry() []Asset

Registry returns a copy of all registered assets.

type AssetStatus

type AssetStatus struct {
	Asset    Asset
	Cached   bool
	Size     int64
	CachedAt time.Time
}

AssetStatus represents the status of an asset.

type DownloadResult

type DownloadResult struct {
	Asset    Asset
	Cached   bool
	Error    error
	Size     int64
	Duration time.Duration
}

DownloadResult represents the result of downloading a single asset.

type Downloader

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

Downloader handles downloading and caching of CDN assets.

func NewDownloader

func NewDownloader(cacheDir string, verifyIntegrity bool) *Downloader

NewDownloader creates a new asset downloader.

func (*Downloader) Clean

func (d *Downloader) Clean() error

Clean removes all cached assets.

func (*Downloader) CopyAllToOutput

func (d *Downloader) CopyAllToOutput(outputDir string) error

CopyAllToOutput copies all cached assets to the output directory.

func (*Downloader) CopyToOutput

func (d *Downloader) CopyToOutput(asset Asset, outputDir string) error

CopyToOutput copies a cached asset to the output directory.

func (*Downloader) Download

func (d *Downloader) Download(ctx context.Context, asset Asset) (*DownloadResult, error)

Download downloads a single asset to the cache directory. Returns the cached file path on success.

func (*Downloader) DownloadAll

func (d *Downloader) DownloadAll(ctx context.Context, concurrency int) []DownloadResult

DownloadAll downloads all registered assets concurrently.

func (*Downloader) GetCachedPath

func (d *Downloader) GetCachedPath(asset Asset) string

GetCachedPath returns the path to the cached asset file. Returns empty string if not cached.

func (*Downloader) IsCached

func (d *Downloader) IsCached(asset Asset) bool

IsCached checks if an asset is already cached.

func (*Downloader) Status

func (d *Downloader) Status() []AssetStatus

Status returns the status of all assets.

Jump to

Keyboard shortcuts

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