@fungi.computer/computer 0.16.0 → 0.16.1

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/dist/index.js CHANGED
@@ -51,6 +51,7 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
51
51
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
52
52
  });
53
53
  import { boundExecStream } from "./exec-output.js";
54
+ import { portPreviewFailure, portPreviewReply } from "./port-failure.js";
54
55
  export { createLocalBackendReplayStore } from "./local-backend-replay.js";
55
56
  import { computerInternetAccess } from "./internet-access.js";
56
57
  import { createComputerVaultReferences, isVaultExecutionContext, vaultExecutionHooks, } from "./vault-references.js";
@@ -897,13 +898,11 @@ export class ComputerDO extends ContainerComputer {
897
898
  const response = await this.withHostedResponse(hosted.backing, () => this.hostedContainerHost(hosted.backing)
898
899
  .port(port)
899
900
  .fetch(new Request(forwarded, { headers })));
900
- const safe = new Response(response.body, response);
901
- safe.headers.delete("set-cookie");
902
- return safe;
901
+ return await portPreviewReply(response, request, port);
903
902
  }
904
903
  catch (cause) {
905
904
  console.error("Computer runtime operation failed", cause);
906
- return Response.json({ error: "Computer port service is unavailable" }, { status: 502 });
905
+ return await portPreviewFailure("unreachable", request, port);
907
906
  }
908
907
  }
909
908
  async #withBinaryFile(operation) {
