@mostra/runtime 0.6.1 → 0.7.0

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.
@@ -593,4 +593,65 @@ export const MIGRATIONS: readonly Migration[] = [
593
593
  `);
594
594
  },
595
595
  },
596
+ {
597
+ version: 11,
598
+ name: "shares and faces",
599
+ up(db) {
600
+ // D158. A face (an external address of the gateway: a tailnet or Funnel address, a quick
601
+ // tunnel's, a host's external base) is a listener of its own on loopback; a face that must
602
+ // keep its port across restarts (a tailnet address is `https://<device>:<port>`, a host's
603
+ // tunnel forwards to a port it was told) leases it like the shell and the artifacts do, under
604
+ // the purpose `face`. SQLite can't change a CHECK, so `port_lease` is rebuilt with its rows.
605
+ // `share` records each share (DATA_MODEL §3) with its plan, the addresses it set up, so a
606
+ // restart sets tailnet and Funnel shares up again; its row only ever ends (a trigger).
607
+ // `external_base` keeps the faces a host's addressing port gave, so their links work after
608
+ // a restart before the host answers again.
609
+ db.exec(`
610
+ CREATE TABLE port_lease_new (
611
+ port INTEGER PRIMARY KEY CHECK (port BETWEEN 1 AND 65535),
612
+ purpose TEXT NOT NULL CHECK (purpose IN ('shell', 'artifact', 'demo', 'face')),
613
+ owner_ref TEXT NOT NULL,
614
+ leased_at TEXT NOT NULL,
615
+ UNIQUE (purpose, owner_ref)
616
+ );
617
+ INSERT INTO port_lease_new (port, purpose, owner_ref, leased_at)
618
+ SELECT port, purpose, owner_ref, leased_at FROM port_lease;
619
+ DROP TABLE port_lease;
620
+ ALTER TABLE port_lease_new RENAME TO port_lease;
621
+ CREATE TABLE share (
622
+ id TEXT PRIMARY KEY,
623
+ artifact_id TEXT NOT NULL REFERENCES artifact(id),
624
+ via TEXT NOT NULL CHECK (via IN ('tailnet', 'funnel', 'cloudflare')),
625
+ hostname TEXT NOT NULL,
626
+ token_id TEXT NOT NULL REFERENCES token(id),
627
+ mode TEXT NOT NULL CHECK (mode IN ('cookie', 'worker')),
628
+ plan_json TEXT NOT NULL,
629
+ started_at TEXT NOT NULL,
630
+ expires_at TEXT,
631
+ stopped_at TEXT,
632
+ stop_reason TEXT,
633
+ CHECK ((stopped_at IS NULL) = (stop_reason IS NULL))
634
+ );
635
+ CREATE INDEX share_artifact ON share (artifact_id);
636
+ CREATE INDEX share_active ON share (stopped_at) WHERE stopped_at IS NULL;
637
+ CREATE TRIGGER share_ends_once BEFORE UPDATE ON share
638
+ WHEN OLD.stopped_at IS NOT NULL OR NEW.id IS NOT OLD.id
639
+ OR NEW.artifact_id IS NOT OLD.artifact_id OR NEW.via IS NOT OLD.via
640
+ OR NEW.token_id IS NOT OLD.token_id OR NEW.started_at IS NOT OLD.started_at
641
+ OR NEW.expires_at IS NOT OLD.expires_at
642
+ BEGIN
643
+ SELECT RAISE(ABORT, 'a share only ever ends, once');
644
+ END;
645
+ CREATE TABLE external_base (
646
+ key TEXT PRIMARY KEY,
647
+ listener TEXT NOT NULL CHECK (listener IN ('shell', 'artifact')),
648
+ artifact_id TEXT NOT NULL REFERENCES artifact(id),
649
+ origin TEXT NOT NULL,
650
+ mode TEXT CHECK (mode IN ('cookie', 'worker')),
651
+ client TEXT NOT NULL,
652
+ updated_at TEXT NOT NULL
653
+ );
654
+ `);
655
+ },
656
+ },
596
657
  ];
@@ -51,3 +51,4 @@ export {
51
51
  } from "./helper.ts";
52
52
  export { isSameProcess, listProcesses, readProcess } from "./identity.ts";
53
53
  export { adoptable, launch, reapRecorded } from "./launch.ts";
