@componentor/fs 3.0.15 → 3.0.17

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.
@@ -2753,6 +2753,23 @@ async function followerLoop() {
2753
2753
  }
2754
2754
  }
2755
2755
  }
2756
+ var OPFS_SKIP = /* @__PURE__ */ new Set([".vfs.bin", ".vfs.bin.tmp"]);
2757
+ async function scanOPFSEntries(dir, prefix) {
2758
+ const result = [];
2759
+ for await (const [name, handle] of dir.entries()) {
2760
+ if (prefix === "" && OPFS_SKIP.has(name)) continue;
2761
+ const fullPath = prefix ? `${prefix}/${name}` : `/${name}`;
2762
+ if (handle.kind === "directory") {
2763
+ result.push({ path: fullPath, type: "directory" });
2764
+ result.push(...await scanOPFSEntries(handle, fullPath));
2765
+ } else {
2766
+ const file = await handle.getFile();
2767
+ const buf = await file.arrayBuffer();
2768
+ result.push({ path: fullPath, type: "file", data: new Uint8Array(buf) });
2769
+ }
2770
+ }
2771
+ return result;
2772
+ }
2756
2773
  async function initEngine(config) {
2757
2774
  debug = config.debug ?? false;
2758
2775
  let rootDir = await navigator.storage.getDirectory();
@@ -2764,6 +2781,7 @@ async function initEngine(config) {
2764
2781
  }
2765
2782
  const vfsFileHandle = await rootDir.getFileHandle(".vfs.bin", { create: true });
2766
2783
  const vfsHandle = await vfsFileHandle.createSyncAccessHandle();
2784
+ const wasFresh = vfsHandle.getSize() === 0;
2767
2785
  try {
2768
2786
  engine.init(vfsHandle, {
2769
2787
  uid: config.uid,
@@ -2779,6 +2797,19 @@ async function initEngine(config) {
2779
2797
  }
2780
2798
  throw err;
2781
2799
  }
2800
+ if (wasFresh) {
2801
+ const opfsEntries = await scanOPFSEntries(rootDir, "");
2802
+ if (opfsEntries.length > 0) {
2803
+ const dirs = opfsEntries.filter((e) => e.type === "directory").sort((a, b) => a.path.localeCompare(b.path));
2804
+ for (const dir of dirs) {
2805
+ engine.mkdir(dir.path, 16877);
2806
+ }
2807
+ for (const file of opfsEntries.filter((e) => e.type === "file")) {
2808
+ engine.write(file.path, file.data);
2809
+ }
2810
+ engine.flush();
2811
+ }
2812
+ }
2782
2813
  if (config.opfsSync) {
2783
2814
  opfsSyncEnabled = true;
2784
2815
  const mc = new MessageChannel();