@@ -0,0 +1,21 @@
1
+ import * as Schema from "effect/Schema";
2
+ declare const hostRefusalSchema: Schema.Literals<readonly ["not_running", "not_listening", "unreachable"]>;
3
+ /**
4
+ * Why a Computer port preview has nothing to show. `not_running`: the
5
+ * Computer is asleep (a preview never wakes it). `not_listening`: nothing in
6
+ * the Computer answers on that port. `unreachable`: Fungi could not reach the
7
+ * Computer. `expired`: the preview link's five-minute ticket ran out.
8
+ */
9
+ export type PortPreviewFailure = Schema.Schema.Type<typeof hostRefusalSchema> | "unreachable" | "expired";
10
+ /**
11
+ * The one answer a failed preview gives: a styled page in plain words for a
12
+ * browser document, or `{ error: reason }` for a script's request. Raw
13
+ * upstream text never reaches the viewer.
14
+ */
15
+ export declare function portPreviewFailure(reason: PortPreviewFailure, request: Request, port?: number): Promise<Response>;
16
+ /**
17
+ * What a preview answers for a forwarded port reply: the VM host's own
18
+ * refusal in plain words, or the guest's reply as it is, minus cookies.
19
+ */
20
+ export declare function portPreviewReply(response: Response, request: Request, port: number): Promise<Response>;
21
+ export {};
@@ -0,0 +1,106 @@
1
+ import { html, raw } from "hono/html";
2
+ import * as Option from "effect/Option";
3
+ import * as Schema from "effect/Schema";
4
+ /**
5
+ * Names the VM host's own refusal of a port request (vmd's `refusePort` in
6
+ * tooling/celld-platform/computer-vm/vmd-proxy.mjs, which strips it from
7
+ * guest replies), so a preview can tell it from the guest's answer.
8
+ */
9
+ const portFailureHeader = "x-fungi-port-failure";
10
+ const hostRefusalSchema = Schema.Literals([
11
+ "not_running",
12
+ "not_listening",
13
+ "unreachable",
14
+ ]);
15
+ /**
16
+ * The VM host's typed refusal, or null when the reply is the guest's own.
17
+ * vmd pipes an upgrade's bytes raw, so on an upgrade request the header could
18
+ * be the guest's and is never read.
19
+ */
20
+ function hostPortRefusal(response, request) {
21
+ if (request.headers.has("upgrade"))
22
+ return null;
23
+ const named = Schema.decodeUnknownOption(hostRefusalSchema)(response.headers.get(portFailureHeader));
24
+ return Option.isSome(named) ? named.value : null;
25
+ }
26
+ const copy = {
27
+ not_running: () => ({
28
+ title: "This Computer is asleep",
29
+ detail: "Start it from Fungi, then reload this page to see the preview.",
30
+ }),
31
+ not_listening: (port) => ({
32
+ title: port === undefined
33
+ ? "Nothing is answering on this port"
34
+ : `Nothing is answering on port ${String(port)}`,
35
+ detail: "Start the server on this Computer, or check its port, then reload this page.",
36
+ }),
37
+ unreachable: () => ({
38
+ title: "Fungi couldn't reach this Computer",
39
+ detail: "Reload in a moment. If it keeps happening, report it.",
40
+ }),
41
+ expired: () => ({
42
+ title: "This preview link has expired",
43
+ detail: "Open the preview again from Fungi to get a fresh link.",
44
+ }),
45
+ };
46
+ const statuses = {
47
+ not_running: 503,
48
+ not_listening: 502,
49
+ unreachable: 502,
50
+ expired: 403,
51
+ };
52
+ // The preview origin is isolated and loads no Fungi asset, so the page's
53
+ // styling travels inline and follows the viewer's light or dark scheme.
54
+ const style = raw(`<style>
55
+ :root{color-scheme:light dark;--bg:#faf8f4;--card:#fff;--ink:#2a2622;--muted:#6f675e;--line:#e4ded4;--accent:#1f6f54;--accent-ink:#fff}
56
+ @media (prefers-color-scheme:dark){:root{--bg:#171512;--card:#211e1a;--ink:#f0ebe3;--muted:#a39a8e;--line:#3a352e;--accent:#58c29b;--accent-ink:#0f1a15}}
57
+ *{box-sizing:border-box}
58
+ body{margin:0;min-height:100vh;display:grid;place-items:center;background:var(--bg);color:var(--ink);font:16px/1.5 system-ui,-apple-system,"Segoe UI",sans-serif;padding:1rem}
59
+ main{width:100%;max-width:28rem;background:var(--card);border:1px solid var(--line);border-radius:12px;padding:1.5rem}
60
+ h1{font-size:1.25rem;margin:0 0 .5rem}
61
+ p{margin:0 0 1rem;color:var(--muted)}
62
+ a{display:inline-block;font-weight:600;border-radius:8px;padding:.45rem 1rem;background:var(--accent);color:var(--accent-ink);text-decoration:none}
63
+ a:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
64
+ </style>`);
65
+ /**
66
+ * The one answer a failed preview gives: a styled page in plain words for a
67
+ * browser document, or `{ error: reason }` for a script's request. Raw
68
+ * upstream text never reaches the viewer.
69
+ */
70
+ export async function portPreviewFailure(reason, request, port) {
71
+ const headers = { "Cache-Control": "no-store" };
72
+ const status = statuses[reason];
73
+ if (request.headers.get("sec-fetch-dest") !== "document")
74
+ return Response.json({ error: reason }, { status, headers });
75
+ const { title, detail } = copy[reason](port);
76
+ const page = await html `<!doctype html>
77
+ <html lang="en">
78
+ <meta charset="utf-8" />
79
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
80
+ <title>${title}</title>
81
+ ${style}
82
+ <main>
83
+ <h1>${title}</h1>
84
+ <p>${detail}</p>
85
+ ${reason === "expired" ? "" : html `<a href="">Reload</a>`}
86
+ </main>
87
+ </html>`;
88
+ return new Response(page, {
89
+ status,
90
+ headers: { ...headers, "Content-Type": "text/html; charset=utf-8" },
91
+ });
92
+ }
93
+ /**
94
+ * What a preview answers for a forwarded port reply: the VM host's own
95
+ * refusal in plain words, or the guest's reply as it is, minus cookies.
96
+ */
97
+ export async function portPreviewReply(response, request, port) {
98
+ const refused = hostPortRefusal(response, request);
99
+ if (refused !== null) {
100
+ await response.body?.cancel();
101
+ return await portPreviewFailure(refused, request, port);
102
+ }
103
+ const safe = new Response(response.body, response);
104
+ safe.headers.delete("set-cookie");
105
+ return safe;
106
+ }
package/dist/preview.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { LONG, nanoid } from "@fungi.computer/cairn";
2
2
  import * as Schema from "effect/Schema";
3
3
  import * as Option from "effect/Option";