54
+ export { installHint, type ToolCommand, toolCommand } from "./tools.ts";
@@ -0,0 +1,145 @@
1
+ /**
2
+ * Where this system keeps the tunnel tools a share drives (D158 item 4): `tailscale` and
3
+ * `cloudflared`, which Mostra never bundles or downloads. A daemon started by a login service may
4
+ * run with a short `PATH`, so the tools' usual install locations are searched after it, and an
5
+ * environment variable names one outright. How to install each is said here too, per system.
6
+ */
7
+ import { accessSync, constants, statSync } from "node:fs";
8
+ import { delimiter, join } from "node:path";
9
+ import { OS, WINDOWS } from "./directories.ts";
10
+
11
+ /** A tunnel tool. */
12
+ export type TunnelTool = "tailscale" | "cloudflared";
13
+
14
+ /** The variable that names a tool's executable outright. */
15
+ const TOOL_VARIABLES: Readonly<Record<TunnelTool, string>> = {
16
+ tailscale: "MOSTRA_TAILSCALE",
17
+ cloudflared: "MOSTRA_CLOUDFLARED",
18
+ };
19
+
20
+ /** The usual install locations of each tool, per system, after `PATH`. */
21
+ export function usualLocations(
22
+ tool: TunnelTool,
23
+ env: Readonly<Record<string, string | undefined>>,
24
+ ): string[] {
25
+ if (WINDOWS) {
26
+ const programs = env["ProgramFiles"] ?? "C:\\Program Files";
27
+ const programs86 = env["ProgramFiles(x86)"] ?? "C:\\Program Files (x86)";
28
+ return tool === "tailscale"
29
+ ? [join(programs, "Tailscale", "tailscale.exe")]
30
+ : [
31
+ join(programs86, "cloudflared", "cloudflared.exe"),
32
+ join(programs, "cloudflared", "cloudflared.exe"),
33
+ ];
34
+ }
35
+ if (OS === "darwin") {
36
+ return tool === "tailscale"
37
+ ? [
38
+ "/opt/homebrew/bin/tailscale",
39
+ "/usr/local/bin/tailscale",
40
+ // Homebrew's formula left unlinked (beside the Mac app, which owns the name), as found on
41
+ // a real Mac in WP-13: its prefix keeps the command line all the same.
42
+ "/opt/homebrew/opt/tailscale/bin/tailscale",
43
+ "/usr/local/opt/tailscale/bin/tailscale",
44
+ // The Mac app's own executable is its command line too.
45
+ "/Applications/Tailscale.app/Contents/MacOS/Tailscale",
46
+ ]
47
+ : [
48
+ "/opt/homebrew/bin/cloudflared",
49
+ "/usr/local/bin/cloudflared",
50
+ "/opt/homebrew/opt/cloudflared/bin/cloudflared",
51
+ "/usr/local/opt/cloudflared/bin/cloudflared",
52
+ ];
53
+ }
54
+ return tool === "tailscale"
55
+ ? ["/usr/bin/tailscale", "/usr/local/bin/tailscale", "/usr/sbin/tailscale"]
56
+ : ["/usr/bin/cloudflared", "/usr/local/bin/cloudflared"];
57
+ }
58
+
59
+ function executable(path: string): boolean {
60
+ try {
61
+ if (!statSync(path).isFile()) return false;
62
+ if (!WINDOWS) accessSync(path, constants.X_OK);
63
+ return true;
64
+ } catch {
65
+ return false;
66
+ }
67
+ }
68
+
69
+ /**
70
+ * The tool's executable: the variable's (which must exist), else the first on `PATH`, else the
71
+ * first usual location that exists; `undefined` when there is none.
72
+ */
73
+ function findTool(
74
+ tool: TunnelTool,
75
+ env: Readonly<Record<string, string | undefined>>,
76
+ ): string | undefined {
77
+ const named = env[TOOL_VARIABLES[tool]];
78
+ if (named !== undefined && named !== "") return executable(named) ? named : undefined;
79
+ const names = WINDOWS ? [`${tool}.exe`] : [tool];
80
+ const path = (WINDOWS ? (env["Path"] ?? env["PATH"]) : env["PATH"]) ?? "";
81
+ for (const dir of path.split(delimiter)) {
82
+ if (dir === "") continue;
83
+ for (const name of names) {
84
+ const candidate = join(dir, name);
85
+ if (executable(candidate)) return candidate;
86
+ }
87
+ }
88
+ return usualLocations(tool, env).find(executable);
89
+ }
90
+
91
+ /** How a tool runs: its executable and the arguments before the tool's own. */
92
+ export interface ToolCommand {
93
+ readonly command: string;
94
+ readonly args: readonly string[];
95
+ /** Run by `MOSTRA_TEST_TUNNELS`: a local fake that forwards plain HTTP on loopback. */
96
+ readonly fake: boolean;
97
+ }
98
+
99
+ /**
100
+ * The variable that replaces both tools with local fakes for tests (`<dir>/tailscale.mjs` and
101
+ * `<dir>/cloudflared.mjs`, run on this Node.js): the conformance kit's and the suites' own, which
102
+ * forward on loopback as the real tools forward from their networks (STANDARDS: real side effects
103
+ * in tests go to local fakes).
104
+ */
105
+ const TEST_TUNNELS_VARIABLE = "MOSTRA_TEST_TUNNELS";
106
+
107
+ /** How to run a tool: {@link findTool}'s executable, or the test fake; `undefined` when missing. */
108
+ export function toolCommand(
109
+ tool: TunnelTool,
110
+ env: Readonly<Record<string, string | undefined>>,
111
+ ): ToolCommand | undefined {
112
+ const fakes = env[TEST_TUNNELS_VARIABLE];
113
+ if (fakes !== undefined && fakes !== "") {
114
+ const script = join(fakes, `${tool}.mjs`);
115
+ return regularFile(script)
116
+ ? { command: process.execPath, args: [script], fake: true }
117
+ : undefined;
118
+ }
119
+ const found = findTool(tool, env);
120
+ return found === undefined ? undefined : { command: found, args: [], fake: false };
121
+ }
122
+
123
+ function regularFile(path: string): boolean {
124
+ try {
125
+ return statSync(path).isFile();
126
+ } catch {
127
+ return false;
128
+ }
129
+ }
130
+
131
+ /** How to install a tool on this system, as one sentence. */
132
+ export function installHint(tool: TunnelTool): string {
133
+ if (tool === "tailscale") {
134
+ if (WINDOWS)
135
+ return "Install Tailscale from https://tailscale.com/download/windows (or `winget install tailscale.tailscale`) and sign in.";
136
+ if (OS === "darwin")
137
+ return "Install Tailscale from https://tailscale.com/download/mac (or `brew install tailscale`) and sign in.";
138
+ return "Install Tailscale with `curl -fsSL https://tailscale.com/install.sh | sh` (https://tailscale.com/download/linux), then run `sudo tailscale up`.";
139
+ }
140
+ if (WINDOWS)
141
+ return "Install cloudflared with `winget install --id Cloudflare.cloudflared` (https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/).";
142
+ if (OS === "darwin")
143
+ return "Install cloudflared with `brew install cloudflared` (https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/).";
144
+ return "Install cloudflared from your distribution's Cloudflare package repository or https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/.";
145
+ }
@@ -54,6 +54,12 @@ export interface SpawnSpec {
54
54
  * `exited` resolves.
55
55
  */
56
56
  readonly onStdout?: (line: string) => void;
57
+ /**
58
+ * Receives each line the child writes to stderr, redacted, instead of the log (the exit's
59
+ * `stderrTail` keeps the last ones as always): for a tool that reports its state there, such as
60
+ * `cloudflared`'s tunnel address (D158).
61
+ */
62
+ readonly onStderr?: (line: string) => void;
57
63
  /**
58
64
  * Opens a JSON-RPC channel over Node IPC; the command must then be a Node.js executable. The
59
65
  * handlers answer the child's requests besides the built-in `child/adopt` and `child/release`.
@@ -270,9 +276,11 @@ function createManager({
270
276
  drained.push(new Promise((resolve) => lines.once("close", resolve)));
271
277
  lines.on("line", (line) => {
272
278
  // The log redacts what it writes; the tail feeds diagnostics, so it is redacted here.
273
- stderrTail.push(redactor.redact(line));
279
+ const redacted = redactor.redact(line);
280
+ stderrTail.push(redacted);
274
281
  if (stderrTail.length > STDERR_TAIL) stderrTail.shift();
275
- log.info("process.stderr", { kind: spec.kind, pid, line });
282
+ if (spec.onStderr) spec.onStderr(redacted);
283
+ else log.info("process.stderr", { kind: spec.kind, pid, line });
276
284
  });
277
285
  }
278
286
 
@@ -14,6 +14,7 @@ import {
14
14
  instanceStatusMethod,
15
15
  mostraShutdownMethod,
16
16
  RpcError,
17
+ type Share,
17
18
  type WorkerStatus,
18
19
  } from "@mostra/protocol";
19
20
  import { handler, type MethodHandler } from "./server.ts";
@@ -37,6 +38,8 @@ export interface SessionMethodsOptions {
37
38
  workers?(): readonly WorkerStatus[];
38
39
  /** When an idle daemon exits (WP-08); `undefined` while busy or for an instance that never does. */
39
40
  idleExitAt?(): string | undefined;
41
+ /** The active shares (WP-13, D158). */
42
+ shares?(): readonly Share[];
40
43
  /**
41
44
  * The supervisor's own checks of its home (`instance/check`, D140 item 18), in order; `deep`
42
45
  * also hashes every version against its output digest.
@@ -62,6 +65,7 @@ export function sessionMethods(options: SessionMethodsOptions): MethodHandler[]
62
65
  ...(gateway ? { gateway } : {}),
63
66
  ...(options.deliveries ? { deliveries: [...options.deliveries()] } : {}),
64
67
  ...(options.workers ? { workers: [...options.workers()] } : {}),
68
+ ...(options.shares ? { shares: [...options.shares()] } : {}),
65
69
  ...(() => {
66
70
  const at = options.idleExitAt?.();
67
71
  return at === undefined ? {} : { idleExitAt: at };
@@ -54,7 +54,11 @@ export function artifactMethods(options: {
54
54
  * The artifact's default capability link (D90, D97 item 3), added to every receipt, replays
55
55
  * included. Recorded receipts never hold it: the ledger keeps no link's secret.
56
56
  */
57
- readonly link?: (artifactId: string) => { readonly url: string; readonly tokenId: string };
57
+ readonly link?: (
58
+ artifactId: string,
59
+ ) =>
60
+ | { readonly url: string; readonly tokenId: string }
61
+ | Promise<{ readonly url: string; readonly tokenId: string }>;
58
62
  /**
59
63
  * Hears every publish that succeeded, a reused version too: a publish clears the artifact's
60
64
  * parked workers (FAILURE_MODEL §2).
@@ -71,7 +75,7 @@ export function artifactMethods(options: {
71
75
  handler(artifactPublishMethod, async (params) => {
72
76
  const receipt = await options.publisher().publish(params);
73
77
  options.published?.(receipt.artifactId);
74
- return options.link ? { ...receipt, ...options.link(receipt.artifactId) } : receipt;
78
+ return options.link ? { ...receipt, ...(await options.link(receipt.artifactId)) } : receipt;
75
79
  }),
76
80
  handler(depsAddMethod, async (params) => {
77
81
  const added = await options.publisher().addDependencies(params);
@@ -0,0 +1,136 @@
1
+ /**
2
+ * The Cloudflare driver (D158 item 4): a Cloudflare quick tunnel per face, `cloudflared tunnel
3
+ * --url http://127.0.0.1:<port>`, which needs no account and gets a random `trycloudflare.com`
4
+ * hostname. Each hostname is its own site (`trycloudflare.com` is a public suffix), so a share's
5
+ * shell and artifact are cross-site and its frames use worker mode (D70). Quick tunnels hold every
6
+ * response back until it ends, so pages long-poll (VERIFIED_FACTS, WP-13). A quick tunnel's
7
+ * hostname dies with its process, so a Cloudflare share ends with its tunnel.
8
+ */
9
+ import { Resolver } from "node:dns/promises";
10
+ import { mkdirSync, writeFileSync } from "node:fs";
11
+ import { join } from "node:path";
12
+ import { createDiagnostic, RpcError } from "@mostra/protocol";
13
+ import type { Log } from "../log.ts";
14
+ import { installHint, type ToolCommand, toolCommand } from "../platform/index.ts";
15
+ import type { ProcessManager } from "../processes/index.ts";
16
+ import { type LineVerdict, runTunnel, type Tunnel } from "./tunnel.ts";
17
+
18
+ /** How long a quick tunnel may take to serve: registering and the hostname's first answer. */
19
+ const QUICK_TUNNEL_TIMEOUT_MS = 60_000;
20
+
21
+ /** What the driver needs. */
22
+ export interface CloudflareContext {
23
+ readonly processes: ProcessManager;
24
+ readonly log: Log;
25
+ readonly env: Readonly<Record<string, string | undefined>>;
26
+ /** A directory of the home's cache for the tunnel's empty configuration file. */
27
+ readonly cacheDir: string;
28
+ }
29
+
30
+ /** How `cloudflared` runs, or `MOSTRA_SHARE_TOOL_MISSING`. */
31
+ export function cloudflared(context: CloudflareContext): ToolCommand {
32
+ const tool = toolCommand("cloudflared", context.env);
33
+ if (!tool) {
34
+ throw new RpcError(
35
+ createDiagnostic("MOSTRA_SHARE_TOOL_MISSING", {
36
+ message: `cloudflared isn't installed on this machine. ${installHint("cloudflared")}`,
37
+ }),
38
+ );
39
+ }
40
+ return tool;
41
+ }
42
+
43
+ /**
44
+ * Reads `cloudflared`'s log: it names the quick tunnel's address in a banner, and serves once a
45
+ * connection to Cloudflare's edge is registered.
46
+ */
47
+ function quickTunnelReader(fake: boolean): (line: string) => LineVerdict {
48
+ return (line) => {
49
+ // The test fake's tunnels are sites of their own on loopback, as quick tunnels are.
50
+ const local = fake ? /\b(http:\/\/[a-z0-9-]+\.localhost:\d+)\b/.exec(line) : null;
51
+ if (local) return { kind: "origin", origin: (local[1] as string).toLowerCase() };
52
+ const address = /https:\/\/([a-z0-9-]+\.trycloudflare\.com)\b/i.exec(line);
53
+ if (address)
54
+ return { kind: "origin", origin: `https://${(address[1] as string).toLowerCase()}` };
55
+ if (/Registered tunnel connection/i.test(line)) return { kind: "serving" };
56
+ if (
57
+ /failed to (request|create) quick tunnel|error unmarshaling QuickTunnel|429 Too Many Requests/i.test(
58
+ line,
59
+ )
60
+ ) {
61
+ return {
62
+ kind: "failed",
63
+ diagnostic: createDiagnostic("MOSTRA_SHARE_TUNNEL_FAILED", {
64
+ message: `Cloudflare didn't open a quick tunnel: ${line.trim().slice(0, 300)}`,
65
+ }),
66
+ };
67
+ }
68
+ return undefined;
69
+ };
70
+ }
71
+
72
+ /**
73
+ * Opens a quick tunnel to `local`. It never reads the user's own `cloudflared` configuration (a
74
+ * file with ingress rules would turn the quick tunnel off): it runs with an empty one of its own.
75
+ */
76
+ export function quickTunnel(
77
+ context: CloudflareContext,
78
+ input: { readonly local: string; readonly ownerRef: string },
79
+ ): Promise<Tunnel> {
80
+ const tool = cloudflared(context);
81
+ mkdirSync(context.cacheDir, { recursive: true, mode: 0o700 });
82
+ const config = join(context.cacheDir, "cloudflared.yml");
83
+ writeFileSync(
84
+ config,
85
+ "# Mostra's quick tunnels read no other configuration.\nno-autoupdate: true\n",
86
+ { mode: 0o600 },
87
+ );
88
+ return runTunnel({
89
+ processes: context.processes,
90
+ log: context.log,
91
+ kind: "tunnel-cloudflare",
92
+ ownerRef: input.ownerRef,
93
+ command: tool.command,
94
+ args: [
95
+ ...tool.args,
96
+ "tunnel",
97
+ "--config",
98
+ config,
99
+ "--no-autoupdate",
100
+ "--metrics",
101
+ "127.0.0.1:0",
102
+ "--management-diagnostics=false",
103
+ "--url",
104
+ input.local,
105
+ ],
106
+ env: context.env,
107
+ read: quickTunnelReader(tool.fake),
108
+ timeoutMs: QUICK_TUNNEL_TIMEOUT_MS,
109
+ label: "A Cloudflare quick tunnel",
110
+ });
111
+ }
112
+
113
+ /**
114
+ * Waits until a quick tunnel's hostname resolves at Cloudflare's own resolvers (1.1.1.1 and
115
+ * 1.0.0.1), asked directly, past this machine's cache. A new `trycloudflare.com` name takes a few
116
+ * seconds to appear, and a resolver that is asked too early caches the miss for many minutes
117
+ * (observed: the link never opened on this machine after an early probe, WP-13), so neither this
118
+ * machine nor a viewer's phone may ask before the name exists. Resolves `false` when the budget
119
+ * runs out (a network that blocks outside resolvers): the share goes on, since a viewer's
120
+ * resolver may still answer.
121
+ */
122
+ export async function hostnameResolves(hostname: string, budgetMs: number): Promise<boolean> {
123
+ const resolver = new Resolver({ timeout: 2_000, tries: 1 });
124
+ resolver.setServers(["1.1.1.1", "1.0.0.1"]);
125
+ const until = Date.now() + budgetMs;
126
+ while (Date.now() < until) {
127
+ try {
128
+ const addresses = await resolver.resolve4(hostname);
129
+ if (addresses.length > 0) return true;
130
+ } catch {
131
+ // Not there yet, or the resolvers can't be reached: ask again shortly.
132
+ }
133
+ await new Promise((resolve) => setTimeout(resolve, 1_000));
134
+ }
135
+ return false;
136
+ }