@waffle-os/runtime 0.0.147 → 0.0.148
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/browser/filesystem-backend.ts +26 -5
- package/browser/fs-worker.ts +5 -2
- package/browser/shared-fs.ts +93 -10
- package/core/filesystem.ts +2 -0
- package/dist/browser/filesystem-backend.js +19 -2
- package/dist/browser/fs-worker.js +7 -2
- package/dist/browser/shared-fs.js +77 -8
- package/dist/fs/client.js +82 -21
- package/dist/fs/directory-pages.js +22 -7
- package/dist/fs/guest-watch.js +9 -3
- package/dist/fs/idle-database.js +1 -1
- package/dist/fs/idle-owner.js +35 -4
- package/dist/fs/indexeddb.js +322 -63
- package/dist/fs/memory.js +18 -9
- package/dist/fs/multitab-backend.js +5065 -0
- package/dist/fs/multitab-protocol.js +301 -0
- package/dist/fs/opfs.js +21 -0
- package/dist/fs/persistent.js +70 -1
- package/dist/fs/protocol.js +1 -1
- package/dist/fs/service.js +266 -86
- package/dist/fs/shared.js +45 -0
- package/dist/fs/vfs.js +26 -8
- package/dist/fs/watch-ring.js +7 -3
- package/dist/proc/command-stdio.js +56 -1
- package/dist/runtime.js +4 -2
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/backend.ts +36 -0
- package/fs/client.ts +81 -19
- package/fs/directory-pages.ts +40 -6
- package/fs/guest-watch.ts +8 -3
- package/fs/idle-database.ts +1 -0
- package/fs/idle-owner.ts +48 -12
- package/fs/indexeddb.ts +396 -78
- package/fs/memory.ts +33 -9
- package/fs/multitab-backend.ts +6870 -0
- package/fs/multitab-protocol.ts +655 -0
- package/fs/opfs.ts +21 -0
- package/fs/persistent.ts +70 -1
- package/fs/protocol.ts +1 -1
- package/fs/service.ts +314 -109
- package/fs/shared.ts +45 -0
- package/fs/storage.ts +3 -0
- package/fs/vfs.ts +44 -15
- package/fs/watch-ring.ts +5 -2
- package/package.json +1 -1
- package/proc/command-stdio.ts +72 -2
- package/runtime.ts +12 -3
- package/tooling/program/program-transformer.json +1 -1
package/dist/fs/service.js
CHANGED
|
@@ -130,6 +130,8 @@ export class FilesystemService {
|
|
|
130
130
|
if (!source.handles.has(directory))
|
|
131
131
|
throw new FilesystemError(76);
|
|
132
132
|
this.check(source, directory);
|
|
133
|
+
if (this.backend.supportsSockets === false)
|
|
134
|
+
throw new FilesystemError(58);
|
|
133
135
|
if (!(this.backend instanceof MemoryBackend) || this.backend.fs.stat(directory).kind !== "directory")
|
|
134
136
|
throw new FilesystemError(76);
|
|
135
137
|
if (isRevoked(connection))
|
|
@@ -166,6 +168,8 @@ export class FilesystemService {
|
|
|
166
168
|
if (this.backend instanceof MemoryBackend) {
|
|
167
169
|
this.backend.attachShared(connection.shared, connection.maxBytes, connection.segments, connection.publication?.buffers);
|
|
168
170
|
this.backend.shared.onContention = () => this.sweep();
|
|
171
|
+
} else {
|
|
172
|
+
this.backend.attachShared?.(connection.shared);
|
|
169
173
|
}
|
|
170
174
|
if (this.requestBudget)
|
|
171
175
|
assertRequestBudget(this.requestBudget, connection.requestBudget);
|
|
@@ -401,6 +405,9 @@ export class FilesystemService {
|
|
|
401
405
|
done.then(() => this.work.delete(done), () => {});
|
|
402
406
|
return done;
|
|
403
407
|
}
|
|
408
|
+
trackRequestWork(session, done) {
|
|
409
|
+
return this.backend.asyncErrorsAreClientResults ? done : this.trackWork(session, done);
|
|
410
|
+
}
|
|
404
411
|
enqueueMaintenance(session, run) {
|
|
405
412
|
const done = this.trackWork(session, this.queue.then(run));
|
|
406
413
|
this.queue = done.then(() => {}, () => {});
|
|
@@ -507,8 +514,12 @@ export class FilesystemService {
|
|
|
507
514
|
const watch = session.watchResponses.get(id);
|
|
508
515
|
session.watchResponses.delete(id);
|
|
509
516
|
const request = session.retainedRequests.get(id);
|
|
510
|
-
if (
|
|
511
|
-
|
|
517
|
+
if (watch) {
|
|
518
|
+
if (sessionRevoked(session) || !this.sessions.has(session) || request && requestCancelled(request))
|
|
519
|
+
watch.cancel();
|
|
520
|
+
else
|
|
521
|
+
watch.release?.();
|
|
522
|
+
}
|
|
512
523
|
this.releaseResponseMemory(session, id);
|
|
513
524
|
const retained = session.retainedRequests.get(id);
|
|
514
525
|
const cancelledWalk = retained && requestCancelled(retained);
|
|
@@ -579,13 +590,14 @@ export class FilesystemService {
|
|
|
579
590
|
trim(session) {
|
|
580
591
|
let idle = 0, detached = false;
|
|
581
592
|
const shared = this.backend instanceof MemoryBackend ? this.backend.shared : undefined;
|
|
593
|
+
const isUnlinked = (handle) => shared ? shared.isUnlinked(handle) : this.backend.isUnlinked?.(handle) ?? false;
|
|
582
594
|
const responseHeld = this.responseReferenceCounts(session);
|
|
583
595
|
for (const handle of session.transient.keys()) {
|
|
584
596
|
const references = Atomics.load(session.capabilities, session.handles.get(handle) + 1);
|
|
585
597
|
if (session.preparingHandles.has(handle) || references !== (responseHeld.get(handle) ?? 0))
|
|
586
598
|
continue;
|
|
587
599
|
idle++;
|
|
588
|
-
if (references === 0 &&
|
|
600
|
+
if (references === 0 && isUnlinked(handle))
|
|
589
601
|
detached = true;
|
|
590
602
|
}
|
|
591
603
|
if (idle <= TRANSIENT_HANDLES && !detached)
|
|
@@ -604,7 +616,7 @@ export class FilesystemService {
|
|
|
604
616
|
for (const handle of session.transient.keys()) {
|
|
605
617
|
if (session.preparingHandles.has(handle))
|
|
606
618
|
continue;
|
|
607
|
-
if (idle <= keep && !
|
|
619
|
+
if (idle <= keep && !isUnlinked(handle))
|
|
608
620
|
continue;
|
|
609
621
|
const key = session.handles.get(handle);
|
|
610
622
|
if (Atomics.compareExchange(session.capabilities, key + 1, 0, -1) !== 0)
|
|
@@ -676,8 +688,12 @@ export class FilesystemService {
|
|
|
676
688
|
"take"
|
|
677
689
|
].includes(operation))
|
|
678
690
|
throw new FilesystemError(76);
|
|
679
|
-
if (["bindSocket", "bindSocketPath", "unbindSocket", "connectSocket"].includes(operation)
|
|
680
|
-
|
|
691
|
+
if (["bindSocket", "bindSocketPath", "unbindSocket", "connectSocket"].includes(operation)) {
|
|
692
|
+
if (this.backend.supportsSockets === false)
|
|
693
|
+
throw new FilesystemError(58);
|
|
694
|
+
if (!scope)
|
|
695
|
+
throw new FilesystemError(76);
|
|
696
|
+
}
|
|
681
697
|
if (scope && (operation === "resolve" || operation === "connectSocket" || operation === "bindSocketPath") && a !== scope.root)
|
|
682
698
|
throw new FilesystemError(76);
|
|
683
699
|
if (operation === "bindSocket" || operation === "bindSocketPath") {
|
|
@@ -762,12 +778,28 @@ export class FilesystemService {
|
|
|
762
778
|
}
|
|
763
779
|
watchRequest(session, requestId, operation, args) {
|
|
764
780
|
const [a, b] = args;
|
|
781
|
+
const request = session.pending.get(requestId);
|
|
782
|
+
const cancelled = () => sessionRevoked(session) || !this.sessions.has(session) || !request || requestCancelled(request);
|
|
765
783
|
if (operation === "watchOpen") {
|
|
784
|
+
if (this.backend.watchOpen) {
|
|
785
|
+
const register = (id) => {
|
|
786
|
+
if (cancelled()) {
|
|
787
|
+
this.backend.watchClose?.(session.connection.owner, id);
|
|
788
|
+
throw new FilesystemError(11);
|
|
789
|
+
}
|
|
790
|
+
session.watchTransactions.set(requestId, {
|
|
791
|
+
cancel: () => this.backend.watchClose?.(session.connection.owner, id),
|
|
792
|
+
release: undefined
|
|
793
|
+
});
|
|
794
|
+
return id;
|
|
795
|
+
};
|
|
796
|
+
const opening = this.backend.watchOpen(session.connection.owner, a, b, cancelled);
|
|
797
|
+
return opening instanceof Promise ? this.trackRequestWork(session, opening).then(register) : register(opening);
|
|
798
|
+
}
|
|
766
799
|
if (!(this.backend instanceof MemoryBackend))
|
|
767
800
|
throw new FilesystemError(58);
|
|
768
801
|
this.watchService ??= new WatchService(this.backend);
|
|
769
|
-
const
|
|
770
|
-
const id = this.watchService.open(session.connection.owner, a, b, () => sessionRevoked(session) || !this.sessions.has(session) || !request || requestCancelled(request));
|
|
802
|
+
const id = this.watchService.open(session.connection.owner, a, b, cancelled);
|
|
771
803
|
session.watchTransactions.set(requestId, {
|
|
772
804
|
cancel: () => this.watchService?.close(session.connection.owner, id),
|
|
773
805
|
release: undefined
|
|
@@ -775,6 +807,24 @@ export class FilesystemService {
|
|
|
775
807
|
return id;
|
|
776
808
|
}
|
|
777
809
|
if (operation === "watchRead") {
|
|
810
|
+
if (this.backend.watchRead) {
|
|
811
|
+
const register = (held) => {
|
|
812
|
+
if (cancelled()) {
|
|
813
|
+
held.rollback();
|
|
814
|
+
throw new FilesystemError(11);
|
|
815
|
+
}
|
|
816
|
+
if (held.batch.events.length)
|
|
817
|
+
session.watchTransactions.set(requestId, {
|
|
818
|
+
cancel: held.rollback,
|
|
819
|
+
release: held.release
|
|
820
|
+
});
|
|
821
|
+
else
|
|
822
|
+
held.release();
|
|
823
|
+
return held.batch;
|
|
824
|
+
};
|
|
825
|
+
const reading = this.backend.watchRead(session.connection.owner, a, cancelled);
|
|
826
|
+
return reading instanceof Promise ? this.trackRequestWork(session, reading).then(register) : register(reading);
|
|
827
|
+
}
|
|
778
828
|
if (!this.watchService)
|
|
779
829
|
throw new FilesystemError(8);
|
|
780
830
|
const held = this.watchService.readHeld(session.connection.owner, a);
|
|
@@ -785,6 +835,10 @@ export class FilesystemService {
|
|
|
785
835
|
return held.batch;
|
|
786
836
|
}
|
|
787
837
|
if (operation === "watchClose") {
|
|
838
|
+
if (this.backend.watchClose) {
|
|
839
|
+
this.backend.watchClose(session.connection.owner, a);
|
|
840
|
+
return;
|
|
841
|
+
}
|
|
788
842
|
this.watchService?.close(session.connection.owner, a);
|
|
789
843
|
return;
|
|
790
844
|
}
|
|
@@ -847,6 +901,23 @@ export class FilesystemService {
|
|
|
847
901
|
} else
|
|
848
902
|
transaction.rollback();
|
|
849
903
|
}
|
|
904
|
+
takeResponseChunk(session, a, b, c) {
|
|
905
|
+
const response = session.responses.get(a);
|
|
906
|
+
const bytes = response?.kind === "bytes" ? response.bytes : response?.payload.kind === "overflow" ? response.payload.bytes : undefined;
|
|
907
|
+
if (!bytes)
|
|
908
|
+
throw new FilesystemError(11);
|
|
909
|
+
const part = bytes.slice(b, b + c);
|
|
910
|
+
if (b + c < bytes.length)
|
|
911
|
+
return part;
|
|
912
|
+
this.releaseResponseMemory(session, a);
|
|
913
|
+
if (response?.kind === "resolve")
|
|
914
|
+
response.payload = { kind: "inline" };
|
|
915
|
+
else if (session.watchResponses.has(a))
|
|
916
|
+
session.responses.delete(a);
|
|
917
|
+
else
|
|
918
|
+
this.discardResponse(session, a);
|
|
919
|
+
return part;
|
|
920
|
+
}
|
|
850
921
|
execute(session, id, operation, args, data) {
|
|
851
922
|
const [a, b, c] = args;
|
|
852
923
|
this.checkSocketTree(session, operation, args);
|
|
@@ -856,21 +927,8 @@ export class FilesystemService {
|
|
|
856
927
|
return this.watchRequest(session, id, operation, args);
|
|
857
928
|
if (operation === "stageWrite")
|
|
858
929
|
return this.stageWrite(session, id, a, b, c, data);
|
|
859
|
-
if (operation === "take")
|
|
860
|
-
|
|
861
|
-
const bytes = response?.kind === "bytes" ? response.bytes : response?.payload.kind === "overflow" ? response.payload.bytes : undefined;
|
|
862
|
-
if (!bytes)
|
|
863
|
-
throw new FilesystemError(11);
|
|
864
|
-
const part = bytes.slice(b, b + c);
|
|
865
|
-
if (b + c < bytes.length)
|
|
866
|
-
return part;
|
|
867
|
-
this.releaseResponseMemory(session, a);
|
|
868
|
-
if (response?.kind === "resolve")
|
|
869
|
-
response.payload = { kind: "inline" };
|
|
870
|
-
else
|
|
871
|
-
this.discardResponse(session, a);
|
|
872
|
-
return part;
|
|
873
|
-
}
|
|
930
|
+
if (operation === "take")
|
|
931
|
+
return this.takeResponseChunk(session, a, b, c);
|
|
874
932
|
if (operation === "retain") {
|
|
875
933
|
const key = session.handles.get(a);
|
|
876
934
|
for (;; ) {
|
|
@@ -911,7 +969,7 @@ export class FilesystemService {
|
|
|
911
969
|
}
|
|
912
970
|
if (operation === "flush" || operation === "sync") {
|
|
913
971
|
const flushing = this.backend.flush?.(operation === "sync" ? a : typeof a === "string" ? a : undefined);
|
|
914
|
-
return this.
|
|
972
|
+
return this.trackRequestWork(session, Promise.resolve(flushing)).then(() => this.finishResult(session, id, operation, operation === "sync" ? 0 : undefined));
|
|
915
973
|
}
|
|
916
974
|
if (operation === "resolve" && session.socketScope && this.backend instanceof MemoryBackend) {
|
|
917
975
|
const result = this.backend.fs.resolveSocket(a, b, c === true);
|
|
@@ -919,25 +977,29 @@ export class FilesystemService {
|
|
|
919
977
|
}
|
|
920
978
|
const result = this.backend.execute(operation, args, data);
|
|
921
979
|
if (result instanceof Promise)
|
|
922
|
-
return this.
|
|
980
|
+
return this.trackRequestWork(session, result).then((value) => this.finishResult(session, id, operation, value, args));
|
|
923
981
|
return this.finishResult(session, id, operation, result, args);
|
|
924
982
|
}
|
|
925
983
|
finishResult(session, id, operation, result, args = []) {
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
984
|
+
try {
|
|
985
|
+
this.finishSocketOperation(session, operation, result, args);
|
|
986
|
+
const pending = session.pending.get(id);
|
|
987
|
+
if (sessionRevoked(session) || !this.sessions.has(session) || !pending || requestCancelled(pending))
|
|
988
|
+
throw new FilesystemError(11);
|
|
989
|
+
if (operation === "preopen" || operation === "create" && typeof result === "number")
|
|
990
|
+
this.pin(session, result, operation === "preopen");
|
|
991
|
+
if (operation === "resolve" && typeof result !== "number")
|
|
992
|
+
this.holdResolvedResponse(session, id, result);
|
|
993
|
+
if (mutations.has(operation)) {
|
|
994
|
+
for (const peer of this.sessions)
|
|
995
|
+
if (!isRevoked(peer.connection))
|
|
996
|
+
this.trim(peer);
|
|
997
|
+
} else
|
|
998
|
+
this.trim(session);
|
|
999
|
+
return result;
|
|
1000
|
+
} finally {
|
|
1001
|
+
this.backend.settleResult?.(operation, result);
|
|
1002
|
+
}
|
|
941
1003
|
}
|
|
942
1004
|
checkSocketTree(session, operation, args) {
|
|
943
1005
|
if (operation !== "bindSocket" || !session.socketScope || !(this.backend instanceof MemoryBackend))
|
|
@@ -968,6 +1030,25 @@ export class FilesystemService {
|
|
|
968
1030
|
this.backend.release(owned.binding.node);
|
|
969
1031
|
this.backend.release(owned.binding.parent);
|
|
970
1032
|
}
|
|
1033
|
+
preparedReadInvalidated(prepared, epoch) {
|
|
1034
|
+
const read = prepared?.read;
|
|
1035
|
+
if (!read)
|
|
1036
|
+
return false;
|
|
1037
|
+
if (read.startEpoch !== undefined && Atomics.load(epoch, 0) !== read.startEpoch)
|
|
1038
|
+
return true;
|
|
1039
|
+
return read.startVersion !== undefined && this.backend.readVersion?.() !== read.startVersion;
|
|
1040
|
+
}
|
|
1041
|
+
dirtyRetryChange(error) {
|
|
1042
|
+
if (!(error instanceof DirtyCapacityError) || error.permanent || !this.backend.waitForDirtyCapacity)
|
|
1043
|
+
return;
|
|
1044
|
+
return this.backend.dirtyChange?.() ?? (this.backend instanceof MemoryBackend ? this.backend.shared.dirtyAdmission.change : undefined);
|
|
1045
|
+
}
|
|
1046
|
+
cancelSessionWatchResponses(session) {
|
|
1047
|
+
for (const [id, watch] of session.watchResponses) {
|
|
1048
|
+
session.watchResponses.delete(id);
|
|
1049
|
+
watch.cancel();
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
971
1052
|
assertBackendAccess() {
|
|
972
1053
|
if (this.backend instanceof MemoryBackend)
|
|
973
1054
|
this.backend.shared.publication?.assertAccess();
|
|
@@ -995,6 +1076,10 @@ export class FilesystemService {
|
|
|
995
1076
|
this.checkRequest(session, operation, args);
|
|
996
1077
|
if (prepared?.failure)
|
|
997
1078
|
throw prepared.failure.error;
|
|
1079
|
+
if (this.preparedReadInvalidated(prepared, epoch)) {
|
|
1080
|
+
this.releasePrepared(session, prepared);
|
|
1081
|
+
prepared = undefined;
|
|
1082
|
+
}
|
|
998
1083
|
this.assertBackendAccess();
|
|
999
1084
|
const continuation = this.prepareRequest(session, id, buffer, operation, args, bytes.subarray(inputLength, inputLength + dataLength), prepared);
|
|
1000
1085
|
if (continuation) {
|
|
@@ -1012,14 +1097,16 @@ export class FilesystemService {
|
|
|
1012
1097
|
opened = this.beginEpochMutation(epoch);
|
|
1013
1098
|
}
|
|
1014
1099
|
const payload = bytes.subarray(inputLength, inputLength + dataLength);
|
|
1015
|
-
const
|
|
1100
|
+
const preparedRead = prepared?.read && (!this.backend.isPreparedReadValid || this.backend.isPreparedReadValid()) ? prepared.read : undefined;
|
|
1101
|
+
const value = preparedRead ? this.finishResult(session, id, operation, preparedRead.value) : local && operation !== "flush" && operation !== "sync" ? this.executePublication(session, id, operation, args, payload, admission) : await this.execute(session, id, operation, args, payload);
|
|
1016
1102
|
this.announceSegments(this.backend.takeSharedSegments?.() ?? [], this.sessions);
|
|
1017
1103
|
state = this.writeResponse(session, id, cells, response, value, bytes);
|
|
1018
1104
|
staging = operation === "stageWrite";
|
|
1019
1105
|
} catch (error) {
|
|
1020
1106
|
this.discardResponse(session, id);
|
|
1021
|
-
|
|
1022
|
-
|
|
1107
|
+
const dirtyChange = this.dirtyRetryChange(error);
|
|
1108
|
+
if (dirtyChange !== undefined) {
|
|
1109
|
+
dirtyRetry = dirtyChange;
|
|
1023
1110
|
prepared ??= { handles: [] };
|
|
1024
1111
|
const input = unpack(bytes.subarray(0, cells[1]));
|
|
1025
1112
|
this.checkRequest(session, input.operation, input.args, prepared.handles);
|
|
@@ -1081,9 +1168,10 @@ export class FilesystemService {
|
|
|
1081
1168
|
transaction.cancel();
|
|
1082
1169
|
return;
|
|
1083
1170
|
}
|
|
1084
|
-
session.watchResponses.set(id,
|
|
1085
|
-
|
|
1086
|
-
|
|
1171
|
+
session.watchResponses.set(id, {
|
|
1172
|
+
cancel: transaction.cancel,
|
|
1173
|
+
release: transaction.release
|
|
1174
|
+
});
|
|
1087
1175
|
}
|
|
1088
1176
|
retainRequestReply(session, id, buffer) {
|
|
1089
1177
|
if (session.responses.has(id) || session.walkResponses.has(id) || session.watchResponses.has(id) || session.stages.has(id)) {
|
|
@@ -1152,10 +1240,12 @@ export class FilesystemService {
|
|
|
1152
1240
|
return this.backend.prepare?.(operation, args, payload);
|
|
1153
1241
|
}
|
|
1154
1242
|
prepareDescriptorRead(session, id, buffer, operation, args, read) {
|
|
1243
|
+
const startEpoch = Atomics.load(session.epoch, 0);
|
|
1244
|
+
const startVersion = this.backend.readVersion?.();
|
|
1155
1245
|
const continuation = { handles: [] };
|
|
1156
1246
|
this.checkRequest(session, operation, args, continuation.handles);
|
|
1157
1247
|
const capture = read.then((value) => {
|
|
1158
|
-
continuation.read = { value };
|
|
1248
|
+
continuation.read = { value, startEpoch, startVersion };
|
|
1159
1249
|
});
|
|
1160
1250
|
this.joinPreparation(session, id, buffer, continuation, capture);
|
|
1161
1251
|
return continuation;
|
|
@@ -1403,8 +1493,41 @@ export class FilesystemService {
|
|
|
1403
1493
|
session.readerClosing.then(() => this.sweep(), () => {});
|
|
1404
1494
|
}
|
|
1405
1495
|
}
|
|
1496
|
+
clearSessionHandlesAndBuffers(session) {
|
|
1497
|
+
for (const key of session.handles.values())
|
|
1498
|
+
Atomics.store(session.capabilities, key + 1, -1);
|
|
1499
|
+
for (const handle of Array.from(session.handles.keys())) {
|
|
1500
|
+
if (Atomics.load(session.capabilities, session.handles.get(handle) + 2) > 0)
|
|
1501
|
+
continue;
|
|
1502
|
+
this.backend.release(handle);
|
|
1503
|
+
session.handles.delete(handle);
|
|
1504
|
+
}
|
|
1505
|
+
Atomics.store(session.capabilities, 0, 0);
|
|
1506
|
+
for (const lease of session.socketScope?.leases.values() ?? []) {
|
|
1507
|
+
this.backend.release(lease.binding.node);
|
|
1508
|
+
this.backend.release(lease.binding.parent);
|
|
1509
|
+
}
|
|
1510
|
+
session.socketScope?.leases.clear();
|
|
1511
|
+
session.preopens.clear();
|
|
1512
|
+
session.transient.clear();
|
|
1513
|
+
session.references.clear();
|
|
1514
|
+
for (const retained of session.retainedRequests.values())
|
|
1515
|
+
releaseRequestBuffer(retained, REQUEST_SERVICE);
|
|
1516
|
+
session.retainedRequests.clear();
|
|
1517
|
+
for (const release of session.responseMemory.values())
|
|
1518
|
+
release();
|
|
1519
|
+
session.responseMemory.clear();
|
|
1520
|
+
session.responses.clear();
|
|
1521
|
+
session.preparingHandles.clear();
|
|
1522
|
+
if (this.backend instanceof MemoryBackend)
|
|
1523
|
+
for (const id of session.publicationPins ?? [])
|
|
1524
|
+
this.backend.shared.publication?.releasePin(id);
|
|
1525
|
+
session.publicationPins?.clear();
|
|
1526
|
+
}
|
|
1406
1527
|
releaseSessionPins(session) {
|
|
1528
|
+
this.cancelSessionWatchResponses(session);
|
|
1407
1529
|
this.watchService?.closeOwner(session.connection.owner);
|
|
1530
|
+
this.backend.closeWatchOwner?.(session.connection.owner);
|
|
1408
1531
|
session.namespace?.close();
|
|
1409
1532
|
this.stopSessionReader(session);
|
|
1410
1533
|
if (!session.transportFailed)
|
|
@@ -1445,36 +1568,9 @@ export class FilesystemService {
|
|
|
1445
1568
|
session.walkResponses.clear();
|
|
1446
1569
|
for (const id of session.watchTransactions.keys())
|
|
1447
1570
|
this.settleWatch(session, id, false);
|
|
1571
|
+
this.cancelSessionWatchResponses(session);
|
|
1448
1572
|
session.watchResponses.clear();
|
|
1449
|
-
|
|
1450
|
-
Atomics.store(session.capabilities, key + 1, -1);
|
|
1451
|
-
for (const handle of Array.from(session.handles.keys())) {
|
|
1452
|
-
if (Atomics.load(session.capabilities, session.handles.get(handle) + 2) > 0)
|
|
1453
|
-
continue;
|
|
1454
|
-
this.backend.release(handle);
|
|
1455
|
-
session.handles.delete(handle);
|
|
1456
|
-
}
|
|
1457
|
-
Atomics.store(session.capabilities, 0, 0);
|
|
1458
|
-
for (const lease of session.socketScope?.leases.values() ?? []) {
|
|
1459
|
-
this.backend.release(lease.binding.node);
|
|
1460
|
-
this.backend.release(lease.binding.parent);
|
|
1461
|
-
}
|
|
1462
|
-
session.socketScope?.leases.clear();
|
|
1463
|
-
session.preopens.clear();
|
|
1464
|
-
session.transient.clear();
|
|
1465
|
-
session.references.clear();
|
|
1466
|
-
for (const retained of session.retainedRequests.values())
|
|
1467
|
-
releaseRequestBuffer(retained, REQUEST_SERVICE);
|
|
1468
|
-
session.retainedRequests.clear();
|
|
1469
|
-
for (const release of session.responseMemory.values())
|
|
1470
|
-
release();
|
|
1471
|
-
session.responseMemory.clear();
|
|
1472
|
-
session.responses.clear();
|
|
1473
|
-
session.preparingHandles.clear();
|
|
1474
|
-
if (this.backend instanceof MemoryBackend)
|
|
1475
|
-
for (const id of session.publicationPins ?? [])
|
|
1476
|
-
this.backend.shared.publication?.releasePin(id);
|
|
1477
|
-
session.publicationPins?.clear();
|
|
1573
|
+
this.clearSessionHandlesAndBuffers(session);
|
|
1478
1574
|
session.connection.port.close();
|
|
1479
1575
|
session.closed = true;
|
|
1480
1576
|
this.retireTransport(session);
|
|
@@ -1576,6 +1672,9 @@ export class FilesystemService {
|
|
|
1576
1672
|
if (this.disposingWatches && !this.sessions.size && this.backend instanceof MemoryBackend)
|
|
1577
1673
|
this.backend.watchAccount.close();
|
|
1578
1674
|
}
|
|
1675
|
+
beginDispose() {
|
|
1676
|
+
this.backend.beginDispose?.();
|
|
1677
|
+
}
|
|
1579
1678
|
dispose() {
|
|
1580
1679
|
if (this.shutdownOwner)
|
|
1581
1680
|
return;
|
|
@@ -1598,6 +1697,7 @@ export class FilesystemService {
|
|
|
1598
1697
|
const owner = { done, sessions };
|
|
1599
1698
|
this.shutdownOwner = owner;
|
|
1600
1699
|
this.ownerStopping = true;
|
|
1700
|
+
this.backend.beginShutdown?.();
|
|
1601
1701
|
for (const session of sessions)
|
|
1602
1702
|
this.stopSessionReader(session);
|
|
1603
1703
|
const fences = sessions.map((session) => this.shutdownFence(session));
|
|
@@ -1710,7 +1810,14 @@ function failureDiagnostic(error) {
|
|
|
1710
1810
|
};
|
|
1711
1811
|
if (detail.name === "VfsError")
|
|
1712
1812
|
message = diagnosticField(error, "message") ?? message;
|
|
1713
|
-
|
|
1813
|
+
const released = diagnosticReleased(error);
|
|
1814
|
+
return {
|
|
1815
|
+
type: "filesystem-error",
|
|
1816
|
+
message,
|
|
1817
|
+
stack,
|
|
1818
|
+
...detail,
|
|
1819
|
+
...released !== undefined ? { released } : {}
|
|
1820
|
+
};
|
|
1714
1821
|
}
|
|
1715
1822
|
function diagnosticField(error, key) {
|
|
1716
1823
|
try {
|
|
@@ -1722,6 +1829,22 @@ function diagnosticField(error, key) {
|
|
|
1722
1829
|
return;
|
|
1723
1830
|
}
|
|
1724
1831
|
}
|
|
1832
|
+
function diagnosticReleased(error) {
|
|
1833
|
+
try {
|
|
1834
|
+
if (!error || typeof error !== "object")
|
|
1835
|
+
return;
|
|
1836
|
+
const value = Reflect.get(error, "released");
|
|
1837
|
+
return typeof value === "boolean" ? value : undefined;
|
|
1838
|
+
} catch {
|
|
1839
|
+
return;
|
|
1840
|
+
}
|
|
1841
|
+
}
|
|
1842
|
+
function isCancellationError(error) {
|
|
1843
|
+
if (error instanceof FilesystemError && error.errno === 11)
|
|
1844
|
+
return true;
|
|
1845
|
+
const name = diagnosticField(error, "name");
|
|
1846
|
+
return name === "AbortError";
|
|
1847
|
+
}
|
|
1725
1848
|
async function prepareAcceptance(binding) {
|
|
1726
1849
|
try {
|
|
1727
1850
|
const { captureAcceptance } = await import("../browser/operations/writer.js");
|
|
@@ -1739,20 +1862,69 @@ function backendOptions(data) {
|
|
|
1739
1862
|
}
|
|
1740
1863
|
export function installFilesystemWorker(scope, backendFactory = (options) => new MemoryBackend(undefined, undefined, undefined, undefined, options.connect)) {
|
|
1741
1864
|
let service;
|
|
1865
|
+
let resolvedService;
|
|
1742
1866
|
let errorReported = false;
|
|
1743
1867
|
let onStartupStage;
|
|
1744
1868
|
const startupAbort = new AbortController;
|
|
1745
1869
|
let disposing = false;
|
|
1870
|
+
let beginDisposeRequested = false;
|
|
1871
|
+
const callbacks = {
|
|
1872
|
+
onState(status) {
|
|
1873
|
+
if (!disposing)
|
|
1874
|
+
scope.postMessage({ type: "filesystem-state", status });
|
|
1875
|
+
},
|
|
1876
|
+
onOwnershipLost() {
|
|
1877
|
+
scope.postMessage({ type: "filesystem-ownership-lost" });
|
|
1878
|
+
},
|
|
1879
|
+
onFatalError(error, released) {
|
|
1880
|
+
if (disposing || errorReported)
|
|
1881
|
+
return;
|
|
1882
|
+
errorReported = true;
|
|
1883
|
+
try {
|
|
1884
|
+
scope.postMessage({ ...failureDiagnostic(error), released });
|
|
1885
|
+
} catch {}
|
|
1886
|
+
}
|
|
1887
|
+
};
|
|
1746
1888
|
scope.onmessage = ({ data }) => {
|
|
1889
|
+
if (data?.beginDispose === true) {
|
|
1890
|
+
beginDisposeRequested = true;
|
|
1891
|
+
if (resolvedService)
|
|
1892
|
+
resolvedService.beginDispose();
|
|
1893
|
+
else
|
|
1894
|
+
service?.then((owner) => owner.beginDispose(), () => {});
|
|
1895
|
+
return;
|
|
1896
|
+
}
|
|
1747
1897
|
if (data?.dispose === true) {
|
|
1748
1898
|
if (disposing)
|
|
1749
1899
|
return;
|
|
1750
1900
|
disposing = true;
|
|
1751
1901
|
startupAbort.abort();
|
|
1752
|
-
(service ?? Promise.resolve(undefined)).then((owner) => owner
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1902
|
+
(service ?? Promise.resolve(undefined)).then((owner) => owner ? owner.shutdown().then(() => ({ released: true, error: undefined })) : { released: true, error: undefined }, (error) => {
|
|
1903
|
+
const released = diagnosticReleased(error);
|
|
1904
|
+
if (released === false)
|
|
1905
|
+
return { released: false, error };
|
|
1906
|
+
if (released === true) {
|
|
1907
|
+
return isCancellationError(error) ? { released: true, error: undefined } : { released: true, error };
|
|
1908
|
+
}
|
|
1909
|
+
return { released: true, error: undefined };
|
|
1910
|
+
}).then(({ released, error }) => {
|
|
1911
|
+
if (error !== undefined) {
|
|
1912
|
+
scope.postMessage({
|
|
1913
|
+
type: "filesystem-disposed",
|
|
1914
|
+
error: failureDiagnostic(error).message,
|
|
1915
|
+
released
|
|
1916
|
+
});
|
|
1917
|
+
} else {
|
|
1918
|
+
scope.postMessage({ type: "filesystem-disposed", released });
|
|
1919
|
+
}
|
|
1920
|
+
}, (error) => {
|
|
1921
|
+
const released = diagnosticReleased(error);
|
|
1922
|
+
scope.postMessage({
|
|
1923
|
+
type: "filesystem-disposed",
|
|
1924
|
+
error: failureDiagnostic(error).message,
|
|
1925
|
+
...released !== undefined ? { released } : {}
|
|
1926
|
+
});
|
|
1927
|
+
});
|
|
1756
1928
|
return;
|
|
1757
1929
|
}
|
|
1758
1930
|
if (disposing) {
|
|
@@ -1767,8 +1939,14 @@ export function installFilesystemWorker(scope, backendFactory = (options) => new
|
|
|
1767
1939
|
return backendFactory(backendOptions(data), onStartupStage, startupAbort.signal, () => {
|
|
1768
1940
|
if (!disposing)
|
|
1769
1941
|
scope.postMessage({ type: "filesystem-state", status: "waiting" });
|
|
1770
|
-
});
|
|
1771
|
-
}).then((backend) =>
|
|
1942
|
+
}, callbacks);
|
|
1943
|
+
}).then((backend) => {
|
|
1944
|
+
const owner = new FilesystemService(backend);
|
|
1945
|
+
resolvedService = owner;
|
|
1946
|
+
if (beginDisposeRequested)
|
|
1947
|
+
owner.beginDispose();
|
|
1948
|
+
return owner;
|
|
1949
|
+
});
|
|
1772
1950
|
service.then(async (owner) => {
|
|
1773
1951
|
if (disposing) {
|
|
1774
1952
|
data.connect?.port.close();
|
|
@@ -1800,9 +1978,11 @@ export function installFilesystemWorker(scope, backendFactory = (options) => new
|
|
|
1800
1978
|
try {
|
|
1801
1979
|
scope.postMessage(failureDiagnostic(error));
|
|
1802
1980
|
} catch {} finally {
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1981
|
+
if (diagnosticReleased(error) !== false) {
|
|
1982
|
+
setTimeout(() => {
|
|
1983
|
+
throw error;
|
|
1984
|
+
}, 0);
|
|
1985
|
+
}
|
|
1806
1986
|
}
|
|
1807
1987
|
});
|
|
1808
1988
|
};
|
package/dist/fs/shared.js
CHANGED
|
@@ -63,6 +63,9 @@ const REGISTERED_SLOTS = 11;
|
|
|
63
63
|
const PHYSICAL_HINT = 27;
|
|
64
64
|
const TABLE_HINT = 28;
|
|
65
65
|
const PINNED_PAGES = 29;
|
|
66
|
+
export const CLIENT_MODE = 30;
|
|
67
|
+
export const CLIENT_MODE_NO_DIRECT_IO = 1;
|
|
68
|
+
export const CLIENT_MODE_NO_CLIENT_CACHE = 2;
|
|
66
69
|
const MODE = 12;
|
|
67
70
|
const FILE_SIZE = 1;
|
|
68
71
|
const MTIME = 2;
|
|
@@ -206,6 +209,48 @@ export class SharedFilesystemState {
|
|
|
206
209
|
readerPinBytes() {
|
|
207
210
|
return this.pageReaders ? this.cells[PINNED_PAGES] * DATA_PAGE_BYTES : 0;
|
|
208
211
|
}
|
|
212
|
+
setClientMode(mode) {
|
|
213
|
+
Atomics.store(this.cells, CLIENT_MODE, mode | 0);
|
|
214
|
+
}
|
|
215
|
+
isDirectDisabled() {
|
|
216
|
+
return (Atomics.load(this.cells, CLIENT_MODE) & CLIENT_MODE_NO_DIRECT_IO) !== 0;
|
|
217
|
+
}
|
|
218
|
+
isClientCacheDisabled() {
|
|
219
|
+
return (Atomics.load(this.cells, CLIENT_MODE) & CLIENT_MODE_NO_CLIENT_CACHE) !== 0;
|
|
220
|
+
}
|
|
221
|
+
resetForRemount(retainedSegments = []) {
|
|
222
|
+
Atomics.store(this.cells, GLOBAL_LOCK, 0);
|
|
223
|
+
Atomics.store(this.cells, USED_PAGES, 0);
|
|
224
|
+
for (let i = 7;i <= 25; i++)
|
|
225
|
+
Atomics.store(this.cells, i, 0);
|
|
226
|
+
Atomics.store(this.cells, PHYSICAL_HINT, 0);
|
|
227
|
+
Atomics.store(this.cells, TABLE_HINT, 0);
|
|
228
|
+
Atomics.store(this.cells, PINNED_PAGES, 0);
|
|
229
|
+
for (let i = 32;i < HEADER_WORDS; i++)
|
|
230
|
+
Atomics.store(this.cells, i, 0);
|
|
231
|
+
this.owners.fill(0);
|
|
232
|
+
this.slots.fill(0);
|
|
233
|
+
this.tableOwners.fill(0);
|
|
234
|
+
this.pages.fill(0);
|
|
235
|
+
this.firstBytes.fill(0);
|
|
236
|
+
this.pageViews.length = 0;
|
|
237
|
+
this.pendingSegments.length = 0;
|
|
238
|
+
this.staged.clear();
|
|
239
|
+
this.serviceMutation = false;
|
|
240
|
+
this.globalOwner = undefined;
|
|
241
|
+
this.globalDepth = 0;
|
|
242
|
+
this.fileOwnsGlobal.clear();
|
|
243
|
+
this.currentOwner = undefined;
|
|
244
|
+
this.segments.length = 1;
|
|
245
|
+
this.segments[0] = this.buffer;
|
|
246
|
+
for (let i = 0;i < retainedSegments.length; i++) {
|
|
247
|
+
const seg = retainedSegments[i];
|
|
248
|
+
if (!seg)
|
|
249
|
+
continue;
|
|
250
|
+
new Uint8Array(seg).fill(0);
|
|
251
|
+
this.segments[i + 1] = seg;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
209
254
|
releaseVersionPages(pages) {
|
|
210
255
|
this.globalLock(undefined, this.lockOwner, false, "release");
|
|
211
256
|
try {
|