| 1 | <!doctype html> |
| 2 | <html lang="es"><head><meta charset="utf-8"><title>Elffuss · broker de modelos</title> |
| 3 | <meta name="robots" content="noindex"> |
| 4 | <style>body{margin:0;background:#090a11;color:#8b929c;font:13px system-ui;display:grid;place-items:center;height:100vh}</style> |
| 5 | </head><body> |
| 6 | <div>Elffuss · caché compartida de modelos. Esta página no se usa directamente.</div> |
| 7 | <script type="module"> |
| 8 | // ───────────────────────────────────────────────────────────────────────────── |
| 9 | // BROKER DE MODELOS · caché OPFS compartida entre todas las webs de Elffuss. |
| 10 | // Se embebe como iframe oculto desde cada propiedad (claw/translator/copilot/…). |
| 11 | // Como todas son subdominios de elffuss.com (mismo «site»), este iframe es |
| 12 | // first-party en todas y comparte UNA sola OPFS → el modelo se descarga UNA vez |
| 13 | // y se reutiliza en todas. (Un tercero que lo integre tiene su propia partición |
| 14 | // por privacidad del navegador: se baja una vez POR SU site. Los hosts viejos de |
| 15 | // elffuss.utopiaia.com son, para el navegador, otro site distinto.) |
| 16 | // API postMessage: {type:'elffuss-model-get', id, url} → progress* → begin → |
| 17 | // chunk* (ArrayBuffer transferible) → end | error. {type:'elffuss-model-has',id,url} |
| 18 | // ───────────────────────────────────────────────────────────────────────────── |
| 19 | const DIR = 'elffuss-models'; |
| 20 | const CHUNK = 8 * 1024 * 1024; |
| 21 | // Orígenes permitidos: las webs de Elffuss que embeben este broker, y nada más. |
| 22 | // NO todo *.utopiaia.com, que es lo que ponía antes: ese dominio aloja más cosas |
| 23 | // que Elffuss. Y tampoco todo *.elffuss.com: git.* es un Gitea que pinta markdown |
| 24 | // escrito por cualquiera, y key.* no tiene nada que pedirle al broker. Como son |
| 25 | // del mismo «site», un script colado ahí hablaría con este iframe en su caché |
| 26 | // compartida: vería lo que hay guardado y podría llenarla de descargas. |
| 27 | // · elffuss.com y claw, code, t2t, translator, copilot, models, m1, m2… .elffuss.com |
| 28 | // · lo mismo bajo elffuss.utopiaia.com (el dominio anterior: se sigue aceptando |
| 29 | // mientras dure el cambio de dominio) |
| 30 | // · los alias más viejos elffuss-claw y elffuss-code .utopiaia.com |
| 31 | // Las integraciones de terceros se añaden por query (?allow=https://otra.com) |
| 32 | // — cada una en su propia partición. |
| 33 | const extra = new URLSearchParams(location.search).getAll('allow'); |
| 34 | const WEBS = /^https:\/\/((claw|code|t2t|translator|copilot|models|m\d+)\.)?elffuss\.(com|utopiaia\.com)$/; |
| 35 | function allowed(origin) { |
| 36 | if (WEBS.test(origin)) return true; |
| 37 | if (/^https:\/\/(elffuss-(claw|code)|burn-my-computer)\.utopiaia\.com$/.test(origin)) return true; // alias antiguos y Burn My Computer |
| 38 | if (/^http:\/\/localhost(:\d+)?$/.test(origin)) return true; // desarrollo |
| 39 | return extra.includes(origin); |
| 40 | } |
| 41 | const keyFor = u => String(u).replace(/[?#].*$/, '').replace(/[^\w.\-]+/g, '_').slice(-180); |
| 42 | async function requestPersist(){ try{ if(navigator.storage?.persist){ return (await navigator.storage.persisted?.()) || await navigator.storage.persist(); } }catch{} return false; } |
| 43 | async function dir(){ const root = await navigator.storage.getDirectory(); return root.getDirectoryHandle(DIR, { create:true }); } |
| 44 | |
| 45 | async function cachedFile(url){ |
| 46 | try{ const d = await dir(); const k = keyFor(url); |
| 47 | await d.getFileHandle(k + '.done'); // marcador de completado |
| 48 | const f = await (await d.getFileHandle(k)).getFile(); |
| 49 | return f.size > 0 ? f : null; |
| 50 | }catch{ return null; } |
| 51 | } |
| 52 | async function downloadToOPFS(url, onProgress){ |
| 53 | await requestPersist(); |
| 54 | const d = await dir(); const k = keyFor(url); |
| 55 | const net = await fetch(url); |
| 56 | if (!net.ok || !net.body) throw new Error('descarga falló: HTTP ' + net.status); |
| 57 | const total = +net.headers.get('content-length') || 0; |
| 58 | await d.removeEntry(k + '.done').catch(()=>{}); |
| 59 | const fh = await d.getFileHandle(k, { create:true }); |
| 60 | const w = await fh.createWritable(); |
| 61 | let loaded = 0; const reader = net.body.getReader(); |
| 62 | try{ |
| 63 | for(;;){ const { done, value } = await reader.read(); if (done) break; |
| 64 | await w.write(value); loaded += value.byteLength; onProgress?.({ loaded, total }); } |
| 65 | await w.close(); |
| 66 | }catch(e){ try{ await w.abort(); }catch{} await d.removeEntry(k).catch(()=>{}); throw e; } |
| 67 | const dh = await d.getFileHandle(k + '.done', { create:true }); const dw = await dh.createWritable(); |
| 68 | await dw.write(new TextEncoder().encode(String(loaded))); await dw.close(); |
| 69 | return await (await d.getFileHandle(k)).getFile(); |
| 70 | } |
| 71 | |
| 72 | let inflight = new Map(); // url → Promise<File> (colapsa descargas simultáneas) |
| 73 | async function getFile(url, onProgress){ |
| 74 | const hit = await cachedFile(url); if (hit) return hit; |
| 75 | if (inflight.has(url)) return inflight.get(url); |
| 76 | const p = downloadToOPFS(url, onProgress).finally(()=> inflight.delete(url)); |
| 77 | inflight.set(url, p); return p; |
| 78 | } |
| 79 | |
| 80 | addEventListener('message', async e => { |
| 81 | if (!allowed(e.origin)) return; |
| 82 | const d = e.data || {}; const { type, id, url } = d; |
| 83 | const post = (msg, transfer) => { try{ e.source.postMessage({ id, ...msg }, e.origin, transfer || []); }catch{} }; |
| 84 | if (type === 'elffuss-model-has') { |
| 85 | const f = await cachedFile(url); post({ kind:'has', cached: !!f, size: f?f.size:0 }); return; |
| 86 | } |
| 87 | if (type === 'elffuss-model-get') { |
| 88 | try{ |
| 89 | const file = await getFile(url, p => post({ kind:'progress', ...p })); |
| 90 | // El File de OPFS es un Blob respaldado en DISCO: structured-clone lo pasa |
| 91 | // POR REFERENCIA (Chromium: vive en el proceso de navegador, ref-contado, |
| 92 | // paginado a disco) → NO copia los GB a la RAM de ningún renderer. El padre |
| 93 | // lo lee perezosamente con file.stream(). Mantener este iframe vivo mientras |
| 94 | // el padre lee (el Blob vivo mantiene referenciados los datos en OPFS). |
| 95 | post({ kind:'file', file }); |
| 96 | }catch(err){ post({ kind:'error', message: String(err?.message || err) }); } |
| 97 | } |
| 98 | }); |
| 99 | // avisar a quien nos embebe que estamos listos |
| 100 | try{ parent.postMessage({ kind:'elffuss-broker-ready' }, '*'); }catch{} |
| 101 | </script> |
| 102 | </body></html> |