File size: 777 Bytes
a9a3715 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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});}
};
|