minicpm5-2b-webgpu / src /cache-worker.js
ProCreations's picture
Launch MiniCPM5 Q8 WebGPU playground with local HTML, DOCX, XLSX and PDF tools
a9a3715 verified
Raw History Blame Contribute Delete
777 Bytes
let handle;
onmessage=async({data})=>{
try{
if(data.action==='open'){
const root=await navigator.storage.getDirectory();const dir=await root.getDirectoryHandle('cache',{create:true});const file=await dir.getFileHandle(data.key,{create:true});
handle=await file.createSyncAccessHandle();handle.truncate(0);
}else if(data.action==='write'){
const bytes=new Uint8Array(data.buffer);let offset=0;while(offset<bytes.length){const wrote=handle.write(bytes.subarray(offset));if(!wrote)throw new Error('The browser cache write made no progress.');offset+=wrote;}
}else if(data.action==='close'){
handle?.flush();handle?.close();handle=null;
}
postMessage({ok:true});
}catch(error){try{handle?.close();}catch{}handle=null;postMessage({error:error.message});}
};