@componentor/fs 1.2.4 → 1.2.6

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.
@@ -403,12 +403,15 @@ var SymlinkManager = class {
403
403
  const buffer = new TextEncoder().encode(data);
404
404
  if (this.useSync) {
405
405
  const access = await fileHandle.createSyncAccessHandle();
406
- access.truncate(0);
407
- let written = 0;
408
- while (written < buffer.length) {
409
- written += access.write(buffer.subarray(written), { at: written });
406
+ try {
407
+ access.truncate(0);
408
+ let written = 0;
409
+ while (written < buffer.length) {
410
+ written += access.write(buffer.subarray(written), { at: written });
411
+ }
412
+ } finally {
413
+ access.close();
410
414
  }
411
- access.close();
412
415
  } else {
413
416
  const writable = await fileHandle.createWritable();
414
417
  await writable.write(buffer);
@@ -653,29 +656,12 @@ var PackedStorage = class {
653
656
  useChecksum;
654
657
  index = null;
655
658
  indexLoaded = false;
656
- lockPromise = null;
657
659
  constructor(handleManager, useSync, useCompression = false, useChecksum = true) {
658
660
  this.handleManager = handleManager;
659
661
  this.useSync = useSync;
660
662
  this.useCompression = useCompression && typeof CompressionStream !== "undefined";
661
663
  this.useChecksum = useChecksum;
662
664
  }
663
- /**
664
- * Acquire lock for pack file access (prevents concurrent handle conflicts)
665
- */
666
- async acquireLock() {
667
- while (this.lockPromise) {
668
- await this.lockPromise;
669
- }
670
- let release;
671
- this.lockPromise = new Promise((resolve) => {
672
- release = () => {
673
- this.lockPromise = null;
674
- resolve();
675
- };
676
- });
677
- return release;
678
- }
679
665
  /**
680
666
  * Reset pack storage state (memory only)
681
667
  */
@@ -759,62 +745,47 @@ var PackedStorage = class {
759
745
  * Check if a path exists in the pack
760
746
  */
761
747
  async has(path) {
762
- const release = await this.acquireLock();
763
- try {
764
- const index = await this.loadIndex();
765
- return path in index;
766
- } finally {
767
- release();
768
- }
748
+ const index = await this.loadIndex();
749
+ return path in index;
769
750
  }
770
751
  /**
771
752
  * Get file size from pack (for stat)
772
753
  * Returns originalSize if compressed, otherwise size
773
754
  */
774
755
  async getSize(path) {
775
- const release = await this.acquireLock();
776
- try {
777
- const index = await this.loadIndex();
778
- const entry = index[path];
779
- if (!entry) return null;
780
- return entry.originalSize ?? entry.size;
781
- } finally {
782
- release();
783
- }
756
+ const index = await this.loadIndex();
757
+ const entry = index[path];
758
+ if (!entry) return null;
759
+ return entry.originalSize ?? entry.size;
784
760
  }
785
761
  /**
786
762
  * Read a file from the pack
787
763
  * Handles decompression if file was stored compressed
788
764
  */
789
765
  async read(path) {
790
- const release = await this.acquireLock();
791
- try {
792
- const index = await this.loadIndex();
793
- const entry = index[path];
794
- if (!entry) return null;
795
- const { fileHandle } = await this.handleManager.getHandle(PACK_FILE);
796
- if (!fileHandle) return null;
797
- let buffer;
798
- if (this.useSync) {
799
- const access = await fileHandle.createSyncAccessHandle();
800
- try {
801
- buffer = new Uint8Array(entry.size);
802
- access.read(buffer, { at: entry.offset });
803
- } finally {
804
- access.close();
805
- }
806
- } else {
807
- const file = await fileHandle.getFile();
808
- const data = new Uint8Array(await file.arrayBuffer());
809
- buffer = data.slice(entry.offset, entry.offset + entry.size);
810
- }
811
- if (entry.originalSize !== void 0) {
812
- return decompress(buffer);
766
+ const index = await this.loadIndex();
767
+ const entry = index[path];
768
+ if (!entry) return null;
769
+ const { fileHandle } = await this.handleManager.getHandle(PACK_FILE);
770
+ if (!fileHandle) return null;
771
+ let buffer;
772
+ if (this.useSync) {
773
+ const access = await fileHandle.createSyncAccessHandle();
774
+ try {
775
+ buffer = new Uint8Array(entry.size);
776
+ access.read(buffer, { at: entry.offset });
777
+ } finally {
778
+ access.close();
813
779
  }
814
- return buffer;
815
- } finally {
816
- release();
780
+ } else {
781
+ const file = await fileHandle.getFile();
782
+ const data = new Uint8Array(await file.arrayBuffer());
783
+ buffer = data.slice(entry.offset, entry.offset + entry.size);
817
784
  }
785
+ if (entry.originalSize !== void 0) {
786
+ return decompress(buffer);
787
+ }
788
+ return buffer;
818
789
  }
819
790
  /**
820
791
  * Read multiple files from the pack in a single operation
@@ -824,61 +795,56 @@ var PackedStorage = class {
824
795
  async readBatch(paths) {
825
796
  const results = /* @__PURE__ */ new Map();
826
797
  if (paths.length === 0) return results;
827
- const release = await this.acquireLock();
828
- try {
829
- const index = await this.loadIndex();
830
- const toRead = [];
831
- for (const path of paths) {
832
- const entry = index[path];
833
- if (entry) {
834
- toRead.push({ path, offset: entry.offset, size: entry.size, originalSize: entry.originalSize });
835
- } else {
836
- results.set(path, null);
837
- }
798
+ const index = await this.loadIndex();
799
+ const toRead = [];
800
+ for (const path of paths) {
801
+ const entry = index[path];
802
+ if (entry) {
803
+ toRead.push({ path, offset: entry.offset, size: entry.size, originalSize: entry.originalSize });
804
+ } else {
805
+ results.set(path, null);
838
806
  }
839
- if (toRead.length === 0) return results;
840
- const { fileHandle } = await this.handleManager.getHandle(PACK_FILE);
841
- if (!fileHandle) {
842
- for (const { path } of toRead) {
843
- results.set(path, null);
844
- }
845
- return results;
807
+ }
808
+ if (toRead.length === 0) return results;
809
+ const { fileHandle } = await this.handleManager.getHandle(PACK_FILE);
810
+ if (!fileHandle) {
811
+ for (const { path } of toRead) {
812
+ results.set(path, null);
846
813
  }
847
- const decompressPromises = [];
848
- if (this.useSync) {
849
- const access = await fileHandle.createSyncAccessHandle();
850
- try {
851
- for (const { path, offset, size, originalSize } of toRead) {
852
- const buffer = new Uint8Array(size);
853
- access.read(buffer, { at: offset });
854
- if (originalSize !== void 0) {
855
- decompressPromises.push({ path, promise: decompress(buffer) });
856
- } else {
857
- results.set(path, buffer);
858
- }
859
- }
860
- } finally {
861
- access.close();
862
- }
863
- } else {
864
- const file = await fileHandle.getFile();
865
- const data = new Uint8Array(await file.arrayBuffer());
814
+ return results;
815
+ }
816
+ const decompressPromises = [];
817
+ if (this.useSync) {
818
+ const access = await fileHandle.createSyncAccessHandle();
819
+ try {
866
820
  for (const { path, offset, size, originalSize } of toRead) {
867
- const buffer = data.slice(offset, offset + size);
821
+ const buffer = new Uint8Array(size);
822
+ access.read(buffer, { at: offset });
868
823
  if (originalSize !== void 0) {
869
824
  decompressPromises.push({ path, promise: decompress(buffer) });
870
825
  } else {
871
826
  results.set(path, buffer);
872
827
  }
873
828
  }
829
+ } finally {
830
+ access.close();
874
831
  }
875
- for (const { path, promise } of decompressPromises) {
876
- results.set(path, await promise);
832
+ } else {
833
+ const file = await fileHandle.getFile();
834
+ const data = new Uint8Array(await file.arrayBuffer());
835
+ for (const { path, offset, size, originalSize } of toRead) {
836
+ const buffer = data.slice(offset, offset + size);
837
+ if (originalSize !== void 0) {
838
+ decompressPromises.push({ path, promise: decompress(buffer) });
839
+ } else {
840
+ results.set(path, buffer);
841
+ }
877
842
  }
878
- return results;
879
- } finally {
880
- release();
881
843
  }
844
+ for (const { path, promise } of decompressPromises) {
845
+ results.set(path, await promise);
846
+ }
847
+ return results;
882
848
  }
883
849
  /**
884
850
  * Write multiple files to the pack in a single operation
@@ -889,62 +855,57 @@ var PackedStorage = class {
889
855
  */
890
856
  async writeBatch(entries) {
891
857
  if (entries.length === 0) return;
892
- const release = await this.acquireLock();
893
- try {
894
- const encoder = new TextEncoder();
895
- let processedEntries;
896
- if (this.useCompression) {
897
- processedEntries = await Promise.all(
898
- entries.map(async ({ path, data }) => {
899
- const compressed = await compress(data);
900
- if (compressed.length < data.length) {
901
- return { path, data: compressed, originalSize: data.length };
902
- }
903
- return { path, data };
904
- })
905
- );
906
- } else {
907
- processedEntries = entries;
908
- }
909
- let totalDataSize = 0;
910
- for (const { data } of processedEntries) {
911
- totalDataSize += data.length;
912
- }
913
- const newIndex = {};
914
- let headerSize = 8;
915
- let prevHeaderSize = 0;
916
- while (headerSize !== prevHeaderSize) {
917
- prevHeaderSize = headerSize;
918
- let currentOffset = headerSize;
919
- for (const { path, data, originalSize } of processedEntries) {
920
- const entry = { offset: currentOffset, size: data.length };
921
- if (originalSize !== void 0) {
922
- entry.originalSize = originalSize;
858
+ const encoder = new TextEncoder();
859
+ let processedEntries;
860
+ if (this.useCompression) {
861
+ processedEntries = await Promise.all(
862
+ entries.map(async ({ path, data }) => {
863
+ const compressed = await compress(data);
864
+ if (compressed.length < data.length) {
865
+ return { path, data: compressed, originalSize: data.length };
923
866
  }
924
- newIndex[path] = entry;
925
- currentOffset += data.length;
867
+ return { path, data };
868
+ })
869
+ );
870
+ } else {
871
+ processedEntries = entries;
872
+ }
873
+ let totalDataSize = 0;
874
+ for (const { data } of processedEntries) {
875
+ totalDataSize += data.length;
876
+ }
877
+ const newIndex = {};
878
+ let headerSize = 8;
879
+ let prevHeaderSize = 0;
880
+ while (headerSize !== prevHeaderSize) {
881
+ prevHeaderSize = headerSize;
882
+ let currentOffset = headerSize;
883
+ for (const { path, data, originalSize } of processedEntries) {
884
+ const entry = { offset: currentOffset, size: data.length };
885
+ if (originalSize !== void 0) {
886
+ entry.originalSize = originalSize;
926
887
  }
927
- const indexBuf = encoder.encode(JSON.stringify(newIndex));
928
- headerSize = 8 + indexBuf.length;
929
- }
930
- const finalIndexBuf = encoder.encode(JSON.stringify(newIndex));
931
- const totalSize = headerSize + totalDataSize;
932
- const packBuffer = new Uint8Array(totalSize);
933
- const view = new DataView(packBuffer.buffer);
934
- packBuffer.set(finalIndexBuf, 8);
935
- for (const { path, data } of processedEntries) {
936
- const entry = newIndex[path];
937
- packBuffer.set(data, entry.offset);
938
- }
939
- const content = packBuffer.subarray(8);
940
- const checksum = this.useChecksum ? crc32(content) : 0;
941
- view.setUint32(0, finalIndexBuf.length, true);
942
- view.setUint32(4, checksum, true);
943
- await this.writePackFile(packBuffer);
944
- this.index = newIndex;
945
- } finally {
946
- release();
888
+ newIndex[path] = entry;
889
+ currentOffset += data.length;
890
+ }
891
+ const indexBuf = encoder.encode(JSON.stringify(newIndex));
892
+ headerSize = 8 + indexBuf.length;
893
+ }
894
+ const finalIndexBuf = encoder.encode(JSON.stringify(newIndex));
895
+ const totalSize = headerSize + totalDataSize;
896
+ const packBuffer = new Uint8Array(totalSize);
897
+ const view = new DataView(packBuffer.buffer);
898
+ packBuffer.set(finalIndexBuf, 8);
899
+ for (const { path, data } of processedEntries) {
900
+ const entry = newIndex[path];
901
+ packBuffer.set(data, entry.offset);
947
902
  }
903
+ const content = packBuffer.subarray(8);
904
+ const checksum = this.useChecksum ? crc32(content) : 0;
905
+ view.setUint32(0, finalIndexBuf.length, true);
906
+ view.setUint32(4, checksum, true);
907
+ await this.writePackFile(packBuffer);
908
+ this.index = newIndex;
948
909
  }
949
910
  /**
950
911
  * Write the pack file to OPFS
@@ -972,82 +933,72 @@ var PackedStorage = class {
972
933
  * Note: Doesn't reclaim space, just removes from index and recalculates CRC32
973
934
  */
974
935
  async remove(path) {
975
- const release = await this.acquireLock();
976
- try {
977
- const index = await this.loadIndex();
978
- if (!(path in index)) return false;
979
- delete index[path];
980
- const { fileHandle } = await this.handleManager.getHandle(PACK_FILE);
981
- if (!fileHandle) return true;
982
- const encoder = new TextEncoder();
983
- const newIndexBuf = encoder.encode(JSON.stringify(index));
984
- if (this.useSync) {
985
- const access = await fileHandle.createSyncAccessHandle();
986
- try {
987
- const size = access.getSize();
988
- const oldHeader = new Uint8Array(8);
989
- access.read(oldHeader, { at: 0 });
990
- const oldIndexLen = new DataView(oldHeader.buffer).getUint32(0, true);
991
- const dataStart = 8 + oldIndexLen;
992
- const dataSize = size - dataStart;
993
- const dataPortion = new Uint8Array(dataSize);
994
- if (dataSize > 0) {
995
- access.read(dataPortion, { at: dataStart });
996
- }
997
- const newContent = new Uint8Array(newIndexBuf.length + dataSize);
998
- newContent.set(newIndexBuf, 0);
999
- if (dataSize > 0) {
1000
- newContent.set(dataPortion, newIndexBuf.length);
1001
- }
1002
- const checksum = this.useChecksum ? crc32(newContent) : 0;
1003
- const newHeader = new Uint8Array(8);
1004
- const view = new DataView(newHeader.buffer);
1005
- view.setUint32(0, newIndexBuf.length, true);
1006
- view.setUint32(4, checksum, true);
1007
- const newFile = new Uint8Array(8 + newContent.length);
1008
- newFile.set(newHeader, 0);
1009
- newFile.set(newContent, 8);
1010
- access.truncate(newFile.length);
1011
- access.write(newFile, { at: 0 });
1012
- } finally {
1013
- access.close();
1014
- }
1015
- } else {
1016
- const file = await fileHandle.getFile();
1017
- const oldData = new Uint8Array(await file.arrayBuffer());
1018
- if (oldData.length < 8) return true;
1019
- const oldIndexLen = new DataView(oldData.buffer).getUint32(0, true);
936
+ const index = await this.loadIndex();
937
+ if (!(path in index)) return false;
938
+ delete index[path];
939
+ const { fileHandle } = await this.handleManager.getHandle(PACK_FILE);
940
+ if (!fileHandle) return true;
941
+ const encoder = new TextEncoder();
942
+ const newIndexBuf = encoder.encode(JSON.stringify(index));
943
+ if (this.useSync) {
944
+ const access = await fileHandle.createSyncAccessHandle();
945
+ try {
946
+ const size = access.getSize();
947
+ const oldHeader = new Uint8Array(8);
948
+ access.read(oldHeader, { at: 0 });
949
+ const oldIndexLen = new DataView(oldHeader.buffer).getUint32(0, true);
1020
950
  const dataStart = 8 + oldIndexLen;
1021
- const dataPortion = oldData.subarray(dataStart);
1022
- const newContent = new Uint8Array(newIndexBuf.length + dataPortion.length);
951
+ const dataSize = size - dataStart;
952
+ const dataPortion = new Uint8Array(dataSize);
953
+ if (dataSize > 0) {
954
+ access.read(dataPortion, { at: dataStart });
955
+ }
956
+ const newContent = new Uint8Array(newIndexBuf.length + dataSize);
1023
957
  newContent.set(newIndexBuf, 0);
1024
- newContent.set(dataPortion, newIndexBuf.length);
958
+ if (dataSize > 0) {
959
+ newContent.set(dataPortion, newIndexBuf.length);
960
+ }
1025
961
  const checksum = this.useChecksum ? crc32(newContent) : 0;
1026
- const newFile = new Uint8Array(8 + newContent.length);
1027
- const view = new DataView(newFile.buffer);
962
+ const newHeader = new Uint8Array(8);
963
+ const view = new DataView(newHeader.buffer);
1028
964
  view.setUint32(0, newIndexBuf.length, true);
1029
965
  view.setUint32(4, checksum, true);
966
+ const newFile = new Uint8Array(8 + newContent.length);
967
+ newFile.set(newHeader, 0);
1030
968
  newFile.set(newContent, 8);
1031
- const writable = await fileHandle.createWritable();
1032
- await writable.write(newFile);
1033
- await writable.close();
969
+ access.truncate(newFile.length);
970
+ access.write(newFile, { at: 0 });
971
+ } finally {
972
+ access.close();
1034
973
  }
1035
- return true;
1036
- } finally {
1037
- release();
974
+ } else {
975
+ const file = await fileHandle.getFile();
976
+ const oldData = new Uint8Array(await file.arrayBuffer());
977
+ if (oldData.length < 8) return true;
978
+ const oldIndexLen = new DataView(oldData.buffer).getUint32(0, true);
979
+ const dataStart = 8 + oldIndexLen;
980
+ const dataPortion = oldData.subarray(dataStart);
981
+ const newContent = new Uint8Array(newIndexBuf.length + dataPortion.length);
982
+ newContent.set(newIndexBuf, 0);
983
+ newContent.set(dataPortion, newIndexBuf.length);
984
+ const checksum = this.useChecksum ? crc32(newContent) : 0;
985
+ const newFile = new Uint8Array(8 + newContent.length);
986
+ const view = new DataView(newFile.buffer);
987
+ view.setUint32(0, newIndexBuf.length, true);
988
+ view.setUint32(4, checksum, true);
989
+ newFile.set(newContent, 8);
990
+ const writable = await fileHandle.createWritable();
991
+ await writable.write(newFile);
992
+ await writable.close();
1038
993
  }
994
+ return true;
1039
995
  }
1040
996
  /**
1041
997
  * Check if pack file is being used (has entries)
1042
998
  */
1043
999
  async isEmpty() {
1044
- const release = await this.acquireLock();
1045
- try {
1046
- const index = await this.loadIndex();
1047
- return Object.keys(index).length === 0;
1048
- } finally {
1049
- release();
1050
- }
1000
+ const index = await this.loadIndex();
1001
+ return Object.keys(index).length === 0;
1051
1002
  }
1052
1003
  };
1053
1004
 
@@ -1690,10 +1641,13 @@ var OPFS = class {
1690
1641
  let buffer;
1691
1642
  if (this.useSync) {
1692
1643
  const access = await fileHandle.createSyncAccessHandle();
1693
- const size = access.getSize();
1694
- buffer = new Uint8Array(size);
1695
- access.read(buffer);
1696
- access.close();
1644
+ try {
1645
+ const size = access.getSize();
1646
+ buffer = new Uint8Array(size);
1647
+ access.read(buffer);
1648
+ } finally {
1649
+ access.close();
1650
+ }
1697
1651
  } else {
1698
1652
  const file = await fileHandle.getFile();
1699
1653
  buffer = new Uint8Array(await file.arrayBuffer());
@@ -1751,10 +1705,13 @@ var OPFS = class {
1751
1705
  let buffer;
1752
1706
  if (this.useSync) {
1753
1707
  const access = await fileHandle.createSyncAccessHandle();
1754
- const size = access.getSize();
1755
- buffer = new Uint8Array(size);
1756
- access.read(buffer);
1757
- access.close();
1708
+ try {
1709
+ const size = access.getSize();
1710
+ buffer = new Uint8Array(size);
1711
+ access.read(buffer);
1712
+ } finally {
1713
+ access.close();
1714
+ }
1758
1715
  } else {
1759
1716
  const file = await fileHandle.getFile();
1760
1717
  buffer = new Uint8Array(await file.arrayBuffer());
@@ -1787,9 +1744,12 @@ var OPFS = class {
1787
1744
  const buffer = typeof data === "string" ? new TextEncoder().encode(data) : data;
1788
1745
  if (this.useSync) {
1789
1746
  const access = await fileHandle.createSyncAccessHandle();
1790
- access.truncate(buffer.length);
1791
- access.write(buffer, { at: 0 });
1792
- access.close();
1747
+ try {
1748
+ access.truncate(buffer.length);
1749
+ access.write(buffer, { at: 0 });
1750
+ } finally {
1751
+ access.close();
1752
+ }
1793
1753
  } else {
1794
1754
  const writable = await fileHandle.createWritable();
1795
1755
  await writable.write(buffer);
@@ -2403,8 +2363,11 @@ var OPFS = class {
2403
2363
  if (!fileHandle) throw createENOENT(path);
2404
2364
  if (this.useSync) {
2405
2365
  const access = await fileHandle.createSyncAccessHandle();
2406
- access.truncate(len);
2407
- access.close();
2366
+ try {
2367
+ access.truncate(len);
2368
+ } finally {
2369
+ access.close();
2370
+ }
2408
2371
  } else {
2409
2372
  const file = await fileHandle.getFile();
2410
2373
  const data = new Uint8Array(await file.arrayBuffer());