4
+ import { portPreviewFailure } from "./port-failure.js";
4
5
  const storedClaimsSchema = Schema.Struct({
5
6
  claims: Schema.String,
6
7
  expires_at: Schema.Number,
@@ -108,8 +109,9 @@ export class ComputerPortPreview {
108
109
  }
109
110
  async dispatchSigned(signature, request, forward) {
110
111
  const claims = await this.resolve(signature);
112
+ // An expired, unknown or altered ticket: the viewer needs a fresh link.
111
113
  if (claims === null)
112
- return new Response(null, { status: 403 });
114
+ return await portPreviewFailure("expired", request);
113
115
  return await forward(claims.selector, claims.port, request);
114
116
  }
115
117
  ensureTable() {
@@ -146,6 +148,6 @@ export async function computerPortPreviewRequest(request, origin, host) {
146
148
  return await host.forward(computerId, new Request(destination, forwarded));
147
149
  }
148
150
  catch {
149
- return new Response(null, { status: 503 });
151
+ return await portPreviewFailure("unreachable", request);
150
152
  }
151
153
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fungi.computer/computer",
3
- "version": "0.16.0",
3
+ "version": "0.16.1",
4
4
  "author": "Levi <levi@technomancy.dev>",
5
5
  "description": "Cloudflare Computer host for one durable Fungi Computer",
6
6
  "type": "module",
@@ -79,6 +79,7 @@
79
79
  "esbuild": "0.28.2",
80
80
  "eslint": "10.9.1",
81
81
  "oxlint": "1.78.0",
82
+ "playwright": "1.62.1",
82
83
  "tsx": "4.23.12",
83
84
  "typescript": "5.9.3",
84
85
  "uuid": "11.1.1",
@@ -125,8 +126,9 @@
125
126
  "test:nightly": "bash ../../tooling/proof-runner/run-proof.sh node test/egress-proof.mjs",
126
127
  "test:app-method-execution": "node test/app-method-execution-proof.mjs",
127
128
  "test:home-patch": "node test/home-instructions-patch-proof.mjs",
129
+ "test:preview-failure:browser": "node test/preview-failure.browser.mjs",
128
130
  "test:execution-lifecycle": "node test/execution-lifecycle-proof.mjs",
129
- "test:unit": "bash ../../tooling/proof-runner/hermetic-host.sh -- sh -c \"node --test --test-concurrency=${BOTANICAL_TEST_WORKERS:-4} test/egress-policy.test.mjs test/preview.test.mjs test/port-access.test.mjs test/process-wait.test.mjs test/fleet.test.mjs test/proof-worker.test.mjs test/proof-client.test.mjs test/native-readiness.test.mjs test/workspace-sync.test.mjs test/runtime-observer.test.mjs test/files-patch.test.mjs test/exec-output.test.mjs && node --test --test-concurrency=${BOTANICAL_TEST_WORKERS:-4} test/seam-signature.test.mjs test/local-backend-replay.test.mjs test/machine-link-reopen-replay.test.mjs test/computer-backends.test.mjs test/terminal-welcome.test.mjs test/on-box-vm.test.mjs && node test/terminal-protocol.test.mjs && node test/container-policy.test.mjs && node test/files-adapter-proof.mjs && node test/operation-record-proof.mjs && node test/process-inventory-proof.mjs\"",
131
+ "test:unit": "bash ../../tooling/proof-runner/hermetic-host.sh -- sh -c \"node --test --test-concurrency=${BOTANICAL_TEST_WORKERS:-4} test/egress-policy.test.mjs test/preview.test.mjs test/port-failure.test.mjs test/port-access.test.mjs test/process-wait.test.mjs test/fleet.test.mjs test/proof-worker.test.mjs test/proof-client.test.mjs test/native-readiness.test.mjs test/workspace-sync.test.mjs test/runtime-observer.test.mjs test/files-patch.test.mjs test/exec-output.test.mjs && node --test --test-concurrency=${BOTANICAL_TEST_WORKERS:-4} test/seam-signature.test.mjs test/local-backend-replay.test.mjs test/machine-link-reopen-replay.test.mjs test/computer-backends.test.mjs test/terminal-welcome.test.mjs test/on-box-vm.test.mjs && node test/terminal-protocol.test.mjs && node test/container-policy.test.mjs && node test/files-adapter-proof.mjs && node test/operation-record-proof.mjs && node test/process-inventory-proof.mjs\"",
130
132
  "test:fleet-native": "node test/fleet.native.mjs",
131
133
  "assemble:image": "node scripts/assemble-image.mjs"
132
134
  }