@componentor/fs 3.0.42 → 3.0.44
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.
|
@@ -67,6 +67,16 @@ function enqueue(event) {
|
|
|
67
67
|
if (event.op === "rename" && event.newPath) {
|
|
68
68
|
trackPending(event.newPath);
|
|
69
69
|
}
|
|
70
|
+
if (event.op === "write") {
|
|
71
|
+
for (let i = queue.length - 1; i >= 0; i--) {
|
|
72
|
+
const pending = queue[i];
|
|
73
|
+
if (pending.op === "write" && normalizePath(pending.path) === normalizePath(event.path)) {
|
|
74
|
+
pending.data = event.data;
|
|
75
|
+
pending.ts = event.ts;
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
70
80
|
queue.push(event);
|
|
71
81
|
if (!processing) processNext();
|
|
72
82
|
}
|
|
@@ -146,25 +156,57 @@ async function mkdirInOPFS(path) {
|
|
|
146
156
|
dir = await dir.getDirectoryHandle(part, { create: true });
|
|
147
157
|
}
|
|
148
158
|
}
|
|
159
|
+
var RENAME_CHUNK = 2 * 1024 * 1024;
|
|
149
160
|
async function renameInOPFS(oldPath, newPath) {
|
|
161
|
+
let srcAccess = null;
|
|
162
|
+
let dstAccess = null;
|
|
150
163
|
try {
|
|
151
164
|
const oldDir = await navigateToParent(oldPath);
|
|
152
165
|
const oldHandle = await oldDir.getFileHandle(basename(oldPath));
|
|
153
|
-
|
|
154
|
-
const
|
|
166
|
+
srcAccess = await oldHandle.createSyncAccessHandle();
|
|
167
|
+
const size = srcAccess.getSize();
|
|
155
168
|
const newDir = await ensureParentDirs(newPath);
|
|
156
169
|
const newHandle = await newDir.getFileHandle(basename(newPath), { create: true });
|
|
157
|
-
|
|
170
|
+
dstAccess = await newHandle.createSyncAccessHandle();
|
|
171
|
+
dstAccess.truncate(0);
|
|
172
|
+
if (size > 0) {
|
|
173
|
+
const chunk = new Uint8Array(Math.min(size, RENAME_CHUNK));
|
|
174
|
+
let offset = 0;
|
|
175
|
+
while (offset < size) {
|
|
176
|
+
const len = Math.min(chunk.length, size - offset);
|
|
177
|
+
const view = len === chunk.length ? chunk : chunk.subarray(0, len);
|
|
178
|
+
srcAccess.read(view, { at: offset });
|
|
179
|
+
dstAccess.write(view, { at: offset });
|
|
180
|
+
offset += len;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
dstAccess.flush();
|
|
158
184
|
try {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
accessHandle.flush();
|
|
162
|
-
} finally {
|
|
163
|
-
accessHandle.close();
|
|
185
|
+
dstAccess.close();
|
|
186
|
+
} catch {
|
|
164
187
|
}
|
|
188
|
+
dstAccess = null;
|
|
189
|
+
try {
|
|
190
|
+
srcAccess.close();
|
|
191
|
+
} catch {
|
|
192
|
+
}
|
|
193
|
+
srcAccess = null;
|
|
165
194
|
await oldDir.removeEntry(basename(oldPath));
|
|
166
195
|
} catch (err) {
|
|
167
196
|
console.warn("[opfs-sync] rename failed:", oldPath, "\u2192", newPath, err);
|
|
197
|
+
} finally {
|
|
198
|
+
if (dstAccess) {
|
|
199
|
+
try {
|
|
200
|
+
dstAccess.close();
|
|
201
|
+
} catch {
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (srcAccess) {
|
|
205
|
+
try {
|
|
206
|
+
srcAccess.close();
|
|
207
|
+
} catch {
|
|
208
|
+
}
|
|
209
|
+
}
|
|
168
210
|
}
|
|
169
211
|
}
|
|
170
212
|
async function navigateToParent(path) {
|
|
@@ -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 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\nasync function renameInOPFS(oldPath: string, newPath: string): Promise<void> {\n // OPFS doesn't have a native rename — copy + delete\n try {\n const oldDir = await navigateToParent(oldPath);\n const oldHandle = await oldDir.getFileHandle(basename(oldPath));\n const file = await oldHandle.getFile();\n const data = await file.arrayBuffer();\n\n const newDir = await ensureParentDirs(newPath);\n const newHandle = await newDir.getFileHandle(basename(newPath), { create: true });\n const accessHandle = await newHandle.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 await oldDir.removeEntry(basename(oldPath));\n } catch (err) {\n console.warn('[opfs-sync] rename failed:', oldPath, '→', newPath, err);\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,K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AAG;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. 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,UAA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names":[]}
|
|
@@ -3016,21 +3016,53 @@ async function followerLoop() {
|
|
|
3016
3016
|
}
|
|
3017
3017
|
}
|
|
3018
3018
|
var OPFS_SKIP = /* @__PURE__ */ new Set([".vfs.bin", ".vfs.bin.tmp"]);
|
|
3019
|
-
|
|
3020
|
-
|
|
3019
|
+
var OPFS_POPULATE_CHUNK = 2 * 1024 * 1024;
|
|
3020
|
+
async function populateVFSFromOPFS(dir, prefix) {
|
|
3021
|
+
const subdirs = [];
|
|
3022
|
+
const files = [];
|
|
3021
3023
|
for await (const [name, handle] of dir.entries()) {
|
|
3022
3024
|
if (prefix === "" && OPFS_SKIP.has(name)) continue;
|
|
3023
|
-
const fullPath = prefix ? `${prefix}/${name}` : `/${name}`;
|
|
3024
3025
|
if (handle.kind === "directory") {
|
|
3025
|
-
|
|
3026
|
-
result.push(...await scanOPFSEntries(handle, fullPath));
|
|
3026
|
+
subdirs.push({ name, handle });
|
|
3027
3027
|
} else {
|
|
3028
|
-
|
|
3029
|
-
const buf = await file.arrayBuffer();
|
|
3030
|
-
result.push({ path: fullPath, type: "file", data: new Uint8Array(buf) });
|
|
3028
|
+
files.push({ name, handle });
|
|
3031
3029
|
}
|
|
3032
3030
|
}
|
|
3033
|
-
|
|
3031
|
+
for (const { name } of subdirs) {
|
|
3032
|
+
const fullPath = prefix ? `${prefix}/${name}` : `/${name}`;
|
|
3033
|
+
engine.mkdir(fullPath, 16877);
|
|
3034
|
+
}
|
|
3035
|
+
for (const { name, handle } of files) {
|
|
3036
|
+
const fullPath = prefix ? `${prefix}/${name}` : `/${name}`;
|
|
3037
|
+
let access = null;
|
|
3038
|
+
try {
|
|
3039
|
+
access = await handle.createSyncAccessHandle();
|
|
3040
|
+
const size = access.getSize();
|
|
3041
|
+
engine.write(fullPath, new Uint8Array(0));
|
|
3042
|
+
if (size > 0) {
|
|
3043
|
+
const chunk = new Uint8Array(Math.min(size, OPFS_POPULATE_CHUNK));
|
|
3044
|
+
let offset = 0;
|
|
3045
|
+
while (offset < size) {
|
|
3046
|
+
const len = Math.min(chunk.length, size - offset);
|
|
3047
|
+
const view = len === chunk.length ? chunk : chunk.subarray(0, len);
|
|
3048
|
+
access.read(view, { at: offset });
|
|
3049
|
+
engine.append(fullPath, view);
|
|
3050
|
+
offset += len;
|
|
3051
|
+
}
|
|
3052
|
+
}
|
|
3053
|
+
} finally {
|
|
3054
|
+
if (access) {
|
|
3055
|
+
try {
|
|
3056
|
+
access.close();
|
|
3057
|
+
} catch {
|
|
3058
|
+
}
|
|
3059
|
+
}
|
|
3060
|
+
}
|
|
3061
|
+
}
|
|
3062
|
+
for (const { name, handle } of subdirs) {
|
|
3063
|
+
const fullPath = prefix ? `${prefix}/${name}` : `/${name}`;
|
|
3064
|
+
await populateVFSFromOPFS(handle, fullPath);
|
|
3065
|
+
}
|
|
3034
3066
|
}
|
|
3035
3067
|
var DEFAULT_LIMITS = {
|
|
3036
3068
|
maxInodes: 4e6,
|
|
@@ -3121,17 +3153,8 @@ async function initEngine(config) {
|
|
|
3121
3153
|
throw err;
|
|
3122
3154
|
}
|
|
3123
3155
|
if (wasFresh) {
|
|
3124
|
-
|
|
3125
|
-
|
|
3126
|
-
const dirs = opfsEntries.filter((e) => e.type === "directory").sort((a, b) => a.path.localeCompare(b.path));
|
|
3127
|
-
for (const dir of dirs) {
|
|
3128
|
-
engine.mkdir(dir.path, 16877);
|
|
3129
|
-
}
|
|
3130
|
-
for (const file of opfsEntries.filter((e) => e.type === "file")) {
|
|
3131
|
-
engine.write(file.path, file.data);
|
|
3132
|
-
}
|
|
3133
|
-
engine.flush();
|
|
3134
|
-
}
|
|
3156
|
+
await populateVFSFromOPFS(rootDir, "");
|
|
3157
|
+
engine.flush();
|
|
3135
3158
|
}
|
|
3136
3159
|
if (config.opfsSync) {
|
|
3137
3160
|
opfsSyncEnabled = true;
|
|
@@ -3197,6 +3220,29 @@ function broadcastWatch(op, path, newPath) {
|
|
|
3197
3220
|
watchBc.postMessage({ eventType: "rename", path: newPath });
|
|
3198
3221
|
}
|
|
3199
3222
|
}
|
|
3223
|
+
var pendingPathSyncs = /* @__PURE__ */ new Map();
|
|
3224
|
+
var SYNC_DEBOUNCE_MS = 50;
|
|
3225
|
+
function flushPathSync(path) {
|
|
3226
|
+
pendingPathSyncs.delete(path);
|
|
3227
|
+
if (!opfsSyncPort) return;
|
|
3228
|
+
try {
|
|
3229
|
+
const result = engine.read(path);
|
|
3230
|
+
if (result.status !== 0) return;
|
|
3231
|
+
const ts = Date.now();
|
|
3232
|
+
if (result.data && result.data.byteLength > 0) {
|
|
3233
|
+
const buf = result.data.buffer.byteLength === result.data.byteLength ? result.data.buffer : result.data.slice().buffer;
|
|
3234
|
+
opfsSyncPort.postMessage({ op: "write", path, data: buf, ts }, [buf]);
|
|
3235
|
+
} else {
|
|
3236
|
+
opfsSyncPort.postMessage({ op: "write", path, data: new ArrayBuffer(0), ts });
|
|
3237
|
+
}
|
|
3238
|
+
} catch {
|
|
3239
|
+
}
|
|
3240
|
+
}
|
|
3241
|
+
function schedulePathSync(path) {
|
|
3242
|
+
const prev = pendingPathSyncs.get(path);
|
|
3243
|
+
if (prev) clearTimeout(prev);
|
|
3244
|
+
pendingPathSyncs.set(path, setTimeout(() => flushPathSync(path), SYNC_DEBOUNCE_MS));
|
|
3245
|
+
}
|
|
3200
3246
|
function notifyOPFSSync(op, path, newPath) {
|
|
3201
3247
|
if (!opfsSyncPort) return;
|
|
3202
3248
|
if (suppressPaths.has(path)) {
|
|
@@ -3212,39 +3258,35 @@ function notifyOPFSSync(op, path, newPath) {
|
|
|
3212
3258
|
case OP.FTRUNCATE:
|
|
3213
3259
|
case OP.COPY:
|
|
3214
3260
|
case OP.LINK: {
|
|
3215
|
-
|
|
3216
|
-
if (result.status === 0) {
|
|
3217
|
-
if (result.data && result.data.byteLength > 0) {
|
|
3218
|
-
const buf = result.data.buffer.byteLength === result.data.byteLength ? result.data.buffer : result.data.slice().buffer;
|
|
3219
|
-
opfsSyncPort.postMessage({ op: "write", path, data: buf, ts }, [buf]);
|
|
3220
|
-
} else {
|
|
3221
|
-
opfsSyncPort.postMessage({ op: "write", path, data: new ArrayBuffer(0), ts });
|
|
3222
|
-
}
|
|
3223
|
-
}
|
|
3261
|
+
schedulePathSync(path);
|
|
3224
3262
|
break;
|
|
3225
3263
|
}
|
|
3226
3264
|
case OP.SYMLINK: {
|
|
3227
|
-
|
|
3228
|
-
if (result.status === 0) {
|
|
3229
|
-
if (result.data && result.data.byteLength > 0) {
|
|
3230
|
-
const buf = result.data.buffer.byteLength === result.data.byteLength ? result.data.buffer : result.data.slice().buffer;
|
|
3231
|
-
opfsSyncPort.postMessage({ op: "write", path, data: buf, ts }, [buf]);
|
|
3232
|
-
} else {
|
|
3233
|
-
opfsSyncPort.postMessage({ op: "write", path, data: new ArrayBuffer(0), ts });
|
|
3234
|
-
}
|
|
3235
|
-
}
|
|
3265
|
+
schedulePathSync(path);
|
|
3236
3266
|
break;
|
|
3237
3267
|
}
|
|
3238
3268
|
case OP.UNLINK:
|
|
3239
|
-
case OP.RMDIR:
|
|
3269
|
+
case OP.RMDIR: {
|
|
3270
|
+
const pending = pendingPathSyncs.get(path);
|
|
3271
|
+
if (pending) {
|
|
3272
|
+
clearTimeout(pending);
|
|
3273
|
+
pendingPathSyncs.delete(path);
|
|
3274
|
+
}
|
|
3240
3275
|
opfsSyncPort.postMessage({ op: "delete", path, ts });
|
|
3241
3276
|
break;
|
|
3277
|
+
}
|
|
3242
3278
|
case OP.MKDIR:
|
|
3243
3279
|
case OP.MKDTEMP:
|
|
3244
3280
|
opfsSyncPort.postMessage({ op: "mkdir", path, ts });
|
|
3245
3281
|
break;
|
|
3246
3282
|
case OP.RENAME:
|
|
3247
3283
|
if (newPath) {
|
|
3284
|
+
const pending = pendingPathSyncs.get(path);
|
|
3285
|
+
if (pending) {
|
|
3286
|
+
clearTimeout(pending);
|
|
3287
|
+
pendingPathSyncs.delete(path);
|
|
3288
|
+
schedulePathSync(newPath);
|
|
3289
|
+
}
|
|
3248
3290
|
opfsSyncPort.postMessage({ op: "rename", path, newPath, ts });
|
|
3249
3291
|
}
|
|
3250
3292
|
break;
|