@componentor/fs 3.0.49 → 3.0.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -160,9 +160,24 @@ var RENAME_CHUNK = 2 * 1024 * 1024;
160
160
  async function renameInOPFS(oldPath, newPath) {
161
161
  let srcAccess = null;
162
162
  let dstAccess = null;
163
+ let oldDir;
164
+ let oldHandle;
165
+ try {
166
+ oldDir = await navigateToParent(oldPath);
167
+ oldHandle = await oldDir.getFileHandle(basename(oldPath));
168
+ } catch (err) {
169
+ if (err?.name === "TypeMismatchError" || err?.name === "NotFoundError" || err?.message?.includes("TypeMismatch") || err?.message?.includes("not a file") || err?.message?.includes("not an entry of requested type")) {
170
+ try {
171
+ await renameDirInOPFS(oldPath, newPath);
172
+ } catch (dirErr) {
173
+ console.warn("[opfs-sync] rename (dir) failed:", oldPath, "\u2192", newPath, dirErr);
174
+ }
175
+ return;
176
+ }
177
+ console.warn("[opfs-sync] rename failed:", oldPath, "\u2192", newPath, err);
178
+ return;
179
+ }
163
180
  try {
164
- const oldDir = await navigateToParent(oldPath);
165
- const oldHandle = await oldDir.getFileHandle(basename(oldPath));
166
181
  srcAccess = await oldHandle.createSyncAccessHandle();
167
182
  const size = srcAccess.getSize();
168
183
  const newDir = await ensureParentDirs(newPath);
@@ -209,6 +224,63 @@ async function renameInOPFS(oldPath, newPath) {
209
224
  }
210
225
  }
211
226
  }
