The observability release gave every request an id and lost it at the process boundary — in the one place a distributed trace is the entire point. The RPC envelope now carries requestId, traceId and url, and the teapot adopts them instead of opening its own investigation. Both fields are optional on the wire, so the protocol version does not move. That decision needed a rule rather than a judgement call, so the rule is now written beside the constant: bump when an old peer would misparse or misbehave silently, not when work happened. Bumping per change would make the number mean the opposite of what it is for. Three things found while in here: - mesh:rpc:error emitted the per-link counter where every other emitter puts a name, so the two sides of one failure could not be matched up. - The teapot had no handshake deadline, so an unauthenticated peer could hold a socket by saying nothing, and no frame size cap, so it could make the process JSON.parse up to whatever the socket layer allowed — 100 MiB with the ws defaults, before authenticating. - The remote-route envelope was cast from the internal request object, putting ip and protocol on the wire as fields the protocol never declared. The cleartext warning is a warning and not a refusal on purpose: a private network already doing mutual TLS is legitimate, and green-tea cannot tell it apart from an exposed one. 4 of the 8 new tests verified to fail against the old code; the rest are the negative cases, which should pass both before and after. The Deno test drives the teapot's frame loop with a raw client, because server-side close codes are where these runtimes have already differed once. 431 vitest / 12 Deno / 7 Bun / edge / JSR dry run. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
64 lines
2.3 KiB
TypeScript
64 lines
2.3 KiB
TypeScript
import type { Link } from './link';
|
|
import type { RequestEnvelope } from './protocol';
|
|
import type { ResponseShape } from '../pipeline';
|
|
|
|
/** Local proxy for a remote scope: runs the RPC and returns its token→value binding. */
|
|
export interface RemoteScopeNode {
|
|
name: string;
|
|
run: (ctx: any) => Promise<Record<string, unknown>>;
|
|
}
|
|
/** Local proxy for a remote route: forwards the request envelope over the link. */
|
|
export interface RemoteRoute {
|
|
method: string;
|
|
pattern: string;
|
|
handler: (req: RequestEnvelope) => Promise<ResponseShape>;
|
|
}
|
|
|
|
/**
|
|
* Extract a {@link RequestEnvelope} from a local request context for transmission over the mesh.
|
|
*
|
|
* `url` and `correlation` are read off `ctx.req`, the handler argument the pipeline seeds the
|
|
* context with — so a request that crosses to a teapot arrives carrying the id it already had,
|
|
* and its events on the far side join the same trace instead of starting a new one.
|
|
*/
|
|
export function envelopeFrom(ctx: any): RequestEnvelope {
|
|
const req = ctx?.req;
|
|
const correlation = { requestId: req?.requestId, traceId: req?.traceId };
|
|
|
|
return {
|
|
method: req?.method ?? ctx?.method ?? 'GET',
|
|
params: ctx?.params ?? {},
|
|
query: ctx?.query ?? {},
|
|
body: ctx?.body,
|
|
headers: ctx?.headers ?? {},
|
|
url: req?.url,
|
|
// omitted rather than sent empty: an envelope with no identity should look like one
|
|
...(correlation.requestId || correlation.traceId ? { correlation } : {}),
|
|
};
|
|
}
|
|
|
|
/** 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);
|
|
}
|
|
|
|
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 };
|
|
}
|