Files
WRNexusJS/examples/inter-app-api-showcase/app/api/product-summary.ts
T
ClintchizandClaude Opus 5 5112cc1a62
Quality / quality (ubuntu-latest) (push) Failing after 13m21s
Quality / quality (windows-latest) (push) Canceled after 0s
docs: measure the runtime and the generated client modules
Adds a per-subsystem measurement of reactive.js, made by minifying it
repeatedly with one subsystem removed rather than counting source bytes.

This corrects the earlier audit on both figures and on the conclusion drawn
from them. Component controllers are 23,722 bytes minified / 6,660 gzipped --
30.6% of transfer, not the "about 18%" previously claimed -- and splitting them
out saves 6.6 kB gzipped on a typical page, not "3-4 kB". Measured against the
example app, / and /login use none of the ten controllers and /layout uses one,
so most pages download and parse the lot for nothing.

The larger finding is that the runtime is not where the weight is. One page
parses 490,212 decoded bytes across 11 generated client modules while
transferring 21,026, and the largest module is 89.8% duplicated lines: the
state-restore prologue appears 162 times because client-codegen.ts inlines the
sync into every peer alias of every client function. Gzip hides it on the wire,
but parse cost follows decoded bytes.

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

61 lines
2.3 KiB
TypeScript

import type { Context } from "@wrnexus/core";
import { httpTransport, serviceClient } from "@wrnexus/rpc";
import { auditService, catalogService } from "../lib/contracts.ts";
interface GitHubRepository {
full_name?: unknown;
stargazers_count?: unknown;
}
function requestedSku(ctx: Context): string {
return new URL(ctx.req.url).searchParams.get("sku")?.trim() || "starter";
}
/** GET /api/product-summary?sku=starter */
export async function GET(ctx: Context): Promise<Response> {
const sku = requestedSku(ctx);
const transport = httpTransport();
// Inter-app call #1: query the catalog app. `{ as: ctx }` forwards the
// signed subject/tenant context; catalog still authorizes independently.
const catalog = serviceClient(catalogService, { app: "catalog", as: ctx, transport });
const product = await catalog.getProduct({ sku });
// External API call: GitHub's public repository endpoint. Do not send the
// user's RPC identity token or internal headers to external services.
let githubResponse: Response;
try {
githubResponse = await fetch("https://api.github.com/repos/octocat/Hello-World", {
headers: { accept: "application/vnd.github+json", "user-agent": "wrnexus-example" },
signal: ctx.req.signal,
});
} catch {
return Response.json({ error: "External repository lookup failed." }, { status: 502 });
}
if (!githubResponse.ok) {
return Response.json({ error: "External repository lookup failed." }, { status: 502 });
}
const github = (await githubResponse.json()) as GitHubRepository;
if (typeof github.full_name !== "string" || typeof github.stargazers_count !== "number") {
return Response.json(
{ error: "External repository returned an unexpected response." },
{ status: 502 },
);
}
// Inter-app call #2: record the completed lookup in the audit app. This is
// intentionally awaited: callers learn whether the audit record was saved.
const audit = serviceClient(auditService, { app: "audit", as: ctx, transport });
const receipt = await audit.recordLookup({
sku: product.sku,
repository: github.full_name,
stars: github.stargazers_count,
});
return Response.json({
product,
external: { repository: github.full_name, stars: github.stargazers_count },
audit: receipt,
});
}