docs: correct the dead-output component count from 11 to 9
Counted from source: the 22 remaining outputs sit in 9 components, not 11. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,14 +2,21 @@ import {
|
||||
RPC_IDENTITY_HEADER,
|
||||
RPC_INTERNAL_HEADER,
|
||||
RPC_PATH_PREFIX,
|
||||
RPC_STREAM_PATH_PREFIX,
|
||||
type ServiceImplementation,
|
||||
type StreamImplementation,
|
||||
} 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}/`);
|
||||
return (
|
||||
pathname === RPC_PATH_PREFIX ||
|
||||
pathname.startsWith(`${RPC_PATH_PREFIX}/`) ||
|
||||
pathname === RPC_STREAM_PATH_PREFIX ||
|
||||
pathname.startsWith(`${RPC_STREAM_PATH_PREFIX}/`)
|
||||
);
|
||||
}
|
||||
|
||||
export function isInternalCaller(req: Request): boolean {
|
||||
@@ -26,11 +33,14 @@ function json(body: unknown, status = 200): Response {
|
||||
export async function handleRpcRequest(
|
||||
req: Request,
|
||||
url: URL,
|
||||
services: Map<string, ServiceImplementation>,
|
||||
services: Map<string, ServiceImplementation | StreamImplementation>,
|
||||
): 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 streaming =
|
||||
url.pathname === RPC_STREAM_PATH_PREFIX ||
|
||||
url.pathname.startsWith(`${RPC_STREAM_PATH_PREFIX}/`);
|
||||
const segments = url.pathname.split("/");
|
||||
const SAFE_SEGMENT = /^[A-Za-z0-9_-]+$/;
|
||||
const serviceName = segments[3];
|
||||
@@ -46,6 +56,51 @@ export async function handleRpcRequest(
|
||||
} catch {
|
||||
return json({ ok: false, code: "RPC_INVALID", message: "Invalid input", retryable: false });
|
||||
}
|
||||
if (streaming) {
|
||||
if (!("stream" in service) || typeof service.stream !== "function") {
|
||||
return json({
|
||||
ok: false,
|
||||
code: "RPC_UNKNOWN",
|
||||
message: "Unknown procedure",
|
||||
retryable: false,
|
||||
});
|
||||
}
|
||||
const encoder = new TextEncoder();
|
||||
const identity = req.headers.get(RPC_IDENTITY_HEADER) ?? undefined;
|
||||
const iterator = service.stream(procedure, payload, identity)[Symbol.asyncIterator]();
|
||||
const body = new ReadableStream<Uint8Array>({
|
||||
async pull(controller) {
|
||||
try {
|
||||
const next = await iterator.next();
|
||||
if (next.done) {
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
controller.enqueue(
|
||||
encoder.encode(`data: ${JSON.stringify({ ok: true, value: next.value })}\n\n`),
|
||||
);
|
||||
} catch {
|
||||
controller.enqueue(
|
||||
encoder.encode('data: {"ok":false,"code":"RPC_HANDLER","message":"Stream failed"}\n\n'),
|
||||
);
|
||||
controller.close();
|
||||
}
|
||||
},
|
||||
async cancel() {
|
||||
await iterator.return?.();
|
||||
},
|
||||
});
|
||||
return new Response(body, {
|
||||
headers: {
|
||||
"content-type": "text/event-stream; charset=utf-8",
|
||||
"cache-control": "private, no-store",
|
||||
"x-accel-buffering": "no",
|
||||
},
|
||||
});
|
||||
}
|
||||
if (!("invoke" in service) || typeof service.invoke !== "function") {
|
||||
return json({ ok: false, code: "RPC_UNKNOWN", message: "Unknown procedure", retryable: false });
|
||||
}
|
||||
return json(
|
||||
await service.invoke(procedure, payload, req.headers.get(RPC_IDENTITY_HEADER) ?? undefined),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user