@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.
Files changed (48) hide show
  1. package/browser/filesystem-backend.ts +26 -5
  2. package/browser/fs-worker.ts +5 -2
  3. package/browser/shared-fs.ts +93 -10
  4. package/core/filesystem.ts +2 -0
  5. package/dist/browser/filesystem-backend.js +19 -2
  6. package/dist/browser/fs-worker.js +7 -2
  7. package/dist/browser/shared-fs.js +77 -8
  8. package/dist/fs/client.js +82 -21
  9. package/dist/fs/directory-pages.js +22 -7
  10. package/dist/fs/guest-watch.js +9 -3
  11. package/dist/fs/idle-database.js +1 -1
  12. package/dist/fs/idle-owner.js +35 -4
  13. package/dist/fs/indexeddb.js +322 -63
  14. package/dist/fs/memory.js +18 -9
  15. package/dist/fs/multitab-backend.js +5065 -0
  16. package/dist/fs/multitab-protocol.js +301 -0
  17. package/dist/fs/opfs.js +21 -0
  18. package/dist/fs/persistent.js +70 -1
  19. package/dist/fs/protocol.js +1 -1
  20. package/dist/fs/service.js +266 -86
  21. package/dist/fs/shared.js +45 -0
  22. package/dist/fs/vfs.js +26 -8
  23. package/dist/fs/watch-ring.js +7 -3
  24. package/dist/proc/command-stdio.js +56 -1
  25. package/dist/runtime.js +4 -2
  26. package/dist/tooling/program/program-transformer.json +1 -1
  27. package/fs/backend.ts +36 -0
  28. package/fs/client.ts +81 -19
  29. package/fs/directory-pages.ts +40 -6
  30. package/fs/guest-watch.ts +8 -3
  31. package/fs/idle-database.ts +1 -0
  32. package/fs/idle-owner.ts +48 -12
  33. package/fs/indexeddb.ts +396 -78
  34. package/fs/memory.ts +33 -9
  35. package/fs/multitab-backend.ts +6870 -0
  36. package/fs/multitab-protocol.ts +655 -0
  37. package/fs/opfs.ts +21 -0
  38. package/fs/persistent.ts +70 -1
  39. package/fs/protocol.ts +1 -1
  40. package/fs/service.ts +314 -109
  41. package/fs/shared.ts +45 -0
  42. package/fs/storage.ts +3 -0
  43. package/fs/vfs.ts +44 -15
  44. package/fs/watch-ring.ts +5 -2
  45. package/package.json +1 -1
  46. package/proc/command-stdio.ts +72 -2
  47. package/runtime.ts +12 -3
  48. package/tooling/program/program-transformer.json +1 -1
