c3b-assets
v0.6.1
Published
Fingerprints a project's images and syncs them to a Bunny storage zone, writing the d8e-asset-manifest.json d8e reads.
Readme
c3b-assets
Fingerprints a project's images, audio and video and syncs them to a Bunny storage zone, so
media never lives in git and never needs uploading by hand. Writes d8e-asset-manifest.json, which d8e
(1.2.0+) uses to point every reference at the CDN.
How it works
Run c3b-assets u in a project root whenever you add, change or remove an image under src/asset/. It:
- Fingerprints each image from its content —
asset/hero-banner.webpis stored ascolab-asset-manager/hero-banner.3f2a9c1e.webp. Change the image and it gets a new name automatically, so it can be cached forever and a stale copy can never be served. - Uploads the ones the storage zone doesn't have yet (checksum-verified).
- Writes
d8e-asset-manifest.json: each image's path undersrc/and its CDN URL. Commit this file — it's a few KB. - Deletes files nobody references any more, 7 days after they stopped being referenced, so pages in browser caches, sent emails and older staging builds keep working in the meantime.
Code keeps referencing images normally (<img src="asset/hero-banner.webp">, url(../asset/hero-banner.webp)), so
local dev still works by opening src/. At deploy, d8e rewrites those references to the CDN URLs and leaves the
images out of dist/.
It only ever lists, uploads to and deletes inside the zone's colab-asset-manager/ folder (the tool's old name,
kept so existing zones need no re-upload) — files uploaded by hand anywhere else in the zone are never touched.
Supported: images avif, gif, jpeg, jpg, png, svg, webp; audio mp3, m4a, ogg, wav; video mp4,
webm. Anything else under src/asset/ (fonts, say) stays in git and in dist/.
It runs on your machine only, never at deploy: the storage key stays off Heroku and Vercel, and staging can't delete production's images.
Setup
1. Install
npm install -g c3b-assets@latest2. Add c3b-assets.json to the project root
{
"storageZone": "viking-gear",
"storageHost": "jh.storage.bunnycdn.com",
"cdnUrl": "https://viking-gear-pz.b-cdn.net"
}storageZone: the storage zone's name.storageHost: optional, defaults tostorage.bunnycdn.com(Frankfurt). Use the zone's primary region:uk.,ny.,la.,sg.,se.,br.,jh.(Johannesburg) orsyd.storage.bunnycdn.com.cdnUrl: the pull zone connected to that storage zone.sdownloads from it with a plain GET, so leave the pull zone's Allowed Referrers, Block No-Referrer Requests and Token Authentication off, or the download gets a 403.
3. Gitignore the media
src/asset/**/*.avif
src/asset/**/*.gif
src/asset/**/*.jpeg
src/asset/**/*.jpg
src/asset/**/*.png
src/asset/**/*.svg
src/asset/**/*.webp
src/asset/**/*.mp3
src/asset/**/*.m4a
src/asset/**/*.ogg
src/asset/**/*.wav
src/asset/**/*.mp4
src/asset/**/*.webmIf the images are already committed, also run git rm -r --cached src/asset once (it keeps your local copies). Their
old versions stay in git history.
4. Set the storage key
BUNNY_STORAGE_KEY is the storage zone password: Bunny dashboard → Storage → your zone → FTP & API Access. Keep it in
your shell profile or a password manager — never in the repo.
Usage
c3b-assets # status: src/asset/ against the manifest — new, changed, missing, removed, retired. Changes nothing, no key needed
c3b-assets u # upload: fingerprint, upload new images, write d8e-asset-manifest.json (deletes need --allow-removals)
c3b-assets s # sync: download the images the manifest lists that are missing locally, then upload as "u"
c3b-assets a # adopt: no manifest yet (a repo started over)? rebuild it from what the zone holds, then download
c3b-assets --help # the commands and flagsa is for a checkout that has no d8e-asset-manifest.json but whose zone already has the images — a repo
started over, or a project joining an existing zone: it lists the tool's folder in the zone, reads every stored
image back to its manifest key (colab-asset-manager/arch/x.2ed3ab61.webp → asset/arch/x.webp), writes the
manifest and downloads as s does. A key stored under several fingerprints keeps the most recently changed copy
and retires the rest from today.
Over an existing manifest, a adds only what's missing: the files src/ references (outside src/asset/) that are
neither in the manifest nor on disk, taken from the zone and downloaded — the fix when another machine uploaded
images and its manifest never reached the repo. Nothing already in the manifest changes, and nothing is retired or
deleted. A file the zone holds under more than one fingerprint is refused, not guessed at; a file on neither is
counted, for whoever has it to upload.
Flags: --dry-run (report, change nothing), --allow-removals (see Safety checks). The version is printed first, so
you can tell which install is running.
s needs no key to download — the CDN is public — and only asks for BUNNY_STORAGE_KEY if there is something to
upload or delete afterwards. So a fresh clone on a machine without the key still gets its images.
Changing an image
- Fresh clone? Run
c3b-assets sfirst — images aren't in git, sosrc/asset/starts empty. - Replace, add or remove the file in
src/asset/(kebab-case names). Keeping the same name for a replacement is fine: the fingerprint changes anyway. Check it locally by openingsrc/. c3b-assetsto see what will happen,c3b-assets u --dry-runif you want the exact Bunny paths, thenc3b-assets u.- Commit
d8e-asset-manifest.json(plus any code change that references a new image) and deploy as usual.
If two people sync on different branches, d8e-asset-manifest.json conflicts on merge: pull both sets of images
into src/asset/ (c3b-assets s on each) and run c3b-assets u again — it rebuilds the manifest from what's there.
Safety checks
- Nothing is deleted just because it's missing locally. If images the manifest lists aren't in
src/asset/(a fresh clone, a half-copied folder),urefuses and says to runs. If you removed them on purpose, add--allow-removals. - An image someone else removed is not uploaded back. Images aren't in git, so after a teammate removes one your
src/asset/still has it. While Bunny holds the retired copy (the 7-day grace period), a local file with that exact content that nothing undersrc/names is reported asremovedand skipped byuands— delete your copy. Name it in a page again and it counts as new, which puts it back. After the grace period the record is gone and a left-over copy reads as new:c3b-lintthen flags it as an asset nothing uses. - The download step of
snever overwrites a local file that differs from Bunny — that's usually a change that hasn't been uploaded yet. It only fills in missing ones, and checks each download against its fingerprint. - Deletions only happen inside the zone's
colab-asset-manager/folder, 7 days after a file stopped being referenced, and only with--allow-removals: "unreferenced" is judged from the checkout you run in, which may be a branch that dropped imagesmainstill serves. A plainureports what is due and stops. A folder left empty by those deletions is deleted with them (Bunny keeps empty folders otherwise). - Only files this project's manifest has listed are ever retired or deleted. If two projects share a storage zone, neither touches the other's images.
- The download step of
sonly fetches image, audio and video files, and only from this project'scdnUrlfolder — the manifest is a committed file, so a tampered entry can't plant something else insrc/asset/. storageHostmust be a Bunny storage endpoint (storage.bunnycdn.com, optionally region-prefixed): the storage key is sent to it, and the config is committed.
Limitations
- Bunny storage zones have one full-access password (plus a read-only one, which isn't enough to upload). Anyone who uploads needs the full one: share it through a password manager, and regenerate it in Bunny if someone leaves.
- Videos and other large files stay where they are (Mux / Bunny Stream).
Tests
npm test — Node's built-in test runner, no extra dependency. test/c3b-assets.test.js runs the real CLI
against fixture projects and a fake Bunny (test/fake-bunny.js, installed as the global fetch), so nothing reaches
the network and no key is needed.
Deploying to npm
- FIRST TIME ONLY: run
npm login - Bump the version in
package.json npm test- Run
npm publish
