@componentor/fs 1.2.5 → 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.
- package/dist/index.js +86 -182
- package/dist/index.js.map +1 -1
- package/dist/opfs-hybrid.js +86 -182
- package/dist/opfs-hybrid.js.map +1 -1
- package/dist/opfs-worker.js +86 -182
- package/dist/opfs-worker.js.map +1 -1
- package/package.json +1 -1
- package/src/handle-manager.ts +0 -64
- package/src/index.ts +19 -39
- package/src/packed-storage.ts +81 -107
- package/src/symlink-manager.ts +6 -12
package/dist/opfs-worker.js
CHANGED
|
@@ -145,52 +145,6 @@ function segments(path) {
|
|
|
145
145
|
// src/handle-manager.ts
|
|
146
146
|
var FILE_HANDLE_POOL_SIZE = 50;
|
|
147
147
|
var DIR_CACHE_MAX_SIZE = 200;
|
|
148
|
-
var FileLockManager = class {
|
|
149
|
-
locks = /* @__PURE__ */ new Map();
|
|
150
|
-
lockResolvers = /* @__PURE__ */ new Map();
|
|
151
|
-
waitQueues = /* @__PURE__ */ new Map();
|
|
152
|
-
/**
|
|
153
|
-
* Acquire an exclusive lock on a file path.
|
|
154
|
-
* If the file is already locked, waits until it's released.
|
|
155
|
-
* Returns a release function that MUST be called when done.
|
|
156
|
-
*/
|
|
157
|
-
async acquire(path) {
|
|
158
|
-
const normalizedPath = normalize(path);
|
|
159
|
-
while (this.locks.has(normalizedPath)) {
|
|
160
|
-
await this.locks.get(normalizedPath);
|
|
161
|
-
}
|
|
162
|
-
let resolve;
|
|
163
|
-
const lockPromise = new Promise((r) => {
|
|
164
|
-
resolve = r;
|
|
165
|
-
});
|
|
166
|
-
this.locks.set(normalizedPath, lockPromise);
|
|
167
|
-
this.lockResolvers.set(normalizedPath, resolve);
|
|
168
|
-
return () => {
|
|
169
|
-
const resolver = this.lockResolvers.get(normalizedPath);
|
|
170
|
-
this.locks.delete(normalizedPath);
|
|
171
|
-
this.lockResolvers.delete(normalizedPath);
|
|
172
|
-
if (resolver) resolver();
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Check if a file is currently locked
|
|
177
|
-
*/
|
|
178
|
-
isLocked(path) {
|
|
179
|
-
return this.locks.has(normalize(path));
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Clear all locks (use with caution, mainly for cleanup)
|
|
183
|
-
*/
|
|
184
|
-
clearAll() {
|
|
185
|
-
for (const resolver of this.lockResolvers.values()) {
|
|
186
|
-
resolver();
|
|
187
|
-
}
|
|
188
|
-
this.locks.clear();
|
|
189
|
-
this.lockResolvers.clear();
|
|
190
|
-
this.waitQueues.clear();
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
var fileLockManager = new FileLockManager();
|
|
194
148
|
var HandleManager = class {
|
|
195
149
|
rootPromise;
|
|
196
150
|
dirCache = /* @__PURE__ */ new Map();
|
|
@@ -448,20 +402,15 @@ var SymlinkManager = class {
|
|
|
448
402
|
if (!fileHandle) return;
|
|
449
403
|
const buffer = new TextEncoder().encode(data);
|
|
450
404
|
if (this.useSync) {
|
|
451
|
-
const
|
|
405
|
+
const access = await fileHandle.createSyncAccessHandle();
|
|
452
406
|
try {
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
while (written < buffer.length) {
|
|
458
|
-
written += access.write(buffer.subarray(written), { at: written });
|
|
459
|
-
}
|
|
460
|
-
} finally {
|
|
461
|
-
access.close();
|
|
407
|
+
access.truncate(0);
|
|
408
|
+
let written = 0;
|
|
409
|
+
while (written < buffer.length) {
|
|
410
|
+
written += access.write(buffer.subarray(written), { at: written });
|
|
462
411
|
}
|
|
463
412
|
} finally {
|
|
464
|
-
|
|
413
|
+
access.close();
|
|
465
414
|
}
|
|
466
415
|
} else {
|
|
467
416
|
const writable = await fileHandle.createWritable();
|
|
@@ -744,35 +693,30 @@ var PackedStorage = class {
|
|
|
744
693
|
return {};
|
|
745
694
|
}
|
|
746
695
|
if (this.useSync) {
|
|
747
|
-
const
|
|
696
|
+
const access = await fileHandle.createSyncAccessHandle();
|
|
748
697
|
try {
|
|
749
|
-
const
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
if (
|
|
764
|
-
|
|
765
|
-
if (calculatedCrc !== storedCrc) {
|
|
766
|
-
throw createECORRUPTED(PACK_FILE);
|
|
767
|
-
}
|
|
698
|
+
const size = access.getSize();
|
|
699
|
+
if (size < 8) {
|
|
700
|
+
return {};
|
|
701
|
+
}
|
|
702
|
+
const header = new Uint8Array(8);
|
|
703
|
+
access.read(header, { at: 0 });
|
|
704
|
+
const view = new DataView(header.buffer);
|
|
705
|
+
const indexLen = view.getUint32(0, true);
|
|
706
|
+
const storedCrc = view.getUint32(4, true);
|
|
707
|
+
const contentSize = size - 8;
|
|
708
|
+
const content = new Uint8Array(contentSize);
|
|
709
|
+
access.read(content, { at: 8 });
|
|
710
|
+
if (this.useChecksum && storedCrc !== 0) {
|
|
711
|
+
const calculatedCrc = crc32(content);
|
|
712
|
+
if (calculatedCrc !== storedCrc) {
|
|
713
|
+
throw createECORRUPTED(PACK_FILE);
|
|
768
714
|
}
|
|
769
|
-
const indexJson = new TextDecoder().decode(content.subarray(0, indexLen));
|
|
770
|
-
return JSON.parse(indexJson);
|
|
771
|
-
} finally {
|
|
772
|
-
access.close();
|
|
773
715
|
}
|
|
716
|
+
const indexJson = new TextDecoder().decode(content.subarray(0, indexLen));
|
|
717
|
+
return JSON.parse(indexJson);
|
|
774
718
|
} finally {
|
|
775
|
-
|
|
719
|
+
access.close();
|
|
776
720
|
}
|
|
777
721
|
} else {
|
|
778
722
|
const file = await fileHandle.getFile();
|
|
@@ -826,17 +770,12 @@ var PackedStorage = class {
|
|
|
826
770
|
if (!fileHandle) return null;
|
|
827
771
|
let buffer;
|
|
828
772
|
if (this.useSync) {
|
|
829
|
-
const
|
|
773
|
+
const access = await fileHandle.createSyncAccessHandle();
|
|
830
774
|
try {
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
buffer = new Uint8Array(entry.size);
|
|
834
|
-
access.read(buffer, { at: entry.offset });
|
|
835
|
-
} finally {
|
|
836
|
-
access.close();
|
|
837
|
-
}
|
|
775
|
+
buffer = new Uint8Array(entry.size);
|
|
776
|
+
access.read(buffer, { at: entry.offset });
|
|
838
777
|
} finally {
|
|
839
|
-
|
|
778
|
+
access.close();
|
|
840
779
|
}
|
|
841
780
|
} else {
|
|
842
781
|
const file = await fileHandle.getFile();
|
|
@@ -876,24 +815,19 @@ var PackedStorage = class {
|
|
|
876
815
|
}
|
|
877
816
|
const decompressPromises = [];
|
|
878
817
|
if (this.useSync) {
|
|
879
|
-
const
|
|
818
|
+
const access = await fileHandle.createSyncAccessHandle();
|
|
880
819
|
try {
|
|
881
|
-
const
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
} else {
|
|
889
|
-
results.set(path, buffer);
|
|
890
|
-
}
|
|
820
|
+
for (const { path, offset, size, originalSize } of toRead) {
|
|
821
|
+
const buffer = new Uint8Array(size);
|
|
822
|
+
access.read(buffer, { at: offset });
|
|
823
|
+
if (originalSize !== void 0) {
|
|
824
|
+
decompressPromises.push({ path, promise: decompress(buffer) });
|
|
825
|
+
} else {
|
|
826
|
+
results.set(path, buffer);
|
|
891
827
|
}
|
|
892
|
-
} finally {
|
|
893
|
-
access.close();
|
|
894
828
|
}
|
|
895
829
|
} finally {
|
|
896
|
-
|
|
830
|
+
access.close();
|
|
897
831
|
}
|
|
898
832
|
} else {
|
|
899
833
|
const file = await fileHandle.getFile();
|
|
@@ -981,17 +915,12 @@ var PackedStorage = class {
|
|
|
981
915
|
const { fileHandle } = await this.handleManager.getHandle(PACK_FILE, { create: true });
|
|
982
916
|
if (!fileHandle) return;
|
|
983
917
|
if (this.useSync) {
|
|
984
|
-
const
|
|
918
|
+
const access = await fileHandle.createSyncAccessHandle();
|
|
985
919
|
try {
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
access.truncate(data.length);
|
|
989
|
-
access.write(data, { at: 0 });
|
|
990
|
-
} finally {
|
|
991
|
-
access.close();
|
|
992
|
-
}
|
|
920
|
+
access.truncate(data.length);
|
|
921
|
+
access.write(data, { at: 0 });
|
|
993
922
|
} finally {
|
|
994
|
-
|
|
923
|
+
access.close();
|
|
995
924
|
}
|
|
996
925
|
} else {
|
|
997
926
|
const writable = await fileHandle.createWritable();
|
|
@@ -1012,40 +941,35 @@ var PackedStorage = class {
|
|
|
1012
941
|
const encoder = new TextEncoder();
|
|
1013
942
|
const newIndexBuf = encoder.encode(JSON.stringify(index));
|
|
1014
943
|
if (this.useSync) {
|
|
1015
|
-
const
|
|
944
|
+
const access = await fileHandle.createSyncAccessHandle();
|
|
1016
945
|
try {
|
|
1017
|
-
const
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
if (dataSize > 0) {
|
|
1027
|
-
access.read(dataPortion, { at: dataStart });
|
|
1028
|
-
}
|
|
1029
|
-
const newContent = new Uint8Array(newIndexBuf.length + dataSize);
|
|
1030
|
-
newContent.set(newIndexBuf, 0);
|
|
1031
|
-
if (dataSize > 0) {
|
|
1032
|
-
newContent.set(dataPortion, newIndexBuf.length);
|
|
1033
|
-
}
|
|
1034
|
-
const checksum = this.useChecksum ? crc32(newContent) : 0;
|
|
1035
|
-
const newHeader = new Uint8Array(8);
|
|
1036
|
-
const view = new DataView(newHeader.buffer);
|
|
1037
|
-
view.setUint32(0, newIndexBuf.length, true);
|
|
1038
|
-
view.setUint32(4, checksum, true);
|
|
1039
|
-
const newFile = new Uint8Array(8 + newContent.length);
|
|
1040
|
-
newFile.set(newHeader, 0);
|
|
1041
|
-
newFile.set(newContent, 8);
|
|
1042
|
-
access.truncate(newFile.length);
|
|
1043
|
-
access.write(newFile, { at: 0 });
|
|
1044
|
-
} finally {
|
|
1045
|
-
access.close();
|
|
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);
|
|
950
|
+
const dataStart = 8 + oldIndexLen;
|
|
951
|
+
const dataSize = size - dataStart;
|
|
952
|
+
const dataPortion = new Uint8Array(dataSize);
|
|
953
|
+
if (dataSize > 0) {
|
|
954
|
+
access.read(dataPortion, { at: dataStart });
|
|
1046
955
|
}
|
|
956
|
+
const newContent = new Uint8Array(newIndexBuf.length + dataSize);
|
|
957
|
+
newContent.set(newIndexBuf, 0);
|
|
958
|
+
if (dataSize > 0) {
|
|
959
|
+
newContent.set(dataPortion, newIndexBuf.length);
|
|
960
|
+
}
|
|
961
|
+
const checksum = this.useChecksum ? crc32(newContent) : 0;
|
|
962
|
+
const newHeader = new Uint8Array(8);
|
|
963
|
+
const view = new DataView(newHeader.buffer);
|
|
964
|
+
view.setUint32(0, newIndexBuf.length, true);
|
|
965
|
+
view.setUint32(4, checksum, true);
|
|
966
|
+
const newFile = new Uint8Array(8 + newContent.length);
|
|
967
|
+
newFile.set(newHeader, 0);
|
|
968
|
+
newFile.set(newContent, 8);
|
|
969
|
+
access.truncate(newFile.length);
|
|
970
|
+
access.write(newFile, { at: 0 });
|
|
1047
971
|
} finally {
|
|
1048
|
-
|
|
972
|
+
access.close();
|
|
1049
973
|
}
|
|
1050
974
|
} else {
|
|
1051
975
|
const file = await fileHandle.getFile();
|
|
@@ -1716,18 +1640,13 @@ var OPFS = class {
|
|
|
1716
1640
|
if (fileHandle) {
|
|
1717
1641
|
let buffer;
|
|
1718
1642
|
if (this.useSync) {
|
|
1719
|
-
const
|
|
1643
|
+
const access = await fileHandle.createSyncAccessHandle();
|
|
1720
1644
|
try {
|
|
1721
|
-
const
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
buffer = new Uint8Array(size);
|
|
1725
|
-
access.read(buffer);
|
|
1726
|
-
} finally {
|
|
1727
|
-
access.close();
|
|
1728
|
-
}
|
|
1645
|
+
const size = access.getSize();
|
|
1646
|
+
buffer = new Uint8Array(size);
|
|
1647
|
+
access.read(buffer);
|
|
1729
1648
|
} finally {
|
|
1730
|
-
|
|
1649
|
+
access.close();
|
|
1731
1650
|
}
|
|
1732
1651
|
} else {
|
|
1733
1652
|
const file = await fileHandle.getFile();
|
|
@@ -1785,18 +1704,13 @@ var OPFS = class {
|
|
|
1785
1704
|
}
|
|
1786
1705
|
let buffer;
|
|
1787
1706
|
if (this.useSync) {
|
|
1788
|
-
const
|
|
1707
|
+
const access = await fileHandle.createSyncAccessHandle();
|
|
1789
1708
|
try {
|
|
1790
|
-
const
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
buffer = new Uint8Array(size);
|
|
1794
|
-
access.read(buffer);
|
|
1795
|
-
} finally {
|
|
1796
|
-
access.close();
|
|
1797
|
-
}
|
|
1709
|
+
const size = access.getSize();
|
|
1710
|
+
buffer = new Uint8Array(size);
|
|
1711
|
+
access.read(buffer);
|
|
1798
1712
|
} finally {
|
|
1799
|
-
|
|
1713
|
+
access.close();
|
|
1800
1714
|
}
|
|
1801
1715
|
} else {
|
|
1802
1716
|
const file = await fileHandle.getFile();
|
|
@@ -1829,17 +1743,12 @@ var OPFS = class {
|
|
|
1829
1743
|
const { fileHandle } = await this.handleManager.getHandle(resolvedPath, { create: true });
|
|
1830
1744
|
const buffer = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
1831
1745
|
if (this.useSync) {
|
|
1832
|
-
const
|
|
1746
|
+
const access = await fileHandle.createSyncAccessHandle();
|
|
1833
1747
|
try {
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
access.truncate(buffer.length);
|
|
1837
|
-
access.write(buffer, { at: 0 });
|
|
1838
|
-
} finally {
|
|
1839
|
-
access.close();
|
|
1840
|
-
}
|
|
1748
|
+
access.truncate(buffer.length);
|
|
1749
|
+
access.write(buffer, { at: 0 });
|
|
1841
1750
|
} finally {
|
|
1842
|
-
|
|
1751
|
+
access.close();
|
|
1843
1752
|
}
|
|
1844
1753
|
} else {
|
|
1845
1754
|
const writable = await fileHandle.createWritable();
|
|
@@ -2453,16 +2362,11 @@ var OPFS = class {
|
|
|
2453
2362
|
const { fileHandle } = await this.handleManager.getHandle(resolvedPath);
|
|
2454
2363
|
if (!fileHandle) throw createENOENT(path);
|
|
2455
2364
|
if (this.useSync) {
|
|
2456
|
-
const
|
|
2365
|
+
const access = await fileHandle.createSyncAccessHandle();
|
|
2457
2366
|
try {
|
|
2458
|
-
|
|
2459
|
-
try {
|
|
2460
|
-
access.truncate(len);
|
|
2461
|
-
} finally {
|
|
2462
|
-
access.close();
|
|
2463
|
-
}
|
|
2367
|
+
access.truncate(len);
|
|
2464
2368
|
} finally {
|
|
2465
|
-
|
|
2369
|
+
access.close();
|
|
2466
2370
|
}
|
|
2467
2371
|
} else {
|
|
2468
2372
|
const file = await fileHandle.getFile();
|