Files
pages/static/js/feed-gecko-profile.mjs
Anya Tokugawa 5321d613b4
Build and publish container images / images (push) Successful in 11m45s
Release container images / release (push) Successful in 21s
feat(feeds): add opt-in Gecko relay and folder shortcuts
Document engine redistribution requirements; exclude upstream engine assets and keep the viewer disabled by default.
2026-11-08 16:14:34 +09:00

21 lines
2.4 KiB
JavaScript

// Per-viewer volatile profile. Never use the SDK's default persistent OPFS
// profile: article cookies/history must disappear when the iframe is removed.
export function memoryProfile(limit = 32 * 1024 * 1024) {
const entries = new Map([['', null]]); let bytes = 0;
const path = value => {
if(typeof value !== 'string' || value.length > 2048 || value.startsWith('/') || value.includes('\\') || value.includes('\0') || value.split('/').some(v => v === '..' || v === '.')) throw Error('Invalid profile path');
return value.replace(/\/$/, '');
};
const parent = p => p.slice(0, Math.max(0, p.lastIndexOf('/')));
const requireParent = p => { if(entries.get(parent(p)) !== null) throw Error('Missing profile directory'); };
return {
async stat(value) { const p = path(value); return !entries.has(p) ? null : {size: entries.get(p)?.length || 0, isDir: entries.get(p) === null}; },
async readdir(value) { const p = path(value); if(entries.get(p) !== null) throw Error('Not a directory'); const prefix = p ? p+'/' : ''; return [...entries].filter(([k])=>k!==p && k.startsWith(prefix) && !k.slice(prefix.length).includes('/')).map(([k,v])=>k.slice(prefix.length)+(v===null?'/':'')); },
async readFile(value) { const data = entries.get(path(value)); if(!data) throw Error('Profile file not found'); return data.slice(); },
async writeFile(value,data) { const p = path(value); requireParent(p); if(entries.get(p)===null || !(data instanceof Uint8Array)) throw Error('Invalid profile file'); const next = bytes-(entries.get(p)?.length||0)+data.length; if(next>limit || (!entries.has(p)&&entries.size>=2048)) throw Error('Profile limit reached'); entries.set(p,data.slice()); bytes=next; },
async mkdir(value) { const p = path(value); if(entries.get(p)===null)return; requireParent(p); if(entries.has(p)||entries.size>=2048)throw Error('Profile limit reached'); entries.set(p,null); },
async unlink(value) { const p=path(value); if(!p)throw Error('Invalid profile path'); for(const [k,v] of entries)if(k===p||k.startsWith(p+'/')){bytes-=v?.length||0;entries.delete(k);} },
async rename(from,to) { const a=path(from),b=path(to); if(!a||!b||!entries.has(a)||b.startsWith(a+'/'))throw Error('Invalid profile rename'); if(a===b)return; requireParent(b); const moved=[...entries].filter(([k])=>k===a||k.startsWith(a+'/')); await this.unlink(b); for(const [k]of moved)entries.delete(k); for(const [k,v]of moved)entries.set(b+k.slice(a.length),v); }
};
}