An open-source, self-hosted system for apps built with web technologies.
Stream authoritative Program installation
zohayrslileh449a2269e94e
diff --git a/source/server/core/link-manager/auth-manager/process-manager/process-manager.ts b/source/server/core/link-manager/auth-manager/process-manager/process-manager.tsindex 6f35187..5b84a82 100644--- a/source/server/core/link-manager/auth-manager/process-manager/process-manager.ts+++ b/source/server/core/link-manager/auth-manager/process-manager/process-manager.ts@@ -561,6 +561,13 @@ export default class ProcessManager extends TheLink {if (event === "end-host") {+ if (values[0] === "stream" && typeof values[1] === "string") {++ this.endHostStream(process, values[1], values.slice(2)).catch(() => undefined)++ return+ }+if (values[0] === "wait" && typeof values[1] === "string") {this.endHostWait(process, values[1], values.slice(2)).catch(() => undefined)@@ -1403,15 +1410,6 @@ export default class ProcessManager extends TheLink {throw new Error(`The host does not know the wallpaper surface "${String(surface)}"`)}- // Laid out here, or removed from here. Both operations mutate the- // installed flag on the exact Program the caller addressed.- if (word === "install") {-- const program = this.heldProgram(rest[0])-- return [await programManager.install(program, process.identity)]- }-if (word === "fork") {const program = this.heldProgram(rest[0])@@ -2010,6 +2008,36 @@ export default class ProcessManager extends TheLink {}}+ private async endHostStream(process: Process, question: string, args: unknown[]) {++ let active = true++ process.server?.retain(question, () => { active = false })++ await this.say(process.server, "host-end", "stream", question, "open")++ try {++ if (args[0] !== "install") throw new Error(`The host does not know the stream operation "${String(args[0])}"`)++ const program = this.heldProgram(args[1])++ for await (const chunk of this.authManager.programManager.installStreaming(program, process.identity)) {++ if (!active) return++ await this.say(process.server, "host-end", "stream", question, "data", chunk)+ }++ if (active) await this.say(process.server, "host-end", "stream", question, "answer", succeeded(undefined))+ }++ catch (exception) {++ if (active) await this.say(process.server, "host-end", "stream", question, "answer", failed(exception))+ }+ }+// A client question retains its cancellation at the server-host// counterpart of that exact desktop boundary. Replacing the document or// losing the session therefore removes the target's queued request evendiff --git a/source/server/core/link-manager/auth-manager/process-manager/server-process-boundary.ts b/source/server/core/link-manager/auth-manager/process-manager/server-process-boundary.tsindex 0999622..ff560bb 100644--- a/source/server/core/link-manager/auth-manager/process-manager/server-process-boundary.ts+++ b/source/server/core/link-manager/auth-manager/process-manager/server-process-boundary.ts@@ -92,6 +92,15 @@ export default class ServerProcessBoundary extends ProcessLink {/** Deliver one routed envelope only when this endpoint requested it. */public async deliver(route: string, ...values: unknown[]) {+ if (values[0] === "stream" && typeof values[1] === "string") {++ if (!this.expected.has(values[1])) return++ await this.$outbound.publish(route, ...values)++ return+ }+if (values[0] === "answer" && typeof values[1] === "string") {if (!this.expected.has(values[1])) returndiff --git a/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts b/source/server/core/link-manager/auth-manager/program-manager/program-manager.tsindex a55388c..ccdfdb7 100644--- a/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts+++ b/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts@@ -13,10 +13,10 @@ import { dirname, isAbsolute, join } from "node:path"import { isDeepStrictEqual } from "node:util"import Logs, { type LogSource } from "./logs"import { isValue, layers, type Layer, type Position, type ProgramConfig, type Size } from "./config"-import { type PermissionName, type WallpaperLaunch } from "@phreshos/core"+import { type PermissionName, type ProgramInstallChunk, type WallpaperLaunch } from "@phreshos/core"import { type default as Process, type ProcessLaunch, type Stream } from "../process-manager/process"import { type StandardShape, type WallpaperShape } from "../process-manager/process-manager"-import Program from "./program"+import Program, { type InstallOutput } from "./program"import Entry, { type HostedEntry } from "./entry"import Keyv from "keyv"import { isIconSize, ProgramIcons } from "./icon"@@ -632,7 +632,23 @@ export default class ProgramManager extends TheLink {// durable files, not the current registry entry's installed flag.const replaced = existsSync(this.fileManager.join(program.identity, "program.json"))- const entry = await this.install(program)+ const installation = this.installStreaming(program)++ let entry: Entry++ while (true) {++ const next = await installation.next()++ if (next.done) {++ entry = next.value++ break+ }++ yield { stage: "output", chunk: next.value }+ }yield { stage: "installed", entry, replaced }@@ -661,7 +677,82 @@ export default class ProgramManager extends TheLink {return await this.start(program, launch)}- public async install(source: Program, asker: string | null = null) {+ /** Install while exposing command output with consumer-driven backpressure. */+ public async *installStreaming(source: Program, asker: string | null = null): AsyncGenerator<ProgramInstallChunk, Entry, void> {++ const queue: { chunk: ProgramInstallChunk, consumed: () => void }[] = []++ let wake: (() => void) | null = null++ let settled = false++ let installed: Entry | undefined++ let failure: unknown++ let detached = false++ const operation = this.install(source, asker, chunk => {++ if (detached) return++ return new Promise<void>(consumed => {++ queue.push({ chunk, consumed })++ wake?.()++ wake = null+ })+ }).then(entry => {++ installed = entry+ }, error => {++ failure = error+ }).finally(() => {++ settled = true++ wake?.()++ wake = null+ })++ try {++ while (!settled || queue.length) {++ const next = queue.shift()++ if (next) {++ try { yield next.chunk }++ finally { next.consumed() }++ continue+ }++ await new Promise<void>(resolve => { wake = resolve })+ }++ await operation++ if (failure) throw failure++ return installed!+ }++ finally {++ detached = true++ for (const pending of queue.splice(0)) pending.consumed()+ }+ }++ public async install(source: Program, asker: string | null = null, output: InstallOutput = () => undefined) {return await this.change(source.identity, async () => {@@ -719,7 +810,7 @@ export default class ProgramManager extends TheLink {// Preparation runs against the laid-out files but before// the registry claims the install succeeded.- await installed.installServer()+ await installed.installServer(output)let entry = this.programs.get(source.identity)@@ -1279,6 +1370,8 @@ export interface InstallSourceOptions {export type InstallSourceStage =+ | { stage: "output", chunk: ProgramInstallChunk }+| { stage: "installed", entry: Entry, replaced: boolean }| { stage: "startup-enabled" }diff --git a/source/server/core/link-manager/auth-manager/program-manager/program.ts b/source/server/core/link-manager/auth-manager/program-manager/program.tsindex f9d5c8b..52e4d36 100644--- a/source/server/core/link-manager/auth-manager/program-manager/program.ts+++ b/source/server/core/link-manager/auth-manager/program-manager/program.ts@@ -4,6 +4,7 @@ import { dirname, isAbsolute, resolve } from "node:path"import { spawn } from "node:child_process"import { randomUUID } from "node:crypto"import { validateIcon } from "./icon"+import type { ProgramInstallChunk } from "@phreshos/core"/*** A program: a description, and the things it names.@@ -265,13 +266,13 @@ export default class Program {// Preparing a server half, when it says it needs preparing. Run// once, where the half lives, before anything starts it.- public async installServer() {+ public async installServer(output: InstallOutput = () => undefined) {const command = this.config.server?.installCommandif (!command) return- await execute(command, this.serverPath!)+ await execute(command, this.serverPath!, output)}}@@ -370,14 +371,59 @@ function file(path: string) {}-function execute(command: string, cwd: string) {+export type InstallOutput = (chunk: ProgramInstallChunk) => void | Promise<void>++function execute(command: string, cwd: string, output: InstallOutput) {return new Promise<void>(function (settle, refuse) {- const child = spawn(command, { shell: true, cwd, stdio: "ignore" })+ const child = spawn(command, { shell: true, cwd, stdio: ["ignore", "pipe", "pipe"] })++ let delivery = Promise.resolve()++ let failed = false++ const relay = function (stream: NodeJS.ReadableStream, name: ProgramInstallChunk["stream"]) {++ stream.on("data", chunk => {++ stream.pause()++ delivery = delivery.then(() => output({ stream: name, text: String(chunk) }))++ delivery.then(() => stream.resume(), error => {++ if (failed) return++ failed = true++ child.kill()++ refuse(error)+ })+ })+ }++ relay(child.stdout!, "stdout")++ relay(child.stderr!, "stderr")++ child.on("error", error => {++ if (failed) return++ failed = true++ refuse(error)+ })++ child.on("close", code => delivery.then(() => {++ if (failed) return- child.on("error", refuse)+ if (code === 0) settle()- child.on("exit", code => code === 0 ? settle() : refuse(new Error(`"${command}" exited with ${code}`)))+ else refuse(new Error(`"${command}" exited with ${code}`))+ }, refuse))})}diff --git a/source/server/view/intake.ts b/source/server/view/intake.tsindex 09a1747..391bd97 100644--- a/source/server/view/intake.ts+++ b/source/server/view/intake.ts@@ -235,7 +235,9 @@ async function answer(application: Application, attached: Set<string>, say: (evefor await (const stage of programManager.installSource(asked.program, { run: asked.run, startup: asked.startup })) {- if (stage.stage === "installed") {+ if (stage.stage === "output") say({ event: "output", ...stage.chunk })++ else if (stage.stage === "installed") {// Whether it replaced one. Installation has already ended every// process that could have retained one of the previous paths.