package/fs/persistent.ts CHANGED
@@ -56,6 +56,10 @@ function copyTimes(node: Node, stat: FileStat): void {
56
56
  export class PersistentBackend extends MemoryBackend {
57
57
  private readonly pressure: CheckpointPressure;
58
58
  private nodes = new Map<number, Node>();
59
+ private byFileId = new Map<bigint, Node>();
60
+ private readonly initialWorkspaceId: string;
61
+ private readonly initialEra: bigint;
62
+ private readonly loadedIndex: boolean;
59
63
  private detached = new WeakSet<Node>();
60
64
  private links = new Map<number, number>();
61
65
  private removed = new Set<number>();
@@ -91,11 +95,15 @@ export class PersistentBackend extends MemoryBackend {
91
95
  sharedRoot?: MemoryBackendSharedRoot,
92
96
  ) {
93
97
  super(fs, reservation, startupRoot, undefined, sharedRoot);
98
+ this.initialWorkspaceId = reservation.workspace;
99
+ this.initialEra = reservation.era;
100
+ this.loadedIndex = index !== undefined;
94
101
  this.pressure = new CheckpointPressure(checkpointLimits);
95
102
  const visit = (node: Node) => {
96
103
  this.links.set(node.ino, (this.links.get(node.ino) ?? 0) + 1);
97
104
  if (this.nodes.has(node.ino)) return;
98
105
  this.nodes.set(node.ino, node);
106
+ if (node.identity) this.byFileId.set(node.identity.id, node);
99
107
  if (node.kind === "dir") for (const child of node.children.values()) visit(child);
100
108
  };
101
109
  visit(fs.root);
@@ -112,11 +120,46 @@ export class PersistentBackend extends MemoryBackend {
112
120
  fs.onChange = (node) => {
113
121
  if (!this.detached.has(node)) {
114
122
  this.nodes.set(node.ino, node);
123
+ if (node.identity) this.byFileId.set(node.identity.id, node);
115
124
  this.dirty.add(node);
116
125
  }
117
126
  };
118
127
  this.startTimer(intervalMs);
119
128
  }
129
+ get initialReservationEra(): bigint {
130
+ return this.initialEra;
131
+ }
132
+ get hasLoadedIndex(): boolean {
133
+ return this.loadedIndex;
134
+ }
135
+ async verifyStorageEpoch(signal?: AbortSignal): Promise<boolean> {
136
+ if ("verifyEpoch" in this.storage && typeof this.storage.verifyEpoch === "function") {
137
+ return (this.storage as { verifyEpoch(s?: AbortSignal): Promise<boolean> }).verifyEpoch(
138
+ signal,
139
+ );
140
+ }
141
+ return true;
142
+ }
143
+ nodeByFileId(fileId: bigint): Node | undefined {
144
+ const node = this.byFileId.get(fileId);
145
+ if (node && node.identity?.id === fileId) return node;
146
+ return undefined;
147
+ }
148
+ resolveNodePublic(path: string): Node | undefined {
149
+ return this.resolveNode(path);
150
+ }
151
+ ensureLoadedHandle(handle: FileHandle): void | Promise<void> {
152
+ return this.ensureLoaded(handle);
153
+ }
154
+ isTerminallyFailed(): boolean {
155
+ return this.storage.isTerminallyFailed?.() ?? false;
156
+ }
157
+ terminalFailureReason(): unknown {
158
+ return this.storage.terminalFailureReason?.();
159
+ }
160
+ setTerminalFailureListener(listener: (reason: unknown) => void): void {
161
+ this.storage.setTerminalFailureListener?.(listener);
162
+ }
120
163
  private startTimer(intervalMs: number): void {
121
164
  if (intervalMs > 0)
122
165
  this.timer = setInterval(() => {
@@ -131,11 +174,15 @@ export class PersistentBackend extends MemoryBackend {
131
174
  dirtyLimits?: Partial<DirtyLimits> | false;
132
175
  startupRoot?: MemoryBudget;
133
176
  sharedRoot?: MemoryBackendSharedRoot;
177
+ signal?: AbortSignal;
134
178
  } = {},
135
179
  ): Promise<PersistentBackend> {
180
+ options.signal?.throwIfAborted();
136
181
  const index = await storage.load();
182
+ options.signal?.throwIfAborted();
137
183
  if (index) validatePersistedIndex(index);
138
184
  const reservation = await storage.reserveIdentity(index?.nodes[0]?.identity?.workspace);
185
+ options.signal?.throwIfAborted();
139
186
  validateReservation(index, reservation);
140
187
  if (!index)
141
188
  return new PersistentBackend(
@@ -166,9 +213,11 @@ export class PersistentBackend extends MemoryBackend {
166
213
  try {
167
214
  if (!index.nodes[0]?.identity) {
168
215
  await storage.flush();
216
+ options.signal?.throwIfAborted();
169
217
  for (const node of backend.nodes.values()) backend.dirty.add(node);
170
- await backend.flush();
218
+ await backend.flush(undefined, options.signal);
171
219
  }
220
+ options.signal?.throwIfAborted();
172
221
  backend.startTimer(options.intervalMs ?? 50);
173
222
  return backend;
174
223
  } catch (error) {
@@ -183,6 +232,7 @@ export class PersistentBackend extends MemoryBackend {
183
232
  else this.links.delete(node.ino);
184
233
  if (delta === 1 && previous === 0) {
185
234
  this.nodes.set(node.ino, node);
235
+ if (node.identity) this.byFileId.set(node.identity.id, node);
186
236
  this.detached.delete(node);
187
237
  const key = this.detachedKeys.get(node.ino);
188
238
  if (key) {
@@ -207,6 +257,7 @@ export class PersistentBackend extends MemoryBackend {
207
257
  }
208
258
  }
209
259
  protected identityChanged(node: Node): void {
260
+ if (node.identity) this.byFileId.set(node.identity.id, node);
210
261
  if (this.isLinked(node)) this.dirty.add(node);
211
262
  }
212
263
  protected isLinked(node: Node): boolean {
@@ -945,6 +996,24 @@ export class PersistentBackend extends MemoryBackend {
945
996
  this.coldReads.clear();
946
997
  }
947
998
  }
999
+ /** Retire persistence without attempting a final checkpoint, joining all admitted work and
1000
+ * closing storage + watch account cleanly before any shared-memory remount reset. */
1001
+ async retireFenced(options: { throwOnStorageCloseError?: boolean } = {}): Promise<void> {
1002
+ this.stopPersistence();
1003
+ const work = Array.from(
1004
+ new Set(Array.from(this.admittedWork).concat([this.lastCommit, this.tail])),
1005
+ );
1006
+ await Promise.allSettled(work);
1007
+ this.coldReads.clear();
1008
+ let closeFailure: { failed: true; reason: unknown } | undefined;
1009
+ try {
1010
+ await this.storage.close();
1011
+ } catch (reason) {
1012
+ if (options.throwOnStorageCloseError) closeFailure = { failed: true, reason };
1013
+ }
1014
+ this.watchAccount.close();
1015
+ if (closeFailure) throw closeFailure.reason;
1016
+ }
948
1017
  close(): Promise<void> {
949
1018
  if (this.closing) return this.closing;
950
1019
  const owner = Promise.withResolvers<void>();
package/fs/protocol.ts CHANGED
@@ -406,7 +406,7 @@ export function createFilesystemConnection(
406
406
  /** The service lifetime is shared even by processes with independent parent lifetimes. */
407
407
  export function revokeFilesystemService(connection: FilesystemIdentity): void {
408
408
  Atomics.store(new Int32Array(connection.ownerStates), 0, 1);
409
- wakeFilesystemWatch(connection.shared);
409
+ wakeFilesystemWatch(connection.shared, true);
410
410
  wake(connection);
411
411
  }
412
412
  export function revokeFilesystem(connection: FilesystemIdentity): void {