Files
WRNexusJS/packages/dev-server/src/rpc-dispatch.ts
T
ClintchizandClaude Opus 5 ce68803471 fix(rpc): close the service-collision fail-open and the fix-wave gaps
Critical:
- router: fail loudly (WRN-SERVICE-COLLISION) when two app/services files
  scan to the same service name, instead of silently letting directory-walk
  order pick a winner.

Important:
- server.ts: wrap a throwing input schema so its raw message cannot escape
  invoke(); returns RPC_INVALID and logs server-side instead.
- client.ts: race timeoutMs against transport.call so a stalled transport
  cannot hang the caller; rejects with a ServiceError(RPC_TRANSPORT).
- client.ts: the proxy returns undefined for undeclared properties (incl.
  then/catch/finally) instead of a function that throws, closing the
  await-client thenable trap.
- gateway.ts / rpc-dispatch.ts: import RPC_PATH_PREFIX / RPC_INTERNAL_HEADER
  from @wrnexus/rpc instead of hardcoding local copies.
- gateway.test.ts: cover the RPC-prefix edge block and internal-header
  stripping across casing variants.
- http.test.ts / client.test.ts: cover anonymous-call header omission, the
  internal marker, the retryable-status sweep, network/malformed/HTML
  failures, AbortSignal propagation, the timeout path, and timer cleanup.

Minor:
- transport.ts: Object.hasOwn for handler lookup; note the entry-only abort
  check.
- client.ts: wrap a missing/invalid WRNEXUS_RPC_SECRET as a ServiceError
  (RPC_IDENTITY) instead of a bare Error.
- rpc/package.json: drop the unused @wrnexus/authz dependency.
- server.ts: implement() now throws at construction time if a declared
  procedure has no own handler.

Verified: reverting the service-collision check and the client timeout race
each make their new test fail, then restore green.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-05 20:10:20 +05:30

53 lines
1.8 KiB
TypeScript

import {
RPC_IDENTITY_HEADER,
RPC_INTERNAL_HEADER,
RPC_PATH_PREFIX,
type ServiceImplementation,
} from "@wrnexus/rpc";
export { RPC_INTERNAL_HEADER };
const EDGE_HEADERS = ["x-forwarded-for", "x-forwarded-host", "x-forwarded-proto", "forwarded"];
export function isRpcPath(pathname: string): boolean {
return pathname === RPC_PATH_PREFIX || pathname.startsWith(`${RPC_PATH_PREFIX}/`);
}
export function isInternalCaller(req: Request): boolean {
return (
req.headers.get(RPC_INTERNAL_HEADER) === "1" &&
!EDGE_HEADERS.some((name) => req.headers.has(name))
);
}
function json(body: unknown, status = 200): Response {
return Response.json(body, { status, headers: { "cache-control": "private, no-store" } });
}
export async function handleRpcRequest(
req: Request,
url: URL,
services: Map<string, ServiceImplementation>,
): Promise<Response | null> {
if (!isRpcPath(url.pathname)) return null;
if (!isInternalCaller(req)) return new Response("Not found", { status: 404 });
if (req.method !== "POST") return new Response("Method not allowed", { status: 405 });
const segments = url.pathname.split("/");
const SAFE_SEGMENT = /^[A-Za-z0-9_-]+$/;
const serviceName = segments[3];
const procedure = segments[4];
const service =
serviceName && SAFE_SEGMENT.test(serviceName) ? services.get(serviceName) : undefined;
if (!service || !procedure || !SAFE_SEGMENT.test(procedure) || segments.length !== 5) {
return json({ ok: false, code: "RPC_UNKNOWN", message: "Unknown procedure", retryable: false });
}
let payload: unknown;
try {
payload = await req.json();
} catch {
return json({ ok: false, code: "RPC_INVALID", message: "Invalid input", retryable: false });
}
return json(
await service.invoke(procedure, payload, req.headers.get(RPC_IDENTITY_HEADER) ?? undefined),
);
}