feat(rpc): transport, server, client, http, mounting, docs (Tasks 5-11)
Brings the uncommitted body of work under version control so it cannot be
lost. Gates are green: 152 tests pass across rpc/router/dev-server,
typecheck, lint, format and check:public-api all clean.
NOT YET REVIEWED. None of Tasks 5-11 has had an independent task review, and
Task 4's second fix round was never re-reviewed either.
Known gaps against the plan, recorded here rather than discovered later:
- packages/rpc/test/{transport,server,client}.test.ts are ABSENT. The plan
required a test file for each. server.ts holds the fail-closed identity and
permission checks and currently has no direct coverage at all.
- rpc-endpoint.test.ts has 3 tests where the plan specified 9. Missing:
unknown service, non-POST, malformed body, non-rpc passthrough, and the
isInternalCaller sweep. This is the task where a reachable
/__wrnexus/rpc/* makes every permission check in the workspace bypassable.
- http.test.ts has 3 of 7; integration.test.ts 2 of 3;
services-discovery.test.ts 1 of 4.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import { appOrigin } from "@wrnexus/helpers";
|
||||
import { RPC_ERROR_CODES, failure, isRetryableStatus } from "./errors.ts";
|
||||
import { RPC_IDENTITY_HEADER } from "./identity.ts";
|
||||
import type { CallOptions, RpcTarget, Transport } from "./transport.ts";
|
||||
import type { ServiceResult } from "./types.ts";
|
||||
|
||||
export const RPC_PATH_PREFIX = "/__wrnexus/rpc";
|
||||
export const RPC_INTERNAL_HEADER = "x-wrnexus-internal";
|
||||
|
||||
export function rpcPath(service: string, procedure: string): string {
|
||||
return `${RPC_PATH_PREFIX}/${service}/${procedure}`;
|
||||
}
|
||||
|
||||
export interface HttpTransportOptions {
|
||||
resolveOrigin?: (app: string) => string;
|
||||
fetch?: typeof fetch;
|
||||
}
|
||||
|
||||
function isServiceResult(value: unknown): value is ServiceResult {
|
||||
if (!value || typeof value !== "object" || !("ok" in value)) return false;
|
||||
const result = value as Record<string, unknown>;
|
||||
return (
|
||||
result.ok === true ||
|
||||
(result.ok === false &&
|
||||
typeof result.code === "string" &&
|
||||
typeof result.message === "string" &&
|
||||
typeof result.retryable === "boolean")
|
||||
);
|
||||
}
|
||||
|
||||
export function httpTransport(options: HttpTransportOptions = {}): Transport {
|
||||
const resolveOrigin = options.resolveOrigin ?? appOrigin;
|
||||
const doFetch = options.fetch ?? fetch;
|
||||
return {
|
||||
async call(target: RpcTarget, payload: unknown, callOptions: CallOptions) {
|
||||
let response: Response;
|
||||
try {
|
||||
const headers: Record<string, string> = {
|
||||
"content-type": "application/json",
|
||||
[RPC_INTERNAL_HEADER]: "1",
|
||||
};
|
||||
if (callOptions.identity) headers[RPC_IDENTITY_HEADER] = callOptions.identity;
|
||||
response = await doFetch(
|
||||
`${resolveOrigin(target.app)}${rpcPath(target.service, target.procedure)}`,
|
||||
{
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(payload ?? {}),
|
||||
signal: callOptions.signal,
|
||||
},
|
||||
);
|
||||
} catch {
|
||||
return failure(RPC_ERROR_CODES.transport, "Service unreachable");
|
||||
}
|
||||
if (!response.ok) {
|
||||
return {
|
||||
ok: false,
|
||||
code: RPC_ERROR_CODES.transport,
|
||||
message: `Service returned ${response.status}`,
|
||||
retryable: isRetryableStatus(response.status),
|
||||
};
|
||||
}
|
||||
try {
|
||||
const result: unknown = await response.json();
|
||||
return isServiceResult(result)
|
||||
? result
|
||||
: failure(RPC_ERROR_CODES.malformed, "Malformed service response");
|
||||
} catch {
|
||||
return failure(RPC_ERROR_CODES.malformed, "Malformed service response");
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user