1// browser/fileWorker.ts 2async function getRoot() { 3 return navigator.storage.getDirectory(); 4} 5async function processNextInQueue() { 6 if (!actionInProgress && actionQueue.length > 0) { 7 const workerData = actionQueue.shift(); 8 switch (workerData.action) { 9 case "write_file" /* WRITE_FILE */: 10 actionInProgress = true; 11 await writeFileAction(workerData); 12 break; 13 default: 14 console.error("Unknown action in file worker", workerData); 15 } 16 if (actionInProgress) { 17 actionInProgress = false; 18 processNextInQueue(); 19 } 20 } 21} 22async function writeFileAction(workerData) { 23 let syncHandle, requestId, success = false, progress = "start"; 24 try { 25 const data = workerData.data || ""; 26 const path = workerData.path; 27 requestId = workerData.requestId || ""; 28 if (!path) { 29 console.error("No path provided to writing a file"); 30 return; 31 } 32 progress = "got path"; 33 let dir = await getRoot(); 34 progress = "got root dir"; 35 const parts = path.split("/").filter((part) => part.length > 0); 36 progress = "got parts"; 37 while (parts.length > 1) { 38 dir = await dir.getDirectoryHandle(parts.shift(), { create: true }); 39 } 40 progress = "got target dir"; 41 const draftHandle = await dir.getFileHandle(parts[0], { create: true }); 42 progress = "got file handle"; 43 syncHandle = await draftHandle.createSyncAccessHandle(); 44 progress = "got sync handle"; 45 syncHandle.truncate(0); 46 progress = "truncated file"; 47 syncHandle.flush(); 48 progress = "flushed truncate"; 49 const encoder = new TextEncoder; 50 progress = "created encoder";
51 const encodedMessage = encoder.encode(data); 52 progress = "encoded data"; 53 syncHandle.write(encodedMessage, { at: 0 }); 54 progress = "wrote data"; 55 success = true; 56 } catch (err) { 57 console.error("write file action failed: ", workerData, "progress", progress, err); 58 } finally { 59 if (syncHandle) { 60 syncHandle.flush(); 61 syncHandle.close(); 62 syncHandle = null; 63 } 64 self.postMessage({ 65 action: "file_response" /* FILE_RESPONSE */, 66 sourceAction: "write_file" /* WRITE_FILE */, 67 requestId, 68 success 69 }); 70 } 71} 72onmessage = async (evt) => { 73 actionQueue.push(evt.data); 74 processNextInQueue(); 75}; 76var actionInProgress = false; 77var actionQueue = [];
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.