An open-source, self-hosted system for apps built with web technologies.
Take the interface a CLI start asks for, and record the one it listens on
zohayrslilehf01d13de6bce
diff --git a/source/server/main.ts b/source/server/main.tsindex 25cbcf9..fb0fc5e 100644--- a/source/server/main.ts+++ b/source/server/main.ts@@ -1,11 +1,12 @@import { name } from "@/package.json"import view from "./view/view"-import { defaultHome, defaultHostname, defaultPorts, environmentHome, environmentHostname, environmentPorts, requestedHome, requestedPorts } from "./view/configuration"+import { defaultHome, defaultHostname, defaultPorts, environmentHome, environmentHostname, environmentPorts, requestedHome, requestedHostname, requestedPorts } from "./view/configuration"import { fileURLToPath } from "node:url"const development = process.env.NODE_ENV === "development"const requested = await requestedHome(process.argv)const requestedPortSelection = await requestedPorts(process.argv)+const requestedHost = await requestedHostname(process.argv)const home = environmentHome(name, process.env)?? requested?? defaultHome(development)@@ -16,7 +17,7 @@ await view({// A published container port cannot reach a listener confined to the// container's loopback interface, so the deployment owns this selection.- hostname: environmentHostname(name, process.env) ?? defaultHostname(),+ hostname: environmentHostname(name, process.env) ?? requestedHost ?? defaultHostname(),home,diff --git a/source/server/view/configuration.ts b/source/server/view/configuration.tsindex 44a13db..9bb39d8 100644--- a/source/server/view/configuration.ts+++ b/source/server/view/configuration.ts@@ -35,11 +35,47 @@ export function environmentHostname(name: string, variables: NodeJS.ProcessEnv)const value = selected.valueif (value === undefined) return undefined- if (!value || value !== value.trim() || /\s/.test(value)) throw new Error(`${selected.key} must contain one hostname or IP address`)++ return hostname(value, selected.key)+}++/** One hostname or IP address, as a listener takes it. */+function hostname(value: string, source: string) {++ if (!value || value !== value.trim() || /\s/.test(value)) throw new Error(`${source} must contain one hostname or IP address`)return value}+/**+ * Read the interface a CLI start asked for once, as it asks for a port: the variable set where+ * `phresh system start` ran never reaches the service, so the CLI hands it over in a file.+ */+export async function requestedHostname(arguments_: string[]) {++ const positions = arguments_.flatMap((value, index) => value === "--host-request" ? [index] : [])++ if (positions.length === 0) return undefined+ if (positions.length !== 1) throw new Error("--host-request can be supplied only once")++ const path = arguments_[positions[0]! + 1]++ if (!path || !isAbsolute(path)) throw new Error("--host-request must be followed by an absolute filesystem path")++ let value: string++ try { value = (await readFile(path, "utf8")).trim() }+ catch (error) {++ if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined+ throw error+ }++ await rm(path, { force: true })++ return hostname(value, "The requested PhreshOS host")+}+/** Read the optional ordered production port selection. */export function environmentPorts(name: string, variables: NodeJS.ProcessEnv) {diff --git a/source/server/view/view.ts b/source/server/view/view.tsindex ab43959..962cfec 100644--- a/source/server/view/view.ts+++ b/source/server/view/view.ts@@ -85,6 +85,10 @@ export default async function (config: Config) {await writeFile(join(application.storage.path, "desktop"), `${origin}\n`, { mode: 0o600 })+ // The Desktop is opened on this machine at localhost; the interface it listens on, which may let+ // other devices reach it, is recorded beside it, so what reports the address can say so.+ await writeFile(join(application.storage.path, "listening"), `${hostname}\n`, { mode: 0o600 })+const localGateway = await gateway(application.linkManager, gatewayAddress(application.storage.path))if (config.assets) console.log(` ➜ ${styleText("bold", "Desktop:")} ${origin}`)diff --git a/tests/ports.test.ts b/tests/ports.test.tsindex b8c0a6c..07fd196 100644--- a/tests/ports.test.ts+++ b/tests/ports.test.ts@@ -3,7 +3,7 @@ import { createServer } from "node:http"import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"import { homedir, tmpdir } from "node:os"import { join, resolve } from "node:path"-import { defaultHome, defaultPorts, environmentPorts, listenOnPorts, parsePorts, portRange, requestedPorts } from "@server/view/configuration"+import { defaultHome, defaultPorts, environmentPorts, listenOnPorts, parsePorts, portRange, requestedHostname, requestedPorts } from "@server/view/configuration"import { test } from "vitest"test("ports contract", async () => {@@ -52,6 +52,15 @@ test("ports contract", async () => {assert.deepEqual(await requestedPorts(["node", "main.js", "--port-request", request]), [4400, 4500, 4501, 4502])await assert.rejects(readFile(request), error => (error as NodeJS.ErrnoException).code === "ENOENT")++ // The interface a CLI start asked for arrives the same way, read once.+ const hostRequest = join(temporary, "next-host")+ assert.equal(await requestedHostname(["node", "main.js", "--host-request", hostRequest]), undefined)+ await writeFile(hostRequest, "0.0.0.0\n")+ assert.equal(await requestedHostname(["node", "main.js", "--host-request", hostRequest]), "0.0.0.0")+ await assert.rejects(readFile(hostRequest), error => (error as NodeJS.ErrnoException).code === "ENOENT")+ await writeFile(hostRequest, "two hosts")+ await assert.rejects(requestedHostname(["node", "main.js", "--host-request", hostRequest]), /one hostname or IP address/)}finally { await rm(temporary, { recursive: true, force: true }) }