@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-hybrid.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();
|
|
@@ -1310,18 +1234,13 @@ var OPFS = class {
|
|
|
1310
1234
|
if (fileHandle) {
|
|
1311
1235
|
let buffer;
|
|
1312
1236
|
if (this.useSync) {
|
|
1313
|
-
const
|
|
1237
|
+
const access = await fileHandle.createSyncAccessHandle();
|
|
1314
1238
|
try {
|
|
1315
|
-
const
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
buffer = new Uint8Array(size);
|
|
1319
|
-
access.read(buffer);
|
|
1320
|
-
} finally {
|
|
1321
|
-
access.close();
|
|
1322
|
-
}
|
|
1239
|
+
const size = access.getSize();
|
|
1240
|
+
buffer = new Uint8Array(size);
|
|
1241
|
+
access.read(buffer);
|
|
1323
1242
|
} finally {
|
|
1324
|
-
|
|
1243
|
+
access.close();
|
|
1325
1244
|
}
|
|
1326
1245
|
} else {
|
|
1327
1246
|
const file = await fileHandle.getFile();
|
|
@@ -1379,18 +1298,13 @@ var OPFS = class {
|
|
|
1379
1298
|
}
|
|
1380
1299
|
let buffer;
|
|
1381
1300
|
if (this.useSync) {
|
|
1382
|
-
const
|
|
1301
|
+
const access = await fileHandle.createSyncAccessHandle();
|
|
1383
1302
|
try {
|
|
1384
|
-
const
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
buffer = new Uint8Array(size);
|
|
1388
|
-
access.read(buffer);
|
|
1389
|
-
} finally {
|
|
1390
|
-
access.close();
|
|
1391
|
-
}
|
|
1303
|
+
const size = access.getSize();
|
|
1304
|
+
buffer = new Uint8Array(size);
|
|
1305
|
+
access.read(buffer);
|
|
1392
1306
|
} finally {
|
|
1393
|
-
|
|
1307
|
+
access.close();
|
|
1394
1308
|
}
|
|
1395
1309
|
} else {
|
|
1396
1310
|
const file = await fileHandle.getFile();
|
|
@@ -1423,17 +1337,12 @@ var OPFS = class {
|
|
|
1423
1337
|
const { fileHandle } = await this.handleManager.getHandle(resolvedPath, { create: true });
|
|
1424
1338
|
const buffer = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
1425
1339
|
if (this.useSync) {
|
|
1426
|
-
const
|
|
1340
|
+
const access = await fileHandle.createSyncAccessHandle();
|
|
1427
1341
|
try {
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
access.truncate(buffer.length);
|
|
1431
|
-
access.write(buffer, { at: 0 });
|
|
1432
|
-
} finally {
|
|
1433
|
-
access.close();
|
|
1434
|
-
}
|
|
1342
|
+
access.truncate(buffer.length);
|
|
1343
|
+
access.write(buffer, { at: 0 });
|
|
1435
1344
|
} finally {
|
|
1436
|
-
|
|
1345
|
+
access.close();
|
|
1437
1346
|
}
|
|
1438
1347
|
} else {
|
|
1439
1348
|
const writable = await fileHandle.createWritable();
|
|
@@ -2047,16 +1956,11 @@ var OPFS = class {
|
|
|
2047
1956
|
const { fileHandle } = await this.handleManager.getHandle(resolvedPath);
|
|
2048
1957
|
if (!fileHandle) throw createENOENT(path);
|
|
2049
1958
|
if (this.useSync) {
|
|
2050
|
-
const
|
|
1959
|
+
const access = await fileHandle.createSyncAccessHandle();
|
|
2051
1960
|
try {
|
|
2052
|
-
|
|
2053
|
-
try {
|
|
2054
|
-
access.truncate(len);
|
|
2055
|
-
} finally {
|
|
2056
|
-
access.close();
|
|
2057
|
-
}
|
|
1961
|
+
access.truncate(len);
|
|
2058
1962
|
} finally {
|
|
2059
|
-
|
|
1963
|
+
access.close();
|
|
2060
1964
|
}
|
|
2061
1965
|
} else {
|
|
2062
1966
|
const file = await fileHandle.getFile();
|