Skip to content

Open source CapCut alternative.

TypeScript

docs: shared media at the edge in the Cut and worker guides

David Lef3f91ff58f90
diff --git a/docs/guides/cut/README.md b/docs/guides/cut/README.md
index 3840ded3..af797121 100644
--- a/docs/guides/cut/README.md
+++ b/docs/guides/cut/README.md
@@ -32,7 +32,7 @@ Every client call goes through a backend seam (`site/src/cut/lib/backend/`): the
Cloud projects store their doc in Postgres (versioned; a stale autosave 409s and the editor reloads the newer copy) and media in R2: uploads go direct from the browser on presigned URLs (the hosted functions never see the bytes), playback streams on signed range URLs, and a flat per-user quota is enforced at presign time.
-A cloud project can be shared read-only, Google-Docs style: the editor's Share dialog invites specific emails (viewers sign in) or opens the link to anyone (no sign-in), and switches opt extra surfaces in — chat, media, AI generations, subtitles, details — on top of the always-shared preview and timeline. The share row's id is the link token, so revoking deletes the link. Viewers ride a third backend driver against `/api/cut-shared/<token>/*`, a deliberately public API surface (the one exception to session auth on the cloud side): the server resolves access per request, filters the doc to the shared surfaces before it leaves, and refuses media outside that filter, so hiding is enforced by the backend rather than the client. A public share's reads are cached at the edge for a lifetime that tracks how long the owner has been idle — a doc saved seconds ago is served near-live, one untouched for an hour is served from cache for minutes — and the same number is handed back as the viewer's poll interval, so a link a crowd opens costs a handful of origin requests rather than one per viewer per tick. A restricted share's reads depend on the viewer's session, so they are never cached. Signed media URLs are minted on fixed windows instead of at the instant asked, which is what lets a viewer's browser (and anything in front of it) reuse bytes across reloads. A shared link unfurls with the project's own card: the render worker derives a first-frame still and a five-second animation from the opening of the cut, the page's metadata points at them through a URL carrying the doc version, and a project with no card yet gets a drawn one. Viewers poll the doc version on an interval the server picks — shared views trail live edits, they don't stream them — and a "Copy project" action duplicates everything the share exposes — the filtered doc, its media, and the chat threads when chat is shared — server-side into the viewer's own account and quota. Copies queue rather than run in the request: each request writes a job row and publishes it to a Cloudflare Queue whose serial consumer executes one copy at a time, so a burst of copy requests can't stampede storage; the client polls the job for the outcome. An owner duplicating their own cloud project from the dashboard rides the same queue. Exports, hover-preview proxies, share cards, and URL imports run on the render worker — a container (`site/src/cut/worker/`) that claims job rows from Postgres, reuses the engine's exact ffmpeg pipeline against R2, and reports progress back through the rows the client polls. Transcription and mic dictation call a hosted, credit-metered route that has Gemini produce cue-level timestamps from browser-rendered audio chunks; word timings are interpolated within each cue. The assistant offers its Gemini models (which already run entirely in the page); the Claude/Codex CLI providers stay app-only because they are the user's local logins.
+A cloud project can be shared read-only, Google-Docs style: the editor's Share dialog invites specific emails (viewers sign in) or opens the link to anyone (no sign-in), and switches opt extra surfaces in — chat, media, AI generations, subtitles, details — on top of the always-shared preview and timeline. The share row's id is the link token, so revoking deletes the link. Viewers ride a third backend driver against `/api/cut-shared/<token>/*`, a deliberately public API surface (the one exception to session auth on the cloud side): the server resolves access per request, filters the doc to the shared surfaces before it leaves, and refuses media outside that filter, so hiding is enforced by the backend rather than the client. A public share's reads are cached at the edge for a lifetime that tracks how long the owner has been idle — a doc saved seconds ago is served near-live, one untouched for an hour is served from cache for minutes — and the same number is handed back as the viewer's poll interval, so a link a crowd opens costs a handful of origin requests rather than one per viewer per tick. A restricted share's reads depend on the viewer's session, so they are never cached. A shared project's media is served from Cloudflare's edge rather than presigned against R2's S3 endpoint, which nothing caches: a Worker holding a private bucket binding checks a short token the hosted API mints, then answers from cache keyed on the object alone. Dropping the token from the cache key is what makes the token affordable to keep short — every viewer and every re-mint share one cached copy — and a short token is what makes unsharing mean something, since a presigned URL is a bearer credential nothing can withdraw before it expires. Owner-side editing still presigns R2 directly, where the URLs are minted on fixed windows so a browser can reuse bytes across reloads. A shared link unfurls with the project's own card: the render worker derives a first-frame still and a five-second animation from the opening of the cut, the page's metadata points at them through a URL carrying the doc version, and a project with no card yet gets a drawn one. Viewers poll the doc version on an interval the server picks — shared views trail live edits, they don't stream them — and a "Copy project" action duplicates everything the share exposes — the filtered doc, its media, and the chat threads when chat is shared — server-side into the viewer's own account and quota. Copies queue rather than run in the request: each request writes a job row and publishes it to a Cloudflare Queue whose serial consumer executes one copy at a time, so a burst of copy requests can't stampede storage; the client polls the job for the outcome. An owner duplicating their own cloud project from the dashboard rides the same queue. Exports, hover-preview proxies, share cards, and URL imports run on the render worker — a container (`site/src/cut/worker/`) that claims job rows from Postgres, reuses the engine's exact ffmpeg pipeline against R2, and reports progress back through the rows the client polls. Transcription and mic dictation call a hosted, credit-metered route that has Gemini produce cue-level timestamps from browser-rendered audio chunks; word timings are interpolated within each cue. The assistant offers its Gemini models (which already run entirely in the page); the Claude/Codex CLI providers stay app-only because they are the user's local logins.
## One API surface, one router
diff --git a/site/src/cut/worker/README.md b/site/src/cut/worker/README.md
index a11d3f27..f0b9083a 100644
--- a/site/src/cut/worker/README.md
+++ b/site/src/cut/worker/README.md
@@ -74,6 +74,40 @@ Manual deploy from `site/`:
npm run worker:build && npx wrangler deploy -c src/cut/worker/wrangler.jsonc
```
+## Shared media at the edge
+
+The Worker also serves a shared project's media (`cf/media.ts`), on its own
+hostname and its own R2 binding — the bucket stays private, so this handler is
+the only way in. The hosted API mints a short HMAC token per URL; the Worker
+checks it, then serves the object from Cloudflare's cache **keyed on the object
+path alone**, with the token dropped. That is what lets the token be short:
+every viewer and every re-mint share one cached copy instead of missing per
+URL.
+
+Two properties fall out of it. Unsharing takes effect within a token's life
+(five minutes) rather than the day a presigned R2 URL would keep working. And
+media is served from the edge instead of R2's S3 endpoint, which Cloudflare
+does not cache at all.
+
+Range requests are served from the cached object (Cloudflare answers `206` from
+a stored `200`); a miss streams the asked-for bytes from R2 and warms the full
+object in the background. Objects past Cloudflare's 512 MB cacheable ceiling
+stream straight through, uncached.
+
+One-time setup:
+
+1. The zone must be on Cloudflare — the Cache API is a no-op on `workers.dev`,
+ so `routes` has to be a real hostname. `wrangler.jsonc` claims
+ `media.donkeycut.com` as a custom domain and sets `CUT_MEDIA_HOST` to match;
+ change both together, or comment both out to leave the feature off.
+2. Worker secret: `npx wrangler secret put CUT_MEDIA_SIGNING_SECRET -c
+ src/cut/worker/wrangler.jsonc` (any long random string).
+3. On the hosted site (Vercel env): `CUT_MEDIA_BASE_URL` = `https://` + that
+ hostname, and `CUT_MEDIA_SIGNING_SECRET` = the same string.
+
+Without those two site variables the hosted API presigns R2 exactly as before,
+so local dev and an un-migrated deployment both keep working.
+
## Copy queue
The Worker is also the consumer of the `donkey-cut-copy` Cloudflare Queue: