Skip to content

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

Make services exact endpoint bindings

zohayrslileh0fa331a772c1
diff --git a/scripts/verify-endpoint-services.mjs b/scripts/verify-endpoint-services.mjs
index e821c4f..7048a79 100644
--- a/scripts/verify-endpoint-services.mjs
+++ b/scripts/verify-endpoint-services.mjs
@@ -8,25 +8,25 @@ const privateProgram = new Program({
})
const publicProgram = new Program({
identity: "public-program",
- server: { location: ".", startCommand: "true", serviceable: true },
- client: { location: ".", serviceable: true }
+ server: { location: ".", startCommand: "true", serviceDocs: "server-api.md" },
+ client: { location: ".", serviceDocs: "client-api.md" }
})
-assert.equal(privateProgram.record().server.serviceable, false)
-assert.equal(publicProgram.record().server.serviceable, true)
-assert.equal(publicProgram.record().client.serviceable, true)
+assert.equal(privateProgram.record().server.hasService, false)
+assert.equal(publicProgram.record().server.hasService, true)
+assert.equal(publicProgram.record().client.hasService, true)
assert.throws(() => new Program({
identity: "invalid-program",
- server: { location: ".", startCommand: "true", serviceable: "yes" }
-}), /serviceable capability/)
+ server: { location: ".", startCommand: "true", serviceDocs: "" }
+}), /serviceDocs/)
-function process(reference, program = "program", serviceable = true) {
+function process(reference, program = "program", hasService = true) {
return {
reference,
program: {
identity: program,
- server: { serviceable },
- client: { serviceable }
+ server: { serviceDocs: hasService ? "server-api.md" : undefined },
+ client: { serviceDocs: hasService ? "client-api.md" : undefined }
},
server: {},
client: {}
@@ -39,31 +39,25 @@ const conflicting = process("conflicting")
const privateEndpoint = process("private", "private", false)
const key = { program: "program", endpoint: "server", name: "counter" }
const lifecycle = []
-const registry = []
const publications = []
let disableTransportCompleted = false
assert.equal(services.enabled(key), false)
-assert.deepEqual(services.list(), [])
assert.equal(services.service(provider, "server"), null)
-await assert.rejects(() => services.enable(privateEndpoint, "server", { name: "counter" }), /not serviceable/)
-await assert.rejects(() => services.enable(privateEndpoint, "client", { name: "counter" }), /not serviceable/)
+await assert.rejects(() => services.enable(privateEndpoint, "server", "counter"), /declared no Service documentation/)
+await assert.rejects(() => services.enable(privateEndpoint, "client", "counter"), /declared no Service documentation/)
services.follow(key, "lifecycle", null, event => lifecycle.push(event))
-services.followRegistry(null, (event, service) => registry.push([event, service]))
services.follow(key, "lifecycle", "disable", async () => {
await Promise.resolve()
disableTransportCompleted = true
})
-await services.enable(provider, "server", { name: "counter", docs: "# Counter" })
+await services.enable(provider, "server", "counter")
assert.deepEqual(services.service(provider, "server"), key)
assert.equal(services.enabled(key), true)
-assert.deepEqual(services.list(), [key])
-assert.equal(services.docs(key), "# Counter")
assert.deepEqual(lifecycle, ["enable"])
-assert.deepEqual(registry, [["enable", key]])
await services.waitReady(key, 0)
// Subscriptions are future-only: joining after enable does not replay it.
@@ -71,8 +65,8 @@ const lateLifecycle = []
services.follow(key, "lifecycle", null, event => lateLifecycle.push(event))
assert.deepEqual(lateLifecycle, [])
-await assert.rejects(() => services.enable(provider, "server", { name: "other" }), /already exposes/)
-await assert.rejects(() => services.enable(conflicting, "server", { name: "counter" }), /already enabled/)
+await assert.rejects(() => services.enable(provider, "server", "other"), /already exposes/)
+await assert.rejects(() => services.enable(conflicting, "server", "counter"), /already enabled/)
services.follow(key, "channel", "change", (_event, payload) => publications.push(payload))
@@ -84,31 +78,29 @@ assert.deepEqual(publications, [2])
await services.release(provider, "server")
assert.equal(services.enabled(key), false)
-assert.deepEqual(services.list(), [])
-assert.throws(() => services.docs(key), /disabled/)
assert.deepEqual(lifecycle, ["enable", "disable"])
assert.deepEqual(lateLifecycle, ["disable"])
assert.equal(disableTransportCompleted, true)
-assert.deepEqual(registry, [["enable", key], ["disable", key]])
await assert.rejects(() => services.waitReady(key, 0), /timeout/)
// Once disabled, endpoint output is no longer mirrored into the service.
await services.emit(provider, "server", "change", 3)
assert.deepEqual(publications, [2])
-// A later Endpoint incarnation may explicitly claim the same stable key.
-await services.enable(conflicting, "server", { name: "counter" })
-assert.equal(services.docs(key), null)
+// Exact readiness observation does not require a global Service registry.
+const ready = services.waitReady(key, 100)
+await services.enable(conflicting, "server", "counter")
+await ready
assert.deepEqual(lifecycle, ["enable", "disable", "enable"])
await services.release(conflicting, "server")
// Server and Client are distinct coordinates even under one Program and name.
-await services.enable(provider, "client", { name: "counter" })
+await services.enable(provider, "client", "counter")
assert.equal(services.enabled({ ...key, endpoint: "client" }), true)
assert.equal(services.enabled(key), false)
await services.release(provider, "client")
-await assert.rejects(() => services.enable(provider, "client", { name: "counter", docs: "no" }), /cannot provide/)
+await assert.rejects(() => services.enable(provider, "client", { name: "counter" }), /service name/)
provider.server = null
-await assert.rejects(() => services.enable(provider, "server", { name: "counter" }), /not running/)
+await assert.rejects(() => services.enable(provider, "server", "counter"), /not running/)
diff --git a/source/client/core/link-manager/auth-manager/process-manager/process-manager.ts b/source/client/core/link-manager/auth-manager/process-manager/process-manager.ts
index ea0b536..2e1bdad 100644
--- a/source/client/core/link-manager/auth-manager/process-manager/process-manager.ts
+++ b/source/client/core/link-manager/auth-manager/process-manager/process-manager.ts
@@ -110,11 +110,6 @@ export default class ProcessManager extends TheLink {
await this.$outbound.publishFirst("/service/wait-ready", key, timeout)
}
- public async serviceDocs(key: ServiceKey) {
-
- return await this.$outbound.publishFirst("/service/docs", key) as string | null
- }
-
/** Registers one exact service interest for this frame lease. */
public async followService(pane: string, owner: string, subscription: string, key: ServiceKey, scope: ServiceScope, event: string | null) {
diff --git a/source/client/core/link-manager/auth-manager/program-manager/program-manager.ts b/source/client/core/link-manager/auth-manager/program-manager/program-manager.ts
index c46b4c9..57e63c1 100644
--- a/source/client/core/link-manager/auth-manager/program-manager/program-manager.ts
+++ b/source/client/core/link-manager/auth-manager/program-manager/program-manager.ts
@@ -93,6 +93,12 @@ export default class ProgramManager extends TheLink {
return await this.$outbound.publishFirst("/icon", identity, size) as number[]
}
+ /** Reads one declared Endpoint's Service documentation from authoritative Core. */
+ public async docs(identity: string, endpoint: "server" | "client") {
+
+ return await this.$outbound.publishFirst("/docs", identity, endpoint) as string | null
+ }
+
@Subscribe("/install")
@Publish("/programs", "inbound")
protected async installed(payload: HostedEntry | null) {
diff --git a/source/client/view/desktop/client-host/host.ts b/source/client/view/desktop/client-host/host.ts
index a63abdc..842e507 100644
--- a/source/client/view/desktop/client-host/host.ts
+++ b/source/client/view/desktop/client-host/host.ts
@@ -166,13 +166,6 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
return [await processManager.waitServiceReady(args[0], args[1] as number | undefined)]
}
- if (word === "service-docs") {
-
- if (!isServiceKey(args[0]) || args[0].endpoint !== "server") throw new Error("A Server service key is required")
-
- return [await processManager.serviceDocs(args[0])]
- }
-
if (word === "service-follow") {
const [subscription, key, scope, event] = args
@@ -437,6 +430,13 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
return [sdkProgram(program)]
}
+ if (word === "endpoint-docs") {
+
+ if (args[0] !== "server" && args[0] !== "client") throw new Error("A Program Endpoint is server or client")
+
+ return [await programManager.docs(process().program, args[0])]
+ }
+
// Icon bytes are requested only when this frame asks. The frame cannot
// provide an identity; its owning Process determines the Program.
if (word === "icon") return [await programManager.icon(process().program, args[0] as ProgramIconSize)]
diff --git a/source/server/core/link-manager/auth-manager/process-manager/endpoint-services.ts b/source/server/core/link-manager/auth-manager/process-manager/endpoint-services.ts
index cb50bc8..f51c621 100644
--- a/source/server/core/link-manager/auth-manager/process-manager/endpoint-services.ts
+++ b/source/server/core/link-manager/auth-manager/process-manager/endpoint-services.ts
@@ -20,11 +20,9 @@ export default class EndpointServices extends TheLink {
const declaration = endpoint === "server" ? process.program.server : process.program.client
- if (!declaration?.serviceable) throw new Error(`The ${endpoint} endpoint is not serviceable`)
+ if (!declaration?.serviceDocs) throw new Error(`The ${endpoint} endpoint declared no Service documentation`)
- const definition = this.definition(value, endpoint)
-
- const { name } = definition
+ const name = this.name(value)
if (!this.live(process, endpoint)) throw new Error(`The ${endpoint} endpoint is not running`)
@@ -38,14 +36,11 @@ export default class EndpointServices extends TheLink {
if (this.bindings.has(identity)) throw new Error(`The "${name}" ${endpoint} service is already enabled for this Program`)
- this.bindings.set(identity, { key, process, endpoint, docs: definition.docs })
+ this.bindings.set(identity, { key, process, endpoint })
this.owners.set(owner, identity)
- await Promise.all([
- this.$inbound.publish(this.event(key, "lifecycle", "enable"), "enable", undefined),
- this.$inbound.publish(this.registryEvent("enable"), "enable", key)
- ])
+ await this.$inbound.publish(this.event(key, "lifecycle", "enable"), "enable", undefined)
return key
}
@@ -74,10 +69,7 @@ export default class EndpointServices extends TheLink {
this.bindings.delete(identity)
- await Promise.all([
- this.$inbound.publish(this.event(binding.key, "lifecycle", "disable"), "disable", undefined),
- this.$inbound.publish(this.registryEvent("disable"), "disable", binding.key)
- ])
+ await this.$inbound.publish(this.event(binding.key, "lifecycle", "disable"), "disable", undefined)
return true
}
@@ -89,11 +81,6 @@ export default class EndpointServices extends TheLink {
return identity ? this.bindings.get(identity)?.key ?? null : null
}
- public list() {
-
- return Object.freeze([...this.bindings.values()].map(binding => binding.key))
- }
-
public enabled(key: unknown) {
return this.bindings.has(this.identity(this.key(key)))
@@ -120,29 +107,13 @@ export default class EndpointServices extends TheLink {
stop()
complete()
}
- const stop = this.$inbound.subscribe(this.registryEvent("enable"), (_event, enabled) => {
-
- if (this.identity(this.key(enabled)) === this.identity(resolved)) finish(resolve)
- })
+ const stop = this.$inbound.subscribe(this.event(resolved, "lifecycle", "enable"), () => finish(resolve))
const timer = setTimeout(() => finish(() => reject(new Error("The service did not become ready before the timeout"))), timeout)
if (this.enabled(resolved)) finish(resolve)
})
}
- public docs(key: unknown) {
-
- const resolved = this.key(key)
-
- if (resolved.endpoint !== "server") throw new Error("Only a Server service can provide API documentation")
-
- const binding = this.binding(resolved, "server")
-
- if (!binding) throw new Error("The service is disabled")
-
- return binding.docs
- }
-
public binding(key: unknown, endpoint?: Half) {
const binding = this.bindings.get(this.identity(this.key(key))) ?? null
@@ -178,20 +149,6 @@ export default class EndpointServices extends TheLink {
}, prefix)
}
- public followRegistry(event: string | null, subscriber: Subscriber) {
-
- const prefix = "registry/"
-
- if (event !== null) {
-
- if (event !== "enable" && event !== "disable") throw new Error("A service registry event must be enable or disable")
-
- return this.$inbound.subscribe(this.registryEvent(event), (_word, key) => subscriber(event, key))
- }
-
- return this.$inbound.forwardTo((word, _event, key) => subscriber(decodeURIComponent(word), key), prefix)
- }
-
private key(value: unknown) {
if (!isServiceKey(value)) throw new Error("A complete service key is required")
@@ -199,19 +156,11 @@ export default class EndpointServices extends TheLink {
return value
}
- private definition(value: unknown, endpoint: Half) {
-
- if (typeof value !== "object" || value === null) throw new Error("A service definition is required")
+ private name(value: unknown) {
- const candidate = value as { name?: unknown, docs?: unknown }
+ if (typeof value !== "string" || value.length === 0) throw new Error("A service name must be a non-empty string")
- if (typeof candidate.name !== "string" || candidate.name.length === 0) throw new Error("A service name must be a non-empty string")
-
- if (candidate.docs !== undefined && typeof candidate.docs !== "string") throw new Error("Service documentation must be a string")
-
- if (endpoint === "client" && candidate.docs !== undefined) throw new Error("A Client service cannot provide API documentation")
-
- return { name: candidate.name, docs: endpoint === "server" ? candidate.docs ?? null : null }
+ return value
}
private live(process: Process, endpoint: Half) {
@@ -239,10 +188,6 @@ export default class EndpointServices extends TheLink {
return this.prefix(key, scope) + encodeURIComponent(event)
}
- private registryEvent(event: string) {
-
- return `registry/${encodeURIComponent(event)}`
- }
}
export type ServiceScope = Scope
@@ -258,8 +203,6 @@ type Binding = Readonly<{
process: Process
endpoint: Half
-
- docs: string | null
}>
type Subscriber = (event: string, payload: unknown) => unknown
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 9a240be..52581b5 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
@@ -1054,12 +1054,6 @@ export default class ProcessManager extends TheLink {
await this.services.waitReady(key, timeout)
}
- @Connect("/service/docs")
- protected async serviceDocs(key: unknown) {
-
- return this.services.docs(key)
- }
-
@Subscribe("/service/send")
protected async sendClientService(source: string, key: unknown, event: unknown, payload: unknown) {
@@ -1289,6 +1283,13 @@ export default class ProcessManager extends TheLink {
if (word === "current-program") return [programManager.find(process.program.identity)]
+ if (word === "endpoint-docs") {
+
+ const program = this.heldProgram(rest[0], process.program)
+
+ return [await programManager.docs(program.identity, rest[1])]
+ }
+
if (word === "current-process") return [processReference(process)]
if (word === "icon") {
@@ -1550,14 +1551,10 @@ export default class ProcessManager extends TheLink {
return [this.services.service(endpoint, rest[1])]
}
- if (word === "host-service-list") return [this.services.list()]
-
if (word === "service-enabled") return [this.services.enabled(rest[0])]
if (word === "service-wait-ready") return [await this.services.waitReady(rest[0], rest[1])]
- if (word === "service-docs") return [this.services.docs(rest[0])]
-
if (word === "service-follow") {
const [subscription, key, scope, event] = rest
@@ -1580,24 +1577,6 @@ export default class ProcessManager extends TheLink {
return []
}
- if (word === "service-registry-follow") {
-
- const [subscription, event] = rest
-
- if (typeof subscription !== "string" || event !== null && typeof event !== "string") return []
-
- process.server?.followServiceRegistry(this.services, subscription, event)
-
- return []
- }
-
- if (word === "service-registry-unfollow") {
-
- process.server?.unfollowService(String(rest[0]))
-
- return []
- }
-
if (word === "service-send") {
const [key, event, payload] = rest
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 9bb19e7..0999622 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
@@ -219,21 +219,6 @@ export default class ServerProcessBoundary extends ProcessLink {
this.serviceSubscriptions.delete(subscription)
}
- /** Follows lifecycle changes across the authoritative service registry. */
- public followServiceRegistry(services: EndpointServices, subscription: string, event: string | null) {
-
- this.unfollowService(subscription)
-
- const stop = services.followRegistry(event, (word, key) => {
-
- const values = event === null ? [word, key] : [key]
-
- return this.deliver("service-registry-event", subscription, ...values)
- })
-
- this.serviceSubscriptions.set(subscription, stop)
- }
-
/** Remove a question whether it is waiting here or inside the SDK. */
public forget(question: string) {
diff --git a/source/server/core/link-manager/auth-manager/program-manager/config.ts b/source/server/core/link-manager/auth-manager/program-manager/config.ts
index f62d721..23878a6 100644
--- a/source/server/core/link-manager/auth-manager/program-manager/config.ts
+++ b/source/server/core/link-manager/auth-manager/program-manager/config.ts
@@ -49,9 +49,9 @@ export interface ServerConfig {
// Whether a Process starts this endpoint by default. True when omitted.
start?: boolean
- // Whether this endpoint may expose its Channel as a public Service.
- // False when omitted.
- serviceable?: boolean
+ // Markdown file documenting the Service this endpoint may expose.
+ // Presence grants the endpoint permission to enable one Service.
+ serviceDocs?: string
// Run once before the program is first started, in that directory.
installCommand?: string
@@ -73,9 +73,9 @@ export interface ClientConfig {
// Whether a Process starts this endpoint by default. True when omitted.
start?: boolean
- // Whether this endpoint may expose its Channel as a public Service.
- // False when omitted.
- serviceable?: boolean
+ // Markdown file documenting the Service this endpoint may expose.
+ // Presence grants the endpoint permission to enable one Service.
+ serviceDocs?: string
// What its window is called when it opens. The window owns its title
// from then on and may change it; this is what it is born with, so
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 99b50ed..d2b65d2 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
@@ -217,7 +217,7 @@ export default class ProgramManager extends TheLink {
// is not holding on disk.
if (typeof source.storage !== "string") throw new Error("A created program names its storage")
- for (const [what, place] of [["storage", source.storage], ["icon", source.icon], ["server", source.server?.location]] as const) {
+ for (const [what, place] of [["storage", source.storage], ["icon", source.icon], ["server", source.server?.location], ["server serviceDocs", source.server?.serviceDocs], ["client serviceDocs", source.client?.serviceDocs]] as const) {
if (place === undefined) continue
@@ -386,6 +386,15 @@ export default class ProgramManager extends TheLink {
return [...await this.icons.render(this.reachOrRefuse(identity), size)]
}
+ /** Reads a declared Endpoint's Service documentation before it is started. */
+ @Connect("/docs")
+ public async docs(identity: string, endpoint: unknown) {
+
+ if (endpoint !== "server" && endpoint !== "client") throw new Error("A Program Endpoint is server or client")
+
+ return this.reachOrRefuse(identity).serviceDocs(endpoint)
+ }
+
/** Read or change the system-managed startup launch for one Program. */
public async startup(program: Program, operation: string, value?: unknown): Promise<Launch | null | void> {
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.ts
index 4e3134f..ad4c735 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
@@ -97,8 +97,7 @@ export default class Program {
return this.config.server ? {
...this.config.server,
- start: this.config.server.start ?? true,
- serviceable: this.config.server.serviceable ?? false
+ start: this.config.server.start ?? true
} : null
}
@@ -106,8 +105,7 @@ export default class Program {
return this.config.client ? {
...this.config.client,
- start: this.config.client.start ?? true,
- serviceable: this.config.client.serviceable ?? false
+ start: this.config.client.start ?? true
} : null
}
@@ -176,6 +174,16 @@ export default class Program {
return this.config.icon ? this.place(this.config.icon) : null
}
+ /** Reads the documentation that declares one Endpoint's Service capability. */
+ public serviceDocs(endpoint: "server" | "client") {
+
+ const declared = this.config[endpoint]?.serviceDocs
+
+ if (!declared) return null
+
+ return readFileSync(this.place(declared), "utf-8")
+ }
+
/** SDK Program record; internal references are consumed by the SDK boundary. */
public record() {
@@ -201,14 +209,14 @@ export default class Program {
start: server.start,
- serviceable: server.serviceable
+ hasService: server.serviceDocs !== undefined
},
client: client && {
start: client.start,
- serviceable: client.serviceable,
+ hasService: client.serviceDocs !== undefined,
title: client.title ?? null,
@@ -250,6 +258,13 @@ export default class Program {
if (this.iconPath) await validateIcon(this.iconPath)
+ for (const endpoint of ["server", "client"] as const) {
+
+ const declared = this.config[endpoint]?.serviceDocs
+
+ if (declared && !file(this.place(declared))) throw new Error(`The ${endpoint} service documentation is not there: ${this.place(declared)}`)
+ }
+
}
// Preparing a server half, when it says it needs preparing. Run
@@ -304,7 +319,7 @@ function coherent(config: ProgramConfig) {
if (declared.start !== undefined && typeof declared.start !== "boolean") throw new Error(`A declared ${half} endpoint's start default must be true or false`)
- if (declared.serviceable !== undefined && typeof declared.serviceable !== "boolean") throw new Error(`A declared ${half} endpoint's serviceable capability must be true or false`)
+ if (declared.serviceDocs !== undefined && (typeof declared.serviceDocs !== "string" || declared.serviceDocs.trim().length === 0)) throw new Error(`A declared ${half} endpoint's serviceDocs must be a non-empty path`)
}
if (config.client && /^https?:\/\//i.test(config.client.location)) {
@@ -343,9 +358,8 @@ function coherent(config: ProgramConfig) {
return config
}
-type Resolved<Half extends { start?: boolean, serviceable?: boolean }> = Omit<Half, "start" | "serviceable"> & {
+type Resolved<Half extends { start?: boolean }> = Omit<Half, "start"> & {
start: boolean
- serviceable: boolean
}
function directory(path: string) {
@@ -353,6 +367,11 @@ function directory(path: string) {
return existsSync(path) && statSync(path).isDirectory()
}
+function file(path: string) {
+
+ return existsSync(path) && statSync(path).isFile()
+}
+
function execute(command: string, cwd: string) {