Open source CapCut alternative.
TypeScript
fix(site): hold the browser's cloud media cache under a 10 GB ceiling
David Le4945bbeb3ee5
diff --git a/site/src/cut/components/RequireSession.tsx b/site/src/cut/components/RequireSession.tsxindex 9d98f032..4ba89d92 100644--- a/site/src/cut/components/RequireSession.tsx+++ b/site/src/cut/components/RequireSession.tsx@@ -1,6 +1,7 @@"use client";import { bindChatRuntime, bindChatgptPolling, bindCutClip, bindCutJudge, bindCutNotes } from "@/cut/lib/chatRuntime";import { bindCutColor } from "@/cut/lib/colorSettings";+import { bindCutMediaCache } from "@/cut/lib/chunkCache";import { useEffect, type ReactNode } from "react";import { useEnvironment } from "@/cut/lib/environment";@@ -47,6 +48,7 @@ export function RequireSession({ children }: { children: ReactNode }) {bindCutClip(config.data.settings.cutClip);bindCutNotes(config.data.settings.cutNotes);bindCutColor(config.data.settings.cutColor);+ bindCutMediaCache(config.data.settings.cutMediaCache);reportExposures(config.data);}, [userId, config.data]);diff --git a/site/src/cut/lib/chunkCache.ts b/site/src/cut/lib/chunkCache.tsindex e5b8770c..c3a22461 100644--- a/site/src/cut/lib/chunkCache.ts+++ b/site/src/cut/lib/chunkCache.ts@@ -53,6 +53,7 @@ import {} from "./backend/browser/opfs";import { CUT_MEDIA_ORIGIN } from "./hosts";import { allowance, holdMemory } from "./memoryBudget";+import { SETTINGS, type Settings } from "@/lib/config/registry";export const CHUNK_SIZE = 2 * 1024 * 1024;/** Longest single ranged request, in chunks, when a read needs a missing run. */@@ -70,6 +71,8 @@ const FETCH_ATTEMPTS = 3;* refusal is a moment's verdict: an eviction elsewhere can free hundreds of* megabytes seconds later, and an object that latched off would never notice. */const PERSIST_RETRY_MS = 30_000;+/** How long one walk of the store's metas stands in for the next. */+const TALLY_TTL_MS = 60_000;/** Chunks written between meta commits. The resident map is what saves a* reopened file from enumerating thousands of directory entries, and it is* safe to lag: a claim the disk can't back costs one refetch. */@@ -746,11 +749,42 @@ async function persistChunk(state: ObjState, idx: number, bytes: Uint8Array): Pr// A cache write only; the bytes were already served. The reservation goes// back so a run of failures can't talk the store into believing it's full.refund(bytes.length);+ if (tally) tally.bytes = Math.max(0, tally.bytes - bytes.length);}}let lastEstimate: { at: number; usage: number; quota: number } | null = null;+// The browser grants an origin most of the disk, so the quota alone would let+// cached media fill the Mac. The cache keeps to its own ceiling, counted over+// the chunks here and the whole-file copies mediaSync reports.+let cacheSetting: Settings["cutMediaCache"] = SETTINGS.cutMediaCache.default;+export function bindCutMediaCache(value: unknown): void {+ const parsed = SETTINGS.cutMediaCache.schema.safeParse(value);+ if (parsed.success) cacheSetting = parsed.data;+}+export function cutMediaCache(): Settings["cutMediaCache"] {+ return cacheSetting;+}++let tally: { at: number; bytes: number } | null = null;+let copyBytes = 0;++/** What the chunk store holds, from its metas; writes and evictions keep the+ * count between walks. */+export async function chunkStoreBytes(): Promise<number> {+ if (!tally || Date.now() - tally.at > TALLY_TTL_MS) {+ const bytes = (await listCached()).reduce((sum, c) => sum + c.bytes, 0);+ tally = { at: Date.now(), bytes };+ }+ return tally.bytes;+}++/** The whole-file copies' total, as mediaSync last measured it. */+export function noteCachedCopyBytes(bytes: number): void {+ copyBytes = bytes;+}+function refund(bytes: number): void {if (lastEstimate) lastEstimate.usage = Math.max(0, lastEstimate.usage - bytes);}@@ -766,7 +800,11 @@ async function roomFor(bytes: number, keepKeyHash: string): Promise<boolean> {return true;}const margin = Math.min(QUOTA_MARGIN, lastEstimate.quota * 0.1);- let deficit = lastEstimate.usage + bytes + margin - lastEstimate.quota;+ const stored = await chunkStoreBytes();+ let deficit = Math.max(+ lastEstimate.usage + bytes + margin - lastEstimate.quota,+ stored + copyBytes + bytes - cacheSetting.maxBytes,+ );if (deficit > 0) {const freed = await evictChunkBytes(deficit, keepKeyHash);refund(freed);@@ -774,6 +812,7 @@ async function roomFor(bytes: number, keepKeyHash: string): Promise<boolean> {}if (deficit > 0) return false;lastEstimate.usage += bytes;+ if (tally) tally.bytes += bytes;return true;}@@ -856,6 +895,7 @@ async function removeVersion(keyHash: string, versionTag: string, bytes: number)} catch {return 0;}+ if (tally) tally.bytes = Math.max(0, tally.bytes - bytes);const k = `${keyHash}/${versionTag}`;memory.forget(k);const live = states.get(k);diff --git a/site/src/cut/lib/mediaSync.ts b/site/src/cut/lib/mediaSync.tsindex b3ec7cc6..f66c5f10 100644--- a/site/src/cut/lib/mediaSync.ts+++ b/site/src/cut/lib/mediaSync.ts@@ -44,7 +44,14 @@ import {revokeRegistered,} from "./backend/browser/registry";import { getBackend, type CutBackend } from "./backend";-import { dropChunksMatching, evictChunkBytes, prefetchChunks } from "./chunkCache";+import {+ chunkStoreBytes,+ cutMediaCache,+ dropChunksMatching,+ evictChunkBytes,+ noteCachedCopyBytes,+ prefetchChunks,+} from "./chunkCache";import { mediaUrl, type AssetType } from "./types";const mediaPath = (projectId: string, fileName: string) =>@@ -61,7 +68,7 @@ export async function stashCloudMedia(): Promise<string | null> {if (!supportsBrowserStore()) return null;try {- await makeRoom(projectId, file.size);+ await makeRoom(projectId, file.size, false);const dir = await mediaDir(projectId, true);if (!dir) return null;await writeFileAt(dir, fileName, file);@@ -88,7 +95,7 @@ export async function stashUnclaimedMedia(): Promise<{ fileName: string; url: string } | null> {if (!supportsBrowserStore()) return null;try {- await makeRoom(projectId, file.size);+ await makeRoom(projectId, file.size, false);const fileName = await saveMedia(projectId, file, file.name);await updateIndex((idx) => {idx.pendingUploads = [@@ -260,6 +267,8 @@ export function prefetchCloudMedia(await updateIndex((idx) => {idx.opens = { ...idx.opens, [projectId]: Date.now() };}).catch(() => {});+ // Opening a project brings a cache that outgrew its ceiling back under it.+ await makeRoom(projectId, 0).catch(() => {});for (let i = 0; i < files.length; i++) {if (run.ctrl.signal.aborted) return;const f = files[i];@@ -344,39 +353,63 @@ async function cacheOne(const QUOTA_MARGIN = 512 * 1024 * 1024;/** Make room for `bytes` of new media, evicting least-recently-opened cloud- * projects' cached copies until the write fits under quota. Pinned files and- * browser-resident projects are exempt; `keepId` (the project being written)- * is never a candidate. */-async function makeRoom(keepId: string, bytes: number): Promise<void> {- let est: StorageEstimate | undefined;+ * projects' cached copies until the write fits under quota and the cache+ * under its ceiling (cutMediaCache). Pinned files and browser-resident+ * projects are exempt; `keepId` (the project being written) is never a+ * candidate. A pinned write (`cached` false) needs quota room only; it is no+ * cache. */+async function makeRoom(keepId: string, bytes: number, cached = true): Promise<void> {+ let quotaDeficit = -Infinity;try {- est = await navigator.storage.estimate?.();+ const est = await navigator.storage.estimate?.();+ if (est?.quota) {+ quotaDeficit = (est.usage ?? 0) + bytes + Math.min(QUOTA_MARGIN, est.quota * 0.1) - est.quota;+ }} catch {- return;+ // No estimate; the ceiling still holds.}- if (!est?.quota) return;- const margin = Math.min(QUOTA_MARGIN, est.quota * 0.1);- let deficit = (est.usage ?? 0) + bytes + margin - est.quota;- if (deficit <= 0) return;- // The chunk cache is the cheapest byte to lose — losing one costs a ranged- // re-fetch, where losing a whole-file copy costs the file — so it goes first.- deficit -= await evictChunkBytes(deficit);- if (deficit <= 0) return;const idx = await readIndex();const pinned = new Set(idx.pendingUploads.map((p) => `${p.projectId}/${p.fileName}`));const candidates: { id: string; openedAt: number }[] = [];+ let copies = 0;for (const id of await listProjectIds()) {- if (id === keepId) continue;// A directory holding a doc is a browser-resident project — the store is// its only home. Cached cloud copies hold media alone.if (await readFileAt(await projectDir(id), "project.json")) continue;- candidates.push({ id, openedAt: idx.opens[id] ?? 0 });+ copies += await cachedCopyBytes(id, pinned);+ if (id !== keepId) candidates.push({ id, openedAt: idx.opens[id] ?? 0 });}+ noteCachedCopyBytes(copies);+ let deficit = Math.max(quotaDeficit, (await chunkStoreBytes()) + copies + (cached ? bytes : 0) - cutMediaCache().maxBytes);+ if (deficit <= 0) return;+ // The chunk cache is the cheapest byte to lose — losing one costs a ranged+ // re-fetch, where losing a whole-file copy costs the file — so it goes first.+ deficit -= await evictChunkBytes(deficit);+ if (deficit <= 0) return;candidates.sort((a, b) => a.openedAt - b.openedAt);for (const c of candidates) {- if (deficit <= 0) return;- deficit -= await evictCachedCopy(c.id, pinned);+ if (deficit <= 0) break;+ const freed = await evictCachedCopy(c.id, pinned);+ deficit -= freed;+ copies -= freed;+ }+ noteCachedCopyBytes(copies);+}++/** Bytes a cloud project's cached copy holds, pinned files aside. */+async function cachedCopyBytes(id: string, pinned: Set<string>): Promise<number> {+ const dir = await mediaDir(id);+ if (!dir) return 0;+ let total = 0;+ try {+ for await (const [name, handle] of dir) {+ if (handle.kind !== "file" || pinned.has(`${id}/${name}`)) continue;+ total += (await (handle as FileSystemFileHandle).getFile()).size;+ }+ } catch {+ // A directory swept mid-walk counts what it had.}+ return total;}/** Drop a cloud project's cached media, keeping pinned files. A copy leftdiff --git a/site/src/lib/config/registry.ts b/site/src/lib/config/registry.tsindex 449481bb..ff3768e5 100644--- a/site/src/lib/config/registry.ts+++ b/site/src/lib/config/registry.ts@@ -299,6 +299,14 @@ export const SETTINGS = defineSettings({title: "Notes",description: "How long a note sits still after the last keystroke before it saves.",},+ cutMediaCache: {+ schema: z.object({ maxBytes: z.number().int().min(1024 ** 3).max(1024 ** 4) }).strict(),+ default: { maxBytes: 10 * 1024 ** 3 },+ public: true,+ title: "Cloud media cache",+ description:+ "The most disk a browser spends on cached copies of cloud media. Past it, the least recently used copies go first.",+ },cutColor: {schema: z.object({