An open-source, self-hosted system for apps built with web technologies.
Preserve command Server IPC
zohayrslileh40cff8a23c28
diff --git a/package.json b/package.jsonindex 5b74490..12e52ad 100644--- a/package.json+++ b/package.json@@ -1,7 +1,7 @@{"name": "phreshos","displayName": "PhreshOS",- "version": "0.1.54",+ "version": "0.1.55","description": "The PhreshOS server, desktop, and authoritative system runtime.","type": "module","private": true,diff --git a/scripts/contracts/worker-runtime.ts b/scripts/contracts/worker-runtime.tsindex 5d4196c..ce97b1d 100644--- a/scripts/contracts/worker-runtime.ts+++ b/scripts/contracts/worker-runtime.ts@@ -4,12 +4,13 @@ import { tmpdir } from "node:os"import { delimiter, join, resolve } from "node:path"import { pathToFileURL } from "node:url"import { decode, encode } from "@msgpack/msgpack"-import { commandServerEnvironment, WorkerServerRuntime } from "@server/core/link-manager/auth-manager/process-manager/server-runtime"+import { CommandServerRuntime, commandServerEnvironment, WorkerServerRuntime } from "@server/core/link-manager/auth-manager/process-manager/server-runtime"import Program from "@server/core/link-manager/auth-manager/program-manager/program"import type { ProgramConfig } from "@server/core/link-manager/auth-manager/program-manager/config"const directory = await mkdtemp(join(tmpdir(), "phresh-worker-runtime-"))const entry = join(directory, "server.mjs")+const commandEntry = join(directory, "command.mjs")const codec = pathToFileURL(resolve("node_modules/@msgpack/msgpack/dist.esm/index.mjs")).hrefassert.equal(@@ -29,6 +30,25 @@ parentPort.on("message", message => {})`)+await writeFile(commandEntry, `+import { decode, encode } from ${JSON.stringify(codec)}++console.log("command output")+process.send?.(encode(["boundary", "ready"]))+process.on("message", message => {+ const [event, value] = decode(bytes(message))+ if (event === "probe") process.send?.(encode(["probe-result", value]))+})++function bytes(value) {+ if (value instanceof Uint8Array) return value+ const record = value+ const result = new Uint8Array(Object.keys(record).length)+ for (let index = 0; index < result.length; index++) result[index] = record[index]+ return result+}+`)+try {const program = new Program({ identity: "worker-verification", server: { location: directory, entryFile: "server.mjs" } })@@ -65,6 +85,31 @@ try {const ending = await runtime.finishedassert.equal(ending.signal, null)++ const command = new CommandServerRuntime(`node ${JSON.stringify(commandEntry)}`, directory)+ const commandMessages: unknown[][] = []+ const commandOutput: ["out" | "err", string][] = []++ command.onMessage(message => {+ if (!(message instanceof Uint8Array)) throw new Error("The command response must be bytes")+ const decoded = decode(message)+ if (!Array.isArray(decoded)) throw new Error("The command response must be an array")+ commandMessages.push(decoded)+ })+ command.onOutput((stream, text) => commandOutput.push([stream, text]))++ await until(() => commandMessages.some(message => message[0] === "boundary" && message[1] === "ready"))++ command.send(encode(["probe", 42]))++ await until(() => commandMessages.some(message => message[0] === "probe-result"))+ await until(() => commandOutput.some(([stream, text]) => stream === "out" && text.includes("command output")))++ assert.deepEqual(commandMessages.find(message => message[0] === "probe-result"), ["probe-result", 42])++ command.stop()++ await command.finished} finally {await rm(directory, { recursive: true, force: true })}diff --git a/source/server/core/link-manager/auth-manager/process-manager/server-runtime.ts b/source/server/core/link-manager/auth-manager/process-manager/server-runtime.tsindex 534c8a1..37c4936 100644--- a/source/server/core/link-manager/auth-manager/process-manager/server-runtime.ts+++ b/source/server/core/link-manager/auth-manager/process-manager/server-runtime.ts@@ -52,7 +52,12 @@ export class CommandServerRuntime implements ServerRuntime {this.tree = new ProcessTree(this.child, (code, signal) => finish({ code, signal }))- this.child.on("message", message => this.inbox.receive(message))+ this.child.on("message", message => {++ const bytes = commandMessageBytes(message)++ if (bytes) this.inbox.receive(bytes)+ })this.child.on("error", () => undefined)}@@ -189,3 +194,29 @@ type Ending = { code: number | null, signal: NodeJS.Signals | null }const maximumPendingMessages = 256const maximumPendingOutput = 256++function commandMessageBytes(value: unknown) {++ if (value instanceof Uint8Array) return Uint8Array.from(value)++ if (value instanceof ArrayBuffer) return new Uint8Array(value)++ if (ArrayBuffer.isView(value)) return Uint8Array.from(new Uint8Array(value.buffer, value.byteOffset, value.byteLength))++ if (value === null || typeof value !== "object") return null++ const record = value as Record<string, unknown>++ const bytes = new Uint8Array(Object.keys(record).length)++ for (let index = 0; index < bytes.length; index++) {++ const byte = record[String(index)]++ if (typeof byte !== "number" || !Number.isInteger(byte) || byte < 0 || byte > 255) return null++ bytes[index] = byte+ }++ return bytes+}