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>
91 lines
3.6 KiB
TypeScript
91 lines
3.6 KiB
TypeScript
import type { Context } from "@wrnexus/core";
|
|
import { RPC_ERROR_CODES, ServiceError } from "./errors.ts";
|
|
import { exportSubjectContext } from "./identity.ts";
|
|
import type { Transport } from "./transport.ts";
|
|
import type {
|
|
AnyProcedures,
|
|
InferProcedureInput,
|
|
InferProcedureOutput,
|
|
ServiceContract,
|
|
} from "./types.ts";
|
|
|
|
export interface ServiceClientOptions {
|
|
app?: string;
|
|
transport: Transport;
|
|
as?: Context;
|
|
timeoutMs?: number;
|
|
}
|
|
|
|
export type ServiceClient<Procedures extends AnyProcedures> = {
|
|
[K in keyof Procedures]: (
|
|
input: InferProcedureInput<Procedures[K]>,
|
|
) => Promise<InferProcedureOutput<Procedures[K]>>;
|
|
};
|
|
|
|
const DEFAULT_TIMEOUT_MS = 10_000;
|
|
|
|
export function serviceClient<Procedures extends AnyProcedures>(
|
|
contract: ServiceContract<Procedures>,
|
|
options: ServiceClientOptions,
|
|
): ServiceClient<Procedures> {
|
|
const app = options.app ?? contract.name;
|
|
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
return new Proxy({} as ServiceClient<Procedures>, {
|
|
get(_target, property) {
|
|
// Every declared procedure is a string key; anything else (including
|
|
// `then`/`catch`/`finally`) is not one of ours. Returning a function for
|
|
// those makes `await client` or `return client` from an async function
|
|
// read the proxy as thenable — the runtime then calls `then(resolve,
|
|
// reject)`, which throws "Unknown procedure". Returning undefined lets
|
|
// the caller be treated as a plain (non-thenable) object instead.
|
|
if (typeof property !== "string" || !Object.hasOwn(contract.procedures, property)) {
|
|
return undefined;
|
|
}
|
|
return async (input: unknown) => {
|
|
let identity: string | undefined;
|
|
if (options.as) {
|
|
try {
|
|
identity = await exportSubjectContext(options.as, app);
|
|
} catch (error) {
|
|
// A missing/misconfigured WRNEXUS_RPC_SECRET otherwise rejects with
|
|
// a bare Error, so a caller matching on ServiceError treats
|
|
// misconfiguration as a crash instead of a handled RPC failure.
|
|
// The operator-facing message names no secret value, so it is safe
|
|
// to preserve.
|
|
const message = error instanceof Error ? error.message : String(error);
|
|
throw new ServiceError(RPC_ERROR_CODES.identity, message);
|
|
}
|
|
}
|
|
const controller = new AbortController();
|
|
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
const timeout = new Promise<never>((_, reject) => {
|
|
controller.signal.addEventListener("abort", () => {
|
|
reject(new ServiceError(RPC_ERROR_CODES.transport, "Call timed out"));
|
|
});
|
|
});
|
|
const callPromise = options.transport.call(
|
|
{ app, service: contract.name, procedure: property },
|
|
input,
|
|
{
|
|
signal: controller.signal,
|
|
idempotent: contract.procedures[property as keyof Procedures].idempotent === true,
|
|
...(identity ? { identity } : {}),
|
|
},
|
|
);
|
|
try {
|
|
const result = await Promise.race([callPromise, timeout]);
|
|
if (result.ok) return result.value;
|
|
throw new ServiceError(result.code, result.message, result.retryable);
|
|
} finally {
|
|
clearTimeout(timer);
|
|
// If the timeout won the race, the transport call may still settle
|
|
// later — a transport that ignores the abort signal keeps running.
|
|
// Nothing awaits it again, so swallow a late rejection here rather
|
|
// than let it surface as an unhandled promise rejection.
|
|
callPromise.catch(() => {});
|
|
}
|
|
};
|
|
},
|
|
});
|
|
}
|