Skip to content

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

Program and Process facts reach Servers by the Program they belong to

zohayrslilehaedb7fa57d8c
diff --git a/source/server/core/link-manager/auth-manager/auth-manager.ts b/source/server/core/link-manager/auth-manager/auth-manager.ts
index 0fdaa62..9028b9f 100644
--- a/source/server/core/link-manager/auth-manager/auth-manager.ts
+++ b/source/server/core/link-manager/auth-manager/auth-manager.ts
@@ -45,7 +45,7 @@ export default class AuthManager extends TheLink {
this.$outbound.publish("/logs/log", record).catch(() => undefined)
- this.processManager.announceHost("log", "log", "system", record).catch(() => undefined)
+ this.processManager.announceHost("log", null, "log", "system", record).catch(() => undefined)
})
this.subscribeTo(this.linkManager, "/auth")
diff --git a/source/server/core/link-manager/auth-manager/process-manager/host-traffic.ts b/source/server/core/link-manager/auth-manager/process-manager/host-traffic.ts
index 1c13890..aa242c9 100644
--- a/source/server/core/link-manager/auth-manager/process-manager/host-traffic.ts
+++ b/source/server/core/link-manager/auth-manager/process-manager/host-traffic.ts
@@ -3,36 +3,24 @@ import { TheLink } from "@the-link/core"
/** Host announcements routed only into Process boundaries that requested them. */
export default class HostTraffic extends TheLink {
- public emitHost(domain: Domain, event: string, subject: string, ...values: unknown[]) {
+ public emitHost(domain: Domain, owner: HostOwner, event: string, subject: string, ...values: unknown[]) {
- return this.$inbound.publish(this.event(domain, null, event), {}, subject, ...values)
- }
-
- public async emit(domain: Domain, event: string, publicSubject: string, scopedSubject: string, ...values: unknown[]) {
-
- const delivery = {}
-
- const deliveries = [
- this.$inbound.publish(this.event(domain, null, event), delivery, publicSubject, ...values),
- this.$inbound.publish(this.event(domain, scopedSubject, event), delivery, scopedSubject, ...values)
- ]
-
- await Promise.all(deliveries)
+ return this.$inbound.publish(this.event(domain, null, event), { owner } satisfies HostDelivery, subject, ...values)
}
/** Emits an entity-scoped fact without also exposing it as a Host-registry event. */
- public emitSubject(domain: Domain, event: string, subject: string, ...values: unknown[]) {
+ public emitSubject(domain: Domain, owner: HostOwner, event: string, subject: string, ...values: unknown[]) {
- return this.$inbound.publish(this.event(domain, subject, event), {}, subject, ...values)
+ return this.$inbound.publish(this.event(domain, subject, event), { owner } satisfies HostDelivery, subject, ...values)
}
public observe(domain: Domain, event: string | null, subject: string | null, subscriber: Subscriber) {
const prefix = this.prefix(domain, subject)
- if (event !== null) return this.$inbound.subscribe(prefix + encodeURIComponent(event), (delivery, ...values) => subscriber(delivery as object, event, ...values))
+ if (event !== null) return this.$inbound.subscribe(prefix + encodeURIComponent(event), (delivery, ...values) => subscriber(delivery as HostDelivery, event, ...values))
- return this.$inbound.forwardTo((word, delivery, ...values) => subscriber(delivery as object, decodeURIComponent(word), ...values), prefix)
+ return this.$inbound.forwardTo((word, delivery, ...values) => subscriber(delivery as HostDelivery, decodeURIComponent(word), ...values), prefix)
}
private prefix(domain: Domain, subject: string | null) {
@@ -46,7 +34,17 @@ export default class HostTraffic extends TheLink {
}
}
-type Subscriber = (delivery: object, event: string, ...values: unknown[]) => unknown
+/**
+ * The Program a fact belongs to, or `null` for a fact of the System itself, such as a connection.
+ * Who may see a fact is decided from its owner, said when it happens and never looked up afterwards:
+ * by then the entity may be gone, as an ended Process is when its ending is announced.
+ */
+export type HostOwner = string | null
+
+/** What travels with every fact besides its values. */
+export type HostDelivery = Readonly<{ owner: HostOwner }>
+
+type Subscriber = (delivery: HostDelivery, event: string, ...values: unknown[]) => unknown
export type HostDomain = Domain
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.ts
index 908cab1..45ad222 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
@@ -9,7 +9,7 @@ import Process, { type HostedProcess, type ProcessLaunch, type ProcessSnapshot }
import ServerProcessBoundary from "./server-process-boundary"
import ProcessTraffic, { type Half, type TrafficKind } from "./process-traffic"
import ClientProcessForwarder from "./client-process-forwarder"
-import HostTraffic from "./host-traffic"
+import HostTraffic, { type HostDomain, type HostOwner } from "./host-traffic"
import { failed, succeeded, type RequestOutcome } from "@libs/request-outcome"
import { endpointReference, processReference } from "./endpoint-reference"
import EndpointEvents from "./endpoint-events"
@@ -118,7 +118,7 @@ export default class ProcessManager extends TheLink {
address => this.resolveService(address),
() => this.processes.values(),
(event, address) => Promise.all([
- this.hostTraffic.emitHost("service", event, address.process, address),
+ this.hostTraffic.emitHost("service", null, event, address.process, address),
this.$outbound.publish(`/service-${event}`, address)
])
)
@@ -887,7 +887,7 @@ export default class ProcessManager extends TheLink {
const record = processReference(process)
- await this.hostTraffic.emitSubject("process", event, process.reference, record, endpoint)
+ await this.hostTraffic.emitSubject("process", process.program.identity, event, process.reference, record, endpoint)
}
private async serverStarted(process: Process) {
@@ -975,7 +975,7 @@ export default class ProcessManager extends TheLink {
this.authManager.linkManager.appearance.tunnel,
- (domain, subject) => this.serverHostVisible(process, domain, subject)
+ (domain, subject, owner) => this.serverHostVisible(process, domain, subject, owner)
)
this.bindServer(process, server)
@@ -983,7 +983,12 @@ export default class ProcessManager extends TheLink {
return server
}
- private serverHostVisible(process: Process, domain: "program" | "process" | "connection" | "session" | "service" | "window" | "permission" | "opening" | "log" | "programLog" | "clientMemory", subject: string | null) {
+ /**
+ * Whether one fact may reach this Server. A fact about a Program, its Processes, their windows,
+ * memory, or logs is seen by whoever can see that Program, decided from the owner the fact
+ * carries; the same rule decides it for Clients on the Desktop.
+ */
+ private serverHostVisible(process: Process, domain: HostDomain, subject: string | null, owner: HostOwner) {
const access = new SystemAccess(this, process)
@@ -993,29 +998,9 @@ export default class ProcessManager extends TheLink {
if (domain === "log") return this.grants(process.identity, "logs", [])
- if (domain === "programLog") {
+ if (domain === "service") return subject !== null && this.grants(process.identity, "services", [subject])
- if (!subject) return false
-
- const program = [...this.authManager.programManager.programs.values()].find(entry => entry.program.reference === subject)?.program
-
- return program ? access.canProgram(program) : false
- }
-
- if (!subject) return false
-
- if (domain === "service") return this.grants(process.identity, "services", [subject])
-
- if (domain === "program") {
-
- const program = [...this.authManager.programManager.programs.values()].find(entry => entry.program.reference === subject)?.program
-
- return program ? access.canProgram(program) : false
- }
-
- const target = [...this.processes.values()].find(candidate => candidate.reference === subject)
-
- return target ? access.canProcess(target) : false
+ return owner !== null && access.canProgram({ identity: owner })
}
private window() {
@@ -1080,7 +1065,7 @@ export default class ProcessManager extends TheLink {
process.client?.memory.changes(({ key, snapshot }) => {
// A browser document may disappear while the Client run remains.
// Publish from the run so every current representation sees one order.
- this.hostTraffic.emitSubject("clientMemory", key, process.reference, processReference(process), snapshot).catch(() => undefined)
+ this.hostTraffic.emitSubject("clientMemory", process.program.identity, key, process.reference, processReference(process), snapshot).catch(() => undefined)
this.$outbound.publish("/client-memory-change", process.identity, key, snapshot).catch(() => undefined)
})
}
@@ -1168,9 +1153,9 @@ export default class ProcessManager extends TheLink {
// receives the subject as its first value.
await Promise.all([
- this.announceHost("process", "create", program.identity, processReference(process)),
+ this.announceHost("process", program.identity, "create", program.identity, processReference(process)),
- this.announceSubject("program", "processCreate", program.reference, processReference(process))
+ this.announceSubject("program", program.identity, "processCreate", program.reference, processReference(process))
])
await this.$outbound.publish("/created", process.hosted())
@@ -1262,15 +1247,15 @@ export default class ProcessManager extends TheLink {
// why holding it is legitimate.
() => Promise.all([
- this.announceHost("process", "exit", process.program.identity, processReference(process), code, signal),
+ this.announceHost("process", process.program.identity, "exit", process.program.identity, processReference(process), code, signal),
- this.announceSubject("program", "processExit", process.program.reference, processReference(process), code, signal)
+ this.announceSubject("program", process.program.identity, "processExit", process.program.reference, processReference(process), code, signal)
]),
// The same ending, said to whoever holds this one process rather
// than to whoever watches the program. A launcher wants the
// second; a program managing its instances wants the first.
- () => this.hostTraffic.emitSubject("process", "exit", process.reference, code, signal),
+ () => this.hostTraffic.emitSubject("process", process.program.identity, "exit", process.reference, code, signal),
// The window that had it is gone, so nobody is told they lost
// it — only whoever inherits it is told they have it.
@@ -1764,15 +1749,15 @@ export default class ProcessManager extends TheLink {
}
/** Announces one fact only through an authoritative Host registry. */
- public async announceHost(domain: "program" | "process" | "connection" | "session" | "service" | "permission" | "opening" | "log" | "programLog", event: string, subject: string, ...values: unknown[]) {
+ public async announceHost(domain: "program" | "process" | "connection" | "session" | "service" | "permission" | "opening" | "log" | "programLog", owner: HostOwner, event: string, subject: string, ...values: unknown[]) {
- await this.hostTraffic.emitHost(domain, event, subject, ...values)
+ await this.hostTraffic.emitHost(domain, owner, event, subject, ...values)
}
/** Announces one fact only to observers of an exact Program or Process subject. */
- public async announceSubject(domain: "program" | "process" | "connection" | "session" | "service" | "permission" | "opening" | "log" | "programLog", event: string, subject: string, ...values: unknown[]) {
+ public async announceSubject(domain: "program" | "process" | "connection" | "session" | "service" | "permission" | "opening" | "log" | "programLog", owner: HostOwner, event: string, subject: string, ...values: unknown[]) {
- await this.hostTraffic.emitSubject(domain, event, subject, ...values)
+ await this.hostTraffic.emitSubject(domain, owner, event, subject, ...values)
}
/** Reads only the persistent user grant belonging to this Process's Program. */
@@ -1834,7 +1819,7 @@ export default class ProcessManager extends TheLink {
const process = this.processes.get(identity)
- if (process?.client) this.hostTraffic.emitSubject("window", event, process.reference, value).catch(() => undefined)
+ if (process?.client) this.hostTraffic.emitSubject("window", process.program.identity, event, process.reference, value).catch(() => undefined)
this.$outbound.publish("/said", identity, event, value).catch(() => undefined)
}
diff --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.ts
index 86b0cc6..194e861 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
@@ -1,6 +1,6 @@
import { TheLink } from "@the-link/core"
import ProcessTraffic, { type Half, type TrafficKind } from "./process-traffic"
-import HostTraffic from "./host-traffic"
+import HostTraffic, { type HostDelivery, type HostDomain, type HostOwner } from "./host-traffic"
import EndpointEvents from "./endpoint-events"
import EndpointServices, { type ServiceScope } from "./endpoint-services"
import type { ServiceAddress } from "@phreshos/core"
@@ -43,6 +43,9 @@ export default class ServerProcessBoundary extends TheLink {
private readonly hostSubscriptions = new Map<string, () => void>()
+ /** The routes each fact has already reached this Server on. */
+ private readonly deliveredFacts = new WeakMap<HostDelivery, Set<string>>()
+
private readonly resources = new Set<() => void>()
private readonly appearanceSubscriptions = new Set<string>()
@@ -384,15 +387,23 @@ export default class ServerProcessBoundary extends TheLink {
this.hostSubscriptions.get(subscription)?.()
- this.hostSubscriptions.set(subscription, this.hostTraffic.observe(hostDomain, event, subject, (_delivery, word, ...values) => {
+ this.hostSubscriptions.set(subscription, this.hostTraffic.observe(hostDomain, event, subject, (delivery, word, ...values) => {
+
+ // A Service's announcement names the Service it is about; every other fact
+ // carries the Program it belongs to.
+ const eventSubject = hostDomain === "service" && typeof values[0] === "string" ? values[0] : subject
+
+ if (!this.hostVisible(hostDomain, eventSubject, delivery.owner)) return
+
+ // One fact reaches this Server once on each route, however many of its
+ // subscriptions it matches; the Server hands it to each of its listeners.
+ const routes = this.deliveredFacts.get(delivery) ?? new Set<string>()
+
+ if (routes.has(route)) return
- const eventSubject = hostDomain === "service" && typeof values[0] === "string"
- ? values[0]
- : typeof values[0] === "object" && values[0] !== null && "reference" in values[0]
- ? String((values[0] as { reference: unknown }).reference)
- : subject
+ routes.add(route)
- if (!this.hostVisible(hostDomain, eventSubject)) return
+ this.deliveredFacts.set(delivery, routes)
this.deliver(route, word, ...values).catch(() => undefined)
}))
@@ -540,4 +551,4 @@ export type { Stream } from "@server/core/server-runtime"
export type Ending = (code: number | null, signal: NodeJS.Signals | null) => void
-export type HostVisibility = (domain: "program" | "process" | "connection" | "session" | "service" | "window" | "permission" | "opening" | "log" | "programLog" | "clientMemory", subject: string | null) => boolean
+export type HostVisibility = (domain: HostDomain, subject: string | null, owner: HostOwner) => boolean
diff --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.ts
index 74ccbd8..a402adb 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
@@ -266,8 +266,8 @@ export default class ProgramManager extends TheLink {
const permissions = entry.permissions()
await Promise.all([
- this.authManager.processManager.announceHost("program", "permissions", entry.identity, entry),
- this.authManager.processManager.announceSubject("program", "permissions", entry.program.reference, permissions),
+ this.authManager.processManager.announceHost("program", entry.identity, "permissions", entry.identity, entry),
+ this.authManager.processManager.announceSubject("program", entry.identity, "permissions", entry.program.reference, permissions),
this.$outbound.publish("/permissions-change", entry.record())
])
}
@@ -479,7 +479,7 @@ export default class ProgramManager extends TheLink {
// replayed event history, so only runtime registrations use this.
private async created(entry: Entry) {
- await this.authManager.processManager.announceHost("program", "create", entry.identity, entry)
+ await this.authManager.processManager.announceHost("program", entry.identity, "create", entry.identity, entry)
await this.$outbound.publish("/create", entry.record())
}
@@ -548,7 +548,7 @@ export default class ProgramManager extends TheLink {
const store = new ProgramStoreState(program.storagePath, (key, snapshot) => {
// The System is the only publisher; endpoint adapters must consume, never echo, this fact.
- this.authManager.processManager.announceSubject("program", "storeChange", program.reference, key, snapshot).catch(() => undefined)
+ this.authManager.processManager.announceSubject("program", program.identity, "storeChange", program.reference, key, snapshot).catch(() => undefined)
this.$outbound.publish("/store-change", program.reference, key, snapshot).catch(() => undefined)
})
@@ -571,7 +571,7 @@ export default class ProgramManager extends TheLink {
// post-commit record to observers that are authorized to see it.
this.$outbound.publish("/log", program.reference, record).catch(() => undefined)
- this.authManager.processManager.announceSubject("programLog", "log", program.reference, record).catch(() => undefined)
+ this.authManager.processManager.announceSubject("programLog", program.identity, "log", program.reference, record).catch(() => undefined)
})
this.said.set(program.identity, logs)
@@ -707,7 +707,7 @@ export default class ProgramManager extends TheLink {
state.setPinned(pinned)
const entry = this.find(program.identity)
- await this.authManager.processManager.announceHost("program", "pinned", entry.identity, entry, pinned)
+ await this.authManager.processManager.announceHost("program", entry.identity, "pinned", entry.identity, entry, pinned)
await this.$outbound.publish("/pinned", entry.record(), pinned)
return pinned
}
@@ -1141,7 +1141,7 @@ export default class ProgramManager extends TheLink {
if (createdHere) await this.created(entry)
- await this.authManager.processManager.announceHost("program", "install", entry.identity, entry)
+ await this.authManager.processManager.announceHost("program", entry.identity, "install", entry.identity, entry)
// Installation is shared registry state, so it is announced
// independently of the response returned to its caller.
@@ -1249,9 +1249,9 @@ export default class ProgramManager extends TheLink {
entry.installed = false
- await this.authManager.processManager.announceHost("program", "uninstall", entry.identity, entry, purge)
+ await this.authManager.processManager.announceHost("program", entry.identity, "uninstall", entry.identity, entry, purge)
- await this.authManager.processManager.announceSubject("program", "uninstall", entry.program.reference, purge)
+ await this.authManager.processManager.announceSubject("program", entry.identity, "uninstall", entry.program.reference, purge)
await this.$outbound.publish("/uninstall", entry.record(), purge)
@@ -1280,9 +1280,9 @@ export default class ProgramManager extends TheLink {
this.programs.delete(entry.identity)
- await this.authManager.processManager.announceHost("program", "forget", entry.identity, entry)
+ await this.authManager.processManager.announceHost("program", entry.identity, "forget", entry.identity, entry)
- await this.authManager.processManager.announceSubject("program", "forget", entry.program.reference)
+ await this.authManager.processManager.announceSubject("program", entry.identity, "forget", entry.program.reference)
await this.$outbound.publish("/forget", entry.identity)
diff --git a/source/server/core/link-manager/link-manager.ts b/source/server/core/link-manager/link-manager.ts
index 7ddae7e..d97521e 100644
--- a/source/server/core/link-manager/link-manager.ts
+++ b/source/server/core/link-manager/link-manager.ts
@@ -122,9 +122,9 @@ export default class LinkManager extends TheLink {
await Promise.all([
- this.authManager.processManager.announceSubject("connection", "disconnect", connection.identity),
+ this.authManager.processManager.announceSubject("connection", null, "disconnect", connection.identity),
- this.authManager.processManager.announceHost("connection", "disconnect", connection.identity, snapshot),
+ this.authManager.processManager.announceHost("connection", null, "disconnect", connection.identity, snapshot),
this.authManager.$outbound.publish("/connection/disconnect", snapshot)
])
@@ -370,7 +370,7 @@ export default class LinkManager extends TheLink {
await Promise.all([
- this.authManager.processManager.announceHost("connection", "create", connection.identity, snapshot),
+ this.authManager.processManager.announceHost("connection", null, "create", connection.identity, snapshot),
this.authManager.$outbound.publish("/connection/create", snapshot)
])
@@ -415,7 +415,7 @@ export default class LinkManager extends TheLink {
await Promise.all([
- this.authManager.processManager.announceSubject("connection", "sessionChange", connection.identity, session),
+ this.authManager.processManager.announceSubject("connection", null, "sessionChange", connection.identity, session),
this.authManager.$outbound.publish("/connection/session-change", this.connectionSnapshot(connection), session)
])
@@ -427,7 +427,7 @@ export default class LinkManager extends TheLink {
await Promise.all([
- this.authManager.processManager.announceHost("session", "create", identity, session),
+ this.authManager.processManager.announceHost("session", null, "create", identity, session),
this.authManager.$outbound.publish("/session/create", session)
])
@@ -445,7 +445,7 @@ export default class LinkManager extends TheLink {
await Promise.all([
- this.authManager.processManager.announceSubject("session", event, identity, snapshot),
+ this.authManager.processManager.announceSubject("session", null, event, identity, snapshot),
this.authManager.$outbound.publish(route, session, snapshot)
])
@@ -457,9 +457,9 @@ export default class LinkManager extends TheLink {
await Promise.all([
- this.authManager.processManager.announceSubject("session", "end", identity, reason),
+ this.authManager.processManager.announceSubject("session", null, "end", identity, reason),
- this.authManager.processManager.announceHost("session", "end", identity, session, reason),
+ this.authManager.processManager.announceHost("session", null, "end", identity, session, reason),
this.authManager.$outbound.publish(
"/session/end",
diff --git a/source/server/core/opening-manager.ts b/source/server/core/opening-manager.ts
index 977adb0..31607fa 100644
--- a/source/server/core/opening-manager.ts
+++ b/source/server/core/opening-manager.ts
@@ -84,7 +84,7 @@ export default class OpeningManager extends TheLink {
this.pendingRequests.set(identity, { snapshot, settle, cancelEndpoint, timer: null, resolving: false })
await Promise.all([
- this.authManager.processManager.announceHost("opening", "request", "system", snapshot).catch(() => undefined),
+ this.authManager.processManager.announceHost("opening", null, "request", "system", snapshot).catch(() => undefined),
this.$outbound.publish("/request", snapshot).catch(() => undefined)
])
@@ -231,7 +231,7 @@ export default class OpeningManager extends TheLink {
request.cancelEndpoint()
await Promise.all([
- this.authManager.processManager.announceHost("opening", "resolve", "system", request.snapshot, chosen).catch(() => undefined),
+ this.authManager.processManager.announceHost("opening", null, "resolve", "system", request.snapshot, chosen).catch(() => undefined),
this.$outbound.publish("/resolve", request.snapshot, chosen).catch(() => undefined)
])
diff --git a/source/server/core/permission-manager.ts b/source/server/core/permission-manager.ts
index db431ad..2c69deb 100644
--- a/source/server/core/permission-manager.ts
+++ b/source/server/core/permission-manager.ts
@@ -77,7 +77,7 @@ export default class PermissionManager extends TheLink {
})
await Promise.all([
- this.authManager.processManager.announceHost("permission", "request", process.program.identity, snapshot).catch(() => undefined),
+ this.authManager.processManager.announceHost("permission", null, "request", process.program.identity, snapshot).catch(() => undefined),
this.$outbound.publish("/request", snapshot).catch(() => undefined)
])
@@ -167,6 +167,7 @@ export default class PermissionManager extends TheLink {
await Promise.all([
this.authManager.processManager.announceHost(
"permission",
+ null,
"resolve",
request.process.program.identity,
request.snapshot,
@@ -174,6 +175,7 @@ export default class PermissionManager extends TheLink {
).catch(() => undefined),
this.authManager.processManager.announceSubject(
"permission",
+ null,
"resolve",
request.snapshot.identity,
request.snapshot,
diff --git a/tests/host-traffic.test.ts b/tests/host-traffic.test.ts
index bc63265..84dc1d4 100644
--- a/tests/host-traffic.test.ts
+++ b/tests/host-traffic.test.ts
@@ -16,8 +16,8 @@ test("host traffic contract", async () => {
traffic.observe("program", "uninstall", null, (_delivery, event, ...values) => hostProgram.push([event, ...values]))
traffic.observe("program", "uninstall", "program-reference", (_delivery, event, ...values) => ownProgram.push([event, ...values]))
- traffic.observe("process", "create", null, (_delivery, event, ...values) => hostProcess.push([event, ...values]))
- traffic.observe("process", "create", "program-reference", (_delivery, event, ...values) => programProcess.push([event, ...values]))
+ traffic.observe("process", "create", null, (delivery, event, ...values) => hostProcess.push([delivery.owner, event, ...values]))
+ traffic.observe("process", "create", "program-reference", (delivery, event, ...values) => programProcess.push([delivery.owner, event, ...values]))
traffic.observe("connection", "create", null, (_delivery, event, ...values) => hostConnection.push([event, ...values]))
traffic.observe("session", "connectionAttach", "session-identity", (_delivery, event, ...values) => ownSession.push([event, ...values]))
traffic.observe("log", "log", null, (_delivery, event, ...values) => systemLogs.push([event, ...values]))
@@ -27,25 +27,27 @@ test("host traffic contract", async () => {
const program = { identity: "counter", reference: "program-reference" }
const process = { identity: "worker", reference: "process-reference" }
- await traffic.emitHost("program", "uninstall", program.identity, program, true)
+ await traffic.emitHost("program", program.identity, "uninstall", program.identity, program, true)
assert.deepEqual(hostProgram, [["uninstall", program.identity, program, true]])
assert.deepEqual(ownProgram, [])
- await traffic.emitSubject("program", "uninstall", program.reference, true)
+ await traffic.emitSubject("program", program.identity, "uninstall", program.reference, true)
assert.deepEqual(ownProgram, [["uninstall", program.reference, true]])
- await traffic.emit("process", "create", program.identity, program.reference, process)
+ // A fact carries the Program it belongs to, both in the registry and to its subject's observers.
+ await traffic.emitHost("process", program.identity, "create", program.identity, process)
+ await traffic.emitSubject("process", program.identity, "create", program.reference, process)
- assert.deepEqual(hostProcess, [["create", program.identity, process]])
- assert.deepEqual(programProcess, [["create", program.reference, process]])
+ assert.deepEqual(hostProcess, [[program.identity, "create", program.identity, process]])
+ assert.deepEqual(programProcess, [[program.identity, "create", program.reference, process]])
- await traffic.emitHost("connection", "create", "connection-identity", { identity: "connection-identity" })
- await traffic.emitSubject("session", "connectionAttach", "session-identity", { identity: "connection-identity" })
- await traffic.emitHost("log", "log", "system", { kind: "started" })
- await traffic.emitSubject("programLog", "log", "program-reference", { kind: "stdout" })
- await traffic.emitSubject("program", "storeChange", "program-reference", "tab", { revision: 1, value: "colors" })
+ await traffic.emitHost("connection", null, "create", "connection-identity", { identity: "connection-identity" })
+ await traffic.emitSubject("session", null, "connectionAttach", "session-identity", { identity: "connection-identity" })
+ await traffic.emitHost("log", null, "log", "system", { kind: "started" })
+ await traffic.emitSubject("programLog", program.identity, "log", "program-reference", { kind: "stdout" })
+ await traffic.emitSubject("program", program.identity, "storeChange", "program-reference", "tab", { revision: 1, value: "colors" })
assert.deepEqual(hostConnection, [["create", "connection-identity", { identity: "connection-identity" }]])
assert.deepEqual(ownSession, [["connectionAttach", "session-identity", { identity: "connection-identity" }]])
diff --git a/tests/permission-requests.test.ts b/tests/permission-requests.test.ts
index c976229..490f8c3 100644
--- a/tests/permission-requests.test.ts
+++ b/tests/permission-requests.test.ts
@@ -85,8 +85,8 @@ test("permission request lifecycle belongs to one live Endpoint", async () => {
assert.equal(await stopped, null)
assert(!manager.pending("endpoint-stop"))
- assert(hostEvents.some(values => values[0] === "permission" && values[1] === "request"))
- assert(hostEvents.some(values => values[0] === "permission" && values[1] === "resolve"))
+ assert(hostEvents.some(values => values[0] === "permission" && values[2] === "request"))
+ assert(hostEvents.some(values => values[0] === "permission" && values[2] === "resolve"))
}
finally {
rmSync(temporary, { recursive: true, force: true })
diff --git a/tests/program-creation.test.ts b/tests/program-creation.test.ts
index 686ff7d..bb4e31a 100644
--- a/tests/program-creation.test.ts
+++ b/tests/program-creation.test.ts
@@ -121,10 +121,10 @@ test("permission changes publish one complete effective snapshot through both Pr
expect(records).toHaveLength(1)
expect(records[0]).toMatchObject({ permissions })
expect(announceHost).toHaveBeenCalledExactlyOnceWith(
- "program", "permissions", program.identity, manager.find(program.identity)
+ "program", program.identity, "permissions", program.identity, manager.find(program.identity)
)
expect(announceSubject).toHaveBeenCalledExactlyOnceWith(
- "program", "permissions", program.reference, permissions
+ "program", program.identity, "permissions", program.reference, permissions
)
})
@@ -137,9 +137,9 @@ test("pinning emits Program and global events only when the boolean changes", as
await manager.pinned(program, "pin")
await manager.pinned(program, "unpin")
- expect(announceHost.mock.calls.map(([, event, , , pinned]) => [event, pinned])).toEqual([
- ["pinned", true],
- ["pinned", false]
+ expect(announceHost.mock.calls.map(([, owner, event, , , pinned]) => [owner, event, pinned])).toEqual([
+ [program.identity, "pinned", true],
+ [program.identity, "pinned", false]
])
})
@@ -163,8 +163,8 @@ test("Program store changes originate once at System and retain their key revisi
expect(delivered.map(([, key, snapshot]) => [key, snapshot.revision, snapshot.value])).toEqual([
["tab", 1, "colors"], ["tab", 2, undefined]
])
- expect(announceSubject.mock.calls.map(([, event, subject]) => [event, subject])).toEqual([
- ["storeChange", program.reference], ["storeChange", program.reference]
+ expect(announceSubject.mock.calls.map(([, owner, event, subject]) => [owner, event, subject])).toEqual([
+ [program.identity, "storeChange", program.reference], [program.identity, "storeChange", program.reference]
])
} finally {
// The store holds an SQLite handle; Windows cannot remove its fixture until the Program releases it.
diff --git a/tests/program-installation-lifecycle.test.ts b/tests/program-installation-lifecycle.test.ts
index 930e81c..c3d1c09 100644
--- a/tests/program-installation-lifecycle.test.ts
+++ b/tests/program-installation-lifecycle.test.ts
@@ -149,11 +149,11 @@ test("uninstallation converges silently while its Program handle remains valid",
expect(entry.installed).toBe(false)
expect(await manager.pinned(program, "get")).toBe(true)
expect(existsSync(join(program.storagePath, "state.json"))).toBe(true)
- expect(announceHost.mock.calls.filter(([, event]) => event === "uninstall")).toHaveLength(1)
+ expect(announceHost.mock.calls.filter(([, , event]) => event === "uninstall")).toHaveLength(1)
await manager.uninstall(program)
- expect(announceHost.mock.calls.filter(([, event]) => event === "uninstall")).toHaveLength(1)
+ expect(announceHost.mock.calls.filter(([, , event]) => event === "uninstall")).toHaveLength(1)
expect(exitAll).not.toHaveBeenCalled()
await manager.uninstall(program, { purge: true })
@@ -161,8 +161,8 @@ test("uninstallation converges silently while its Program handle remains valid",
expect(manager.programs.has(program.identity)).toBe(false)
expect(existsSync(home)).toBe(false)
expect(exitAll).toHaveBeenCalledExactlyOnceWith(program.identity, null)
- expect(announceHost.mock.calls.filter(([, event]) => event === "uninstall")).toHaveLength(1)
- expect(announceHost.mock.calls.filter(([, event]) => event === "forget")).toHaveLength(1)
+ expect(announceHost.mock.calls.filter(([, , event]) => event === "uninstall")).toHaveLength(1)
+ expect(announceHost.mock.calls.filter(([, , event]) => event === "forget")).toHaveLength(1)
await expect(manager.uninstall(program)).rejects.toThrow("does not know this program")
})
diff --git a/tests/server-process-boundary.test.ts b/tests/server-process-boundary.test.ts
index 70a9121..068d849 100644
--- a/tests/server-process-boundary.test.ts
+++ b/tests/server-process-boundary.test.ts
@@ -1,7 +1,7 @@
import assert from "node:assert/strict"
import type { ServerRuntime } from "@server/core/server-runtime"
import ServerProcessBoundary from "@server/core/link-manager/auth-manager/process-manager/server-process-boundary"
-import type HostTraffic from "@server/core/link-manager/auth-manager/process-manager/host-traffic"
+import HostTraffic from "@server/core/link-manager/auth-manager/process-manager/host-traffic"
import type { Tunnel } from "@the-link/core"
import { test } from "vitest"
@@ -30,3 +30,71 @@ test("the Server boundary receives readiness already buffered by its runtime", a
assert.equal(boundary.ready, true)
})
+
+test("the Server boundary delivers Program and Process facts by the Program they belong to", async () => {
+
+ const sent: unknown[][] = []
+
+ let receive!: (event: string, ...values: unknown[]) => void
+
+ const runtime: ServerRuntime = {
+ finished: new Promise(() => undefined),
+ send(...values) { sent.push(values) },
+ onOutput() {},
+ stop() {},
+ onMessage(listener) { receive = listener }
+ }
+
+ const traffic = new HostTraffic()
+
+ const asked: unknown[][] = []
+
+ // This Server may see the "counter" Program and nothing else.
+ new ServerProcessBoundary(runtime, false, false, async () => undefined, () => undefined, traffic, {} as Tunnel, (domain, subject, owner) => {
+
+ asked.push([domain, subject, owner])
+
+ return owner === "counter"
+ })
+
+ receive("boundary", "subscribe", "every-create", "publish", "host-process", "create", null, false)
+ // A second listener for the same fact must not make the fact arrive twice.
+ receive("boundary", "subscribe", "every-create-again", "publish", "host-process", "create", null, false)
+ receive("boundary", "subscribe", "every-exit", "publish", "host-process", "exit", null, false)
+ receive("boundary", "subscribe", "program-create", "publish", "host-program", "create", null, false)
+ receive("boundary", "subscribe", "one-exit", "publish", "process-host", "exit", "process-reference", false)
+
+ await new Promise(resolve => setTimeout(resolve))
+
+ const counter = { identity: "counter", reference: "program-reference" }
+ const process = { identity: "worker", reference: "process-reference", program: "counter" }
+ const hidden = { identity: "hidden", reference: "hidden-process", program: "hidden" }
+
+ // The registry names the Program first and the Process after it.
+ await traffic.emitHost("process", "counter", "create", "counter", process)
+ await traffic.emitHost("process", "hidden", "create", "hidden", hidden)
+ await traffic.emitHost("program", "counter", "create", "counter", counter)
+
+ // An ending is announced once the Process is gone, so nothing about it could be looked up.
+ await traffic.emitHost("process", "counter", "exit", "counter", process, 0, null)
+ await traffic.emitSubject("process", "counter", "exit", "process-reference", 0, null)
+
+ await new Promise(resolve => setTimeout(resolve))
+
+ assert.deepEqual(sent, [
+ ["host-process", "create", "counter", process],
+ ["host-program", "create", "counter", counter],
+ ["host-process", "exit", "counter", process, 0, null],
+ ["process-host", "exit", "process-reference", 0, null]
+ ])
+
+ assert.deepEqual(asked, [
+ ["process", null, "counter"],
+ ["process", null, "counter"],
+ ["process", null, "hidden"],
+ ["process", null, "hidden"],
+ ["program", null, "counter"],
+ ["process", null, "counter"],
+ ["process", "process-reference", "counter"]
+ ])
+})