An open-source, self-hosted system for apps built with web technologies.
Preserve client subscription ordering
zohayrslileh0ca7a28b1d42
diff --git a/package.json b/package.jsonindex f7ba93b..6c9506d 100644--- a/package.json+++ b/package.json@@ -1,7 +1,7 @@{"name": "phreshos","displayName": "PhreshOS",- "version": "0.1.84",+ "version": "0.1.85","description": "The PhreshOS server, desktop, and authoritative system runtime.","type": "module","private": true,diff --git a/source/client/view/components/desktop-host/client-process-boundary.ts b/source/client/view/components/desktop-host/client-process-boundary.tsindex 75477e8..0e479ce 100644--- a/source/client/view/components/desktop-host/client-process-boundary.ts+++ b/source/client/view/components/desktop-host/client-process-boundary.ts@@ -42,6 +42,11 @@ export default class ClientProcessBoundary extends TheLink {/** The exact System authority exercised by each routed subscription. */private readonly systemSubscriptions = new Map<string, SystemSubscriptionTarget>()+ // A synchronous SDK subscription is useful only if later work from that+ // same document cannot overtake its remote registration. Keep registration+ // as a narrow ordering barrier without serializing independent requests.+ private hostRegistrations: Promise<void> = Promise.resolve()+private readonly desktopPreferencesSubscriptions = new Set<string>()private stopDesktopPreferences: (() => void) | null = null@@ -116,9 +121,13 @@ export default class ClientProcessBoundary extends TheLink {this.leased = owner+ this.hostRegistrations = Promise.resolve()+for (const [subscription, description] of this.subscriptions) {- if (directSubscription(description)) this.authManager.processManager.subscribeFrame(this.pane, owner, subscription, description.kind, description.event).catch(() => undefined)+ if (directSubscription(description)) this.orderHostRegistration(+ () => this.authManager.processManager.subscribeFrame(this.pane, owner, subscription, description.kind, description.event)+ ).catch(() => undefined)}pending.push(...this.pending.splice(0))@@ -184,7 +193,9 @@ export default class ClientProcessBoundary extends TheLink {this.authManager.processManager.cancel(this.pane, asking).catch(() => undefined)})- this.authManager.processManager.processes.get(this.pane)?.endEnd(...values).catch((error: Error) => {+ this.afterHostRegistrations(+ () => this.authManager.processManager.processes.get(this.pane)?.endEnd(...values) ?? Promise.resolve()+ ).catch((error: Error) => {if (asking) {@@ -235,7 +246,7 @@ export default class ClientProcessBoundary extends TheLink {this.trackSystemSubscription(values)- host(this.authManager, this.pane, this.viewport, () => this.owner, this.presentation)(values[0], ...values.slice(1)).catch((error: Error) => {+ this.runHost(values[0], values.slice(1)).catch((error: Error) => {if (values[0] === "observe" && typeof values[1] === "string" && values[6] === true) {@@ -275,6 +286,29 @@ export default class ClientProcessBoundary extends TheLink {})}+ private runHost(word: unknown, args: unknown[]) {++ const operation = () => host(this.authManager, this.pane, this.viewport, () => this.owner, this.presentation)(word, ...args)++ return hostRegistrationOperation(word)+ ? this.orderHostRegistration(operation)+ : this.afterHostRegistrations(operation)+ }++ private orderHostRegistration<Result>(operation: () => Promise<Result>) {++ const ordered = this.hostRegistrations.then(operation)++ this.hostRegistrations = ordered.then(() => undefined, () => undefined)++ return ordered+ }++ private afterHostRegistrations<Result>(operation: () => Promise<Result>) {++ return this.hostRegistrations.then(operation)+ }+/** Deliver one routed envelope only when this endpoint requested it. */public async deliver(route: string, ...values: unknown[]) {@@ -464,7 +498,11 @@ export default class ClientProcessBoundary extends TheLink {if (!desktopOwnedSubscription(description)) this.addTraffic(kind, route, event)- if (this.owner && directSubscription(description)) this.authManager.processManager.subscribeFrame(this.pane, this.owner, subscription, kind, event).catch(() => undefined)+ const owner = this.owner++ if (owner && directSubscription(description)) this.orderHostRegistration(+ () => this.authManager.processManager.subscribeFrame(this.pane, owner, subscription, kind, event)+ ).catch(() => undefined)if (desktopPreferencesSubscription(description)) {@@ -531,7 +569,7 @@ export default class ClientProcessBoundary extends TheLink {if (args[0] === "wait-ready") {- this.waitReady(question, args[1], args[2], args[3] === true)+ void this.afterHostRegistrations(() => this.waitReady(question, args[1], args[2], args[3] === true))return}@@ -540,14 +578,14 @@ export default class ClientProcessBoundary extends TheLink {this.answerRequest(question,- host(this.authManager, this.pane, this.viewport, () => this.owner, this.presentation)(args[0], ...args.slice(1)),+ this.runHost(args[0], args.slice(1)),() => { this.authManager.cancelPermission(this.pane, args[3] as string).catch(() => undefined) })return}- this.answerRequest(question, host(this.authManager, this.pane, this.viewport, () => this.owner, this.presentation)(args[0], ...args.slice(1)))+ this.answerRequest(question, this.runHost(args[0], args.slice(1)))}private answerRequest(question: string, operation: Promise<unknown[] | TransferredAnswer>, cancel: () => void = () => undefined) {@@ -600,6 +638,7 @@ export default class ClientProcessBoundary extends TheLink {})const run = async () => {+ await this.hostRegistrationsawait this.deliver("host-end", "stream", question, "open")if (args[0] === "shell") {@@ -870,7 +909,11 @@ export default class ClientProcessBoundary extends TheLink {if (!existing) return- if (this.owner && directSubscription(existing)) this.authManager.processManager.unsubscribeFrame(this.pane, this.owner, subscription).catch(() => undefined)+ const owner = this.owner++ if (owner && directSubscription(existing)) this.orderHostRegistration(+ () => this.authManager.processManager.unsubscribeFrame(this.pane, owner, subscription)+ ).catch(() => undefined)this.subscriptions.delete(subscription)@@ -972,9 +1015,13 @@ export default class ClientProcessBoundary extends TheLink {this.requests.clear()- if (this.owner) for (const [subscription, description] of this.subscriptions) {+ const owner = this.owner++ if (owner) for (const [subscription, description] of this.subscriptions) {- if (directSubscription(description)) this.authManager.processManager.unsubscribeFrame(this.pane, this.owner, subscription).catch(() => undefined)+ if (directSubscription(description)) this.orderHostRegistration(+ () => this.authManager.processManager.unsubscribeFrame(this.pane, owner, subscription)+ ).catch(() => undefined)}this.subscriptions.clear()@@ -1147,6 +1194,16 @@ function directSubscription(subscription: EndpointSubscription) {return subscription.route === "end-end" && subscription.kind === "publish"}+function hostRegistrationOperation(value: unknown) {++ return value === "observe"+ || value === "unobserve"+ || value === "follow"+ || value === "unfollow"+ || value === "service-follow"+ || value === "service-unfollow"+}+function isTrafficKind(value: unknown): value is TrafficKind {return value === "publish" || value === "ask" || value === "answer"diff --git a/tests/client-process-boundary-lifecycle.test.ts b/tests/client-process-boundary-lifecycle.test.tsindex 8e9eb08..b95286e 100644--- a/tests/client-process-boundary-lifecycle.test.ts+++ b/tests/client-process-boundary-lifecycle.test.ts@@ -4,7 +4,7 @@ import ClientProcessBoundary from "@client/view/components/desktop-host/client-pimport type AuthManager from "@client/core/link-manager/auth-manager/auth-manager"import type ClientTraffic from "@client/view/components/desktop-host/client-traffic"import type { WindowPresentationHost } from "@client/view/components/desktop-host/window-presentation"-import { test } from "vitest"+import { test, vi } from "vitest"test("a Client question expectation survives initial iframe ownership", async () => {@@ -40,3 +40,42 @@ test("a Client question expectation survives initial iframe ownership", async ()assert.deepEqual(sent, [["end-end", "answer", question, "public-question", "read", { success: true, result: 4 }]])})++test("a later Client request cannot overtake its Service subscription registration", async () => {++ let completeFollow!: () => void+ const following = new Promise<void>(resolve => { completeFollow = resolve })+ const order: string[] = []+ const processManager = {+ processes: new Map([["process", { program: "flambo" }]]),+ async ownFrame() {},+ async releaseFrame() {},+ async followService() {+ order.push("follow:start")+ await following+ order.push("follow:end")+ },+ async askService() { order.push("ask") }+ }+ const authManager = {+ processManager,+ async grantsPermission() { return true }+ } as unknown as AuthManager+ const boundary = new ClientProcessBoundary(+ "process",+ { contentWindow: null } as unknown as HTMLIFrameElement,+ authManager,+ () => ({ size: { width: 800, height: 600 } }),+ {} as ClientTraffic,+ { begin() {} } as unknown as WindowPresentationHost+ )+ const address = { program: "flambo", process: "browser-server", endpoint: "server" } as const++ await boundary.own("document")+ boundary.receive(["end-host", "service-follow", "subscription", address, "events", "workspace.change", false])+ boundary.receive(["end-host", "service-ask", address, "question", "public-question", "workspace.attach", {}])++ await vi.waitFor(() => assert.deepEqual(order, ["follow:start"]))+ completeFollow()+ await vi.waitFor(() => assert.deepEqual(order, ["follow:start", "follow:end", "ask"]))+})