import type { ServerProbeResult } from "./health-machine" import type { ServerConnection } from "./registry" /** What a server's controller is bound to, as plain values read from its listed connection. */ export type ServerCredentials = { /** * An HTTP controller keeps the endpoint it started with, so new credentials need a new controller. Other servers * move their transport to a new endpoint in place and keep one controller (`""`). */ readonly controller: string /** The endpoint health checks probe, or undefined while a plugin server is not ready. */ readonly auth: string | undefined } /** One server's controller. `starting` lasts only until the driver reports the controller it created. */ export type ServerControllerState = | { readonly type: "absent"; readonly attempt: number } | { readonly type: "starting" readonly attempt: number readonly conn: ServerConnection.Any readonly credentials: ServerCredentials } | { readonly type: "live" readonly attempt: number readonly conn: ServerConnection.Any readonly credentials: ServerCredentials readonly ctx: T readonly dispose: () => void } | { readonly type: "unauthorized" readonly attempt: number readonly conn: ServerConnection.Any readonly credentials: ServerCredentials /** The `auth` the server answered with HTTP 401. */ readonly rejected: string } export type ServerControllersState = ReadonlyMap> export type ServerControllersEvent = | { readonly type: "servers.synced" readonly entries: readonly { readonly key: ServerConnection.Key readonly conn: ServerConnection.Any readonly credentials: ServerCredentials }[] } | { readonly type: "controller.created" readonly key: ServerConnection.Key readonly attempt: number readonly ctx: T readonly dispose: () => void } | { readonly type: "health.reported" readonly key: ServerConnection.Key readonly auth: string readonly result: ServerProbeResult } | { readonly type: "disposed" } export type ServerControllersEffect = | { readonly type: "create" readonly key: ServerConnection.Key readonly attempt: number readonly conn: ServerConnection.Any } | { readonly type: "dispose"; readonly dispose: () => void } export type ServerControllersTransition = { readonly state: ServerControllersState readonly effects: readonly ServerControllersEffect[] } type ServerEvent = | { readonly type: "server.listed"; readonly conn: ServerConnection.Any; readonly credentials: ServerCredentials } | { readonly type: "server.unlisted" } | Extract, { type: "controller.created" | "health.reported" }> type ServerEffect = | { readonly type: "create"; readonly attempt: number; readonly conn: ServerConnection.Any } | { readonly type: "dispose"; readonly dispose: () => void } type ServerTransition = { readonly state: ServerControllerState; readonly effects: readonly ServerEffect[] } export function endpointCredentials(http: ServerConnection.HttpBase) { return `${http.url}\n${http.password ?? ""}` } export function serverCredentials(conn: ServerConnection.Any): ServerCredentials { if (conn.type === "plugin") return { controller: "", auth: conn.status.type === "ready" ? endpointCredentials(conn.status.http) : undefined } const endpoint = endpointCredentials(conn.http) return { controller: conn.type === "http" ? endpoint : "", auth: endpoint } } export function createServerControllersState(): ServerControllersState { return new Map() } export function transitionServerControllers( state: ServerControllersState, event: ServerControllersEvent, ): ServerControllersTransition { if (event.type === "servers.synced") { const listed = new Set(event.entries.map((entry) => entry.key)) return apply(state, [ ...[...state.keys()].flatMap((key) => (listed.has(key) ? [] : [{ key, event: unlisted }])), ...event.entries.map((entry) => ({ key: entry.key, event: { type: "server.listed" as const, conn: entry.conn, credentials: entry.credentials }, })), ]) } if (event.type === "disposed") return apply(state, [...state.keys()].map((key) => ({ key, event: unlisted }))) return apply(state, [{ key: event.key, event }]) } /** The live controller of a server; undefined while it is unlisted or rejects our credentials. */ export function selectServerCtx(state: ServerControllersState, key: ServerConnection.Key) { const slot = state.get(key) return slot?.type === "live" ? slot.ctx : undefined } /** The server answered our current credentials with HTTP 401, so it has no controller until it signs in again. */ export function selectServerSignedOut(state: ServerControllersState, key: ServerConnection.Key) { return state.get(key)?.type === "unauthorized" } const unlisted = { type: "server.unlisted" } as const function apply( state: ServerControllersState, steps: readonly { key: ServerConnection.Key; event: ServerEvent }[], ): ServerControllersTransition { return steps.reduce>( (result, step) => { const current = result.state.get(step.key) ?? { type: "absent", attempt: 0 } const next = transitionServer(current, step.event) return { state: next.state === current ? result.state : new Map(result.state).set(step.key, next.state), effects: [ ...result.effects, ...next.effects.map((effect) => (effect.type === "create" ? { ...effect, key: step.key } : effect)), ], } }, { state, effects: [] }, ) } function transitionServer(state: ServerControllerState, event: ServerEvent): ServerTransition { if (event.type === "controller.created") { if (state.type === "starting" && state.attempt === event.attempt) { return { state: { type: "live", attempt: state.attempt, conn: state.conn, credentials: state.credentials, ctx: event.ctx, dispose: event.dispose, }, effects: [], } } // A creation that a newer listing, a rejection or an unlisting superseded never becomes visible. return { state, effects: [{ type: "dispose", dispose: event.dispose }] } } if (event.type === "server.unlisted") { if (state.type === "absent") return { state, effects: [] } return { state: { type: "absent", attempt: state.attempt }, effects: release(state) } } if (event.type === "server.listed") return listed(state, event.conn, event.credentials) return reported(state, event) } function listed( state: ServerControllerState, conn: ServerConnection.Any, credentials: ServerCredentials, ): ServerTransition { if (state.type === "absent") return start(state, conn, credentials, []) if (state.type === "unauthorized") { // The rejected credentials would fail again, and a plugin server that is not ready has none to try yet. if (credentials.auth === undefined || credentials.auth === state.rejected) return update(state, conn, credentials) return start(state, conn, credentials, []) } if (credentials.controller !== state.credentials.controller) return start(state, conn, credentials, release(state)) return update(state, conn, credentials) } function reported( state: ServerControllerState, event: Extract, { type: "health.reported" }>, ): ServerTransition { if (state.type === "absent") return { state, effects: [] } // A server that accepts the rejected credentials again, such as after its password was restored, gets a controller. if (state.type === "unauthorized") { if (event.result.type === "healthy" && event.auth === state.credentials.auth) return start(state, state.conn, state.credentials, []) return { state, effects: [] } } // A probe of credentials the server has since replaced says nothing about the current ones. if (event.result.type !== "unauthorized" || event.auth !== state.credentials.auth) return { state, effects: [] } return { state: { type: "unauthorized", attempt: state.attempt, conn: state.conn, credentials: state.credentials, rejected: event.auth, }, effects: release(state), } } function start( state: ServerControllerState, conn: ServerConnection.Any, credentials: ServerCredentials, effects: readonly ServerEffect[], ): ServerTransition { const attempt = state.attempt + 1 return { state: { type: "starting", attempt, conn, credentials }, effects: [...effects, { type: "create", attempt, conn }], } } function update( state: Exclude, { type: "absent" }>, conn: ServerConnection.Any, credentials: ServerCredentials, ): ServerTransition { if ( state.conn === conn && state.credentials.controller === credentials.controller && state.credentials.auth === credentials.auth ) return { state, effects: [] } return { state: { ...state, conn, credentials }, effects: [] } } function release(state: ServerControllerState): ServerEffect[] { return state.type === "live" ? [{ type: "dispose", dispose: state.dispose }] : [] }