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 ¶
- Variables
- func AssetGroups() map[string][]Asset
- func AssetNames() []string
- type Asset
- type AssetStatus
- type DownloadResult
- type Downloader
- func (d *Downloader) Clean() error
- func (d *Downloader) CopyAllToOutput(outputDir string) error
- func (d *Downloader) CopyToOutput(asset Asset, outputDir string) error
- func (d *Downloader) Download(ctx context.Context, asset Asset) (*DownloadResult, error)
- func (d *Downloader) DownloadAll(ctx context.Context, concurrency int) []DownloadResult
- func (d *Downloader) GetCachedPath(asset Asset) string
- func (d *Downloader) IsCached(asset Asset) bool
- func (d *Downloader) Status() []AssetStatus
Constants ¶
This section is empty.
Variables ¶
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 ¶
AssetGroups returns assets grouped by their library name. For example: {"glightbox": [...], "htmx": [...], ...}
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 GetAssetsByType ¶
GetAssetsByType returns all assets of a given type.
type AssetStatus ¶
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) 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.