@waffle-os/runtime 0.0.152 → 0.0.154

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 (56) hide show
  1. package/browser/net/helper.ts +114 -19
  2. package/browser/net/ring.ts +18 -6
  3. package/browser/net/runtime.ts +2 -2
  4. package/browser/notification-relay.ts +105 -18
  5. package/browser/notification-wait.ts +45 -4
  6. package/browser/notification-worker.ts +169 -76
  7. package/browser/preview.ts +15 -2
  8. package/browser/run-command.ts +14 -8
  9. package/browser/runtime-terminals.ts +26 -0
  10. package/browser/terminal-output.ts +3 -1
  11. package/core/filesystem.ts +4 -0
  12. package/core/wasi.ts +49 -0
  13. package/dist/browser/net/helper.js +83 -18
  14. package/dist/browser/net/ring.js +6 -5
  15. package/dist/browser/net/runtime.js +2 -2
  16. package/dist/browser/notification-relay.js +78 -15
  17. package/dist/browser/notification-wait.js +37 -5
  18. package/dist/browser/notification-worker.js +146 -67
  19. package/dist/browser/run-command.js +11 -7
  20. package/dist/browser/runtime-terminals.js +16 -0
  21. package/dist/browser/terminal-output.js +2 -2
  22. package/dist/core/wasi.js +40 -0
  23. package/dist/fs/backend.js +7 -0
  24. package/dist/fs/client.js +69 -15
  25. package/dist/fs/dirty-admission.js +15 -8
  26. package/dist/fs/indexeddb.js +288 -96
  27. package/dist/fs/memory.js +106 -20
  28. package/dist/fs/multitab-backend.js +121 -37
  29. package/dist/fs/multitab-protocol.js +1 -0
  30. package/dist/fs/persistent.js +70 -16
  31. package/dist/fs/protocol.js +15 -7
  32. package/dist/fs/publication-attachment.js +9 -7
  33. package/dist/fs/service.js +34 -9
  34. package/dist/fs/shared.js +96 -6
  35. package/dist/proc/command-stdio.js +1 -1
  36. package/dist/proc/start-command.js +4 -0
  37. package/dist/tooling/program/program-transformer.json +1 -1
  38. package/fs/backend.ts +11 -0
  39. package/fs/client.ts +93 -16
  40. package/fs/dirty-admission.ts +22 -7
  41. package/fs/guest-watch.ts +1 -1
  42. package/fs/indexeddb.ts +379 -110
  43. package/fs/memory.ts +120 -19
  44. package/fs/multitab-backend.ts +206 -61
  45. package/fs/multitab-protocol.ts +1 -0
  46. package/fs/persistent.ts +82 -16
  47. package/fs/protocol.ts +26 -7
  48. package/fs/publication-attachment.ts +9 -7
  49. package/fs/service.ts +35 -7
  50. package/fs/shared.ts +106 -8
  51. package/package.json +1 -1
  52. package/proc/command-stdio.ts +1 -1
  53. package/proc/start-command.ts +9 -0
  54. package/testing/idle-workspace-cancellation-browser.ts +22 -13
  55. package/testing/indexeddb-storage-browser.ts +435 -1
  56. package/tooling/program/program-transformer.json +1 -1
package/fs/memory.ts CHANGED
@@ -92,9 +92,14 @@ export function nodeSize(node: Node): number {
92
92
  }
93
93
  interface MemoryPath extends ResolvedPath {
94
94
  readonly canonicalPath: string;
95
+ readonly base: PathBase;
95
96
  }
97
+ /** What a walk's `canonicalPath` is relative to: the start directory, the rooted walk's root
98
+ * (after an absolute link target), or neither (a rooted `..` climbed above the start). */
99
+ type PathBase = "start" | "root" | "lost";
96
100
  interface TreePath {
97
101
  canonicalPath: string;
102
+ base: PathBase;
98
103
  parent: DirNode | undefined;
99
104
  node: Node | undefined;
100
105
  name: string;
@@ -160,6 +165,14 @@ function expandTreeLink(
160
165
  }
161
166
  );
162
167
  }
168
+ /** A rooted `..` above the start leaves names that no longer start anywhere known. */
169
+ function climbedBase(base: PathBase, aboveStart: boolean): PathBase {
170
+ return aboveStart && base === "start" ? "lost" : base;
171
+ }
172
+ /** An absolute link target restarts the names at the rooted walk's root. */
173
+ function restartedBase(base: PathBase, options: TreeWalk): PathBase {
174
+ return options.rooted ? "root" : base;
175
+ }
163
176
  /** Expand links against actual parent edges, never a lexically normalized pathname. */
