Spaces:
Running
Running
Download assets/filecache.js from clyxconsultingdev/amc-console: direct link, hf CLI and curl.
- Browser
- Download file 3.06 kB
-
https://huggingface.co/spaces/clyxconsultingdev/amc-console/resolve/main/assets/filecache.js
- Command line
-
hf download hf://spaces/clyxconsultingdev/amc-console/assets/filecache.js
-
curl -L -o filecache.js https://huggingface.co/spaces/clyxconsultingdev/amc-console/resolve/main/assets/filecache.js
3.06 kB
| // AMC — published data files kept in the browser between visits. | |
| // | |
| // Hugging Face serves each file through an address signed anew on every | |
| // visit, with no cache instructions, so the browser downloaded the same | |
| // megabytes every time: a second visit to a company's page fetched 12.8 MB | |
| // again, the explorer 5.5 MB (measured live, 2026-10-26). The manifest | |
| // publishes each file's sha256, so a copy kept under that checksum is the | |
| // published file until an edition changes it, and the next visit reads it | |
| // from the disk. A new edition's checksum misses, downloads once, and the | |
| // file's older copies are deleted. | |
| // | |
| // A FIRST VISIT DOES NOT WAIT FOR THE MANIFEST. Waiting for it to know the | |
| // checksum before downloading started every download a second or two later, | |
| // and a cold name search came about 2.5 s later on the live site (A/B, | |
| // 2026-10-27). So when the browser holds no copy of a file, the download | |
| // starts at once and the manifest only decides, afterwards, under which | |
| // checksum the copy is kept. | |
| // | |
| // Only for files a page downloads whole. Where the browser keeps nothing (a | |
| // private window, storage refused), the download goes ahead as before. | |
| const STORE = "amc-data-v1"; | |
| const open = () => (typeof caches === "undefined" ? Promise.resolve(null) : caches.open(STORE).catch(() => null)); | |
| const prefixOf = (name) => `${location.origin}/__amc-data/${encodeURIComponent(name)}?`; | |
| const keyOf = (name, sha) => `${prefixOf(name)}sha256=${sha}`; | |
| // The file's bytes, from the browser's copy when its checksum is the | |
| // manifest's. `meta` is { sha256, bytes } from the manifest, or a promise of it. | |
| export async function fetchFile(url, { name, meta } = {}) { | |
| const store = name ? await open() : null; | |
| let held = null; | |
| if (store) { | |
| try { held = (await store.keys()).find((k) => k.url.startsWith(prefixOf(name))) || null; } catch {} | |
| } | |
| if (held) { | |
| const m = await Promise.resolve(meta).catch(() => null); | |
| if (m?.sha256 && held.url === keyOf(name, m.sha256)) { | |
| try { | |
| const buf = await (await store.match(held)).arrayBuffer(); | |
| if (!m.bytes || buf.byteLength === m.bytes) return buf; | |
| } catch {} | |
| } | |
| } | |
| const r = await fetch(url); | |
| if (!r.ok) throw new Error(`${name || url.split("/").pop()}: HTTP ${r.status}`); | |
| const buf = await r.arrayBuffer(); | |
| if (store) keep(store, name, buf, meta); | |
| return buf; | |
| } | |
| // Stored once the manifest says under which checksum, if the size is its | |
| // size, and the edition before is cleared. The page already has its bytes. | |
| function keep(store, name, buf, meta) { | |
| const copy = buf.slice(0); | |
| Promise.resolve(meta).then((m) => { | |
| if (!m?.sha256 || (m.bytes && copy.byteLength !== m.bytes)) return; | |
| const key = keyOf(name, m.sha256); | |
| return store.put(key, new Response(copy, { headers: { "Content-Type": "application/octet-stream" } })) | |
| .then(() => store.keys()) | |
| .then((keys) => Promise.all(keys.filter((k) => k.url.startsWith(prefixOf(name)) && k.url !== key).map((k) => store.delete(k)))); | |
| }).catch(() => {}); | |
| } | |