Skip to content

An open-source, self-hosted system for apps built with web technologies.

Move launch configuration into Server View

zohayrslileh642d2cecdb48
diff --git a/source/server/main.ts b/source/server/main.ts
index 36e7883..42aa986 100644
--- a/source/server/main.ts
+++ b/source/server/main.ts
@@ -1,8 +1,6 @@
import { displayName, name, version } from "@/package.json"
-import view, { environmentHome, environmentPort } from "./view/view"
+import view, { defaultHome, environmentHome, environmentPort, requestedHome } from "./view/view"
import { fileURLToPath } from "node:url"
-import { isAbsolute, normalize } from "node:path"
-import { readFile, rm } from "node:fs/promises"
const requested = await requestedHome(process.argv)
const home = environmentHome(name, process.env)
@@ -26,44 +24,3 @@ await view({
assets: fileURLToPath(new URL("../client", import.meta.url))
})
-
-function defaultHome(arguments_: string[]) {
-
- const positions = arguments_.flatMap((value, index) => value === "--default-home" ? [index] : [])
-
- if (positions.length === 0) return undefined
- if (positions.length !== 1) throw new Error("--default-home can be supplied only once")
-
- const value = arguments_[positions[0]! + 1]
-
- if (!value || !isAbsolute(value)) throw new Error("--default-home must be followed by an absolute filesystem path")
-
- return normalize(value)
-}
-
-async function requestedHome(arguments_: string[]) {
-
- const positions = arguments_.flatMap((value, index) => value === "--home-request" ? [index] : [])
-
- if (positions.length === 0) return undefined
- if (positions.length !== 1) throw new Error("--home-request can be supplied only once")
-
- const path = arguments_[positions[0]! + 1]
-
- if (!path || !isAbsolute(path)) throw new Error("--home-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 })
-
- if (!isAbsolute(value)) throw new Error("The requested PhreshOS home must be an absolute filesystem path")
-
- return normalize(value)
-}
diff --git a/source/server/view/view.ts b/source/server/view/view.ts
index 1386276..09c4e4d 100644
--- a/source/server/view/view.ts
+++ b/source/server/view/view.ts
@@ -13,7 +13,8 @@ import link from "./link"
import getPort from "get-port"
import cfonts from "cfonts"
import { Hono } from "hono"
-import { isAbsolute, resolve } from "node:path"
+import { isAbsolute, normalize, resolve } from "node:path"
+import { readFile, rm } from "node:fs/promises"
import { styleText } from "node:util"
import environment from "@libs/environment"
@@ -101,6 +102,49 @@ export interface Config {
port?: number
}
+/** Read the stable fallback home supplied by the native service definition. */
+export function defaultHome(arguments_: string[]) {
+
+ const positions = arguments_.flatMap((value, index) => value === "--default-home" ? [index] : [])
+
+ if (positions.length === 0) return undefined
+ if (positions.length !== 1) throw new Error("--default-home can be supplied only once")
+
+ const value = arguments_[positions[0]! + 1]
+
+ if (!value || !isAbsolute(value)) throw new Error("--default-home must be followed by an absolute filesystem path")
+
+ return normalize(value)
+}
+
+/** Consume the optional one-time home request supplied through the native service boundary. */
+export async function requestedHome(arguments_: string[]) {
+
+ const positions = arguments_.flatMap((value, index) => value === "--home-request" ? [index] : [])
+
+ if (positions.length === 0) return undefined
+ if (positions.length !== 1) throw new Error("--home-request can be supplied only once")
+
+ const path = arguments_[positions[0]! + 1]
+
+ if (!path || !isAbsolute(path)) throw new Error("--home-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 })
+
+ if (!isAbsolute(value)) throw new Error("The requested PhreshOS home must be an absolute filesystem path")
+
+ return normalize(value)
+}
+
/** Read the optional public port override named from the application identity. */
export function environmentPort(name: string, variables: NodeJS.ProcessEnv) {