ghettocdn-helper
v1.2.1
Published
A programmatic client for resolving assets built with [`ghettocdn`](https://gitlab.com/its-astro/ghettocdn). Fetches your CDN's manifest and lets you resolve original asset names to their hashed, optimized output URLs.
Maintainers
Readme
ghettocdn-helper
A programmatic client for resolving assets built with ghettocdn. Fetches your CDN's manifest and lets you resolve original asset names to their hashed, optimized output URLs.
Installation
npm install ghettocdn-helperUsage
import { GhettoCDN } from "ghettocdn-helper"
const cdn = await GhettoCDN.create("https://your-cdn.example.com")
// Single-format asset
cdn.asset("config/config.json")
// => "https://your-cdn.example.com/config/config.9c8d7e6f.json"
// Multi-format asset — pick a format
cdn.asset("image/logo.image", "webp")
// => "https://your-cdn.example.com/image/logo.a1b2c3d4.webp"
// Multi-format asset — get first available format
cdn.asset("image/logo.image")
// => "https://your-cdn.example.com/image/logo.a1b2c3d4.webp"API
GhettoCDN.create(endpoint: string)
Fetches the manifest from {endpoint}/.ghettocdn/manifest.json and returns a GhettoCDN instance. This is the only way to construct an instance.
const cdn = await GhettoCDN.create("https://your-cdn.example.com")cdn.asset(name: string, format?: string): string
Resolves an asset name to its CDN URL.
name— the asset key as it appears in the manifest (e.g."image/logo.image","config/config.json")format— optional format to select for multi-format assets (e.g."webp","png")
If format is passed for a single-output asset, it is ignored with a console warning and the single URL is returned.
Throws if the asset name is not found in the manifest, or if the requested format is not available.
cdn.refetch()
Re-fetches the manifest from the CDN endpoint and updates the instance in place. Useful for long-running processes where the CDN may have been redeployed.
await cdn.refetch()Manifest Format
ghettocdn-helper is designed to work with the manifest generated by ghettocdn. Asset keys follow the same conventions:
- Multi-format assets (images, animations) use a generic type extension as their key (
.image,.animation) - Single-format assets use their actual extension (
.json,.yaml,.js, etc.)
{
"image/logo.image": {
"webp": "image/logo.a1b2c3d4.webp",
"png": "image/logo.e5f6a7b8.png"
},
"config/config.json": "config/config.9c8d7e6f.json"
}See the ghettocdn docs for more on how the manifest is generated.
License
BSD-3-Clause-Clear
