refactor(mesh)!: a manifest carries step names, not scoped tokens
Every exported token is request-scope now, so the lifetime field had one value and told a reader nothing. No protocol bump: mesh is alpha, both peers ship from this repo, and the number should mean incompatibility between deployed versions. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
13 files changed
+104
-108
No files matched your search
+7
-3
@@ -33,7 +33,7 @@ import { buildOpenApi, type OpenApiInfo } from '../openapi';
|
||||
import { connectLink, isPermanentRefusal, type Link } from '../mesh/link';
|
||||
import { Rooms } from '../rooms';
|
||||
import type { TlsOptions, SecurityOptions, CorsOptions } from '../security';
|
||||
import { buildRemote } from '../mesh/teacup';
|
||||
import { buildRemote, type RemoteScopeNode } from '../mesh/teacup';
|
||||
import { buildManifest, createMeshControl, MESH_CONTROL_PATH } from '../mesh/teapot';
|
||||
import type { MeshControl } from '../http';
|
||||
import type { RequestEnvelope, RouteEntry } from '../mesh/protocol';
|
||||
@@ -850,7 +850,11 @@ async function spliceRemoteScopes(
|
||||
}),
|
||||
);
|
||||
meshLinks.push(link);
|
||||
const { providers, steps, routes } = buildRemote(link);
|
||||
// `buildRemote` no longer returns `providers` — every exported token is a step now (Task 1
|
||||
// refuses a provider export at boot). Kept as an empty array, not deleted, so the loop below
|
||||
// still typechecks; Task 3 removes the loop itself along with the rest of provider-splicing.
|
||||
const providers: RemoteScopeNode[] = [];
|
||||
const { steps, routes } = buildRemote(link);
|
||||
const origin = `mesh:${teapot.url}`;
|
||||
|
||||
for (const provider of providers) {
|
||||
@@ -1094,7 +1098,7 @@ function buildMeshControl(
|
||||
if (!mesh?.secret || !hasExports) return undefined;
|
||||
const { container, orderedProviders, orderedSteps } = deps;
|
||||
const { bus, providedSeed, planSteps, onError, logger } = deps.deps;
|
||||
const manifest = buildManifest({ providers: exportedProviders, steps: exportedSteps, routes: exportedRoutes });
|
||||
const manifest = buildManifest({ steps: exportedSteps, routes: exportedRoutes });
|
||||
|
||||
const resolveScope = async (name: string, env: RequestEnvelope): Promise<unknown> => {
|
||||
// context is intentionally `any`: providers and steps merge arbitrary keys into it
|
||||
|
||||
+8
-8
@@ -218,7 +218,7 @@ function settleManifest(frame: Extract<Frame, { type: 'manifest' }>, hs: Handsha
|
||||
// `mesh:connect` is emitted by the caller, not here: a reconnect whose manifest no longer backs
|
||||
// the graph is refused after this point, and announcing a connection we are about to hang up on
|
||||
// would tell an operator the opposite of what happened.
|
||||
hs.resolve({ socket: hs.socket, manifest: { scopes: frame.scopes, routes: frame.routes }, heartbeat });
|
||||
hs.resolve({ socket: hs.socket, manifest: { steps: frame.steps, routes: frame.routes }, heartbeat });
|
||||
}
|
||||
|
||||
/** Consume the session's inbound frames until it closes, settling the handshake then each RPC. */
|
||||
@@ -323,9 +323,9 @@ function openSession(args: SessionArgs): Promise<Session> {
|
||||
});
|
||||
}
|
||||
|
||||
/** A scope token and its lifetime, flattened to the string the refusal check compares. */
|
||||
function scopeKeys(manifest: Manifest): Set<string> {
|
||||
return new Set(manifest.scopes.map((scope) => `${scope.scope}:${scope.token}`));
|
||||
/** A manifest's step tokens, flattened to the set the refusal check compares. */
|
||||
function stepKeys(manifest: Manifest): Set<string> {
|
||||
return new Set(manifest.steps);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -345,12 +345,12 @@ function routeKeys(manifest: Manifest): Set<string> {
|
||||
* missing and are ignored: the graph is fixed at boot and nothing new is spliced into it.
|
||||
*/
|
||||
export function missingFromManifest(booted: Manifest, returned: Manifest): string[] {
|
||||
const scopes = scopeKeys(returned);
|
||||
const steps = stepKeys(returned);
|
||||
const routes = routeKeys(returned);
|
||||
const missing: string[] = [];
|
||||
|
||||
for (const scope of booted.scopes) {
|
||||
if (!scopes.has(`${scope.scope}:${scope.token}`)) missing.push(`${scope.scope}-scope '${scope.token}'`);
|
||||
for (const step of booted.steps) {
|
||||
if (!steps.has(step)) missing.push(`step '${step}'`);
|
||||
}
|
||||
|
||||
for (const route of booted.routes) {
|
||||
@@ -407,7 +407,7 @@ export function connectLink(args: {
|
||||
const link: Link = {
|
||||
// replaced with the real manifest before this object escapes; a reconnect never changes it,
|
||||
// because the graph is fixed at boot and only the *boot* manifest describes what it holds.
|
||||
manifest: { scopes: [], routes: [] },
|
||||
manifest: { steps: [], routes: [] },
|
||||
rpc: (kind, name, ctx) => sendRpc(state.socket, pending, String(counter++), kind, name, ctx, timeoutMs),
|
||||
close: () => {
|
||||
// Terminal, by contract. `closeLinks` runs on shutdown, and a link that reconnected after
|
||||
|
||||
+11
-9
@@ -43,19 +43,21 @@ export interface RequestEnvelope {
|
||||
correlation?: { requestId?: string; traceId?: string };
|
||||
}
|
||||
|
||||
/** Manifest entry for an exported scope token and its lifetime. */
|
||||
export interface ScopeEntry {
|
||||
token: string;
|
||||
scope: 'app' | 'request';
|
||||
}
|
||||
/** Manifest entry for an exported route. */
|
||||
export interface RouteEntry {
|
||||
method: string;
|
||||
pattern: string;
|
||||
}
|
||||
/** A mesh server's advertised scopes and routes. */
|
||||
|
||||
/**
|
||||
* A mesh server's advertised steps and routes.
|
||||
*
|
||||
* Steps only: a provider's value is the object it builds, which cannot cross a JSON wire, so
|
||||
* exporting one is refused at boot (`collectProviders`). Every entry here is request-scope and
|
||||
* lazy — the token exists in the teacup's graph, and running it is an RPC.
|
||||
*/
|
||||
export interface Manifest {
|
||||
scopes: ScopeEntry[];
|
||||
steps: string[];
|
||||
routes: RouteEntry[];
|
||||
}
|
||||
|
||||
@@ -68,7 +70,7 @@ export interface Manifest {
|
||||
*/
|
||||
export type Frame =
|
||||
| { type: 'hello'; v: number; secret: string }
|
||||
| { type: 'manifest'; v: number; scopes: ScopeEntry[]; routes: RouteEntry[] }
|
||||
| { type: 'manifest'; v: number; steps: string[]; routes: RouteEntry[] }
|
||||
| { type: 'rpc-req'; id: string; kind: 'scope' | 'route'; name: string; ctx: RequestEnvelope }
|
||||
| { type: 'rpc-res'; id: string; ok: true; result: unknown }
|
||||
| { type: 'rpc-res'; id: string; ok: false; error: { message: string; status?: number } }
|
||||
@@ -94,7 +96,7 @@ const SHAPE: Record<Frame['type'], ShapeCheck> = {
|
||||
},
|
||||
manifest: (frame, bad) => {
|
||||
if (!isNumber(frame.v)) bad('missing protocol version');
|
||||
if (!Array.isArray(frame.scopes) || !Array.isArray(frame.routes)) bad('scopes and routes must be arrays');
|
||||
if (!Array.isArray(frame.steps) || !Array.isArray(frame.routes)) bad('steps and routes must be arrays');
|
||||
},
|
||||
'rpc-req': (frame, bad) => {
|
||||
if (!isString(frame.id)) bad('id must be a string');
|
||||
|
||||
+8
-17
@@ -37,27 +37,18 @@ export function envelopeFrom(ctx: any): RequestEnvelope {
|
||||
};
|
||||
}
|
||||
|
||||
/** Turn a link's manifest into local proxy nodes: app-scope providers, request-scope steps, and routes. */
|
||||
export function buildRemote(link: Link): {
|
||||
providers: RemoteScopeNode[];
|
||||
steps: RemoteScopeNode[];
|
||||
routes: RemoteRoute[];
|
||||
} {
|
||||
const providers: RemoteScopeNode[] = [];
|
||||
const steps: RemoteScopeNode[] = [];
|
||||
|
||||
for (const scope of link.manifest.scopes) {
|
||||
const node: RemoteScopeNode = {
|
||||
name: scope.token,
|
||||
run: async (ctx: any) => ({ [scope.token]: await link.rpc('scope', scope.token, envelopeFrom(ctx)) }),
|
||||
};
|
||||
(scope.scope === 'app' ? providers : steps).push(node);
|
||||
}
|
||||
/** Turn a link's manifest into local proxy nodes: lazy request-scope steps, and routes. */
|
||||
export function buildRemote(link: Link): { steps: RemoteScopeNode[]; routes: RemoteRoute[] } {
|
||||
const steps: RemoteScopeNode[] = link.manifest.steps.map((token) => ({
|
||||
name: token,
|
||||
run: async (ctx: any) => ({ [token]: await link.rpc('scope', token, envelopeFrom(ctx)) }),
|
||||
}));
|
||||
|
||||
const routes: RemoteRoute[] = link.manifest.routes.map((route) => ({
|
||||
method: route.method,
|
||||
pattern: route.pattern,
|
||||
handler: async (req: RequestEnvelope) => (await link.rpc('route', route.pattern, req)) as ResponseShape,
|
||||
}));
|
||||
return { providers, steps, routes };
|
||||
|
||||
return { steps, routes };
|
||||
}
|
||||
+5
-15
@@ -42,19 +42,9 @@ const HANDSHAKE_TIMEOUT_MS = 10_000;
|
||||
*/
|
||||
const MAX_FRAME_CHARS = 4_000_000;
|
||||
|
||||
/** Assemble a {@link Manifest} from exported provider/step tokens and buffered routes. */
|
||||
export function buildManifest(args: {
|
||||
providers: string[]; // exported provider tokens (app-scope)
|
||||
steps: string[]; // exported step tokens (request-scope)
|
||||
routes: RouteEntry[]; // exported buffered routes
|
||||
}): Manifest {
|
||||
return {
|
||||
scopes: [
|
||||
...args.providers.map((token) => ({ token, scope: 'app' as const })),
|
||||
...args.steps.map((token) => ({ token, scope: 'request' as const })),
|
||||
],
|
||||
routes: args.routes,
|
||||
};
|
||||
/** Assemble a {@link Manifest} from exported step tokens and buffered routes. */
|
||||
export function buildManifest(args: { steps: string[]; routes: RouteEntry[] }): Manifest {
|
||||
return { steps: [...args.steps], routes: args.routes };
|
||||
}
|
||||
|
||||
function safeEqual(left: string, right: string): boolean {
|
||||
@@ -115,7 +105,7 @@ function handleHandshake(socket: WsSocket, frame: Frame, deps: MeshControlDeps):
|
||||
encode({
|
||||
type: 'manifest',
|
||||
v: MESH_PROTOCOL_VERSION,
|
||||
scopes: deps.manifest.scopes,
|
||||
steps: deps.manifest.steps,
|
||||
routes: deps.manifest.routes,
|
||||
}),
|
||||
);
|
||||
@@ -244,7 +234,7 @@ export function createMeshControl(deps: MeshControlDeps): {
|
||||
path: string;
|
||||
handle: (socket: WsSocket) => Promise<void>;
|
||||
} {
|
||||
const exportedScopes = new Set(deps.manifest.scopes.map((scope) => scope.token));
|
||||
const exportedScopes = new Set(deps.manifest.steps);
|
||||
const exportedRoutes = new Set(deps.manifest.routes.map((route) => `${route.method} ${route.pattern}`));
|
||||
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user