Skip to content

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

Move API documentation authority to services

zohayrslileh893b624bdb6e
diff --git a/scripts/verify-endpoint-services.mjs b/scripts/verify-endpoint-services.mjs
index 9a0bfe8..333ee75 100644
--- a/scripts/verify-endpoint-services.mjs
+++ b/scripts/verify-endpoint-services.mjs
@@ -27,10 +27,11 @@ services.follow(key, "lifecycle", "disable", async () => {
disableTransportCompleted = true
})
-await services.enable(provider, "server", "counter")
+await services.enable(provider, "server", { name: "counter", docs: "# Counter" })
assert.deepEqual(services.service(provider, "server"), key)
assert.equal(services.disabled(key), false)
+assert.equal(services.docs(key), "# Counter")
assert.deepEqual(lifecycle, ["enable"])
// Subscriptions are future-only: joining after enable does not replay it.
@@ -38,8 +39,8 @@ const lateLifecycle = []
services.follow(key, "lifecycle", null, event => lateLifecycle.push(event))
assert.deepEqual(lateLifecycle, [])
-await assert.rejects(() => services.enable(provider, "server", "other"), /already exposes/)
-await assert.rejects(() => services.enable(conflicting, "server", "counter"), /already enabled/)
+await assert.rejects(() => services.enable(provider, "server", { name: "other" }), /already exposes/)
+await assert.rejects(() => services.enable(conflicting, "server", { name: "counter" }), /already enabled/)
services.follow(key, "channel", "change", (_event, payload) => publications.push(payload))
@@ -51,6 +52,7 @@ assert.deepEqual(publications, [2])
await services.release(provider, "server")
assert.equal(services.disabled(key), true)
+assert.throws(() => services.docs(key), /disabled/)
assert.deepEqual(lifecycle, ["enable", "disable"])
assert.deepEqual(lateLifecycle, ["disable"])
assert.equal(disableTransportCompleted, true)
@@ -60,15 +62,18 @@ 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", "counter")
+await services.enable(conflicting, "server", { name: "counter" })
+assert.equal(services.docs(key), null)
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", "counter")
+await services.enable(provider, "client", { name: "counter" })
assert.equal(services.disabled({ ...key, endpoint: "client" }), false)
assert.equal(services.disabled(key), true)
await services.release(provider, "client")
+await assert.rejects(() => services.enable(provider, "client", { name: "counter", docs: "no" }), /cannot provide/)
+
provider.server = null
-await assert.rejects(() => services.enable(provider, "server", "counter"), /not running/)
+await assert.rejects(() => services.enable(provider, "server", { name: "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 629bfef..aa3f84e 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
@@ -56,9 +56,9 @@ export default class ProcessManager extends TheLink {
}
/** Changes the service exposed by the structurally identified Client Channel. */
- public async enableService(source: string, name: string) {
+ public async enableService(source: string, definition: unknown) {
- await this.$outbound.publishFirst("/service/enable", source, name)
+ await this.$outbound.publishFirst("/service/enable", source, definition)
}
public async disableService(source: string) {
@@ -77,6 +77,11 @@ export default class ProcessManager extends TheLink {
return await this.$outbound.publishFirst("/service/disabled", key) as boolean
}
+ 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 b10652d..c46b4c9 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
@@ -87,13 +87,6 @@ export default class ProgramManager extends TheLink {
return await this.$outbound.publishFirst("/database", identity, sql, values)
}
- // Requested on demand rather than carried in the desktop's incomplete
- // Program record. The server remains authoritative for the document.
- public async apiDocs(identity: string) {
-
- return await this.$outbound.publishFirst("/api-docs", identity) as string | null
- }
-
/** Request one rendered icon from the authoritative server Program. */
public async icon(identity: string, size: ProgramIconSize) {
diff --git a/source/client/view/desktop/client-host/host.ts b/source/client/view/desktop/client-host/host.ts
index 6c8031c..4550d22 100644
--- a/source/client/view/desktop/client-host/host.ts
+++ b/source/client/view/desktop/client-host/host.ts
@@ -220,8 +220,6 @@ export default function host(authManager: AuthManager, pane: string, space: (lay
if (word === "enable-service") {
- if (typeof args[0] !== "string" || args[0].length === 0) throw new Error("A service name must be a non-empty string")
-
await processManager.enableService(pane, args[0])
return []
@@ -250,6 +248,13 @@ export default function host(authManager: AuthManager, pane: string, space: (lay
return [await processManager.serviceDisabled(args[0])]
}
+ 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
@@ -532,11 +537,6 @@ export default function host(authManager: AuthManager, pane: string, space: (lay
return [sdkProgram(program)]
}
- // Requested content, not synchronized metadata. The frame supplies no
- // Program identity: its owning Process is the structural authority,
- // and the desktop asks the server for that Program's canonical text.
- if (word === "api-docs") return [await programManager.apiDocs(process().program)]
-
// 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 f7a16e0..0f5542a 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
@@ -16,9 +16,11 @@ export default class EndpointServices extends TheLink {
private readonly owners = new Map<string, string>()
- public async enable(process: Process, endpoint: Half, name: unknown) {
+ public async enable(process: Process, endpoint: Half, value: unknown) {
- if (typeof name !== "string" || name.length === 0) throw new Error("A service name must be a non-empty string")
+ const definition = this.definition(value, endpoint)
+
+ const { name } = definition
if (!this.live(process, endpoint)) throw new Error(`The ${endpoint} endpoint is not running`)
@@ -32,7 +34,7 @@ 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 })
+ this.bindings.set(identity, { key, process, endpoint, docs: definition.docs })
this.owners.set(owner, identity)
@@ -82,6 +84,19 @@ export default class EndpointServices extends TheLink {
return !this.bindings.has(this.identity(this.key(key)))
}
+ 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
@@ -124,6 +139,21 @@ 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")
+
+ const candidate = value as { name?: unknown, docs?: unknown }
+
+ 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 }
+ }
+
private live(process: Process, endpoint: Half) {
return endpoint === "server" ? process.server !== null : process.client !== null
@@ -163,6 +193,8 @@ 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 2f351b0..26a3e44 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
@@ -1018,13 +1018,13 @@ export default class ProcessManager extends TheLink {
}
@Connect("/service/enable")
- protected async enableClientService(source: string, name: unknown) {
+ protected async enableClientService(source: string, definition: unknown) {
const process = this.find(source)
if (!process.client) throw new Error("The providing client endpoint is not running")
- return await this.services.enable(process, "client", name)
+ return await this.services.enable(process, "client", definition)
}
@Connect("/service/disable")
@@ -1059,6 +1059,12 @@ export default class ProcessManager extends TheLink {
return this.services.disabled(key)
}
+ @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) {
@@ -1274,11 +1280,6 @@ export default class ProcessManager extends TheLink {
return [programManager.find(identity)]
}
- if (word === "api-docs") {
-
- return [this.heldProgram(rest[0], process.program).apiDocs()]
- }
-
if (word === "icon") {
return [await programManager.icon(this.heldProgram(rest[0], process.program).identity, rest[1])]
@@ -1535,6 +1536,8 @@ export default class ProcessManager extends TheLink {
if (word === "service-disabled") return [this.services.disabled(rest[0])]
+ if (word === "service-docs") return [this.services.docs(rest[0])]
+
if (word === "service-follow") {
const [subscription, key, scope, event] = rest
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 7409c0b..7971ac7 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
@@ -32,11 +32,6 @@ interface Described {
description?: string
- // The official Markdown entry point to this Program's API. It is a path
- // here, but paths never cross into an SDK; Program.apiDocs() returns only
- // its contents. Installed descriptions name the canonical api-docs.md.
- apiDocs?: string
-
// One PNG source from which hosting derives every standard size. How a
// Program is shown belongs to the Program rather than its client half — a
// headless Program is shown too.
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 106d993..1246c5d 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], ["apiDocs", source.apiDocs], ["icon", source.icon], ["server", source.server?.location]] as const) {
+ for (const [what, place] of [["storage", source.storage], ["icon", source.icon], ["server", source.server?.location]] as const) {
if (place === undefined) continue
@@ -377,15 +377,6 @@ export default class ProgramManager extends TheLink {
return this.databaseOf(this.reachOrRefuse(identity)).query(sql, values)
}
- // The official API entry point is requested, never synchronized. This is
- // the one authoritative read used by both SDK roads; the desktop derives
- // the identity from its frame before asking through this connection.
- @Connect("/api-docs")
- public async apiDocs(identity: string) {
-
- return this.reachOrRefuse(identity).apiDocs()
- }
-
/** One authoritative icon operation shared by SDKs and HTTP hosting. */
@Connect("/icon")
public async icon(identity: string, size: unknown) {
@@ -1280,18 +1271,6 @@ function copy(program: Program, into: string) {
else delete config.icon
- // Unlike a half or icon source this is one file. Installed Programs
- // nevertheless give it the same canonical shape regardless of where the
- // author kept it, and the persisted description names that canonical file.
- if (program.apiDocsPath) {
-
- cpSync(program.apiDocsPath, join(into, "api-docs.md"))
-
- config.apiDocs = "api-docs.md"
- }
-
- else delete config.apiDocs
-
// The description names the places the system chose. Presence is
// part of a half's declaration, so its location is never omitted.
delete config.storage
@@ -1299,7 +1278,7 @@ function copy(program: Program, into: string) {
writeFileSync(join(into, "program.json"), JSON.stringify(strip(config), null, 4))
}
-const installedParts = ["server", "client", "icon.png", "api-docs.md", "program.json"] as const
+const installedParts = ["server", "client", "icon.png", "program.json"] as const
// JSON has no `undefined`, and a key whose value is one would be written
// as nothing at all — so they are removed rather than left to vanish.
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 d245279..dd9976f 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
@@ -1,5 +1,5 @@
import { ClientConfig, isValue, kebab, layers, Position, ProgramConfig, ServerConfig, Size } from "./config"
-import { accessSync, constants, existsSync, readFileSync, statSync } from "node:fs"
+import { existsSync, readFileSync, statSync } from "node:fs"
import { dirname, isAbsolute, resolve } from "node:path"
import { spawn } from "node:child_process"
import { randomUUID } from "node:crypto"
@@ -161,21 +161,6 @@ export default class Program {
return this.place(this.config.storage, "./storage")
}
- public get apiDocsPath() {
-
- return this.config.apiDocs ? this.place(this.config.apiDocs) : null
- }
-
- // Contents, never the path. Reading on demand keeps an attached
- // development Program's documentation current while installed Programs
- // naturally read their immutable canonical copy.
- public apiDocs() {
-
- const path = this.apiDocsPath
-
- return path ? readFileSync(path, "utf-8") : null
- }
-
// One authored source, or none. Hosting owns every derived size and the
// system-owned default, so neither becomes part of the Program contract.
public get iconPath() {
@@ -248,9 +233,6 @@ export default class Program {
if (this.iconPath) await validateIcon(this.iconPath)
- const apiDocs = this.apiDocsPath
-
- if (apiDocs && !file(apiDocs)) throw new Error(`The API documentation file is not there: ${apiDocs}`)
}
// Preparing a server half, when it says it needs preparing. Run
@@ -293,8 +275,6 @@ function coherent(config: ProgramConfig) {
if (config[field] !== undefined && typeof config[field] !== "string") throw new Error(`A program's ${field} must be text`)
}
- if (config.apiDocs !== undefined && (typeof config.apiDocs !== "string" || config.apiDocs.trim().length === 0)) throw new Error("A program's apiDocs must be a non-empty path")
-
for (const half of ["server", "client"] as const) {
const declared = config[half]
@@ -351,19 +331,6 @@ function directory(path: string) {
return existsSync(path) && statSync(path).isDirectory()
}
-function file(path: string) {
-
- try {
-
- if (!statSync(path).isFile()) return false
-
- accessSync(path, constants.R_OK)
-
- return true
- }
-
- catch { return false }
-}
function execute(command: string, cwd: string) {