@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
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { FilesystemError, pack } from "./protocol.js";
|
|
2
|
+
export const MAX_FRAME_BYTES = 1024 * 1024;
|
|
3
|
+
export const MIN_PEER_FRAMES = 2;
|
|
4
|
+
export const MIN_PEER_BYTES = 1024 * 1024;
|
|
5
|
+
export const MAX_HOST_FRAMES = 64;
|
|
6
|
+
export const MAX_HOST_WIRE_BYTES = 16 * 1024 * 1024;
|
|
7
|
+
export const DEFAULT_PEER_FRAMES = 4;
|
|
8
|
+
export const DEFAULT_PEER_BYTES = 2 * MAX_FRAME_BYTES;
|
|
9
|
+
export const MAX_STAGED_UPLOAD_BYTES = 32 * 1024 * 1024;
|
|
10
|
+
export const MAX_STAGED_DOWNLOAD_BYTES = 32 * 1024 * 1024;
|
|
11
|
+
export const MAX_CONCURRENT_UPLOADS_PER_PEER = 4;
|
|
12
|
+
export const MAX_CONCURRENT_DOWNLOADS_PER_PEER = 4;
|
|
13
|
+
export const MAX_PENDING_SETTLEMENTS = 1024;
|
|
14
|
+
export const SETTLEMENT_BATCH_SIZE = 32;
|
|
15
|
+
export const PONG_SETTLEMENT_LIMIT = 256;
|
|
16
|
+
export const DIR_EPOCH_MAX = 0xffffn;
|
|
17
|
+
const COOKIE_LOW_MASK = 0xffffffffffffn;
|
|
18
|
+
const WIRE_BUDGET_SLAB_BYTES = 64 * 1024;
|
|
19
|
+
const DEFAULT_MULTITAB_TIMINGS = Object.freeze({
|
|
20
|
+
leaseDurationMs: 1200,
|
|
21
|
+
leaseRenewMs: 400,
|
|
22
|
+
leaseClockSafetyMs: 200,
|
|
23
|
+
heartbeatIntervalMs: 800,
|
|
24
|
+
heartbeatTimeoutMs: 2400,
|
|
25
|
+
confirmGraceMs: 400,
|
|
26
|
+
attachRetryMs: 300,
|
|
27
|
+
waitPollMs: 150,
|
|
28
|
+
recoverTimeoutMs: 2000,
|
|
29
|
+
drainGraceMs: 150,
|
|
30
|
+
attachIdleTimeoutMs: 2400 * 3
|
|
31
|
+
});
|
|
32
|
+
export function resolveMultiTabTimings(overrides) {
|
|
33
|
+
if (!overrides)
|
|
34
|
+
return DEFAULT_MULTITAB_TIMINGS;
|
|
35
|
+
const heartbeatTimeoutMs = overrides.heartbeatTimeoutMs ?? DEFAULT_MULTITAB_TIMINGS.heartbeatTimeoutMs;
|
|
36
|
+
return Object.freeze({
|
|
37
|
+
...DEFAULT_MULTITAB_TIMINGS,
|
|
38
|
+
...overrides,
|
|
39
|
+
attachIdleTimeoutMs: overrides.attachIdleTimeoutMs ?? heartbeatTimeoutMs * 3
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
export function multitabScope(implementation, name) {
|
|
43
|
+
return `${implementation}:${encodeURIComponent(name)}`;
|
|
44
|
+
}
|
|
45
|
+
export function controlChannelName(scope) {
|
|
46
|
+
return `waffle:multitab:v1:ctrl:${scope}`;
|
|
47
|
+
}
|
|
48
|
+
export function peerChannelName(scope, hostTerm, peerId) {
|
|
49
|
+
return `waffle:multitab:v1:peer:${scope}:${hostTerm.toString(16)}:${peerId}`;
|
|
50
|
+
}
|
|
51
|
+
export function electionLockName(scope) {
|
|
52
|
+
return `waffle:multitab:v1:host:${scope}`;
|
|
53
|
+
}
|
|
54
|
+
export function attachClaimPrefix(scope) {
|
|
55
|
+
return `waffle:multitab:v1:attach-claim:${scope}:`;
|
|
56
|
+
}
|
|
57
|
+
export function attachClaimLockName(scope, attemptId, mode) {
|
|
58
|
+
return `${attachClaimPrefix(scope)}${attemptId}:${mode}`;
|
|
59
|
+
}
|
|
60
|
+
export function parseAttachClaimAttemptId(scope, lockName) {
|
|
61
|
+
const prefix = attachClaimPrefix(scope);
|
|
62
|
+
if (!lockName.startsWith(prefix))
|
|
63
|
+
return;
|
|
64
|
+
const rest = lockName.slice(prefix.length);
|
|
65
|
+
const sep = rest.indexOf(":");
|
|
66
|
+
return sep > 0 ? rest.slice(0, sep) : undefined;
|
|
67
|
+
}
|
|
68
|
+
export function aliveLockPrefix(scope) {
|
|
69
|
+
return `waffle:multitab:v1:alive:${scope}:`;
|
|
70
|
+
}
|
|
71
|
+
export function aliveLockName(scope, attemptId) {
|
|
72
|
+
return `${aliveLockPrefix(scope)}${attemptId}`;
|
|
73
|
+
}
|
|
74
|
+
export function peerAliveLockName(scope, peerId) {
|
|
75
|
+
return `waffle:multitab:v1:peer-alive:${scope}:${peerId}`;
|
|
76
|
+
}
|
|
77
|
+
export function recoverLockName(scope) {
|
|
78
|
+
return `waffle:multitab:v1:recover:${scope}`;
|
|
79
|
+
}
|
|
80
|
+
export function storageLockNameFor(implementation, name) {
|
|
81
|
+
return implementation === "indexeddb" ? `bun-wasm:filesystem:indexeddb:${name}` : `bun-wasm:filesystem:${name}`;
|
|
82
|
+
}
|
|
83
|
+
export function maintenanceLockNameFor(name) {
|
|
84
|
+
return `bun-wasm:filesystem:indexeddb-maintenance:${name}`;
|
|
85
|
+
}
|
|
86
|
+
export function formatTerm(term) {
|
|
87
|
+
return term.toString(16);
|
|
88
|
+
}
|
|
89
|
+
export function parseTerm(value) {
|
|
90
|
+
if (typeof value !== "string" || !/^[0-9a-f]+$/i.test(value))
|
|
91
|
+
return;
|
|
92
|
+
try {
|
|
93
|
+
return BigInt(`0x${value}`);
|
|
94
|
+
} catch {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export class MultiTabReleaseError extends Error {
|
|
100
|
+
released;
|
|
101
|
+
errno;
|
|
102
|
+
constructor(message, released, cause, code) {
|
|
103
|
+
super(message);
|
|
104
|
+
this.name = "MultiTabReleaseError";
|
|
105
|
+
this.released = released;
|
|
106
|
+
if (cause !== undefined)
|
|
107
|
+
this.cause = cause;
|
|
108
|
+
if (code !== undefined)
|
|
109
|
+
this.errno = code;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
export function describeReason(reason, fallback = "Filesystem storage failure") {
|
|
113
|
+
if (reason instanceof Error && reason.message)
|
|
114
|
+
return reason.message;
|
|
115
|
+
if (typeof reason === "string" && reason.length)
|
|
116
|
+
return reason;
|
|
117
|
+
try {
|
|
118
|
+
const text = String(reason);
|
|
119
|
+
return text.length ? text : fallback;
|
|
120
|
+
} catch {
|
|
121
|
+
return fallback;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
export function wrapReleaseError(reason, released, fallback = "Filesystem disposal failed") {
|
|
125
|
+
const code = reason instanceof FilesystemError ? reason.errno : undefined;
|
|
126
|
+
return new MultiTabReleaseError(describeReason(reason, fallback), released, reason, code);
|
|
127
|
+
}
|
|
128
|
+
export function unsharedBytes(bytes) {
|
|
129
|
+
if (bytes.buffer instanceof ArrayBuffer && !bytes.buffer.resizable && bytes.byteOffset === 0 && bytes.byteLength === bytes.buffer.byteLength)
|
|
130
|
+
return bytes;
|
|
131
|
+
const copy = new Uint8Array(bytes.byteLength);
|
|
132
|
+
copy.set(bytes);
|
|
133
|
+
return copy;
|
|
134
|
+
}
|
|
135
|
+
const PURE_STATELESS_READ_OPS = new Set([
|
|
136
|
+
"readFile",
|
|
137
|
+
"lookup",
|
|
138
|
+
"listFiles",
|
|
139
|
+
"resolvePath",
|
|
140
|
+
"stat",
|
|
141
|
+
"permissions",
|
|
142
|
+
"read",
|
|
143
|
+
"readdir",
|
|
144
|
+
"readlink",
|
|
145
|
+
"resolve",
|
|
146
|
+
"readdirPage"
|
|
147
|
+
]);
|
|
148
|
+
export function isReplayableRead(operation, args) {
|
|
149
|
+
if (!PURE_STATELESS_READ_OPS.has(operation))
|
|
150
|
+
return false;
|
|
151
|
+
if (operation === "readdirPage") {
|
|
152
|
+
const cookie = args[1];
|
|
153
|
+
if (typeof cookie === "bigint" && cookie >= 3n)
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
export function validateAndStripDirCookie(wireCookie, currentDirEpoch) {
|
|
159
|
+
if (wireCookie < 0n)
|
|
160
|
+
throw new FilesystemError(28, "invalid directory cookie");
|
|
161
|
+
if (wireCookie <= 2n)
|
|
162
|
+
return wireCookie;
|
|
163
|
+
const tag = wireCookie >> 48n;
|
|
164
|
+
if (tag !== currentDirEpoch)
|
|
165
|
+
throw new FilesystemError(8, "stale directory cookie");
|
|
166
|
+
return wireCookie & COOKIE_LOW_MASK;
|
|
167
|
+
}
|
|
168
|
+
function isObject(value) {
|
|
169
|
+
return typeof value === "object" && value !== null;
|
|
170
|
+
}
|
|
171
|
+
export function isControlMessage(value) {
|
|
172
|
+
if (!isObject(value) || typeof value.kind !== "string")
|
|
173
|
+
return false;
|
|
174
|
+
switch (value.kind) {
|
|
175
|
+
case "discover":
|
|
176
|
+
return typeof value.fromPeerId === "string";
|
|
177
|
+
case "host-status":
|
|
178
|
+
return typeof value.attemptId === "string" && typeof value.clientId === "string" && (value.phase === "opening" || value.phase === "waiting-storage" || value.phase === "retiring");
|
|
179
|
+
case "host-ready":
|
|
180
|
+
return typeof value.attemptId === "string" && typeof value.clientId === "string" && typeof value.hostTerm === "string" && typeof value.workspaceId === "string" && typeof value.storageEpoch === "number" && typeof value.lastExclusiveEpoch === "number";
|
|
181
|
+
case "host-draining":
|
|
182
|
+
case "host-stepping-down":
|
|
183
|
+
return typeof value.attemptId === "string" && typeof value.hostTerm === "string";
|
|
184
|
+
case "host-failed":
|
|
185
|
+
return typeof value.attemptId === "string" && typeof value.clientId === "string" && typeof value.hostTerm === "string" && typeof value.workspaceId === "string";
|
|
186
|
+
case "cooperative-step-down":
|
|
187
|
+
return typeof value.targetAttemptId === "string" && typeof value.targetTerm === "string";
|
|
188
|
+
case "external-takeover":
|
|
189
|
+
return typeof value.attemptId === "string" && typeof value.hostTerm === "string" && typeof value.workspaceId === "string" && typeof value.lastExclusiveEpoch === "number" && typeof value.affectedEpoch === "number";
|
|
190
|
+
case "workspace-replaced":
|
|
191
|
+
return typeof value.workspaceId === "string" && typeof value.expectedWorkspaceId === "string";
|
|
192
|
+
case "attach":
|
|
193
|
+
return typeof value.peerId === "string" && typeof value.sessionId === "string" && typeof value.hostTerm === "string";
|
|
194
|
+
default:
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function isValidCreditLimit(value) {
|
|
199
|
+
return isObject(value) && typeof value.frames === "number" && Number.isInteger(value.frames) && value.frames >= 0 && typeof value.bytes === "number" && Number.isInteger(value.bytes) && value.bytes >= 0;
|
|
200
|
+
}
|
|
201
|
+
export function isPeerMessage(value) {
|
|
202
|
+
if (!isObject(value) || typeof value.kind !== "string" || typeof value.hostTerm !== "string" || typeof value.sessionId !== "string")
|
|
203
|
+
return false;
|
|
204
|
+
switch (value.kind) {
|
|
205
|
+
case "attached":
|
|
206
|
+
case "credit-limit":
|
|
207
|
+
return isValidCreditLimit(value.creditLimit);
|
|
208
|
+
case "attach-rejected":
|
|
209
|
+
return typeof value.errno === "number";
|
|
210
|
+
case "req":
|
|
211
|
+
return typeof value.reqId === "number" && typeof value.op === "string" && Array.isArray(value.args);
|
|
212
|
+
case "chunk-put":
|
|
213
|
+
return typeof value.reqId === "number" && typeof value.uploadId === "string" && typeof value.offset === "number" && typeof value.totalBytes === "number" && value.chunk instanceof Uint8Array;
|
|
214
|
+
case "chunk-get":
|
|
215
|
+
return typeof value.reqId === "number" && typeof value.downloadId === "string" && typeof value.offset === "number" && typeof value.length === "number";
|
|
216
|
+
case "res":
|
|
217
|
+
return typeof value.reqId === "number" && typeof value.ok === "boolean";
|
|
218
|
+
case "ping":
|
|
219
|
+
return typeof value.pingId === "number" && typeof value.latestAckedWakeSeq === "number";
|
|
220
|
+
case "pong":
|
|
221
|
+
return typeof value.pingId === "number" && Array.isArray(value.activeReqIds) && typeof value.latestWakeSeq === "number" && isValidCreditLimit(value.creditLimit) && (value.hostState === "ready" || value.hostState === "draining" || value.hostState === "failed");
|
|
222
|
+
case "watch-wake":
|
|
223
|
+
case "watch-wake-ack":
|
|
224
|
+
return typeof value.wakeSeq === "number";
|
|
225
|
+
case "watch-ack":
|
|
226
|
+
return typeof value.ackId === "number" && typeof value.accepted === "boolean";
|
|
227
|
+
case "unpin":
|
|
228
|
+
return typeof value.pinToken === "number";
|
|
229
|
+
case "watch-close":
|
|
230
|
+
return typeof value.hostWatchId === "number";
|
|
231
|
+
case "settled":
|
|
232
|
+
case "detach":
|
|
233
|
+
return true;
|
|
234
|
+
default:
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
const wireEncoder = new TextEncoder;
|
|
239
|
+
|
|
240
|
+
export class BoundedWireBuilder {
|
|
241
|
+
hardCapBytes;
|
|
242
|
+
reserveSlab;
|
|
243
|
+
releaseAll;
|
|
244
|
+
chunks = [];
|
|
245
|
+
totalBytes = 0;
|
|
246
|
+
reservedBytes = 0;
|
|
247
|
+
finished = false;
|
|
248
|
+
constructor(hardCapBytes, reserveSlab, releaseAll) {
|
|
249
|
+
this.hardCapBytes = hardCapBytes;
|
|
250
|
+
this.reserveSlab = reserveSlab;
|
|
251
|
+
this.releaseAll = releaseAll;
|
|
252
|
+
}
|
|
253
|
+
appendText(text) {
|
|
254
|
+
this.appendBytes(wireEncoder.encode(text));
|
|
255
|
+
}
|
|
256
|
+
appendPacked(value) {
|
|
257
|
+
this.appendBytes(pack(value));
|
|
258
|
+
}
|
|
259
|
+
appendBytes(chunk) {
|
|
260
|
+
if (!chunk.byteLength)
|
|
261
|
+
return;
|
|
262
|
+
const nextTotal = this.totalBytes + chunk.byteLength;
|
|
263
|
+
if (nextTotal > this.hardCapBytes) {
|
|
264
|
+
this.abort();
|
|
265
|
+
throw new FilesystemError(22, "wire response exceeds download cap");
|
|
266
|
+
}
|
|
267
|
+
if (nextTotal > this.reservedBytes) {
|
|
268
|
+
const target = Math.min(this.hardCapBytes, Math.ceil(nextTotal / WIRE_BUDGET_SLAB_BYTES) * WIRE_BUDGET_SLAB_BYTES);
|
|
269
|
+
const delta = target - this.reservedBytes;
|
|
270
|
+
if (!this.reserveSlab(delta)) {
|
|
271
|
+
this.abort();
|
|
272
|
+
throw new FilesystemError(6, "staged download pool exhausted");
|
|
273
|
+
}
|
|
274
|
+
this.reservedBytes = target;
|
|
275
|
+
}
|
|
276
|
+
this.chunks.push(chunk);
|
|
277
|
+
this.totalBytes = nextTotal;
|
|
278
|
+
}
|
|
279
|
+
finish() {
|
|
280
|
+
this.finished = true;
|
|
281
|
+
const out = new Uint8Array(this.totalBytes);
|
|
282
|
+
let offset = 0;
|
|
283
|
+
for (const chunk of this.chunks) {
|
|
284
|
+
out.set(chunk, offset);
|
|
285
|
+
offset += chunk.byteLength;
|
|
286
|
+
}
|
|
287
|
+
this.chunks.length = 0;
|
|
288
|
+
return { bytes: out, reservedBytes: this.reservedBytes };
|
|
289
|
+
}
|
|
290
|
+
abort() {
|
|
291
|
+
if (this.finished)
|
|
292
|
+
return;
|
|
293
|
+
this.finished = true;
|
|
294
|
+
this.chunks.length = 0;
|
|
295
|
+
if (this.reservedBytes > 0) {
|
|
296
|
+
const bytes = this.reservedBytes;
|
|
297
|
+
this.reservedBytes = 0;
|
|
298
|
+
this.releaseAll(bytes);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
package/dist/fs/opfs.js
CHANGED
|
@@ -98,6 +98,7 @@ export class OpfsStorage {
|
|
|
98
98
|
handles = new Map;
|
|
99
99
|
owned = new Map;
|
|
100
100
|
failedClose;
|
|
101
|
+
terminalFailureListener;
|
|
101
102
|
peakHandles = 0;
|
|
102
103
|
garbage = new Set;
|
|
103
104
|
needsOrphanSweep = true;
|
|
@@ -464,6 +465,20 @@ export class OpfsStorage {
|
|
|
464
465
|
if (result.kind === "failed-close")
|
|
465
466
|
throw result.reason;
|
|
466
467
|
}
|
|
468
|
+
isTerminallyFailed() {
|
|
469
|
+
return this.failedClose !== undefined;
|
|
470
|
+
}
|
|
471
|
+
terminalFailureReason() {
|
|
472
|
+
return this.failedClose?.reason;
|
|
473
|
+
}
|
|
474
|
+
setTerminalFailureListener(listener) {
|
|
475
|
+
this.terminalFailureListener = listener;
|
|
476
|
+
if (this.failedClose !== undefined) {
|
|
477
|
+
try {
|
|
478
|
+
listener(this.failedClose.reason);
|
|
479
|
+
} catch {}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
467
482
|
closeResult(access) {
|
|
468
483
|
const previous = this.owned.get(access);
|
|
469
484
|
if (previous?.kind === "failed-close")
|
|
@@ -473,8 +488,14 @@ export class OpfsStorage {
|
|
|
473
488
|
} catch (reason) {
|
|
474
489
|
const failure = { kind: "failed-close", reason };
|
|
475
490
|
this.owned.set(access, failure);
|
|
491
|
+
const first = this.failedClose === undefined;
|
|
476
492
|
this.failedClose ??= failure;
|
|
477
493
|
this.closed = true;
|
|
494
|
+
if (first) {
|
|
495
|
+
try {
|
|
496
|
+
this.terminalFailureListener?.(reason);
|
|
497
|
+
} catch {}
|
|
498
|
+
}
|
|
478
499
|
return failure;
|
|
479
500
|
}
|
|
480
501
|
this.owned.delete(access);
|
package/dist/fs/persistent.js
CHANGED
|
@@ -24,6 +24,10 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
24
24
|
dirtyLimits;
|
|
25
25
|
pressure;
|
|
26
26
|
nodes = new Map;
|
|
27
|
+
byFileId = new Map;
|
|
28
|
+
initialWorkspaceId;
|
|
29
|
+
initialEra;
|
|
30
|
+
loadedIndex;
|
|
27
31
|
detached = new WeakSet;
|
|
28
32
|
links = new Map;
|
|
29
33
|
removed = new Set;
|
|
@@ -51,12 +55,17 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
51
55
|
super(fs, reservation, startupRoot, undefined, sharedRoot);
|
|
52
56
|
this.storage = storage;
|
|
53
57
|
this.dirtyLimits = dirtyLimits;
|
|
58
|
+
this.initialWorkspaceId = reservation.workspace;
|
|
59
|
+
this.initialEra = reservation.era;
|
|
60
|
+
this.loadedIndex = index !== undefined;
|
|
54
61
|
this.pressure = new CheckpointPressure(checkpointLimits);
|
|
55
62
|
const visit = (node) => {
|
|
56
63
|
this.links.set(node.ino, (this.links.get(node.ino) ?? 0) + 1);
|
|
57
64
|
if (this.nodes.has(node.ino))
|
|
58
65
|
return;
|
|
59
66
|
this.nodes.set(node.ino, node);
|
|
67
|
+
if (node.identity)
|
|
68
|
+
this.byFileId.set(node.identity.id, node);
|
|
60
69
|
if (node.kind === "dir")
|
|
61
70
|
for (const child of node.children.values())
|
|
62
71
|
visit(child);
|
|
@@ -78,11 +87,46 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
78
87
|
fs.onChange = (node) => {
|
|
79
88
|
if (!this.detached.has(node)) {
|
|
80
89
|
this.nodes.set(node.ino, node);
|
|
90
|
+
if (node.identity)
|
|
91
|
+
this.byFileId.set(node.identity.id, node);
|
|
81
92
|
this.dirty.add(node);
|
|
82
93
|
}
|
|
83
94
|
};
|
|
84
95
|
this.startTimer(intervalMs);
|
|
85
96
|
}
|
|
97
|
+
get initialReservationEra() {
|
|
98
|
+
return this.initialEra;
|
|
99
|
+
}
|
|
100
|
+
get hasLoadedIndex() {
|
|
101
|
+
return this.loadedIndex;
|
|
102
|
+
}
|
|
103
|
+
async verifyStorageEpoch(signal) {
|
|
104
|
+
if ("verifyEpoch" in this.storage && typeof this.storage.verifyEpoch === "function") {
|
|
105
|
+
return this.storage.verifyEpoch(signal);
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
nodeByFileId(fileId) {
|
|
110
|
+
const node = this.byFileId.get(fileId);
|
|
111
|
+
if (node && node.identity?.id === fileId)
|
|
112
|
+
return node;
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
resolveNodePublic(path) {
|
|
116
|
+
return this.resolveNode(path);
|
|
117
|
+
}
|
|
118
|
+
ensureLoadedHandle(handle) {
|
|
119
|
+
return this.ensureLoaded(handle);
|
|
120
|
+
}
|
|
121
|
+
isTerminallyFailed() {
|
|
122
|
+
return this.storage.isTerminallyFailed?.() ?? false;
|
|
123
|
+
}
|
|
124
|
+
terminalFailureReason() {
|
|
125
|
+
return this.storage.terminalFailureReason?.();
|
|
126
|
+
}
|
|
127
|
+
setTerminalFailureListener(listener) {
|
|
128
|
+
this.storage.setTerminalFailureListener?.(listener);
|
|
129
|
+
}
|
|
86
130
|
startTimer(intervalMs) {
|
|
87
131
|
if (intervalMs > 0)
|
|
88
132
|
this.timer = setInterval(() => {
|
|
@@ -91,10 +135,13 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
91
135
|
}, intervalMs);
|
|
92
136
|
}
|
|
93
137
|
static async open(storage, options = {}) {
|
|
138
|
+
options.signal?.throwIfAborted();
|
|
94
139
|
const index = await storage.load();
|
|
140
|
+
options.signal?.throwIfAborted();
|
|
95
141
|
if (index)
|
|
96
142
|
validatePersistedIndex(index);
|
|
97
143
|
const reservation = await storage.reserveIdentity(index?.nodes[0]?.identity?.workspace);
|
|
144
|
+
options.signal?.throwIfAborted();
|
|
98
145
|
validateReservation(index, reservation);
|
|
99
146
|
if (!index)
|
|
100
147
|
return new PersistentBackend(storage, new MemFS, reservation, undefined, options.intervalMs, options.checkpointLimits, options.dirtyLimits, options.startupRoot, options.sharedRoot);
|
|
@@ -105,10 +152,12 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
105
152
|
try {
|
|
106
153
|
if (!index.nodes[0]?.identity) {
|
|
107
154
|
await storage.flush();
|
|
155
|
+
options.signal?.throwIfAborted();
|
|
108
156
|
for (const node of backend.nodes.values())
|
|
109
157
|
backend.dirty.add(node);
|
|
110
|
-
await backend.flush();
|
|
158
|
+
await backend.flush(undefined, options.signal);
|
|
111
159
|
}
|
|
160
|
+
options.signal?.throwIfAborted();
|
|
112
161
|
backend.startTimer(options.intervalMs ?? 50);
|
|
113
162
|
return backend;
|
|
114
163
|
} catch (error) {
|
|
@@ -125,6 +174,8 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
125
174
|
this.links.delete(node.ino);
|
|
126
175
|
if (delta === 1 && previous === 0) {
|
|
127
176
|
this.nodes.set(node.ino, node);
|
|
177
|
+
if (node.identity)
|
|
178
|
+
this.byFileId.set(node.identity.id, node);
|
|
128
179
|
this.detached.delete(node);
|
|
129
180
|
const key = this.detachedKeys.get(node.ino);
|
|
130
181
|
if (key) {
|
|
@@ -150,6 +201,8 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
150
201
|
}
|
|
151
202
|
}
|
|
152
203
|
identityChanged(node) {
|
|
204
|
+
if (node.identity)
|
|
205
|
+
this.byFileId.set(node.identity.id, node);
|
|
153
206
|
if (this.isLinked(node))
|
|
154
207
|
this.dirty.add(node);
|
|
155
208
|
}
|
|
@@ -822,6 +875,22 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
822
875
|
this.coldReads.clear();
|
|
823
876
|
}
|
|
824
877
|
}
|
|
878
|
+
async retireFenced(options = {}) {
|
|
879
|
+
this.stopPersistence();
|
|
880
|
+
const work = Array.from(new Set(Array.from(this.admittedWork).concat([this.lastCommit, this.tail])));
|
|
881
|
+
await Promise.allSettled(work);
|
|
882
|
+
this.coldReads.clear();
|
|
883
|
+
let closeFailure;
|
|
884
|
+
try {
|
|
885
|
+
await this.storage.close();
|
|
886
|
+
} catch (reason) {
|
|
887
|
+
if (options.throwOnStorageCloseError)
|
|
888
|
+
closeFailure = { failed: true, reason };
|
|
889
|
+
}
|
|
890
|
+
this.watchAccount.close();
|
|
891
|
+
if (closeFailure)
|
|
892
|
+
throw closeFailure.reason;
|
|
893
|
+
}
|
|
825
894
|
close() {
|
|
826
895
|
if (this.closing)
|
|
827
896
|
return this.closing;
|
package/dist/fs/protocol.js
CHANGED
|
@@ -266,7 +266,7 @@ export function createFilesystemConnection(parent, publication, construction) {
|
|
|
266
266
|
}
|
|
267
267
|
export function revokeFilesystemService(connection) {
|
|
268
268
|
Atomics.store(new Int32Array(connection.ownerStates), 0, 1);
|
|
269
|
-
wakeFilesystemWatch(connection.shared);
|
|
269
|
+
wakeFilesystemWatch(connection.shared, true);
|
|
270
270
|
wake(connection);
|
|
271
271
|
}
|
|
272
272
|
export function revokeFilesystem(connection) {
|