164
177
  export function walkTree(root: DirNode, path: string, options: TreeWalk): TreePath | number {
165
178
  const error = treePathError(path, options.pathUnits, options.pathBytes);
@@ -168,6 +181,7 @@ export function walkTree(root: DirNode, path: string, options: TreeWalk): TreePa
168
181
  const names: string[] = [];
169
182
  let node: Node = root;
170
183
  let parent: DirNode | undefined;
184
+ let base: PathBase = "start";
171
185
  let name = "",
172
186
  terminalDot = false,
173
187
  expansions = 0,
@@ -186,8 +200,10 @@ export function walkTree(root: DirNode, path: string, options: TreeWalk): TreePa
186
200
  terminalDot = part === "." || part === "..";
187
201
  if (part === ".") continue;
188
202
  if (part === "..") {
189
- const up: DirNode | number = dirs.pop() ?? climbAbove(node, root, options);
203
+ const popped = dirs.pop();
204
+ const up: DirNode | number = popped ?? climbAbove(node, root, options);
190
205
  if (typeof up === "number") return up;
206
+ base = climbedBase(base, popped === undefined && up !== node);
191
207
  node = up;
192
208
  names.pop();
193
209
  parent = undefined;
@@ -203,6 +219,7 @@ export function walkTree(root: DirNode, path: string, options: TreeWalk): TreePa
203
219
  if (!child)
204
220
  return {
205
221
  canonicalPath: "/" + names.concat(part).join("/"),
222
+ base,
206
223
  parent,
207
224
  name,
208
225
  node: undefined,
@@ -214,6 +231,7 @@ export function walkTree(root: DirNode, path: string, options: TreeWalk): TreePa
214
231
  if (link?.absolute) {
215
232
  dirs.length = 0;
216
233
  names.length = 0;
234
+ base = restartedBase(base, options);
217
235
  }
218
236
  if (link) {
219
237
  expansions++;
@@ -230,7 +248,25 @@ export function walkTree(root: DirNode, path: string, options: TreeWalk): TreePa
230
248
  }
231
249
  const trailingSlash = path.endsWith("/");
232
250
  if (trailingSlash && node.kind !== "dir") return 54;
233
- return { parent, name, node, trailingSlash, terminalDot, canonicalPath: "/" + names.join("/") };
251
+ return {
252
+ parent,
253
+ name,
254
+ node,
255
+ trailingSlash,
256
+ terminalDot,
257
+ canonicalPath: "/" + names.join("/"),
258
+ base,
259
+ };
260
+ }
261
+
262
+ /** `below` ("/" or "/a/b") appended to the absolute directory path `prefix`, or its errno. */
263
+ function joinBelow(prefix: string | number, below: string): string | number {
264
+ if (typeof prefix === "number" || below === "/") return prefix;
265
+ return (prefix === "/" ? "" : prefix) + below;
266
+ }
267
+ function childName(parent: DirNode, child: Node): string | undefined {
268
+ for (const [name, entry] of parent.children) if (entry === child) return name;
269
+ return undefined;
234
270
  }
235
271
 
236
272
  export interface NamespaceEdge {
@@ -315,8 +351,20 @@ export function decode(data: Uint8Array): string {
315
351
 
316
352
  export class MemFS implements FileSystem {
317
353
  onChange?: (node: Node) => void;
354
+ /** Fires when a directory gains or loses a child name (bytes = 64 + name.length * 2). */
355
+ onDirEntryDelta?: (dir: DirNode, deltaBytes: number) => void;
318
356
  /** Namespace edge changes; moving an existing edge leaves its count unchanged. */
319
357
  onLinkChange?: (node: Node, delta: 1 | -1) => void;
358
+ private setChild(parent: DirNode, name: string, child: Node): void {
359
+ const existed = parent.children.has(name);
360
+ parent.children.set(name, child);
361
+ if (!existed) this.onDirEntryDelta?.(parent, 64 + name.length * 2);
362
+ }
363
+ private deleteChild(parent: DirNode, name: string): boolean {
364
+ const deleted = parent.children.delete(name);
365
+ if (deleted) this.onDirEntryDelta?.(parent, -(64 + name.length * 2));
366
+ return deleted;
367
+ }
320
368
  private changed(node: Node): void {
321
369
  this.onChange?.(node);
322
370
  }
@@ -414,13 +462,17 @@ export class MemFS implements FileSystem {
414
462
  if (handle !== undefined && !this.references.has(handle)) this.handles.delete(handle);
415
463
  if (node.kind === "dir") {
416
464
  this.removedDirectories.add(node);
417
- for (const child of node.children.values()) {
465
+ const trackDelta = this.onDirEntryDelta !== undefined;
466
+ let delta = 0;
467
+ for (const [name, child] of node.children) {
468
+ if (trackDelta) delta -= 64 + name.length * 2;
418
469
  this.changeLinkCount(child, -1);
419
470
  this.forget(child);
420
471
  }
421
472
  // Recursive removal removes directory entries, not the bytes of separately open files.
422
473
  this.directoryPages(node).clear();
423
474
  node.children.clear();
475
+ if (delta) this.onDirEntryDelta?.(node, delta);
424
476
  }
425
477
  }
426
478
  nodeForHandle(handle: FileHandle): Node {
@@ -509,20 +561,18 @@ export class MemFS implements FileSystem {
509
561
  pathBytes?: number,
510
562
  rooted = false,
511
563
  ): MemoryPath | number {
512
- const resolved = walkTree(this.directory(directory), path, {
564
+ const resolved = this.walkNodes(
565
+ this.directory(directory),
566
+ path,
513
567
  clampRoot,
514
568
  followFinal,
515
569
  pathBytes,
516
- rooted: rooted ? this.rootedWalk() : undefined,
517
- child: (parent, name) => {
518
- const child = parent.children.get(name);
519
- if (child?.kind === "dir") this.directoryParents.set(child, parent);
520
- return child;
521
- },
522
- });
570
+ rooted,
571
+ );
523
572
  if (typeof resolved === "number") return resolved;
524
573
  return {
525
574
  canonicalPath: resolved.canonicalPath,
575
+ base: resolved.base,
526
576
  name: resolved.name,
527
577
  trailingSlash: resolved.trailingSlash,
528
578
  terminalDot: resolved.terminalDot,
@@ -530,6 +580,57 @@ export class MemFS implements FileSystem {
530
580
  node: resolved.node ? this.handle(resolved.node) : undefined,
531
581
  };
532
582
  }
583
+ private walkNodes(
584
+ start: DirNode,
585
+ path: string,
586
+ clampRoot: boolean,
587
+ followFinal: boolean,
588
+ pathBytes?: number,
589
+ rooted = false,
590
+ ): TreePath | number {
591
+ return walkTree(start, path, {
592
+ clampRoot,
593
+ followFinal,
594
+ pathBytes,
595
+ rooted: rooted ? this.rootedWalk() : undefined,
596
+ child: (parent, name) => {
597
+ const child = parent.children.get(name);
598
+ if (child?.kind === "dir") this.directoryParents.set(child, parent);
599
+ return child;
600
+ },
601
+ });
602
+ }
603
+ /** realpath(3) from `directory`: what `path` names with every link followed, in one lookup.
604
+ * A rooted lookup answers with an absolute path from the root. Any other lookup cannot leave
605
+ * `directory`, and answers relative to it ("." for the directory itself). Errors are the
606
+ * lookup's positive WASI errno: ENOENT for a missing component, ENOTDIR, ELOOP, EACCES. */
607
+ realpath(directory: FileHandle, path: string, rooted = false): string | number {
608
+ if (path.startsWith("/")) return 63;
609
+ const start = this.directory(directory);
610
+ const resolved = this.walkNodes(start, path, false, true, undefined, rooted);
611
+ if (typeof resolved === "number") return resolved;
612
+ if (resolved.node === undefined) return 44;
613
+ if (!rooted) return resolved.canonicalPath.slice(1) || ".";
614
+ if (resolved.base === "root" || (resolved.base === "start" && start === this.root))
615
+ return resolved.canonicalPath;
616
+ if (resolved.base === "start") return joinBelow(this.pathOf(start), resolved.canonicalPath);
617
+ // A `..` above the start left the walk's names behind: name the result by its ancestry.
618
+ if (resolved.node.kind === "dir") return this.pathOf(resolved.node);
619
+ return joinBelow(this.pathOf(resolved.parent!), "/" + resolved.name);
620
+ }
621
+ /** The absolute path of a live directory, by its parent edges up to the root. */
622
+ private pathOf(dir: DirNode): string | number {
623
+ const names: string[] = [];
624
+ for (let at = dir; at !== this.root;) {
625
+ const parent = this.liveParent(at);
626
+ if (typeof parent === "number") return parent === 63 ? 44 : parent;
627
+ const name = childName(parent, at);
628
+ if (name === undefined) return 44;
629
+ names.push(name);
630
+ at = parent;
631
+ }
632
+ return "/" + names.reverse().join("/");
633
+ }
533
634
  symlink(path: ResolvedPath, target: string): number {
534
635
  const prepared = this.planSymlink(path, target, 4);
535
636
  this.applyNamespace(prepared.plan);
@@ -690,7 +791,7 @@ export class MemFS implements FileSystem {
690
791
  const node = kind === "file" ? newFile() : kind === "socket" ? newSocket() : newDir();
691
792
  this.directoryPages(parent).reserve(1);
692
793
  this.pageEntry(parent, path.name, node);
693
- parent.children.set(path.name, node);
794
+ this.setChild(parent, path.name, node);
694
795
  this.changeLinkCount(node, 1);
695
796
  this.onLinkChange?.(node, 1);
696
797
  modified(parent, node.ctime);
@@ -829,7 +930,7 @@ export class MemFS implements FileSystem {
829
930
  if (typeof parent === "number") return parent;
830
931
  this.directoryPages(parent).reserve(1);
831
932
  this.pageEntry(parent, to.name, node);
832
- parent.children.set(to.name, node);
933
+ this.setChild(parent, to.name, node);
833
934
  this.changeLinkCount(node, 1);
834
935
  this.onLinkChange?.(node, 1);
835
936
  const time = fileTimeNow();
@@ -850,7 +951,7 @@ export class MemFS implements FileSystem {
850
951
  const parent = this.directory(path.parent);
851
952
  if ((parent.mode & 0o300) !== 0o300) return 2;
852
953
  this.pageEntry(parent, path.name, undefined);
853
- parent.children.delete(path.name);
954
+ this.deleteChild(parent, path.name);
854
955
  this.changeLinkCount(node, -1);
855
956
  this.onLinkChange?.(node, -1);
856
957
  const time = fileTimeNow();
@@ -886,11 +987,11 @@ export class MemFS implements FileSystem {
886
987
  this.directoryPages(parent).reserve(1);
887
988
  this.pageEntry(sourceParent, from.name, undefined);
888
989
  this.pageEntry(parent, to.name, node);
889
- sourceParent.children.delete(from.name);
990
+ this.deleteChild(sourceParent, from.name);
890
991
  const time = fileTimeNow();
891
992
  modified(sourceParent, time);
892
993
  node.ctime = time;
893
- parent.children.set(to.name, node);
994
+ this.setChild(parent, to.name, node);
894
995
  if (node.kind === "dir") this.directoryParents.set(node, parent);
895
996
  if (target) {
896
997
  this.forget(target);
@@ -1176,8 +1277,8 @@ export class MemFS implements FileSystem {
1176
1277
  this.pageEntry(edge.parent, edge.name, edge.after);
1177
1278
  if (edge.before) this.changeLinkCount(edge.before, -1);
1178
1279
  if (edge.after) this.changeLinkCount(edge.after, 1);
1179
- if (edge.after) edge.parent.children.set(edge.name, edge.after);
1180
- else edge.parent.children.delete(edge.name);
1280
+ if (edge.after) this.setChild(edge.parent, edge.name, edge.after);
1281
+ else this.deleteChild(edge.parent, edge.name);
1181
1282
  if (edge.after?.kind === "dir") this.directoryParents.set(edge.after, edge.parent);
1182
1283
  if (edge.after && created.has(edge.after)) this.onLinkChange?.(edge.after, 1);
1183
1284
  }
@@ -1266,7 +1367,7 @@ export class MemFS implements FileSystem {
1266
1367
  if (!edge) return false;
1267
1368
  const { parent, name, node } = edge;
1268
1369
  this.pageEntry(parent, name, undefined);
1269
- const removed = parent.children.delete(name);
1370
+ const removed = this.deleteChild(parent, name);
1270
1371
  if (removed) {
1271
1372
  this.changeLinkCount(node, -1);
1272
1373
  this.onLinkChange?.(node, -1);