@waffle-os/runtime 0.0.147 → 0.0.149
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/process-broker.ts +6 -3
- package/browser/run-command.ts +18 -7
- 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/process-broker.js +3 -2
- package/dist/browser/run-command.js +8 -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/proc/registry.js +8 -8
- package/dist/proc/start-command.js +10 -0
- package/dist/proc/supervisor.js +4 -4
- 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/proc/registry.ts +13 -19
- package/proc/start-command.ts +29 -0
- package/proc/supervisor.ts +8 -6
- package/runtime.ts +12 -3
- package/tooling/program/program-transformer.json +1 -1
|
@@ -1,15 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
MemoryBackend,
|
|
3
|
+
type FilesystemBackend,
|
|
4
|
+
type MemoryBackendSharedRoot,
|
|
5
|
+
} from "../fs/backend.js";
|
|
2
6
|
import type { MemoryBudget } from "../fs/memory-budget.js";
|
|
3
7
|
import { PersistentBackend } from "../fs/persistent.js";
|
|
4
8
|
import { IndexedDBStorage } from "../fs/indexeddb.js";
|
|
5
9
|
import { OpfsStorage } from "../fs/opfs.js";
|
|
6
10
|
import type { StorageBackend } from "../fs/storage.js";
|
|
7
11
|
import type { RuntimeStartupObserver } from "../fs/startup.js";
|
|
8
|
-
import { resolveVfs, requireVfsCapabilities } from "../fs/vfs.js";
|
|
12
|
+
import { resolveVfs, requireVfsCapabilities, type VfsWhenBusy } from "../fs/vfs.js";
|
|
9
13
|
|
|
10
14
|
export interface FilesystemBackendOptions {
|
|
11
15
|
readonly mode?: "fast" | "safe" | "memory";
|
|
12
|
-
readonly whenBusy?:
|
|
16
|
+
readonly whenBusy?: VfsWhenBusy;
|
|
13
17
|
readonly name?: string;
|
|
14
18
|
readonly implementation?: "indexeddb" | "opfs-memory" | "memory";
|
|
15
19
|
readonly startupRoot?: MemoryBudget;
|
|
@@ -20,18 +24,35 @@ export interface FilesystemBackendLifecycle {
|
|
|
20
24
|
readonly onStartupStage?: RuntimeStartupObserver;
|
|
21
25
|
readonly signal?: AbortSignal;
|
|
22
26
|
readonly onWaiting?: () => void;
|
|
27
|
+
readonly onState?: (status: "waiting" | "ready") => void;
|
|
23
28
|
readonly onOwnershipLost: () => void;
|
|
29
|
+
readonly onFatalError?: (error: unknown, released: boolean) => void;
|
|
24
30
|
readonly retainStorage: (storage: StorageBackend) => void;
|
|
25
31
|
}
|
|
26
32
|
/** The caller owns failed-open cleanup, including any failed native close. */
|
|
27
33
|
export async function openFilesystemBackend(
|
|
28
34
|
options: FilesystemBackendOptions,
|
|
29
35
|
lifecycle: FilesystemBackendLifecycle,
|
|
30
|
-
): Promise<
|
|
36
|
+
): Promise<FilesystemBackend> {
|
|
31
37
|
const { descriptor, whenBusy } = resolveVfs(options, options.implementation);
|
|
32
|
-
requireVfsCapabilities(descriptor.implementation);
|
|
38
|
+
requireVfsCapabilities(descriptor.implementation, whenBusy);
|
|
33
39
|
if (descriptor.implementation === "memory")
|
|
34
40
|
return new MemoryBackend(undefined, undefined, options.startupRoot, undefined, options.connect);
|
|
41
|
+
if (whenBusy === "attach") {
|
|
42
|
+
const { MultiTabBackend } = await import("../fs/multitab-backend.js");
|
|
43
|
+
return MultiTabBackend.open({
|
|
44
|
+
implementation: descriptor.implementation,
|
|
45
|
+
name: options.name ?? "waffle-workspace",
|
|
46
|
+
startupRoot: options.startupRoot,
|
|
47
|
+
sharedRoot: options.connect,
|
|
48
|
+
onStartupStage: lifecycle.onStartupStage,
|
|
49
|
+
signal: lifecycle.signal,
|
|
50
|
+
onWaiting: lifecycle.onWaiting,
|
|
51
|
+
onState: lifecycle.onState,
|
|
52
|
+
onOwnershipLost: lifecycle.onOwnershipLost,
|
|
53
|
+
onFatalError: lifecycle.onFatalError,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
35
56
|
let backend: PersistentBackend | undefined = undefined;
|
|
36
57
|
let ownershipLost = false;
|
|
37
58
|
const storage =
|
package/browser/fs-worker.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import { installFilesystemWorker } from "../fs/service.js";
|
|
2
2
|
import { openFilesystemWorkerBackend } from "./filesystem-worker-backend.js";
|
|
3
3
|
|
|
4
|
-
installFilesystemWorker(self, (options, onStartupStage, signal, onWaiting) =>
|
|
4
|
+
installFilesystemWorker(self, (options, onStartupStage, signal, onWaiting, callbacks) =>
|
|
5
5
|
openFilesystemWorkerBackend(options, {
|
|
6
6
|
onStartupStage,
|
|
7
7
|
signal,
|
|
8
8
|
onWaiting,
|
|
9
|
+
onState: callbacks?.onState,
|
|
10
|
+
onFatalError: callbacks?.onFatalError,
|
|
9
11
|
onOwnershipLost() {
|
|
10
|
-
|
|
12
|
+
if (callbacks?.onOwnershipLost) callbacks.onOwnershipLost();
|
|
13
|
+
else self.postMessage({ type: "filesystem-ownership-lost" });
|
|
11
14
|
},
|
|
12
15
|
}),
|
|
13
16
|
);
|
|
@@ -97,7 +97,7 @@ import {
|
|
|
97
97
|
import { type FilesystemConnection, type FilesystemIdentity, isRevoked } from "../fs/protocol.js";
|
|
98
98
|
|
|
99
99
|
import type { ProcessSupervisor, RootProcessOptions } from "../proc/supervisor.js";
|
|
100
|
-
import type { ExecutableRegistry, ResolvedExecutable } from "../proc/registry.js";
|
|
100
|
+
import type { CommandPrograms, ExecutableRegistry, ResolvedExecutable } from "../proc/registry.js";
|
|
101
101
|
import type { ProcessIdentityAllocator } from "../proc/identity.js";
|
|
102
102
|
import type { ProcessRecord, ProcessTable } from "../proc/table.js";
|
|
103
103
|
import {
|
|
@@ -463,6 +463,7 @@ function senderRevoked(
|
|
|
463
463
|
|
|
464
464
|
interface CommandRootState {
|
|
465
465
|
readonly root: WorkerProcessTransport;
|
|
466
|
+
readonly programs: CommandPrograms;
|
|
466
467
|
readonly pending: Set<Promise<void>>;
|
|
467
468
|
readonly failures: Map<WorkerProcessTransport, unknown>;
|
|
468
469
|
stopped: boolean;
|
|
@@ -588,8 +589,8 @@ export class BrowserProcessBroker {
|
|
|
588
589
|
(proc) => {
|
|
589
590
|
const supervisor = (this.supervisor = new proc.ProcessSupervisor({
|
|
590
591
|
registry: this.options.registry,
|
|
591
|
-
|
|
592
|
-
rootPid
|
|
592
|
+
commandPrograms: (rootPid) =>
|
|
593
|
+
rootPid === undefined ? undefined : this.commandRoots?.get(rootPid)?.programs,
|
|
593
594
|
table: this.options.table,
|
|
594
595
|
launch: (context) => this.launchPrepared(context),
|
|
595
596
|
launchCommand: (context) => this.launchPrepared(context),
|
|
@@ -1379,6 +1380,7 @@ export class BrowserProcessBroker {
|
|
|
1379
1380
|
async adoptCommandRoot(
|
|
1380
1381
|
transport: WorkerProcessTransport,
|
|
1381
1382
|
options: RuntimeRootOptions,
|
|
1383
|
+
programs: CommandPrograms,
|
|
1382
1384
|
): Promise<void> {
|
|
1383
1385
|
if (this.suspended || !this.supervisor) throw new Error("Process broker is unavailable");
|
|
1384
1386
|
const processCapacity = directoryCapacity(transport.directory);
|
|
@@ -1398,6 +1400,7 @@ export class BrowserProcessBroker {
|
|
|
1398
1400
|
transport.data = { filesystem: options.filesystem, pipeRegistry: registry.handle() };
|
|
1399
1401
|
(this.commandRoots ??= new Map()).set(transport.pid, {
|
|
1400
1402
|
root: transport,
|
|
1403
|
+
programs,
|
|
1401
1404
|
pending: new Set(),
|
|
1402
1405
|
failures: new Map(),
|
|
1403
1406
|
stopped: false,
|
package/browser/run-command.ts
CHANGED
|
@@ -195,12 +195,16 @@ class Command {
|
|
|
195
195
|
let adopted = false;
|
|
196
196
|
let leader: Leader | undefined;
|
|
197
197
|
try {
|
|
198
|
-
await owner.adoptCommandRoot(
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
198
|
+
await owner.adoptCommandRoot(
|
|
199
|
+
root,
|
|
200
|
+
{
|
|
201
|
+
filesystem,
|
|
202
|
+
memory: [],
|
|
203
|
+
tryOutput: (fd, bytes) => this.deliver(fd, bytes),
|
|
204
|
+
stop: () => this.input?.stop(),
|
|
205
|
+
},
|
|
206
|
+
this.options.programs ?? "byte-backed",
|
|
207
|
+
);
|
|
204
208
|
adopted = true;
|
|
205
209
|
if (this.runtime.disposed) return this.disposed();
|
|
206
210
|
if (!this.stopped.aborted) leader = this.spawn(owner, root);
|
|
@@ -357,6 +361,13 @@ class Command {
|
|
|
357
361
|
return bytes.length;
|
|
358
362
|
}
|
|
359
363
|
|
|
364
|
+
/** Reads the leader's outcome once it has one, stopping the rest of its tree when asked to. */
|
|
365
|
+
private leaderExit(leader: Leader): ProcessExit | null {
|
|
366
|
+
const exit = readOutcome(leader.state.outcome);
|
|
367
|
+
if (exit && this.options.onLeaderExit === "stop") this.stop();
|
|
368
|
+
return exit;
|
|
369
|
+
}
|
|
370
|
+
|
|
360
371
|
private async join(owner: BrowserProcessBroker, root: WorkerProcessTransport, leader?: Leader) {
|
|
361
372
|
const wake = workerProcessState(root).wake;
|
|
362
373
|
let exit: ProcessExit | null = null;
|
|
@@ -364,7 +375,7 @@ class Command {
|
|
|
364
375
|
const expected = Atomics.load(wake, 0);
|
|
365
376
|
if (this.runtime.disposed) return this.disposed();
|
|
366
377
|
if (this.stopped.aborted || this.ioFailure) this.stopInput();
|
|
367
|
-
if (leader && !exit) exit =
|
|
378
|
+
if (leader && !exit) exit = this.leaderExit(leader);
|
|
368
379
|
if (owner.checkCommandCleanup(root, !leader || exit !== null)) {
|
|
369
380
|
if (!leader || exit) return exit;
|
|
370
381
|
throw new Error("Command leader retired without an outcome");
|
package/browser/shared-fs.ts
CHANGED
|
@@ -143,7 +143,8 @@ export class SharedFilesystem {
|
|
|
143
143
|
private readonly requestPool: FilesystemRequestPool;
|
|
144
144
|
private clientStopped = false;
|
|
145
145
|
private disposal?: Promise<void>;
|
|
146
|
-
private finishDisposal?: (error?: Error) => void;
|
|
146
|
+
private finishDisposal?: (error?: Error, released?: boolean) => void;
|
|
147
|
+
private _ownershipRetained = false;
|
|
147
148
|
ownershipLost = false;
|
|
148
149
|
onOwnershipLost?: () => void;
|
|
149
150
|
onStateChange?: SharedFilesystemStateObserver;
|
|
@@ -158,6 +159,12 @@ export class SharedFilesystem {
|
|
|
158
159
|
if (projectionBudget !== undefined && options.workspace !== true)
|
|
159
160
|
throw new TypeError("localMemory requires workspace: true");
|
|
160
161
|
const selection = resolveVfs(options, Reflect.get(options, vfsImplementation));
|
|
162
|
+
if (options.workspace === true && selection.whenBusy === "attach")
|
|
163
|
+
throw new VfsError(
|
|
164
|
+
"INVALID_VFS_SETTINGS",
|
|
165
|
+
selection.descriptor.implementation,
|
|
166
|
+
'workspace: true cannot be combined with whenBusy: "attach"',
|
|
167
|
+
);
|
|
161
168
|
this.vfs = selection.descriptor;
|
|
162
169
|
this.onStateChange = options.onStateChange;
|
|
163
170
|
this.cancellableStartup = this.vfs.persistent;
|
|
@@ -229,7 +236,7 @@ export class SharedFilesystem {
|
|
|
229
236
|
name?: string;
|
|
230
237
|
mode: VfsDescriptor["mode"];
|
|
231
238
|
implementation: VfsDescriptor["implementation"];
|
|
232
|
-
whenBusy?: "takeover" | "wait";
|
|
239
|
+
whenBusy?: "takeover" | "wait" | "attach";
|
|
233
240
|
observeStartup?: true;
|
|
234
241
|
} = {
|
|
235
242
|
connect: connection.server,
|
|
@@ -295,12 +302,27 @@ export class SharedFilesystem {
|
|
|
295
302
|
get state(): SharedFilesystemState {
|
|
296
303
|
return this._state;
|
|
297
304
|
}
|
|
305
|
+
/** True when the filesystem worker/storage ownership could not be confirmed released. */
|
|
306
|
+
ownershipRetained(): boolean {
|
|
307
|
+
return this._ownershipRetained;
|
|
308
|
+
}
|
|
309
|
+
/** Latch disposal in a shared-host worker before Runtime.dispose() awaits filesystem.drain(). */
|
|
310
|
+
beginRuntimeDispose(): void {
|
|
311
|
+
if (this.stopped || this.vfs.multiTab !== "shared-host") return;
|
|
312
|
+
try {
|
|
313
|
+
this.worker.postMessage({ beginDispose: true });
|
|
314
|
+
} catch {
|
|
315
|
+
/* worker already terminated */
|
|
316
|
+
}
|
|
317
|
+
}
|
|
298
318
|
private receiveMessage(event: MessageEvent, onStartupStage?: RuntimeStartupObserver): void {
|
|
299
319
|
if (event.data?.type === "filesystem-disposed") {
|
|
300
320
|
if (this.joinedReady() && "error" in event.data && typeof event.data.error !== "string")
|
|
301
321
|
return;
|
|
322
|
+
const released = typeof event.data.released === "boolean" ? event.data.released : undefined;
|
|
302
323
|
this.finishDisposal?.(
|
|
303
324
|
typeof event.data.error === "string" ? new Error(event.data.error) : undefined,
|
|
325
|
+
released,
|
|
304
326
|
);
|
|
305
327
|
return;
|
|
306
328
|
}
|
|
@@ -313,7 +335,8 @@ export class SharedFilesystem {
|
|
|
313
335
|
}
|
|
314
336
|
const error = workerError(event.data);
|
|
315
337
|
if (error) {
|
|
316
|
-
|
|
338
|
+
const released = typeof event.data?.released === "boolean" ? event.data.released : undefined;
|
|
339
|
+
this.fail(error, released);
|
|
317
340
|
return;
|
|
318
341
|
}
|
|
319
342
|
if (this.disposal) return;
|
|
@@ -340,11 +363,23 @@ export class SharedFilesystem {
|
|
|
340
363
|
/* State observers cannot own filesystem startup or teardown. */
|
|
341
364
|
}
|
|
342
365
|
}
|
|
343
|
-
private fail(error: Error): void {
|
|
366
|
+
private fail(error: Error, released?: boolean): void {
|
|
344
367
|
for (const cancel of this.socketAttachments) cancel(error);
|
|
345
368
|
if (this.stopped) return;
|
|
369
|
+
if (released === false) {
|
|
370
|
+
this._ownershipRetained = true;
|
|
371
|
+
this.fatal ??= error;
|
|
372
|
+
if (this.disposal) {
|
|
373
|
+
this.finishDisposal?.(error, false);
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
revokeFilesystemService(this.client.connection);
|
|
377
|
+
this.stopClient();
|
|
378
|
+
this.setState("failed", this.fatal);
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
346
381
|
if (this.disposal && this.joinedReady()) {
|
|
347
|
-
this.finishDisposal?.(error);
|
|
382
|
+
this.finishDisposal?.(error, released);
|
|
348
383
|
return;
|
|
349
384
|
}
|
|
350
385
|
this.fatal ??= error;
|
|
@@ -668,6 +703,7 @@ export class SharedFilesystem {
|
|
|
668
703
|
reject = failed;
|
|
669
704
|
}).then(
|
|
670
705
|
() => {
|
|
706
|
+
this._ownershipRetained = false;
|
|
671
707
|
this.stop();
|
|
672
708
|
this.requestPool.releaseRetired();
|
|
673
709
|
this.setState("disposed");
|
|
@@ -682,22 +718,49 @@ export class SharedFilesystem {
|
|
|
682
718
|
void this.disposal.catch(() => {});
|
|
683
719
|
revokeFilesystemService(this.client.connection);
|
|
684
720
|
const quiescence = waitForFilesystemQuiescence(this.client.connection);
|
|
685
|
-
void quiescence.catch(
|
|
686
|
-
|
|
721
|
+
void quiescence.catch((reason) => {
|
|
722
|
+
this._ownershipRetained = true;
|
|
723
|
+
reject(reason);
|
|
724
|
+
});
|
|
725
|
+
this.finishDisposal = (error, released) => {
|
|
687
726
|
if (error) {
|
|
727
|
+
if (released === true) {
|
|
728
|
+
void quiescence.then(
|
|
729
|
+
() => {
|
|
730
|
+
this._ownershipRetained = false;
|
|
731
|
+
this.stop();
|
|
732
|
+
this.requestPool.releaseRetired();
|
|
733
|
+
reject(error);
|
|
734
|
+
},
|
|
735
|
+
(fenceError) => {
|
|
736
|
+
this._ownershipRetained = true;
|
|
737
|
+
reject(fenceError);
|
|
738
|
+
},
|
|
739
|
+
);
|
|
740
|
+
return;
|
|
741
|
+
}
|
|
742
|
+
this._ownershipRetained = true;
|
|
688
743
|
reject(error);
|
|
689
744
|
return;
|
|
690
745
|
}
|
|
691
746
|
// Do not adopt a pending fence: a later failure must still reject this owner.
|
|
692
|
-
void quiescence.then(
|
|
747
|
+
void quiescence.then(
|
|
748
|
+
() => resolve(),
|
|
749
|
+
(fenceError) => {
|
|
750
|
+
this._ownershipRetained = true;
|
|
751
|
+
reject(fenceError);
|
|
752
|
+
},
|
|
753
|
+
);
|
|
693
754
|
};
|
|
694
755
|
if (failure) {
|
|
756
|
+
this._ownershipRetained = true;
|
|
695
757
|
reject(failure);
|
|
696
758
|
return this.disposal;
|
|
697
759
|
}
|
|
698
760
|
try {
|
|
699
761
|
this.worker.postMessage({ dispose: true });
|
|
700
762
|
} catch (error) {
|
|
763
|
+
this._ownershipRetained = true;
|
|
701
764
|
reject(error);
|
|
702
765
|
}
|
|
703
766
|
return this.disposal;
|
|
@@ -707,12 +770,32 @@ export class SharedFilesystem {
|
|
|
707
770
|
dispose(): Promise<void> {
|
|
708
771
|
for (const cancel of this.socketAttachments) cancel(new FilesystemError(11));
|
|
709
772
|
if (this.disposal) return this.disposal;
|
|
773
|
+
if (this._ownershipRetained) {
|
|
774
|
+
revokeFilesystemService(this.client.connection);
|
|
775
|
+
this.stopClient();
|
|
776
|
+
const error = this.fatal ?? new Error("Filesystem storage ownership is retained");
|
|
777
|
+
this.disposal = Promise.reject(error);
|
|
778
|
+
void this.disposal.catch(() => {});
|
|
779
|
+
return this.disposal;
|
|
780
|
+
}
|
|
710
781
|
if (this.joinedReady() && !this.stopped) return this.disposeReady(this.fatal);
|
|
711
|
-
let finish!: (error?: Error) => void;
|
|
782
|
+
let finish!: (error?: Error, released?: boolean) => void;
|
|
712
783
|
this.disposal = new Promise<void>((resolve, reject) => {
|
|
713
|
-
finish = (error) => {
|
|
784
|
+
finish = (error, released) => {
|
|
785
|
+
if (released === false) {
|
|
786
|
+
this._ownershipRetained = true;
|
|
787
|
+
this.stopClient();
|
|
788
|
+
const failure = error ?? new Error("Filesystem storage ownership is retained");
|
|
789
|
+
this.fatal ??= failure;
|
|
790
|
+
this.setState("failed", this.fatal);
|
|
791
|
+
reject(failure);
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
794
|
+
this._ownershipRetained = false;
|
|
714
795
|
this.stop();
|
|
715
796
|
if (error) {
|
|
797
|
+
this.fatal ??= error;
|
|
798
|
+
this.setState("failed", this.fatal);
|
|
716
799
|
reject(error);
|
|
717
800
|
return;
|
|
718
801
|
}
|
package/core/filesystem.ts
CHANGED
|
@@ -155,6 +155,8 @@ export interface WatchFileSystem {
|
|
|
155
155
|
/** The root's watch cells. Index 3 counts committed watch records. */
|
|
156
156
|
readonly cells: Int32Array;
|
|
157
157
|
readonly revoked: boolean;
|
|
158
|
+
/** True when watches are served remotely (attach mode); disables local-generation short-circuit. */
|
|
159
|
+
readonly remote?: boolean;
|
|
158
160
|
/** Throws FilesystemError. */
|
|
159
161
|
open(handle: FileHandle, recursive: boolean): number;
|
|
160
162
|
read(id: number): WatchBatch;
|
|
@@ -1,13 +1,30 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
MemoryBackend
|
|
3
|
+
} from "../fs/backend.js";
|
|
2
4
|
import { PersistentBackend } from "../fs/persistent.js";
|
|
3
5
|
import { IndexedDBStorage } from "../fs/indexeddb.js";
|
|
4
6
|
import { OpfsStorage } from "../fs/opfs.js";
|
|
5
7
|
import { resolveVfs, requireVfsCapabilities } from "../fs/vfs.js";
|
|
6
8
|
export async function openFilesystemBackend(options, lifecycle) {
|
|
7
9
|
const { descriptor, whenBusy } = resolveVfs(options, options.implementation);
|
|
8
|
-
requireVfsCapabilities(descriptor.implementation);
|
|
10
|
+
requireVfsCapabilities(descriptor.implementation, whenBusy);
|
|
9
11
|
if (descriptor.implementation === "memory")
|
|
10
12
|
return new MemoryBackend(undefined, undefined, options.startupRoot, undefined, options.connect);
|
|
13
|
+
if (whenBusy === "attach") {
|
|
14
|
+
const { MultiTabBackend } = await import("../fs/multitab-backend.js");
|
|
15
|
+
return MultiTabBackend.open({
|
|
16
|
+
implementation: descriptor.implementation,
|
|
17
|
+
name: options.name ?? "waffle-workspace",
|
|
18
|
+
startupRoot: options.startupRoot,
|
|
19
|
+
sharedRoot: options.connect,
|
|
20
|
+
onStartupStage: lifecycle.onStartupStage,
|
|
21
|
+
signal: lifecycle.signal,
|
|
22
|
+
onWaiting: lifecycle.onWaiting,
|
|
23
|
+
onState: lifecycle.onState,
|
|
24
|
+
onOwnershipLost: lifecycle.onOwnershipLost,
|
|
25
|
+
onFatalError: lifecycle.onFatalError
|
|
26
|
+
});
|
|
27
|
+
}
|
|
11
28
|
let backend = undefined;
|
|
12
29
|
let ownershipLost = false;
|
|
13
30
|
const storage = descriptor.implementation === "opfs-memory" ? await OpfsStorage.open(options.name ?? "waffle-workspace", lifecycle) : await IndexedDBStorage.open(options.name ?? "waffle-workspace", {
|
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import { installFilesystemWorker } from "../fs/service.js";
|
|
2
2
|
import { openFilesystemWorkerBackend } from "./filesystem-worker-backend.js";
|
|
3
|
-
installFilesystemWorker(self, (options, onStartupStage, signal, onWaiting) => openFilesystemWorkerBackend(options, {
|
|
3
|
+
installFilesystemWorker(self, (options, onStartupStage, signal, onWaiting, callbacks) => openFilesystemWorkerBackend(options, {
|
|
4
4
|
onStartupStage,
|
|
5
5
|
signal,
|
|
6
6
|
onWaiting,
|
|
7
|
+
onState: callbacks?.onState,
|
|
8
|
+
onFatalError: callbacks?.onFatalError,
|
|
7
9
|
onOwnershipLost() {
|
|
8
|
-
|
|
10
|
+
if (callbacks?.onOwnershipLost)
|
|
11
|
+
callbacks.onOwnershipLost();
|
|
12
|
+
else
|
|
13
|
+
self.postMessage({ type: "filesystem-ownership-lost" });
|
|
9
14
|
}
|
|
10
15
|
}));
|
|
@@ -256,7 +256,7 @@ export class BrowserProcessBroker {
|
|
|
256
256
|
return this.activation ??= (this.options.activate?.() ?? import("../proc/index.js")).then((proc) => {
|
|
257
257
|
const supervisor = this.supervisor = new proc.ProcessSupervisor({
|
|
258
258
|
registry: this.options.registry,
|
|
259
|
-
|
|
259
|
+
commandPrograms: (rootPid) => rootPid === undefined ? undefined : this.commandRoots?.get(rootPid)?.programs,
|
|
260
260
|
table: this.options.table,
|
|
261
261
|
launch: (context) => this.launchPrepared(context),
|
|
262
262
|
launchCommand: (context) => this.launchPrepared(context),
|
|
@@ -920,7 +920,7 @@ export class BrowserProcessBroker {
|
|
|
920
920
|
this.activateRuntimeRoot(supervisor, transport);
|
|
921
921
|
}
|
|
922
922
|
}
|
|
923
|
-
async adoptCommandRoot(transport, options) {
|
|
923
|
+
async adoptCommandRoot(transport, options, programs) {
|
|
924
924
|
if (this.suspended || !this.supervisor)
|
|
925
925
|
throw new Error("Process broker is unavailable");
|
|
926
926
|
const processCapacity = directoryCapacity(transport.directory);
|
|
@@ -939,6 +939,7 @@ export class BrowserProcessBroker {
|
|
|
939
939
|
transport.data = { filesystem: options.filesystem, pipeRegistry: registry.handle() };
|
|
940
940
|
(this.commandRoots ??= new Map).set(transport.pid, {
|
|
941
941
|
root: transport,
|
|
942
|
+
programs,
|
|
942
943
|
pending: new Set,
|
|
943
944
|
failures: new Map,
|
|
944
945
|
stopped: false
|
|
@@ -197,7 +197,7 @@ class Command {
|
|
|
197
197
|
memory: [],
|
|
198
198
|
tryOutput: (fd, bytes) => this.deliver(fd, bytes),
|
|
199
199
|
stop: () => this.input?.stop()
|
|
200
|
-
});
|
|
200
|
+
}, this.options.programs ?? "byte-backed");
|
|
201
201
|
adopted = true;
|
|
202
202
|
if (this.runtime.disposed)
|
|
203
203
|
return this.disposed();
|
|
@@ -348,6 +348,12 @@ class Command {
|
|
|
348
348
|
}
|
|
349
349
|
return bytes.length;
|
|
350
350
|
}
|
|
351
|
+
leaderExit(leader) {
|
|
352
|
+
const exit = readOutcome(leader.state.outcome);
|
|
353
|
+
if (exit && this.options.onLeaderExit === "stop")
|
|
354
|
+
this.stop();
|
|
355
|
+
return exit;
|
|
356
|
+
}
|
|
351
357
|
async join(owner, root, leader) {
|
|
352
358
|
const wake = workerProcessState(root).wake;
|
|
353
359
|
let exit = null;
|
|
@@ -358,7 +364,7 @@ class Command {
|
|
|
358
364
|
if (this.stopped.aborted || this.ioFailure)
|
|
359
365
|
this.stopInput();
|
|
360
366
|
if (leader && !exit)
|
|
361
|
-
exit =
|
|
367
|
+
exit = this.leaderExit(leader);
|
|
362
368
|
if (owner.checkCommandCleanup(root, !leader || exit !== null)) {
|
|
363
369
|
if (!leader || exit)
|
|
364
370
|
return exit;
|
|
@@ -75,6 +75,7 @@ export class SharedFilesystem {
|
|
|
75
75
|
clientStopped = false;
|
|
76
76
|
disposal;
|
|
77
77
|
finishDisposal;
|
|
78
|
+
_ownershipRetained = false;
|
|
78
79
|
ownershipLost = false;
|
|
79
80
|
onOwnershipLost;
|
|
80
81
|
onStateChange;
|
|
@@ -85,6 +86,8 @@ export class SharedFilesystem {
|
|
|
85
86
|
if (projectionBudget !== undefined && options.workspace !== true)
|
|
86
87
|
throw new TypeError("localMemory requires workspace: true");
|
|
87
88
|
const selection = resolveVfs(options, Reflect.get(options, vfsImplementation));
|
|
89
|
+
if (options.workspace === true && selection.whenBusy === "attach")
|
|
90
|
+
throw new VfsError("INVALID_VFS_SETTINGS", selection.descriptor.implementation, 'workspace: true cannot be combined with whenBusy: "attach"');
|
|
88
91
|
this.vfs = selection.descriptor;
|
|
89
92
|
this.onStateChange = options.onStateChange;
|
|
90
93
|
this.cancellableStartup = this.vfs.persistent;
|
|
@@ -183,11 +186,22 @@ export class SharedFilesystem {
|
|
|
183
186
|
get state() {
|
|
184
187
|
return this._state;
|
|
185
188
|
}
|
|
189
|
+
ownershipRetained() {
|
|
190
|
+
return this._ownershipRetained;
|
|
191
|
+
}
|
|
192
|
+
beginRuntimeDispose() {
|
|
193
|
+
if (this.stopped || this.vfs.multiTab !== "shared-host")
|
|
194
|
+
return;
|
|
195
|
+
try {
|
|
196
|
+
this.worker.postMessage({ beginDispose: true });
|
|
197
|
+
} catch {}
|
|
198
|
+
}
|
|
186
199
|
receiveMessage(event, onStartupStage) {
|
|
187
200
|
if (event.data?.type === "filesystem-disposed") {
|
|
188
201
|
if (this.joinedReady() && "error" in event.data && typeof event.data.error !== "string")
|
|
189
202
|
return;
|
|
190
|
-
|
|
203
|
+
const released = typeof event.data.released === "boolean" ? event.data.released : undefined;
|
|
204
|
+
this.finishDisposal?.(typeof event.data.error === "string" ? new Error(event.data.error) : undefined, released);
|
|
191
205
|
return;
|
|
192
206
|
}
|
|
193
207
|
if (this.stopped || this.disposal && !this.joinedReady())
|
|
@@ -200,7 +214,8 @@ export class SharedFilesystem {
|
|
|
200
214
|
}
|
|
201
215
|
const error = workerError(event.data);
|
|
202
216
|
if (error) {
|
|
203
|
-
|
|
217
|
+
const released = typeof event.data?.released === "boolean" ? event.data.released : undefined;
|
|
218
|
+
this.fail(error, released);
|
|
204
219
|
return;
|
|
205
220
|
}
|
|
206
221
|
if (this.disposal)
|
|
@@ -226,13 +241,25 @@ export class SharedFilesystem {
|
|
|
226
241
|
Promise.resolve(pending).catch(() => {});
|
|
227
242
|
} catch {}
|
|
228
243
|
}
|
|
229
|
-
fail(error) {
|
|
244
|
+
fail(error, released) {
|
|
230
245
|
for (const cancel of this.socketAttachments)
|
|
231
246
|
cancel(error);
|
|
232
247
|
if (this.stopped)
|
|
233
248
|
return;
|
|
249
|
+
if (released === false) {
|
|
250
|
+
this._ownershipRetained = true;
|
|
251
|
+
this.fatal ??= error;
|
|
252
|
+
if (this.disposal) {
|
|
253
|
+
this.finishDisposal?.(error, false);
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
revokeFilesystemService(this.client.connection);
|
|
257
|
+
this.stopClient();
|
|
258
|
+
this.setState("failed", this.fatal);
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
234
261
|
if (this.disposal && this.joinedReady()) {
|
|
235
|
-
this.finishDisposal?.(error);
|
|
262
|
+
this.finishDisposal?.(error, released);
|
|
236
263
|
return;
|
|
237
264
|
}
|
|
238
265
|
this.fatal ??= error;
|
|
@@ -498,6 +525,7 @@ export class SharedFilesystem {
|
|
|
498
525
|
resolve = done;
|
|
499
526
|
reject = failed;
|
|
500
527
|
}).then(() => {
|
|
528
|
+
this._ownershipRetained = false;
|
|
501
529
|
this.stop();
|
|
502
530
|
this.requestPool.releaseRetired();
|
|
503
531
|
this.setState("disposed");
|
|
@@ -509,21 +537,42 @@ export class SharedFilesystem {
|
|
|
509
537
|
this.disposal.catch(() => {});
|
|
510
538
|
revokeFilesystemService(this.client.connection);
|
|
511
539
|
const quiescence = waitForFilesystemQuiescence(this.client.connection);
|
|
512
|
-
quiescence.catch(
|
|
513
|
-
|
|
540
|
+
quiescence.catch((reason) => {
|
|
541
|
+
this._ownershipRetained = true;
|
|
542
|
+
reject(reason);
|
|
543
|
+
});
|
|
544
|
+
this.finishDisposal = (error, released) => {
|
|
514
545
|
if (error) {
|
|
546
|
+
if (released === true) {
|
|
547
|
+
quiescence.then(() => {
|
|
548
|
+
this._ownershipRetained = false;
|
|
549
|
+
this.stop();
|
|
550
|
+
this.requestPool.releaseRetired();
|
|
551
|
+
reject(error);
|
|
552
|
+
}, (fenceError) => {
|
|
553
|
+
this._ownershipRetained = true;
|
|
554
|
+
reject(fenceError);
|
|
555
|
+
});
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
this._ownershipRetained = true;
|
|
515
559
|
reject(error);
|
|
516
560
|
return;
|
|
517
561
|
}
|
|
518
|
-
quiescence.then(() => resolve(),
|
|
562
|
+
quiescence.then(() => resolve(), (fenceError) => {
|
|
563
|
+
this._ownershipRetained = true;
|
|
564
|
+
reject(fenceError);
|
|
565
|
+
});
|
|
519
566
|
};
|
|
520
567
|
if (failure) {
|
|
568
|
+
this._ownershipRetained = true;
|
|
521
569
|
reject(failure);
|
|
522
570
|
return this.disposal;
|
|
523
571
|
}
|
|
524
572
|
try {
|
|
525
573
|
this.worker.postMessage({ dispose: true });
|
|
526
574
|
} catch (error) {
|
|
575
|
+
this._ownershipRetained = true;
|
|
527
576
|
reject(error);
|
|
528
577
|
}
|
|
529
578
|
return this.disposal;
|
|
@@ -533,13 +582,33 @@ export class SharedFilesystem {
|
|
|
533
582
|
cancel(new FilesystemError(11));
|
|
534
583
|
if (this.disposal)
|
|
535
584
|
return this.disposal;
|
|
585
|
+
if (this._ownershipRetained) {
|
|
586
|
+
revokeFilesystemService(this.client.connection);
|
|
587
|
+
this.stopClient();
|
|
588
|
+
const error = this.fatal ?? new Error("Filesystem storage ownership is retained");
|
|
589
|
+
this.disposal = Promise.reject(error);
|
|
590
|
+
this.disposal.catch(() => {});
|
|
591
|
+
return this.disposal;
|
|
592
|
+
}
|
|
536
593
|
if (this.joinedReady() && !this.stopped)
|
|
537
594
|
return this.disposeReady(this.fatal);
|
|
538
595
|
let finish;
|
|
539
596
|
this.disposal = new Promise((resolve, reject) => {
|
|
540
|
-
finish = (error) => {
|
|
597
|
+
finish = (error, released) => {
|
|
598
|
+
if (released === false) {
|
|
599
|
+
this._ownershipRetained = true;
|
|
600
|
+
this.stopClient();
|
|
601
|
+
const failure = error ?? new Error("Filesystem storage ownership is retained");
|
|
602
|
+
this.fatal ??= failure;
|
|
603
|
+
this.setState("failed", this.fatal);
|
|
604
|
+
reject(failure);
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
this._ownershipRetained = false;
|
|
541
608
|
this.stop();
|
|
542
609
|
if (error) {
|
|
610
|
+
this.fatal ??= error;
|
|
611
|
+
this.setState("failed", this.fatal);
|
|
543
612
|
reject(error);
|
|
544
613
|
return;
|
|
545
614
|
}
|