@waffle-os/runtime 0.0.155 → 0.0.157
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 +11 -1
- package/browser/preview-protocol.ts +2 -0
- package/browser/preview.ts +128 -18
- package/browser/runtime-terminals.ts +65 -24
- package/browser/shared-fs.ts +141 -14
- package/browser/worker.ts +73 -6
- package/core/descriptors.ts +19 -2
- package/core/devices.ts +237 -2
- package/core/fd-reopen.ts +105 -20
- package/core/filesystem.ts +79 -0
- package/core/index.ts +4 -0
- package/core/open-file.ts +212 -18
- package/core/wasi.ts +449 -47
- package/dist/browser/filesystem-backend.js +4 -2
- package/dist/browser/preview.js +120 -17
- package/dist/browser/runtime-terminals.js +53 -10
- package/dist/browser/shared-fs.js +124 -11
- package/dist/browser/worker.js +59 -6
- package/dist/core/descriptors.js +9 -1
- package/dist/core/devices.js +211 -1
- package/dist/core/fd-reopen.js +79 -18
- package/dist/core/filesystem.js +14 -0
- package/dist/core/open-file.js +166 -13
- package/dist/core/wasi.js +402 -29
- package/dist/fs/backend.js +51 -3
- package/dist/fs/client.js +204 -7
- package/dist/fs/cold-read-cache.js +23 -0
- package/dist/fs/index.js +21 -1
- package/dist/fs/indexeddb.js +30 -2
- package/dist/fs/locks.js +1112 -0
- package/dist/fs/memory.js +126 -2
- package/dist/fs/multitab-backend.js +317 -196
- package/dist/fs/multitab-protocol.js +2 -0
- package/dist/fs/persistent.js +32 -7
- package/dist/fs/protocol.js +9 -3
- package/dist/fs/service.js +387 -22
- package/dist/fs/shared.js +54 -1
- package/dist/fs/storage-geometry.js +2 -0
- package/dist/interrupt/rewriter-internal.js +4 -1
- package/dist/proc/abi.js +80 -6
- package/dist/proc/errors.js +1 -0
- package/dist/proc/filesystem-codec.js +40 -9
- package/dist/proc/index.js +10 -1
- package/dist/proc/pty.js +193 -0
- package/dist/proc/worker-client.js +11 -2
- package/dist/proc/worker-pipes.js +51 -0
- package/dist/runtime.js +11 -2
- package/dist/search/engine.js +53 -0
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/backend.ts +54 -2
- package/fs/client.ts +230 -6
- package/fs/cold-read-cache.ts +38 -0
- package/fs/index.ts +22 -1
- package/fs/indexeddb.ts +36 -2
- package/fs/locks.ts +1406 -0
- package/fs/memory.ts +132 -1
- package/fs/multitab-backend.ts +563 -304
- package/fs/multitab-protocol.ts +2 -0
- package/fs/persistent.ts +46 -4
- package/fs/protocol.ts +17 -5
- package/fs/service.ts +469 -23
- package/fs/shared.ts +60 -2
- package/fs/storage-geometry.ts +3 -0
- package/index.ts +2 -0
- package/interrupt/rewriter-internal.ts +4 -1
- package/package.json +2 -2
- package/proc/abi.ts +79 -6
- package/proc/errors.ts +1 -0
- package/proc/filesystem-codec.ts +39 -9
- package/proc/index.ts +11 -0
- package/proc/pty.ts +227 -0
- package/proc/worker-client.ts +28 -6
- package/proc/worker-pipes.ts +57 -0
- package/runtime.ts +12 -2
- package/search/engine.ts +49 -0
- package/testing/indexeddb-storage-browser.ts +173 -2
- package/tooling/program/program-transformer.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"commit":"
|
|
1
|
+
{"commit":"009023cdfec2eef3e9e0502ad18609021dc86dd3","buildToolsSha256":"b617652c1601f8722bb85c3a688a1fca4985b53f2c2066c10c76c2e693081137","optimizerSourceSha256":"34c4a040f3cf639f039823880822c6541c384c9b4eaa116d27dda8d9cb5c9f9e"}
|
package/fs/backend.ts
CHANGED
|
@@ -16,10 +16,18 @@ import {
|
|
|
16
16
|
import { type FileIdentity, type IdentityReservation, IdentityAllocator } from "./file-identity.js";
|
|
17
17
|
import type { PublicationBuffers } from "./publication-internal.js";
|
|
18
18
|
import { type FileHandle, type FileStat, type ResolvedPath } from "../core/index.js";
|
|
19
|
-
import {
|
|
19
|
+
import {
|
|
20
|
+
fileTimeMs,
|
|
21
|
+
fileTimeNow,
|
|
22
|
+
msFileTime,
|
|
23
|
+
PROBE_PATH_LIMIT,
|
|
24
|
+
type FileTime,
|
|
25
|
+
type ProbeKind,
|
|
26
|
+
} from "../core/filesystem.js";
|
|
20
27
|
import { MemFS, type Node, type NamespacePlan } from "./memory.js";
|
|
21
28
|
import { FilesystemError, type FilesystemIdentity } from "./protocol.js";
|
|
22
|
-
import { SharedFilesystemState, type SharedSegment } from "./shared.js";
|
|
29
|
+
import { machineIdFromWorkspace, SharedFilesystemState, type SharedSegment } from "./shared.js";
|
|
30
|
+
import { validateMachineId } from "../core/devices.js";
|
|
23
31
|
|
|
24
32
|
/** Storage boundary. 3b can add asynchronous loading/commit without changing guest transport. */
|
|
25
33
|
export interface FilesystemBackend {
|
|
@@ -71,6 +79,8 @@ export interface FilesystemBackend {
|
|
|
71
79
|
beginShutdown?(): void;
|
|
72
80
|
/** Whether this backend supports Unix domain socket scope attachment (defaults to true for MemoryBackend). */
|
|
73
81
|
readonly supportsSockets?: boolean;
|
|
82
|
+
/** Whether this backend supports advisory file locks (defaults to true for single-owner backends). */
|
|
83
|
+
readonly supportsLocks?: boolean;
|
|
74
84
|
/** Whether async execute/flush/watch rejections are handled client results rather than fatal service shutdown failures. */
|
|
75
85
|
readonly asyncErrorsAreClientResults?: boolean;
|
|
76
86
|
/** Re-verify lease / terminal state before publishing a prepared cold descriptor read. */
|
|
@@ -95,6 +105,16 @@ export interface FilesystemBackend {
|
|
|
95
105
|
/** Release startup resources when cancellation races backend creation. */
|
|
96
106
|
close?(): Promise<void>;
|
|
97
107
|
}
|
|
108
|
+
/** A `probe` request's pathnames: an array of at most PROBE_PATH_LIMIT strings, or EINVAL. */
|
|
109
|
+
function probePaths(value: unknown): readonly string[] {
|
|
110
|
+
if (!Array.isArray(value) || value.length > PROBE_PATH_LIMIT) throw new FilesystemError(28);
|
|
111
|
+
for (const path of value) if (typeof path !== "string") throw new FilesystemError(28);
|
|
112
|
+
return value as string[];
|
|
113
|
+
}
|
|
114
|
+
function probeKind(value: unknown): ProbeKind {
|
|
115
|
+
if (value !== "file" && value !== "directory") throw new FilesystemError(28);
|
|
116
|
+
return value;
|
|
117
|
+
}
|
|
98
118
|
type DirectoryTimes = Pick<FileStat, "atime" | "mtime" | "ctime">;
|
|
99
119
|
function sameTimes(left: DirectoryTimes | undefined, right: DirectoryTimes): boolean {
|
|
100
120
|
return (
|
|
@@ -191,6 +211,7 @@ export class MemoryBackend implements FilesystemBackend {
|
|
|
191
211
|
return (node.identity ??= this.shared.identities.create());
|
|
192
212
|
}
|
|
193
213
|
readonly workspaceId: string;
|
|
214
|
+
readonly machineId: string;
|
|
194
215
|
readonly fs: MemFS;
|
|
195
216
|
readonly watchAccount: RootWatchAccount;
|
|
196
217
|
constructor(
|
|
@@ -199,6 +220,7 @@ export class MemoryBackend implements FilesystemBackend {
|
|
|
199
220
|
startupRoot?: MemoryBudget | MemoryBackend,
|
|
200
221
|
watchLimits?: WatchResourceLimits,
|
|
201
222
|
sharedRoot?: MemoryBackendSharedRoot,
|
|
223
|
+
machineId?: string,
|
|
202
224
|
) {
|
|
203
225
|
if (sharedRoot && startupRoot)
|
|
204
226
|
throw new TypeError("an imported root cannot also own a startup root");
|
|
@@ -208,6 +230,7 @@ export class MemoryBackend implements FilesystemBackend {
|
|
|
208
230
|
this.watchAccount = startupRoot.watchAccount;
|
|
209
231
|
this.fs = startupRoot.fs;
|
|
210
232
|
this.workspaceId = startupRoot.workspaceId;
|
|
233
|
+
this.machineId = startupRoot.machineId;
|
|
211
234
|
this.shared = startupRoot.shared;
|
|
212
235
|
this.startupRoot = startupRoot.startupRoot;
|
|
213
236
|
this.sharedPhase = startupRoot.sharedPhase;
|
|
@@ -229,7 +252,12 @@ export class MemoryBackend implements FilesystemBackend {
|
|
|
229
252
|
if (sharedRoot) this.sharedPhase = "imported";
|
|
230
253
|
this.fs = fs;
|
|
231
254
|
this.workspaceId = reservation.workspace;
|
|
255
|
+
this.machineId =
|
|
256
|
+
machineId !== undefined
|
|
257
|
+
? validateMachineId(machineId)
|
|
258
|
+
: machineIdFromWorkspace(reservation.workspace);
|
|
232
259
|
this.shared.identities.configure(reservation);
|
|
260
|
+
this.shared.setMachineId(this.machineId);
|
|
233
261
|
const seen = new Set<Node>();
|
|
234
262
|
const pending: Node[] = [fs.root];
|
|
235
263
|
while (pending.length) {
|
|
@@ -283,6 +311,7 @@ export class MemoryBackend implements FilesystemBackend {
|
|
|
283
311
|
publication,
|
|
284
312
|
);
|
|
285
313
|
if (this.sharedPhase !== "imported") this.shared.identities.copyFrom(previous.identities);
|
|
314
|
+
this.shared.setMachineId(this.machineId);
|
|
286
315
|
this.shared.continueUnlinks(previous);
|
|
287
316
|
this.sharedPhase = "attached";
|
|
288
317
|
}
|
|
@@ -465,6 +494,9 @@ export class MemoryBackend implements FilesystemBackend {
|
|
|
465
494
|
case "realpath":
|
|
466
495
|
this.check(a, "directory");
|
|
467
496
|
return this.fs.realpath(a, b, c === true);
|
|
497
|
+
case "probe":
|
|
498
|
+
this.check(a, "directory");
|
|
499
|
+
return this.fs.probe(a, probePaths(b), probeKind(c), args[3] === true);
|
|
468
500
|
case "readlink":
|
|
469
501
|
this.check(a);
|
|
470
502
|
return this.fs.readlink(a);
|
|
@@ -503,6 +535,8 @@ export class MemoryBackend implements FilesystemBackend {
|
|
|
503
535
|
}
|
|
504
536
|
case "lookup":
|
|
505
537
|
return this.lookup(a);
|
|
538
|
+
case "statPath":
|
|
539
|
+
return this.statPath(a, b !== false);
|
|
506
540
|
case "readFile": {
|
|
507
541
|
const existing = this.existing(a);
|
|
508
542
|
if (!existing) return;
|
|
@@ -515,6 +549,24 @@ export class MemoryBackend implements FilesystemBackend {
|
|
|
515
549
|
throw new FilesystemError(52, `unknown filesystem operation ${operation}`);
|
|
516
550
|
}
|
|
517
551
|
}
|
|
552
|
+
private statPath(path: string, follow: boolean): unknown {
|
|
553
|
+
const node = this.fs.lookup(path, follow);
|
|
554
|
+
if (!node) return;
|
|
555
|
+
const handle = this.fs.handleForNode(node);
|
|
556
|
+
const fsStat = this.fs.stat(handle);
|
|
557
|
+
const stat = this.shared.stat(handle);
|
|
558
|
+
return {
|
|
559
|
+
kind: node.kind,
|
|
560
|
+
size: stat ? stat.size : fsStat.size,
|
|
561
|
+
ino: node.ino,
|
|
562
|
+
mtime: fileTimeMs(stat ? stat.mtime : fsStat.mtime),
|
|
563
|
+
ctime: fileTimeMs(stat ? stat.ctime : fsStat.ctime),
|
|
564
|
+
atime: fileTimeMs(stat ? stat.atime : fsStat.atime),
|
|
565
|
+
mode: stat ? stat.mode : fsStat.mode,
|
|
566
|
+
nlink: stat ? stat.nlink : fsStat.nlink,
|
|
567
|
+
...(node.kind === "symlink" ? { target: node.target } : {}),
|
|
568
|
+
};
|
|
569
|
+
}
|
|
518
570
|
private lookup(path: string): unknown {
|
|
519
571
|
const node = this.fs.lookup(path, false);
|
|
520
572
|
if (!node) return;
|
package/fs/client.ts
CHANGED
|
@@ -40,9 +40,15 @@ import {
|
|
|
40
40
|
fileTimeNow,
|
|
41
41
|
isFileTime,
|
|
42
42
|
permissionsFor,
|
|
43
|
+
type FileLockConflict,
|
|
44
|
+
type FileLockRange,
|
|
45
|
+
type FileLockSystem,
|
|
43
46
|
type FileTime,
|
|
47
|
+
type LockWaitControl,
|
|
48
|
+
type ProbeKind,
|
|
44
49
|
type WatchFileSystem,
|
|
45
50
|
} from "../core/filesystem.js";
|
|
51
|
+
import { ExecutionUnwind } from "../core/unwind.js";
|
|
46
52
|
import {
|
|
47
53
|
ArrayBufferConstructor,
|
|
48
54
|
Int32ArrayConstructor,
|
|
@@ -210,6 +216,7 @@ const PLAIN_REPLIES = new SetConstructor([
|
|
|
210
216
|
"setMode",
|
|
211
217
|
"read",
|
|
212
218
|
"readlink",
|
|
219
|
+
"probe",
|
|
213
220
|
]);
|
|
214
221
|
export class FilesystemCompletion {
|
|
215
222
|
static {
|
|
@@ -375,12 +382,40 @@ export class FilesystemCompletion {
|
|
|
375
382
|
wait(): any {
|
|
376
383
|
return this.#wait();
|
|
377
384
|
}
|
|
378
|
-
|
|
385
|
+
waitInterruptible(control?: LockWaitControl): any {
|
|
386
|
+
return this.#wait(undefined, control);
|
|
387
|
+
}
|
|
388
|
+
private checkWaitControl(control: LockWaitControl | undefined): void {
|
|
389
|
+
if (!control) return;
|
|
390
|
+
try {
|
|
391
|
+
control.checkActive?.();
|
|
392
|
+
} catch (error) {
|
|
393
|
+
this.cancel();
|
|
394
|
+
throw error;
|
|
395
|
+
}
|
|
396
|
+
if (!control.interruptPending?.()) return;
|
|
397
|
+
this.cancel();
|
|
398
|
+
throw new FilesystemError(27);
|
|
399
|
+
}
|
|
400
|
+
private parkWait(signal: Int32Array, observed: number, control?: LockWaitControl): void {
|
|
401
|
+
if (control?.wait) {
|
|
402
|
+
try {
|
|
403
|
+
control.wait(signal, observed, 10);
|
|
404
|
+
} catch (error) {
|
|
405
|
+
this.cancel();
|
|
406
|
+
throw error;
|
|
407
|
+
}
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
atomicsWait(signal, 0, observed, control?.interruptPending ? 10 : undefined);
|
|
411
|
+
}
|
|
412
|
+
#wait(cancellation?: WaitCancellation, control?: LockWaitControl): any {
|
|
379
413
|
const signal = this.client.signal;
|
|
380
414
|
while (!this.ready) {
|
|
381
415
|
const observed = atomicsLoad(signal, 0);
|
|
382
416
|
this.check(cancellation);
|
|
383
|
-
|
|
417
|
+
this.checkWaitControl(control);
|
|
418
|
+
if (!this.ready) this.parkWait(signal, observed, control);
|
|
384
419
|
}
|
|
385
420
|
this.check(cancellation);
|
|
386
421
|
if (this.consumed) return this.decoded;
|
|
@@ -526,6 +561,69 @@ function clientWatches(
|
|
|
526
561
|
};
|
|
527
562
|
}
|
|
528
563
|
|
|
564
|
+
function isTerminalLockReleaseErrno(code: number): boolean {
|
|
565
|
+
// Non-retryable release outcomes (already gone, bad fd, unsupported, revoked/cancelled/reset).
|
|
566
|
+
return (
|
|
567
|
+
code === 2 ||
|
|
568
|
+
code === 8 ||
|
|
569
|
+
code === 11 ||
|
|
570
|
+
code === 15 ||
|
|
571
|
+
code === 28 ||
|
|
572
|
+
code === 44 ||
|
|
573
|
+
code === 58
|
|
574
|
+
);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
function clientLocks(
|
|
578
|
+
owner: number,
|
|
579
|
+
call: (operation: string, args: unknown[], control?: LockWaitControl) => any,
|
|
580
|
+
release: (operation: "lockReleaseOfd" | "lockReleasePosix", args: unknown[]) => void,
|
|
581
|
+
hasActivePosixLocks: () => boolean,
|
|
582
|
+
): FileLockSystem {
|
|
583
|
+
return {
|
|
584
|
+
owner,
|
|
585
|
+
flock: (handle, ofdId, operation, control) => {
|
|
586
|
+
try {
|
|
587
|
+
return call("lockFlock", [handle, ofdId, operation], control);
|
|
588
|
+
} catch (error) {
|
|
589
|
+
if (isInstance(error, ExecutionUnwind)) throw error;
|
|
590
|
+
return errno(error);
|
|
591
|
+
}
|
|
592
|
+
},
|
|
593
|
+
setRangeLock: (handle, ofdId, pid, range: FileLockRange, blocking, control) => {
|
|
594
|
+
try {
|
|
595
|
+
return call(
|
|
596
|
+
"lockRange",
|
|
597
|
+
[handle, ofdId ?? null, pid, range.type, range.start, range.len, blocking],
|
|
598
|
+
blocking ? control : undefined,
|
|
599
|
+
);
|
|
600
|
+
} catch (error) {
|
|
601
|
+
if (isInstance(error, ExecutionUnwind)) throw error;
|
|
602
|
+
return errno(error);
|
|
603
|
+
}
|
|
604
|
+
},
|
|
605
|
+
getRangeLock: (handle, ofdId, range: FileLockRange): FileLockConflict | number => {
|
|
606
|
+
try {
|
|
607
|
+
return call("lockGetRange", [handle, ofdId ?? null, range.type, range.start, range.len]);
|
|
608
|
+
} catch (error) {
|
|
609
|
+
return errno(error);
|
|
610
|
+
}
|
|
611
|
+
},
|
|
612
|
+
releaseOfd: (handle, ofdId, inFlight = false) => {
|
|
613
|
+
release("lockReleaseOfd", [handle, ofdId, inFlight]);
|
|
614
|
+
},
|
|
615
|
+
releasePosix: (handle) => {
|
|
616
|
+
release("lockReleasePosix", [handle]);
|
|
617
|
+
},
|
|
618
|
+
handoffPosix: (successorOwner) => {
|
|
619
|
+
try {
|
|
620
|
+
call("lockHandoffPosix", [successorOwner]);
|
|
621
|
+
} catch {}
|
|
622
|
+
},
|
|
623
|
+
hasActivePosixLocks,
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
|
|
529
627
|
/** Blocking preview1 adapter and non-blocking request/completion API for the async host.
|
|
530
628
|
* Read/stat caches are validated by the service's global seqlock. A concurrent mutation invalidates
|
|
531
629
|
* before changing data; a cached read that overlaps it is discarded, never returned as fresh.
|
|
@@ -619,6 +717,17 @@ export class FilesystemClient implements FileSystem {
|
|
|
619
717
|
private readonly section: FilesystemSection;
|
|
620
718
|
readonly shared: SharedFilesystemState;
|
|
621
719
|
readonly watches: WatchFileSystem;
|
|
720
|
+
readonly locks: FileLockSystem;
|
|
721
|
+
get machineId(): string | undefined {
|
|
722
|
+
return this.shared.getMachineId();
|
|
723
|
+
}
|
|
724
|
+
private pendingLockReleases: {
|
|
725
|
+
readonly operation: "lockReleaseOfd" | "lockReleasePosix";
|
|
726
|
+
readonly args: unknown[];
|
|
727
|
+
readonly retainedHandle?: number;
|
|
728
|
+
}[] = [];
|
|
729
|
+
private lockReleaseTimer?: ReturnType<typeof scheduleTimeout>;
|
|
730
|
+
private flushingLockReleases = false;
|
|
622
731
|
private watchingRevocation = false;
|
|
623
732
|
private resident: ResidentAttachment | undefined;
|
|
624
733
|
private workspaceStop: Promise<void> | undefined;
|
|
@@ -656,6 +765,20 @@ export class FilesystemClient implements FileSystem {
|
|
|
656
765
|
() => !this.shared.isWatchDrainDisabled(),
|
|
657
766
|
(operation, args) => this.call(operation, args),
|
|
658
767
|
);
|
|
768
|
+
this.locks = clientLocks(
|
|
769
|
+
connection.owner,
|
|
770
|
+
(operation, args, control) => {
|
|
771
|
+
if (this.pendingLockReleases.length > 0) this.flushPendingLockReleases();
|
|
772
|
+
const completion = this.submit(operation, args);
|
|
773
|
+
try {
|
|
774
|
+
return control ? completion.waitInterruptible(control) : completion.wait();
|
|
775
|
+
} finally {
|
|
776
|
+
completion.release();
|
|
777
|
+
}
|
|
778
|
+
},
|
|
779
|
+
(operation, args) => this.enqueueLockRelease(operation, args),
|
|
780
|
+
() => this.shared.posixLockCount() > 0,
|
|
781
|
+
);
|
|
659
782
|
connection.port.onmessage = ({ data }) => {
|
|
660
783
|
if (data.segment) this.attachSegment(data.segment, data.buffer);
|
|
661
784
|
else mapGet(this.completions, data.completed)?.notify();
|
|
@@ -933,10 +1056,11 @@ export class FilesystemClient implements FileSystem {
|
|
|
933
1056
|
mapSet(this.capabilityEntries, reservation.handle, reservation.targetKey);
|
|
934
1057
|
}
|
|
935
1058
|
releaseReservation(reservation: FilesystemReservation): void {
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
1059
|
+
this.finishReleasedReservation(reservation, releaseFilesystemReservation(reservation));
|
|
1060
|
+
}
|
|
1061
|
+
/** Drop local handle accounting after `releaseFilesystemReservation` has already run. */
|
|
1062
|
+
finishReleasedReservation(reservation: FilesystemReservation, released: boolean): void {
|
|
1063
|
+
if (released) {
|
|
940
1064
|
const count = mapGet(this.openHandles, reservation.handle) ?? 0;
|
|
941
1065
|
if (count > 1) mapSet(this.openHandles, reservation.handle, count - 1);
|
|
942
1066
|
else {
|
|
@@ -1092,7 +1216,102 @@ export class FilesystemClient implements FileSystem {
|
|
|
1092
1216
|
const completion = mapGet(this.completions, id);
|
|
1093
1217
|
return completion && { kind: 3, id, ready: completion.ready };
|
|
1094
1218
|
}
|
|
1219
|
+
private retainLockReleaseHandle(handle: number): number | undefined {
|
|
1220
|
+
if (!Number.isInteger(handle) || handle < 0) return undefined;
|
|
1221
|
+
try {
|
|
1222
|
+
this.retain(handle);
|
|
1223
|
+
return handle;
|
|
1224
|
+
} catch {
|
|
1225
|
+
return undefined;
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
private releaseLockReleaseHandle(retainedHandle: number | undefined): void {
|
|
1229
|
+
if (retainedHandle === undefined) return;
|
|
1230
|
+
try {
|
|
1231
|
+
this.release(retainedHandle);
|
|
1232
|
+
} catch {
|
|
1233
|
+
// Session already revoked or disposed; server teardown drops locks.
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
private finishNextLockRelease(): void {
|
|
1237
|
+
const removed = this.pendingLockReleases.splice(0, 1);
|
|
1238
|
+
if (removed.length > 0) this.releaseLockReleaseHandle(removed[0]!.retainedHandle);
|
|
1239
|
+
}
|
|
1240
|
+
private enqueueLockRelease(
|
|
1241
|
+
operation: "lockReleaseOfd" | "lockReleasePosix",
|
|
1242
|
+
args: unknown[],
|
|
1243
|
+
): void {
|
|
1244
|
+
if (this.disposed || this.revoked()) return;
|
|
1245
|
+
const handle = typeof args[0] === "number" ? args[0] : -1;
|
|
1246
|
+
const retainedHandle = this.retainLockReleaseHandle(handle);
|
|
1247
|
+
arrayAppend(this.pendingLockReleases, { operation, args, retainedHandle });
|
|
1248
|
+
this.flushPendingLockReleases();
|
|
1249
|
+
}
|
|
1250
|
+
private scheduleLockReleaseRetry(): void {
|
|
1251
|
+
if (this.lockReleaseTimer !== undefined || this.disposed || this.revoked()) return;
|
|
1252
|
+
this.lockReleaseTimer = scheduleTimeout(() => {
|
|
1253
|
+
this.lockReleaseTimer = undefined;
|
|
1254
|
+
this.flushPendingLockReleases();
|
|
1255
|
+
}, 1);
|
|
1256
|
+
}
|
|
1257
|
+
private clearLockReleaseQueue(): void {
|
|
1258
|
+
const pending = this.pendingLockReleases.splice(0);
|
|
1259
|
+
for (let i = 0; i < pending.length; i++)
|
|
1260
|
+
this.releaseLockReleaseHandle(pending[i]!.retainedHandle);
|
|
1261
|
+
if (this.lockReleaseTimer !== undefined) {
|
|
1262
|
+
cancelTimeout(this.lockReleaseTimer);
|
|
1263
|
+
this.lockReleaseTimer = undefined;
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
private deliverNextLockRelease(): boolean {
|
|
1267
|
+
const next = this.pendingLockReleases[0]!;
|
|
1268
|
+
let completion: FilesystemCompletion;
|
|
1269
|
+
try {
|
|
1270
|
+
completion = this.submit(next.operation, next.args);
|
|
1271
|
+
} catch (error) {
|
|
1272
|
+
if (this.revoked() || isTerminalLockReleaseErrno(errno(error))) {
|
|
1273
|
+
this.finishNextLockRelease();
|
|
1274
|
+
return true;
|
|
1275
|
+
}
|
|
1276
|
+
this.scheduleLockReleaseRetry();
|
|
1277
|
+
return false;
|
|
1278
|
+
}
|
|
1279
|
+
try {
|
|
1280
|
+
completion.wait();
|
|
1281
|
+
this.finishNextLockRelease();
|
|
1282
|
+
return true;
|
|
1283
|
+
} catch (error) {
|
|
1284
|
+
if (this.revoked() || isTerminalLockReleaseErrno(errno(error))) {
|
|
1285
|
+
this.finishNextLockRelease();
|
|
1286
|
+
return true;
|
|
1287
|
+
}
|
|
1288
|
+
this.scheduleLockReleaseRetry();
|
|
1289
|
+
return false;
|
|
1290
|
+
} finally {
|
|
1291
|
+
completion.release();
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
private flushPendingLockReleases(): void {
|
|
1295
|
+
if (this.flushingLockReleases || this.pendingLockReleases.length === 0) return;
|
|
1296
|
+
if (this.disposed || this.revoked()) {
|
|
1297
|
+
this.clearLockReleaseQueue();
|
|
1298
|
+
return;
|
|
1299
|
+
}
|
|
1300
|
+
this.flushingLockReleases = true;
|
|
1301
|
+
try {
|
|
1302
|
+
while (this.pendingLockReleases.length > 0) {
|
|
1303
|
+
if (this.disposed || this.revoked()) {
|
|
1304
|
+
this.clearLockReleaseQueue();
|
|
1305
|
+
return;
|
|
1306
|
+
}
|
|
1307
|
+
if (!this.deliverNextLockRelease()) return;
|
|
1308
|
+
}
|
|
1309
|
+
} finally {
|
|
1310
|
+
this.flushingLockReleases = false;
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1095
1313
|
call(operation: string, args: unknown[] = [], data?: Uint8Array): any {
|
|
1314
|
+
if (this.pendingLockReleases.length > 0) this.flushPendingLockReleases();
|
|
1096
1315
|
const completion = this.submit(operation, args, data);
|
|
1097
1316
|
try {
|
|
1098
1317
|
return completion.wait();
|
|
@@ -1153,6 +1372,8 @@ export class FilesystemClient implements FileSystem {
|
|
|
1153
1372
|
}
|
|
1154
1373
|
dispose(): void {
|
|
1155
1374
|
if (this.finishedDispose) return;
|
|
1375
|
+
this.flushPendingLockReleases();
|
|
1376
|
+
this.clearLockReleaseQueue();
|
|
1156
1377
|
this.disposed = true;
|
|
1157
1378
|
this.resident?.invalidate();
|
|
1158
1379
|
this.beginWorkspaceStop();
|
|
@@ -2612,6 +2833,9 @@ export class FilesystemClient implements FileSystem {
|
|
|
2612
2833
|
return errno(error);
|
|
2613
2834
|
}
|
|
2614
2835
|
}
|
|
2836
|
+
probe(directory: FileHandle, paths: readonly string[], kind: ProbeKind, rooted = false): number {
|
|
2837
|
+
return this.call("probe", [directory, paths, kind, rooted]);
|
|
2838
|
+
}
|
|
2615
2839
|
readlink(handle: FileHandle): string {
|
|
2616
2840
|
this.retain(handle);
|
|
2617
2841
|
try {
|
package/fs/cold-read-cache.ts
CHANGED
|
@@ -5,6 +5,12 @@ const COLD_READ_CACHE_BYTES = 32 * 1024 * 1024;
|
|
|
5
5
|
/** Charged per page on top of its bytes: an estimate of its map entry, key string and typed
|
|
6
6
|
* array and buffer headers. It keeps many tiny files from exceeding the bound in real heap. */
|
|
7
7
|
export const COLD_PAGE_OVERHEAD = 256;
|
|
8
|
+
/** Pages one storage read fetches for a sequential cold scan: 1 MiB, so a 13 MB binary costs
|
|
9
|
+
* 14 storage reads instead of 210, while its transport still moves one page per request. */
|
|
10
|
+
const READAHEAD_PAGES = 16;
|
|
11
|
+
export const READAHEAD_BYTES = READAHEAD_PAGES * PAGE;
|
|
12
|
+
/** Payloads whose last read end is remembered for sequential detection. */
|
|
13
|
+
const STREAMS = 64;
|
|
8
14
|
|
|
9
15
|
/** Verified bytes of cold payloads, kept by whole aligned page so a file read again while it
|
|
10
16
|
* stays unmodified skips storage and its checksums. A payload key names one immutable stored
|
|
@@ -13,8 +19,39 @@ export const COLD_PAGE_OVERHEAD = 256;
|
|
|
13
19
|
* cache owns private copies, so a caller may transfer or modify what it receives. */
|
|
14
20
|
export class ColdReadCache {
|
|
15
21
|
private readonly pages = new Map<string, Uint8Array>();
|
|
22
|
+
/** Where the last read of each recent payload ended. */
|
|
23
|
+
private readonly ends = new Map<string, number>();
|
|
16
24
|
private bytes = 0;
|
|
17
25
|
constructor(private readonly limit = COLD_READ_CACHE_BYTES) {}
|
|
26
|
+
/** Aligned storage range for a miss at `[offset, offset + count)`. A read that starts exactly
|
|
27
|
+
* where the previous read of this payload ended is a sequential scan, so it fetches a
|
|
28
|
+
* page-aligned readahead window (as Linux page-cache readahead does) up to `READAHEAD_BYTES`
|
|
29
|
+
* and half the cache budget; any other read fetches only what it needs. Aligning the start to a
|
|
30
|
+
* page boundary lets small sequential reads populate their current page too instead of
|
|
31
|
+
* repeatedly missing and refetching the window. */
|
|
32
|
+
readWindow(
|
|
33
|
+
key: string,
|
|
34
|
+
offset: number,
|
|
35
|
+
count: number,
|
|
36
|
+
size: number,
|
|
37
|
+
): { offset: number; length: number } {
|
|
38
|
+
if (this.ends.get(key) !== offset) return { offset, length: count };
|
|
39
|
+
const windowPages = Math.min(
|
|
40
|
+
READAHEAD_PAGES,
|
|
41
|
+
Math.floor(this.limit / 2 / (PAGE + COLD_PAGE_OVERHEAD)),
|
|
42
|
+
);
|
|
43
|
+
if (windowPages < 1) return { offset, length: count };
|
|
44
|
+
const start = Math.floor(offset / PAGE) * PAGE;
|
|
45
|
+
const end = Math.min(size, start + windowPages * PAGE);
|
|
46
|
+
if (end < offset + count) return { offset, length: count };
|
|
47
|
+
return { offset: start, length: end - start };
|
|
48
|
+
}
|
|
49
|
+
/** Record where a read of this payload ended, hit or miss. */
|
|
50
|
+
noteRead(key: string, end: number): void {
|
|
51
|
+
this.ends.delete(key);
|
|
52
|
+
if (this.ends.size >= STREAMS) this.ends.delete(this.ends.keys().next().value!);
|
|
53
|
+
this.ends.set(key, end);
|
|
54
|
+
}
|
|
18
55
|
/** A copy of the range, or undefined unless every page it touches is cached. */
|
|
19
56
|
read(key: string, offset: number, length: number): Uint8Array | undefined {
|
|
20
57
|
const first = Math.floor(offset / PAGE);
|
|
@@ -54,6 +91,7 @@ export class ColdReadCache {
|
|
|
54
91
|
}
|
|
55
92
|
clear(): void {
|
|
56
93
|
this.pages.clear();
|
|
94
|
+
this.ends.clear();
|
|
57
95
|
this.bytes = 0;
|
|
58
96
|
}
|
|
59
97
|
private remove(name: string): void {
|
package/fs/index.ts
CHANGED
|
@@ -37,7 +37,12 @@ export {
|
|
|
37
37
|
} from "./shared.js";
|
|
38
38
|
|
|
39
39
|
export { PersistentBackend } from "./persistent.js";
|
|
40
|
-
export {
|
|
40
|
+
export {
|
|
41
|
+
IndexedDBStorage,
|
|
42
|
+
IndexedDBStorageBusyError,
|
|
43
|
+
IndexedDBStorageMissingError,
|
|
44
|
+
type IndexedDBStorageOptions,
|
|
45
|
+
} from "./indexeddb.js";
|
|
41
46
|
export { OpfsStorage, type OpfsStorageOptions } from "./opfs.js";
|
|
42
47
|
export type { StorageBackend, PersistedIndex, PersistedNode, PersistenceBatch } from "./storage.js";
|
|
43
48
|
|
|
@@ -70,6 +75,22 @@ export {
|
|
|
70
75
|
export { WorkspaceArchiveError } from "./workspace-archive.js";
|
|
71
76
|
|
|
72
77
|
export { FilesystemWatch, type WatchOptions, type WatchEvent, type WatchBatch } from "./watch.js";
|
|
78
|
+
export {
|
|
79
|
+
LOCK_SH,
|
|
80
|
+
LOCK_EX,
|
|
81
|
+
LOCK_NB,
|
|
82
|
+
LOCK_UN,
|
|
83
|
+
F_GETLK,
|
|
84
|
+
F_SETLK,
|
|
85
|
+
F_SETLKW,
|
|
86
|
+
F_OFD_GETLK,
|
|
87
|
+
F_OFD_SETLK,
|
|
88
|
+
F_OFD_SETLKW,
|
|
89
|
+
F_RDLCK,
|
|
90
|
+
F_WRLCK,
|
|
91
|
+
F_UNLCK,
|
|
92
|
+
} from "../core/filesystem.js";
|
|
93
|
+
export { LockTable, LockContentionError, type LockCommit } from "./locks.js";
|
|
73
94
|
|
|
74
95
|
export {
|
|
75
96
|
openWorkspaceRead,
|
package/fs/indexeddb.ts
CHANGED
|
@@ -88,6 +88,13 @@ export class IndexedDBStorageBusyError extends Error {
|
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
+
export class IndexedDBStorageMissingError extends Error {
|
|
92
|
+
override readonly name = "IndexedDBStorageMissingError";
|
|
93
|
+
constructor(message = "IndexedDB storage database does not exist") {
|
|
94
|
+
super(message);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
91
98
|
export interface IndexedDBStorageOptions {
|
|
92
99
|
readonly retainOpeningDebt?: (debt: Promise<void>) => void;
|
|
93
100
|
indexedDB?: IDBFactory;
|
|
@@ -107,6 +114,12 @@ export interface IndexedDBStorageOptions {
|
|
|
107
114
|
expectedEpoch?: number;
|
|
108
115
|
/** Verified under exclusive arbLock before submitting steal:true on the writer lock. */
|
|
109
116
|
verifyStealTarget?: (snapshot: LockManagerSnapshot) => boolean;
|
|
117
|
+
/**
|
|
118
|
+
* When false, opening an absent database aborts the upgrade transaction while holding the
|
|
119
|
+
* maintenance lock and rejects with IndexedDBStorageMissingError without creating the database
|
|
120
|
+
* or allocating epoch 1. Defaults to true.
|
|
121
|
+
*/
|
|
122
|
+
createIfMissing?: boolean;
|
|
110
123
|
}
|
|
111
124
|
/** Retains physical native completion independently of caller-visible startup rejection. */
|
|
112
125
|
class NativeDatabaseOpening {
|
|
@@ -116,6 +129,7 @@ class NativeDatabaseOpening {
|
|
|
116
129
|
private database: IDBDatabase | undefined;
|
|
117
130
|
private closing: Promise<void> | undefined;
|
|
118
131
|
private blocked = false;
|
|
132
|
+
private missing = false;
|
|
119
133
|
private readonly abort = () => this.outcome.reject(this.signal?.reason);
|
|
120
134
|
|
|
121
135
|
constructor(
|
|
@@ -133,10 +147,18 @@ class NativeDatabaseOpening {
|
|
|
133
147
|
this.completion.resolve();
|
|
134
148
|
return;
|
|
135
149
|
}
|
|
150
|
+
const createIfMissing = options.createIfMissing ?? true;
|
|
136
151
|
try {
|
|
137
152
|
const request = factory.open(name, 1);
|
|
138
153
|
signal?.addEventListener("abort", this.abort, { once: true });
|
|
139
|
-
request.onupgradeneeded = () =>
|
|
154
|
+
request.onupgradeneeded = () => {
|
|
155
|
+
if (createIfMissing) {
|
|
156
|
+
createStores(request.result);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
this.missing = true;
|
|
160
|
+
request.transaction?.abort();
|
|
161
|
+
};
|
|
140
162
|
request.onsuccess = () => this.delivered(request.result);
|
|
141
163
|
request.onerror = () => this.failed(request.error);
|
|
142
164
|
request.onblocked = () => this.blockedRequest();
|
|
@@ -153,6 +175,11 @@ class NativeDatabaseOpening {
|
|
|
153
175
|
private delivered(database: IDBDatabase): void {
|
|
154
176
|
this.stopObserving();
|
|
155
177
|
this.database = database;
|
|
178
|
+
if (this.missing) {
|
|
179
|
+
this.outcome.reject(new IndexedDBStorageMissingError());
|
|
180
|
+
void this.retire().catch(() => {});
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
156
183
|
if (this.signal?.aborted || this.blocked) {
|
|
157
184
|
void this.retire().catch(() => {});
|
|
158
185
|
return;
|
|
@@ -165,6 +192,11 @@ class NativeDatabaseOpening {
|
|
|
165
192
|
}
|
|
166
193
|
private failed(error: DOMException | null): void {
|
|
167
194
|
this.stopObserving();
|
|
195
|
+
if (this.missing) {
|
|
196
|
+
this.outcome.reject(new IndexedDBStorageMissingError());
|
|
197
|
+
this.completion.resolve();
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
168
200
|
this.outcome.reject(
|
|
169
201
|
error?.name === "VersionError"
|
|
170
202
|
? new VfsError("INVALID_VFS_FORMAT", "indexeddb", "Incompatible IndexedDB database version")
|
|
@@ -1237,9 +1269,11 @@ export class IndexedDBStorage implements StorageBackend {
|
|
|
1237
1269
|
() => maintenance.release(),
|
|
1238
1270
|
);
|
|
1239
1271
|
} catch (error) {
|
|
1240
|
-
|
|
1272
|
+
const cleanup = Promise.all(debts)
|
|
1241
1273
|
.then(() => maintenance.release())
|
|
1242
1274
|
.catch(() => {});
|
|
1275
|
+
if (error instanceof IndexedDBStorageMissingError) await cleanup;
|
|
1276
|
+
else void cleanup;
|
|
1243
1277
|
throw error;
|
|
1244
1278
|
}
|
|
1245
1279
|
}
|