227
+ async function renameDirInOPFS(oldPath, newPath) {
228
+ const oldParent = await navigateToParent(oldPath);
229
+ const srcDir = await oldParent.getDirectoryHandle(basename(oldPath));
230
+ const newParent = await ensureParentDirs(newPath);
231
+ try {
232
+ await newParent.removeEntry(basename(newPath), { recursive: true });
233
+ } catch (e) {
234
+ if (e?.name !== "NotFoundError") throw e;
235
+ }
236
+ const dstDir = await newParent.getDirectoryHandle(basename(newPath), { create: true });
237
+ await copyDirContents(srcDir, dstDir);
238
+ await oldParent.removeEntry(basename(oldPath), { recursive: true });
239
+ }
240
+ async function copyDirContents(src, dst) {
241
+ for await (const [name, handle] of src.entries()) {
242
+ if (handle.kind === "directory") {
243
+ const childDst = await dst.getDirectoryHandle(name, { create: true });
244
+ await copyDirContents(handle, childDst);
245
+ } else {
246
+ const fileHandle = handle;
247
+ const dstFile = await dst.getFileHandle(name, { create: true });
248
+ let srcAccess = null;
249
+ let dstAccess = null;
250
+ try {
251
+ srcAccess = await fileHandle.createSyncAccessHandle();
252
+ dstAccess = await dstFile.createSyncAccessHandle();
253
+ const size = srcAccess.getSize();
254
+ dstAccess.truncate(0);
255
+ if (size > 0) {
256
+ const chunk = new Uint8Array(Math.min(size, RENAME_CHUNK));
257
+ let offset = 0;
258
+ while (offset < size) {
259
+ const len = Math.min(chunk.length, size - offset);
260
+ const view = len === chunk.length ? chunk : chunk.subarray(0, len);
261
+ srcAccess.read(view, { at: offset });
262
+ dstAccess.write(view, { at: offset });
263
+ offset += len;
264
+ }
265
+ }
266
+ dstAccess.flush();
267
+ } finally {
268
+ if (dstAccess) {
269
+ try {
270
+ dstAccess.close();
271
+ } catch {
272
+ }
273
+ }
274
+ if (srcAccess) {
275
+ try {
276
+ srcAccess.close();
277
+ } catch {
278
+ }
279
+ }
280
+ }
281
+ }
282
+ }
283
+ }
212
284
  async function navigateToParent(path) {
213
285
  const parts = pathSegments(path);
214
286
  parts.pop();
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/src/workers/opfs-sync.worker.ts"],"sourcesContent":["/**\n * OPFS Sync Worker — optional bidirectional mirror between VFS and real OPFS.\n *\n * Spawned by the server worker when opfsSync is enabled.\n * Receives mutation events from the server, writes them to real OPFS files.\n * Uses FileSystemObserver to detect external OPFS changes and syncs them back.\n */\n\ninterface SyncEvent {\n op: 'write' | 'delete' | 'mkdir' | 'rename';\n path: string;\n newPath?: string;\n data?: ArrayBuffer;\n ts: number;\n}\n\nlet serverPort: MessagePort;\nlet mirrorRoot: FileSystemDirectoryHandle;\n\n// Normalize path: resolve \".\" and \"..\", ensure leading /, collapse //, strip trailing /\nfunction normalizePath(p: string): string {\n if (p.charCodeAt(0) !== 47) p = '/' + p;\n if (p.indexOf('//') !== -1) p = p.replace(/\\/\\/+/g, '/');\n // Resolve \".\" and \"..\" segments\n if (p.indexOf('/.') !== -1) {\n const parts = p.split('/');\n const resolved: string[] = [];\n for (const part of parts) {\n if (part === '.' || part === '') continue;\n if (part === '..') { resolved.pop(); continue; }\n resolved.push(part);\n }\n p = '/' + resolved.join('/');\n }\n if (p.length > 1 && p.charCodeAt(p.length - 1) === 47) p = p.slice(0, -1);\n return p || '/';\n}\n\n// Split a normalized path into segments safe for OPFS (no empty, \".\", or \"..\" entries)\nfunction pathSegments(p: string): string[] {\n return normalizePath(p).split('/').filter(Boolean);\n}\n\n// Echo suppression — two structures:\n//\n// pendingPaths (Set): paths currently in the queue or being processed.\n// Added on enqueue, removed after OPFS operation completes.\n// No timeout — stays as long as the item is in the queue.\n// Prevents false externals when queue takes >1s (e.g., 500-file batches).\n//\n// completedPaths (Map<path, timestamp>): paths recently written by us.\n// Added when processing completes, removed ONLY by periodic cleanup.\n// Grace window catches delayed/batched observer events after processing.\n//\n// Parent path check (opt-in): if /dir was deleted by us, /dir/file disappearing\n// is also our echo (recursive removeEntry fires per-child events).\n// ONLY used for 'disappeared' events — NOT for 'appeared'/'modified', since\n// creating /dir doesn't mean /dir/new-file appearing is our echo.\n\nconst pendingPaths = new Set<string>();\nconst completedPaths = new Map<string, number>();\nconst GRACE_MS = 3000;\n\nfunction trackPending(path: string): void {\n pendingPaths.add(normalizePath(path));\n}\n\nfunction untrackPending(path: string): void {\n pendingPaths.delete(normalizePath(path));\n}\n\nfunction trackCompleted(path: string): void {\n completedPaths.set(normalizePath(path), Date.now());\n}\n\nfunction isOurEcho(path: string, checkParents = false): boolean {\n path = normalizePath(path);\n const now = Date.now();\n\n // Check exact path\n if (pendingPaths.has(path)) return true;\n const ts = completedPaths.get(path);\n if (ts && now - ts < GRACE_MS) return true;\n\n // Walk up parent paths — ONLY for 'disappeared' events.\n // Handles recursive delete cascading: removeEntry(dir, {recursive:true})\n // fires individual 'disappeared' for every child file.\n // NOT used for 'appeared'/'modified' — a parent being tracked doesn't mean\n // a new file appearing inside it is our echo (could be genuinely external).\n if (checkParents) {\n let parent = path;\n while (true) {\n const slash = parent.lastIndexOf('/');\n if (slash <= 0) break;\n parent = parent.substring(0, slash);\n if (pendingPaths.has(parent)) return true;\n const pts = completedPaths.get(parent);\n if (pts && now - pts < GRACE_MS) return true;\n }\n }\n\n return false;\n}\n\n// Periodic cleanup — the ONLY way completedPaths entries get removed\nsetInterval(() => {\n const cutoff = Date.now() - GRACE_MS;\n for (const [p, ts] of completedPaths) {\n if (ts < cutoff) completedPaths.delete(p);\n }\n}, 5000);\n\n// Event queue — process one at a time, in order\nconst queue: SyncEvent[] = [];\nlet processing = false;\n\nfunction enqueue(event: SyncEvent): void {\n trackPending(event.path);\n if (event.op === 'rename' && event.newPath) {\n trackPending(event.newPath);\n }\n\n // Coalesce bursts of 'write' events for the same path that are still\n // waiting in the queue. Each write event carries a full-file\n // ArrayBuffer — under backpressure (slow OPFS, big files) the queue\n // can otherwise hold many superseded buffers at once. Replacing the\n // pending event's payload with the newest drops the stale buffer for\n // GC while preserving queue ordering for non-write ops.\n //\n // Note: the event at index 0 may already be mid-flight inside\n // processNext (after its `queue.shift()`), so we only scan what's\n // still queued.\n if (event.op === 'write') {\n for (let i = queue.length - 1; i >= 0; i--) {\n const pending = queue[i];\n if (pending.op === 'write' && normalizePath(pending.path) === normalizePath(event.path)) {\n pending.data = event.data;\n pending.ts = event.ts;\n return;\n }\n }\n }\n\n queue.push(event);\n if (!processing) processNext();\n}\n\nasync function processNext(): Promise<void> {\n if (queue.length === 0) {\n processing = false;\n return;\n }\n processing = true;\n\n const event = queue.shift()!;\n\n try {\n switch (event.op) {\n case 'write':\n if (event.data) {\n await writeToOPFS(event.path, event.data);\n } else {\n // No data but file should still exist (empty file like .gitkeep)\n await writeToOPFS(event.path, new ArrayBuffer(0));\n }\n break;\n case 'delete':\n await deleteFromOPFS(event.path);\n break;\n case 'mkdir':\n await mkdirInOPFS(event.path);\n break;\n case 'rename':\n await renameInOPFS(event.path, event.newPath!);\n break;\n }\n } catch (err) {\n console.warn('[opfs-sync] mirror failed:', event.op, event.path, err);\n }\n\n // Move from pending → completed (starts grace window for delayed observer events)\n untrackPending(event.path);\n trackCompleted(event.path);\n if (event.op === 'rename' && event.newPath) {\n untrackPending(event.newPath);\n trackCompleted(event.newPath);\n }\n\n processNext();\n}\n\nasync function ensureParentDirs(path: string): Promise<FileSystemDirectoryHandle> {\n const parts = pathSegments(path);\n parts.pop(); // Remove filename\n\n let dir = mirrorRoot;\n for (const part of parts) {\n dir = await dir.getDirectoryHandle(part, { create: true });\n }\n return dir;\n}\n\nfunction basename(path: string): string {\n const parts = pathSegments(path);\n return parts[parts.length - 1] || '';\n}\n\nasync function writeToOPFS(path: string, data: ArrayBuffer): Promise<void> {\n const dir = await ensureParentDirs(path);\n const name = basename(path);\n const fileHandle = await dir.getFileHandle(name, { create: true });\n // Use createSyncAccessHandle for reliable writes in Worker context\n // (createWritable can silently fail in nested workers for OPFS files)\n const accessHandle = await fileHandle.createSyncAccessHandle();\n try {\n accessHandle.truncate(0);\n accessHandle.write(new Uint8Array(data), { at: 0 });\n accessHandle.flush();\n } finally {\n accessHandle.close();\n }\n}\n\nasync function deleteFromOPFS(path: string): Promise<void> {\n try {\n const dir = await navigateToParent(path);\n await dir.removeEntry(basename(path), { recursive: true });\n } catch {\n // File may not exist in OPFS — that's fine\n }\n}\n\nasync function mkdirInOPFS(path: string): Promise<void> {\n let dir = mirrorRoot;\n for (const part of pathSegments(path)) {\n dir = await dir.getDirectoryHandle(part, { create: true });\n }\n}\n\n// Chunk size for the chunked rename copy. Caps peak memory during a\n// rename at this size rather than the full file size.\nconst RENAME_CHUNK = 2 * 1024 * 1024;\n\nasync function renameInOPFS(oldPath: string, newPath: string): Promise<void> {\n // OPFS has no native rename — copy + delete. Copy through two sync\n // access handles in fixed-size chunks so a rename of a large file\n // doesn't require materializing the whole file in memory.\n let srcAccess: FileSystemSyncAccessHandle | null = null;\n let dstAccess: FileSystemSyncAccessHandle | null = null;\n try {\n const oldDir = await navigateToParent(oldPath);\n const oldHandle = await oldDir.getFileHandle(basename(oldPath));\n srcAccess = await oldHandle.createSyncAccessHandle();\n const size = srcAccess.getSize();\n\n const newDir = await ensureParentDirs(newPath);\n const newHandle = await newDir.getFileHandle(basename(newPath), { create: true });\n dstAccess = await newHandle.createSyncAccessHandle();\n dstAccess.truncate(0);\n\n if (size > 0) {\n const chunk = new Uint8Array(Math.min(size, RENAME_CHUNK));\n let offset = 0;\n while (offset < size) {\n const len = Math.min(chunk.length, size - offset);\n const view = len === chunk.length ? chunk : chunk.subarray(0, len);\n srcAccess.read(view, { at: offset });\n dstAccess.write(view, { at: offset });\n offset += len;\n }\n }\n dstAccess.flush();\n\n // Release handles before removeEntry — can't unlink a file that\n // still has an open sync access handle.\n try { dstAccess.close(); } catch { /* ignore */ }\n dstAccess = null;\n try { srcAccess.close(); } catch { /* ignore */ }\n srcAccess = null;\n\n await oldDir.removeEntry(basename(oldPath));\n } catch (err) {\n console.warn('[opfs-sync] rename failed:', oldPath, '→', newPath, err);\n } finally {\n if (dstAccess) { try { dstAccess.close(); } catch { /* ignore */ } }\n if (srcAccess) { try { srcAccess.close(); } catch { /* ignore */ } }\n }\n}\n\nasync function navigateToParent(path: string): Promise<FileSystemDirectoryHandle> {\n const parts = pathSegments(path);\n parts.pop();\n\n let dir = mirrorRoot;\n for (const part of parts) {\n dir = await dir.getDirectoryHandle(part);\n }\n return dir;\n}\n\n// ========== FileSystemObserver for external changes ==========\n\nfunction setupObserver(): void {\n if (typeof FileSystemObserver === 'undefined') {\n console.warn('[opfs-sync] FileSystemObserver not available — external changes will not be detected');\n return;\n }\n\n console.log('[opfs-sync] Setting up FileSystemObserver on mirrorRoot:', mirrorRoot.name || '(opfs-root)');\n\n const observer = new FileSystemObserver((records) => {\n //console.log(`[opfs-sync] observer fired: ${records.length} record(s), pending=${pendingPaths.size}, completed=${completedPaths.size}`);\n for (const record of records) {\n const path = normalizePath('/' + record.relativePathComponents.join('/'));\n\n // Skip VFS binary file and internal files\n if (path === '/.vfs.bin' || path === '/.vfs' || path.startsWith('/.vfs')) continue;\n\n // Echo suppression — check parents only for 'disappeared' (recursive delete cascading)\n const isDelete = record.type === 'disappeared';\n if (isOurEcho(path, isDelete)) {\n //console.log('[opfs-sync] suppressed (echo):', record.type, path);\n continue;\n }\n\n //console.log('[opfs-sync] external:', record.type, path);\n switch (record.type) {\n case 'appeared':\n case 'modified':\n syncExternalChange(path, record.changedHandle);\n break;\n case 'disappeared':\n syncExternalDelete(path);\n break;\n case 'moved': {\n const from = normalizePath('/' + record.relativePathMovedFrom!.join('/'));\n //console.log('[opfs-sync] external: moved from', from, '→', path);\n syncExternalRename(from, path);\n break;\n }\n }\n }\n });\n\n observer.observe(mirrorRoot, { recursive: true });\n}\n\nasync function syncExternalChange(path: string, handle: FileSystemHandle | null): Promise<void> {\n try {\n if (!handle || handle.kind !== 'file') return;\n\n const fileHandle = handle as FileSystemFileHandle;\n const file = await fileHandle.getFile();\n const data = await file.arrayBuffer();\n\n serverPort.postMessage({\n op: 'external-write',\n path,\n data,\n ts: Date.now(),\n }, [data]);\n } catch (err) {\n // File may have been deleted between observer event and our read, or\n // a sync access handle may be holding the lock — either is fine to skip\n console.warn('[opfs-sync] external change read failed:', path, err);\n }\n}\n\nfunction syncExternalDelete(path: string): void {\n serverPort.postMessage({\n op: 'external-delete',\n path,\n ts: Date.now(),\n });\n}\n\nfunction syncExternalRename(oldPath: string, newPath: string): void {\n serverPort.postMessage({\n op: 'external-rename',\n path: oldPath,\n newPath,\n ts: Date.now(),\n });\n}\n\n// ========== Initialization ==========\n\nself.onmessage = async (e: MessageEvent) => {\n const msg = e.data;\n\n if (msg.type === 'init') {\n serverPort = e.ports[0];\n mirrorRoot = await navigator.storage.getDirectory();\n\n // Navigate to mirror root if specified\n if (msg.root && msg.root !== '/') {\n const segments = msg.root.split('/').filter(Boolean);\n for (const segment of segments) {\n mirrorRoot = await mirrorRoot.getDirectoryHandle(segment, { create: true });\n }\n }\n\n console.log('[opfs-sync] initialized with root:', msg.root || '/', 'mirrorRoot.name:', mirrorRoot.name || '(opfs-root)');\n\n // Set up FileSystemObserver\n setupObserver();\n\n // Listen for events from server\n serverPort.onmessage = (ev: MessageEvent) => {\n const event = ev.data as SyncEvent;\n enqueue(event);\n };\n serverPort.start();\n\n (self as unknown as Worker).postMessage({ type: 'ready' });\n return;\n }\n};\n"],"mappings":";AAgBA,IAAI;AACJ,IAAI;AAGJ,SAAS,cAAc,GAAmB;AACxC,MAAI,EAAE,WAAW,CAAC,MAAM,GAAI,KAAI,MAAM;AACtC,MAAI,EAAE,QAAQ,IAAI,MAAM,GAAI,KAAI,EAAE,QAAQ,UAAU,GAAG;AAEvD,MAAI,EAAE,QAAQ,IAAI,MAAM,IAAI;AAC1B,UAAM,QAAQ,EAAE,MAAM,GAAG;AACzB,UAAM,WAAqB,CAAC;AAC5B,eAAW,QAAQ,OAAO;AACxB,UAAI,SAAS,OAAO,SAAS,GAAI;AACjC,UAAI,SAAS,MAAM;AAAE,iBAAS,IAAI;AAAG;AAAA,MAAU;AAC/C,eAAS,KAAK,IAAI;AAAA,IACpB;AACA,QAAI,MAAM,SAAS,KAAK,GAAG;AAAA,EAC7B;AACA,MAAI,EAAE,SAAS,KAAK,EAAE,WAAW,EAAE,SAAS,CAAC,MAAM,GAAI,KAAI,EAAE,MAAM,GAAG,EAAE;AACxE,SAAO,KAAK;AACd;AAGA,SAAS,aAAa,GAAqB;AACzC,SAAO,cAAc,CAAC,EAAE,MAAM,GAAG,EAAE,OAAO,OAAO;AACnD;AAkBA,IAAM,eAAe,oBAAI,IAAY;AACrC,IAAM,iBAAiB,oBAAI,IAAoB;AAC/C,IAAM,WAAW;AAEjB,SAAS,aAAa,MAAoB;AACxC,eAAa,IAAI,cAAc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AAAA,IACN;AAAA,IACA,IAAI,KAAK,IAAI;AAAA,EACf,CAAC;AACH;AAIA,KAAK,YAAY,OAAO,MAAoB;AAC1C,QAAM,MAAM,EAAE;AAEd,MAAI,IAAI,SAAS,QAAQ;AACvB,iBAAa,EAAE,MAAM,CAAC;AACtB,iBAAa,MAAM,UAAU,QAAQ,aAAa;AAGlD,QAAI,IAAI,QAAQ,IAAI,SAAS,KAAK;AAChC,YAAM,WAAW,IAAI,KAAK,MAAM,GAAG,EAAE,OAAO,OAAO;AACnD,iBAAW,WAAW,UAAU;AAC9B,qBAAa,MAAM,WAAW,mBAAmB,SAAS,EAAE,QAAQ,KAAK,CAAC;AAAA,MAC5E;AAAA,IACF;AAEA,YAAQ,IAAI,sCAAsC,IAAI,QAAQ,KAAK,oBAAoB,WAAW,QAAQ,aAAa;AAGvH,kBAAc;AAGd,eAAW,YAAY,CAAC,OAAqB;AAC3C,YAAM,QAAQ,GAAG;AACjB,cAAQ,KAAK;AAAA,IACf;AACA,eAAW,MAAM;AAEjB,IAAC,KAA2B,YAAY,EAAE,MAAM,QAAQ,CAAC;AACzD;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/src/workers/opfs-sync.worker.ts"],"sourcesContent":["/**\n * OPFS Sync Worker — optional bidirectional mirror between VFS and real OPFS.\n *\n * Spawned by the server worker when opfsSync is enabled.\n * Receives mutation events from the server, writes them to real OPFS files.\n * Uses FileSystemObserver to detect external OPFS changes and syncs them back.\n */\n\ninterface SyncEvent {\n op: 'write' | 'delete' | 'mkdir' | 'rename';\n path: string;\n newPath?: string;\n data?: ArrayBuffer;\n ts: number;\n}\n\nlet serverPort: MessagePort;\nlet mirrorRoot: FileSystemDirectoryHandle;\n\n// Normalize path: resolve \".\" and \"..\", ensure leading /, collapse //, strip trailing /\nfunction normalizePath(p: string): string {\n if (p.charCodeAt(0) !== 47) p = '/' + p;\n if (p.indexOf('//') !== -1) p = p.replace(/\\/\\/+/g, '/');\n // Resolve \".\" and \"..\" segments\n if (p.indexOf('/.') !== -1) {\n const parts = p.split('/');\n const resolved: string[] = [];\n for (const part of parts) {\n if (part === '.' || part === '') continue;\n if (part === '..') { resolved.pop(); continue; }\n resolved.push(part);\n }\n p = '/' + resolved.join('/');\n }\n if (p.length > 1 && p.charCodeAt(p.length - 1) === 47) p = p.slice(0, -1);\n return p || '/';\n}\n\n// Split a normalized path into segments safe for OPFS (no empty, \".\", or \"..\" entries)\nfunction pathSegments(p: string): string[] {\n return normalizePath(p).split('/').filter(Boolean);\n}\n\n// Echo suppression — two structures:\n//\n// pendingPaths (Set): paths currently in the queue or being processed.\n// Added on enqueue, removed after OPFS operation completes.\n// No timeout — stays as long as the item is in the queue.\n// Prevents false externals when queue takes >1s (e.g., 500-file batches).\n//\n// completedPaths (Map<path, timestamp>): paths recently written by us.\n// Added when processing completes, removed ONLY by periodic cleanup.\n// Grace window catches delayed/batched observer events after processing.\n//\n// Parent path check (opt-in): if /dir was deleted by us, /dir/file disappearing\n// is also our echo (recursive removeEntry fires per-child events).\n// ONLY used for 'disappeared' events — NOT for 'appeared'/'modified', since\n// creating /dir doesn't mean /dir/new-file appearing is our echo.\n\nconst pendingPaths = new Set<string>();\nconst completedPaths = new Map<string, number>();\nconst GRACE_MS = 3000;\n\nfunction trackPending(path: string): void {\n pendingPaths.add(normalizePath(path));\n}\n\nfunction untrackPending(path: string): void {\n pendingPaths.delete(normalizePath(path));\n}\n\nfunction trackCompleted(path: string): void {\n completedPaths.set(normalizePath(path), Date.now());\n}\n\nfunction isOurEcho(path: string, checkParents = false): boolean {\n path = normalizePath(path);\n const now = Date.now();\n\n // Check exact path\n if (pendingPaths.has(path)) return true;\n const ts = completedPaths.get(path);\n if (ts && now - ts < GRACE_MS) return true;\n\n // Walk up parent paths — ONLY for 'disappeared' events.\n // Handles recursive delete cascading: removeEntry(dir, {recursive:true})\n // fires individual 'disappeared' for every child file.\n // NOT used for 'appeared'/'modified' — a parent being tracked doesn't mean\n // a new file appearing inside it is our echo (could be genuinely external).\n if (checkParents) {\n let parent = path;\n while (true) {\n const slash = parent.lastIndexOf('/');\n if (slash <= 0) break;\n parent = parent.substring(0, slash);\n if (pendingPaths.has(parent)) return true;\n const pts = completedPaths.get(parent);\n if (pts && now - pts < GRACE_MS) return true;\n }\n }\n\n return false;\n}\n\n// Periodic cleanup — the ONLY way completedPaths entries get removed\nsetInterval(() => {\n const cutoff = Date.now() - GRACE_MS;\n for (const [p, ts] of completedPaths) {\n if (ts < cutoff) completedPaths.delete(p);\n }\n}, 5000);\n\n// Event queue — process one at a time, in order\nconst queue: SyncEvent[] = [];\nlet processing = false;\n\nfunction enqueue(event: SyncEvent): void {\n trackPending(event.path);\n if (event.op === 'rename' && event.newPath) {\n trackPending(event.newPath);\n }\n\n // Coalesce bursts of 'write' events for the same path that are still\n // waiting in the queue. Each write event carries a full-file\n // ArrayBuffer — under backpressure (slow OPFS, big files) the queue\n // can otherwise hold many superseded buffers at once. Replacing the\n // pending event's payload with the newest drops the stale buffer for\n // GC while preserving queue ordering for non-write ops.\n //\n // Note: the event at index 0 may already be mid-flight inside\n // processNext (after its `queue.shift()`), so we only scan what's\n // still queued.\n if (event.op === 'write') {\n for (let i = queue.length - 1; i >= 0; i--) {\n const pending = queue[i];\n if (pending.op === 'write' && normalizePath(pending.path) === normalizePath(event.path)) {\n pending.data = event.data;\n pending.ts = event.ts;\n return;\n }\n }\n }\n\n queue.push(event);\n if (!processing) processNext();\n}\n\nasync function processNext(): Promise<void> {\n if (queue.length === 0) {\n processing = false;\n return;\n }\n processing = true;\n\n const event = queue.shift()!;\n\n try {\n switch (event.op) {\n case 'write':\n if (event.data) {\n await writeToOPFS(event.path, event.data);\n } else {\n // No data but file should still exist (empty file like .gitkeep)\n await writeToOPFS(event.path, new ArrayBuffer(0));\n }\n break;\n case 'delete':\n await deleteFromOPFS(event.path);\n break;\n case 'mkdir':\n await mkdirInOPFS(event.path);\n break;\n case 'rename':\n await renameInOPFS(event.path, event.newPath!);\n break;\n }\n } catch (err) {\n console.warn('[opfs-sync] mirror failed:', event.op, event.path, err);\n }\n\n // Move from pending → completed (starts grace window for delayed observer events)\n untrackPending(event.path);\n trackCompleted(event.path);\n if (event.op === 'rename' && event.newPath) {\n untrackPending(event.newPath);\n trackCompleted(event.newPath);\n }\n\n processNext();\n}\n\nasync function ensureParentDirs(path: string): Promise<FileSystemDirectoryHandle> {\n const parts = pathSegments(path);\n parts.pop(); // Remove filename\n\n let dir = mirrorRoot;\n for (const part of parts) {\n dir = await dir.getDirectoryHandle(part, { create: true });\n }\n return dir;\n}\n\nfunction basename(path: string): string {\n const parts = pathSegments(path);\n return parts[parts.length - 1] || '';\n}\n\nasync function writeToOPFS(path: string, data: ArrayBuffer): Promise<void> {\n const dir = await ensureParentDirs(path);\n const name = basename(path);\n const fileHandle = await dir.getFileHandle(name, { create: true });\n // Use createSyncAccessHandle for reliable writes in Worker context\n // (createWritable can silently fail in nested workers for OPFS files)\n const accessHandle = await fileHandle.createSyncAccessHandle();\n try {\n accessHandle.truncate(0);\n accessHandle.write(new Uint8Array(data), { at: 0 });\n accessHandle.flush();\n } finally {\n accessHandle.close();\n }\n}\n\nasync function deleteFromOPFS(path: string): Promise<void> {\n try {\n const dir = await navigateToParent(path);\n await dir.removeEntry(basename(path), { recursive: true });\n } catch {\n // File may not exist in OPFS — that's fine\n }\n}\n\nasync function mkdirInOPFS(path: string): Promise<void> {\n let dir = mirrorRoot;\n for (const part of pathSegments(path)) {\n dir = await dir.getDirectoryHandle(part, { create: true });\n }\n}\n\n// Chunk size for the chunked rename copy. Caps peak memory during a\n// rename at this size rather than the full file size.\nconst RENAME_CHUNK = 2 * 1024 * 1024;\n\nasync function renameInOPFS(oldPath: string, newPath: string): Promise<void> {\n // OPFS has no native rename — copy + delete. Try the file path first\n // (which is the common case: write/copy/etc events). If `oldPath` is\n // actually a directory (e.g. Vite committing `.vite/deps_temp_X` →\n // `.vite/deps`), `getFileHandle` rejects with TypeMismatchError —\n // fall through to the directory-aware branch which recursively walks\n // the source tree.\n let srcAccess: FileSystemSyncAccessHandle | null = null;\n let dstAccess: FileSystemSyncAccessHandle | null = null;\n let oldDir: FileSystemDirectoryHandle;\n let oldHandle: FileSystemFileHandle;\n try {\n oldDir = await navigateToParent(oldPath);\n oldHandle = await oldDir.getFileHandle(basename(oldPath));\n } catch (err: any) {\n // Not a file — try directory. (TypeMismatchError on Safari, plus\n // generic \"not a file\" / \"wrong handle type\" messages on other\n // engines.)\n if (err?.name === 'TypeMismatchError' ||\n err?.name === 'NotFoundError' ||\n err?.message?.includes('TypeMismatch') ||\n err?.message?.includes('not a file') ||\n err?.message?.includes('not an entry of requested type')) {\n try {\n await renameDirInOPFS(oldPath, newPath);\n } catch (dirErr) {\n console.warn('[opfs-sync] rename (dir) failed:', oldPath, '→', newPath, dirErr);\n }\n return;\n }\n console.warn('[opfs-sync] rename failed:', oldPath, '→', newPath, err);\n return;\n }\n // File rename — copy through two sync access handles in fixed-size\n // chunks so a rename of a large file doesn't require materializing\n // the whole file in memory.\n try {\n srcAccess = await oldHandle.createSyncAccessHandle();\n const size = srcAccess.getSize();\n\n const newDir = await ensureParentDirs(newPath);\n const newHandle = await newDir.getFileHandle(basename(newPath), { create: true });\n dstAccess = await newHandle.createSyncAccessHandle();\n dstAccess.truncate(0);\n\n if (size > 0) {\n const chunk = new Uint8Array(Math.min(size, RENAME_CHUNK));\n let offset = 0;\n while (offset < size) {\n const len = Math.min(chunk.length, size - offset);\n const view = len === chunk.length ? chunk : chunk.subarray(0, len);\n srcAccess.read(view, { at: offset });\n dstAccess.write(view, { at: offset });\n offset += len;\n }\n }\n dstAccess.flush();\n\n // Release handles before removeEntry — can't unlink a file that\n // still has an open sync access handle.\n try { dstAccess.close(); } catch { /* ignore */ }\n dstAccess = null;\n try { srcAccess.close(); } catch { /* ignore */ }\n srcAccess = null;\n\n await oldDir.removeEntry(basename(oldPath));\n } catch (err) {\n console.warn('[opfs-sync] rename failed:', oldPath, '→', newPath, err);\n } finally {\n if (dstAccess) { try { dstAccess.close(); } catch { /* ignore */ } }\n if (srcAccess) { try { srcAccess.close(); } catch { /* ignore */ } }\n }\n}\n\n/**\n * Directory-aware rename in OPFS — walks the source tree and copies\n * each file via sync access handles, then deletes the source. Used\n * when `renameInOPFS` discovers oldPath is a directory rather than\n * a file.\n *\n * Replaces the destination directory entirely if it already exists,\n * matching Node.js rename semantics for directories.\n */\nasync function renameDirInOPFS(oldPath: string, newPath: string): Promise<void> {\n // Resolve the source directory handle.\n const oldParent = await navigateToParent(oldPath);\n const srcDir = await oldParent.getDirectoryHandle(basename(oldPath));\n\n // Wipe the target if it exists (file or directory) so the rename\n // is a clean replace, then recreate it as a fresh directory.\n const newParent = await ensureParentDirs(newPath);\n try {\n await newParent.removeEntry(basename(newPath), { recursive: true });\n } catch (e: any) {\n // NotFoundError is expected when target doesn't exist; everything\n // else we let bubble (caller logs a single warning).\n if (e?.name !== 'NotFoundError') throw e;\n }\n const dstDir = await newParent.getDirectoryHandle(basename(newPath), { create: true });\n\n await copyDirContents(srcDir, dstDir);\n\n // Remove the now-copied source.\n await oldParent.removeEntry(basename(oldPath), { recursive: true });\n}\n\n/**\n * Recursively copy every entry under `src` into `dst`.\n * Uses sync access handles for files (chunked) so memory peaks at\n * RENAME_CHUNK regardless of file size.\n */\nasync function copyDirContents(\n src: FileSystemDirectoryHandle,\n dst: FileSystemDirectoryHandle,\n): Promise<void> {\n // entries() is async-iterable: [name, FileSystemHandle]\n for await (const [name, handle] of (src as any).entries()) {\n if (handle.kind === 'directory') {\n const childDst = await dst.getDirectoryHandle(name, { create: true });\n await copyDirContents(handle as FileSystemDirectoryHandle, childDst);\n } else {\n const fileHandle = handle as FileSystemFileHandle;\n const dstFile = await dst.getFileHandle(name, { create: true });\n let srcAccess: FileSystemSyncAccessHandle | null = null;\n let dstAccess: FileSystemSyncAccessHandle | null = null;\n try {\n srcAccess = await fileHandle.createSyncAccessHandle();\n dstAccess = await dstFile.createSyncAccessHandle();\n const size = srcAccess.getSize();\n dstAccess.truncate(0);\n if (size > 0) {\n const chunk = new Uint8Array(Math.min(size, RENAME_CHUNK));\n let offset = 0;\n while (offset < size) {\n const len = Math.min(chunk.length, size - offset);\n const view = len === chunk.length ? chunk : chunk.subarray(0, len);\n srcAccess.read(view, { at: offset });\n dstAccess.write(view, { at: offset });\n offset += len;\n }\n }\n dstAccess.flush();\n } finally {\n if (dstAccess) { try { dstAccess.close(); } catch { /* ignore */ } }\n if (srcAccess) { try { srcAccess.close(); } catch { /* ignore */ } }\n }\n }\n }\n}\n\nasync function navigateToParent(path: string): Promise<FileSystemDirectoryHandle> {\n const parts = pathSegments(path);\n parts.pop();\n\n let dir = mirrorRoot;\n for (const part of parts) {\n dir = await dir.getDirectoryHandle(part);\n }\n return dir;\n}\n\n// ========== FileSystemObserver for external changes ==========\n\nfunction setupObserver(): void {\n if (typeof FileSystemObserver === 'undefined') {\n console.warn('[opfs-sync] FileSystemObserver not available — external changes will not be detected');\n return;\n }\n\n console.log('[opfs-sync] Setting up FileSystemObserver on mirrorRoot:', mirrorRoot.name || '(opfs-root)');\n\n const observer = new FileSystemObserver((records) => {\n //console.log(`[opfs-sync] observer fired: ${records.length} record(s), pending=${pendingPaths.size}, completed=${completedPaths.size}`);\n for (const record of records) {\n const path = normalizePath('/' + record.relativePathComponents.join('/'));\n\n // Skip VFS binary file and internal files\n if (path === '/.vfs.bin' || path === '/.vfs' || path.startsWith('/.vfs')) continue;\n\n // Echo suppression — check parents only for 'disappeared' (recursive delete cascading)\n const isDelete = record.type === 'disappeared';\n if (isOurEcho(path, isDelete)) {\n //console.log('[opfs-sync] suppressed (echo):', record.type, path);\n continue;\n }\n\n //console.log('[opfs-sync] external:', record.type, path);\n switch (record.type) {\n case 'appeared':\n case 'modified':\n syncExternalChange(path, record.changedHandle);\n break;\n case 'disappeared':\n syncExternalDelete(path);\n break;\n case 'moved': {\n const from = normalizePath('/' + record.relativePathMovedFrom!.join('/'));\n //console.log('[opfs-sync] external: moved from', from, '→', path);\n syncExternalRename(from, path);\n break;\n }\n }\n }\n });\n\n observer.observe(mirrorRoot, { recursive: true });\n}\n\nasync function syncExternalChange(path: string, handle: FileSystemHandle | null): Promise<void> {\n try {\n if (!handle || handle.kind !== 'file') return;\n\n const fileHandle = handle as FileSystemFileHandle;\n const file = await fileHandle.getFile();\n const data = await file.arrayBuffer();\n\n serverPort.postMessage({\n op: 'external-write',\n path,\n data,\n ts: Date.now(),\n }, [data]);\n } catch (err) {\n // File may have been deleted between observer event and our read, or\n // a sync access handle may be holding the lock — either is fine to skip\n console.warn('[opfs-sync] external change read failed:', path, err);\n }\n}\n\nfunction syncExternalDelete(path: string): void {\n serverPort.postMessage({\n op: 'external-delete',\n path,\n ts: Date.now(),\n });\n}\n\nfunction syncExternalRename(oldPath: string, newPath: string): void {\n serverPort.postMessage({\n op: 'external-rename',\n path: oldPath,\n newPath,\n ts: Date.now(),\n });\n}\n\n// ========== Initialization ==========\n\nself.onmessage = async (e: MessageEvent) => {\n const msg = e.data;\n\n if (msg.type === 'init') {\n serverPort = e.ports[0];\n mirrorRoot = await navigator.storage.getDirectory();\n\n // Navigate to mirror root if specified\n if (msg.root && msg.root !== '/') {\n const segments = msg.root.split('/').filter(Boolean);\n for (const segment of segments) {\n mirrorRoot = await mirrorRoot.getDirectoryHandle(segment, { create: true });\n }\n }\n\n console.log('[opfs-sync] initialized with root:', msg.root || '/', 'mirrorRoot.name:', mirrorRoot.name || '(opfs-root)');\n\n // Set up FileSystemObserver\n setupObserver();\n\n // Listen for events from server\n serverPort.onmessage = (ev: MessageEvent) => {\n const event = ev.data as SyncEvent;\n enqueue(event);\n };\n serverPort.start();\n\n (self as unknown as Worker).postMessage({ type: 'ready' });\n return;\n }\n};\n"],"mappings":";AAgBA,IAAI;AACJ,IAAI;AAGJ,SAAS,cAAc,GAAmB;AACxC,MAAI,EAAE,WAAW,CAAC,MAAM,GAAI,KAAI,MAAM;AACtC,MAAI,EAAE,QAAQ,IAAI,MAAM,GAAI,KAAI,E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A,EACX,SAAS,KAAK;AAGZ,YAAQ,KAAK,4CAA4C,MAAM,GAAG;AAAA,EACpE;AACF;AAEA,SAAS,mBAAmB,MAAoB;AAC9C,aAAW,YAAY;AAAA,IACrB,IAAI;AAAA,IACJ;AAAA,IACA,IAAI,KAAK,IAAI;AAAA,EACf,CAAC;AACH;AAEA,SAAS,mBAAmB,SAAiB,SAAuB;AAClE,aAAW,YAAY;AAAA,IACrB,IAAI;AAAA,IACJ,MAAM;AAAA,IACN;AAAA,IACA,IAAI,KAAK,IAAI;AAAA,EACf,CAAC;AACH;AAIA,KAAK,YAAY,OAAO,MAAoB;AAC1C,QAAM,MAAM,EAAE;AAEd,MAAI,IAAI,SAAS,QAAQ;AACvB,iBAAa,EAAE,MAAM,CAAC;AACtB,iBAAa,MAAM,UAAU,QAAQ,aAAa;AAGlD,QAAI,IAAI,QAAQ,IAAI,SAAS,KAAK;AAChC,YAAM,WAAW,IAAI,KAAK,MAAM,GAAG,EAAE,OAAO,OAAO;AACnD,iBAAW,WAAW,UAAU;AAC9B,qBAAa,MAAM,WAAW,mBAAmB,SAAS,EAAE,QAAQ,KAAK,CAAC;AAAA,MAC5E;AAAA,IACF;AAEA,YAAQ,IAAI,sCAAsC,IAAI,QAAQ,KAAK,oBAAoB,WAAW,QAAQ,aAAa;AAGvH,kBAAc;AAGd,eAAW,YAAY,CAAC,OAAqB;AAC3C,YAAM,QAAQ,GAAG;AACjB,cAAQ,KAAK;AAAA,IACf;AACA,eAAW,MAAM;AAEjB,IAAC,KAA2B,YAAY,EAAE,MAAM,QAAQ,CAAC;AACzD;AAAA,EACF;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@componentor/fs",
3
- "version": "3.0.49",
3
+ "version": "3.0.50",
4
4
  "description": "High-performance OPFS-based Node.js fs polyfill with true sync API, VFS binary format, and bidirectional OPFS mirroring",
5
5
  "license": "MIT",
6
6
  "author": "Componentor",
package/readme.md CHANGED
@@ -604,6 +604,12 @@ Make sure `opfsSync` is enabled (it's `true` by default). Files are mirrored to
604
604
 
605
605
  See [CHANGELOG.md](./CHANGELOG.md) for the full version history.
606
606
 
607
+ ### v3.0.50 (2026)
608
+
609
+ **Fixes:**
610
+ - OPFS mirror no longer diverges from VFS state when the rename source is a directory. `renameInOPFS` previously hard-coded `getFileHandle`, which throws `TypeMismatchError` for directories — so the VFS rename fix in 3.0.49 (e.g. Vite's `deps_temp_<hash>` → `deps`) succeeded in-memory but the on-disk OPFS mirror silently warned-and-skipped, leaving the two diverged until the next full reconcile
611
+ - On `TypeMismatchError`/`NotFoundError`, falls through to a directory-aware path: recursively `removeEntry` the destination (matching the engine's "rm-then-rename" semantics), recreate as an empty dir, walk the source tree copying every file via two sync access handles in 2 MB chunks (peak memory bounded by `RENAME_CHUNK` regardless of subtree size), then `removeEntry` the source
612
+
607
613
  ### v3.0.49 (2026)
608
614
 
609
615
  **Fixes:**