mirror of
https://github.com/morluto/rea.git
synced 2026-11-15 11:13:21 +00:00
* refactor: prune internal seams and preserve configuration ownership * fix(setup): keep YAML aliases and inherited registration settings on refresh - Reuse an unchanged YAML collection only while every alias in it still resolves after the edit; otherwise rebuild it so the alias becomes its value. A sibling anchor replaced or moved in the same refresh no longer leaves an unresolved alias in Hermes or Goose output. - Keep a connection selector whose existing value already chooses stdio (for example an inherited Hermes transport: stdio) instead of superseding it; remote selectors are still replaced. Explicit stdio selectors are therefore no longer rewritten (Kimi expectation updated). - When the whole Hermes registration is inherited through a merge, override it with that merge and write only the changed fields. * fix(setup): edit registration fields whose YAML key is an alias A key written as an alias to a string scalar (for example *field with field: &field env) was treated as a source-only key, so any change to its value was refused in Hermes and Goose. Resolve such keys to their string, keep the aliased spelling while its anchor survives the edit, and write the plain key when the same edit rebuilds the anchor. * test(process): bound cleanup-boundary waits and always release gates Both cancellation tests awaited a bare started promise and released their gates only on success, so a missed boundary waited for the outer deadline and could leave cleanup parked. Use awaitAcknowledgement and release and join the cleanup in finally. * fix(setup): repair only aliases an edit leaves unresolved Replace the pre-edit alias survival guess, which expanded aliases to sibling anchors that survive unchanged, with an exact repair: record what each alias in the replaced value resolves to, write the edit, then replace only aliases left without a preceding anchor by that value. - Keep an aliased key's source position, so its anchor stays ahead of later aliases. - Treat an aliased key as its string only when no other key in the map resolves to the same string; an ambiguous one stays guarded. - Override an inherited Hermes registration only when the requested entry keeps every inherited field, so an explicit replacement can drop remote selectors. * fix(setup): repair aliases whose binding an edit changes, keeping comments An alias whose own anchor was removed could resolve to an earlier anchor with the same name and silently change value. Record the node each alias resolved to and repair any alias that no longer resolves to that node. Carry the alias's comments and spacing onto the repaired value. * test(setup): split the Hermes anchor and merge refresh cases * fix(setup): refuse removing an overridden key its merge would restore The merge-retention check skipped keys the mapping also overrides, so dropping such a key (for example a remote url during an explicit stdio replacement) let the retained merge restore it. Check removal before skipping overridden keys, so the edit is refused with the existing message instead of producing a registration that cannot be applied.
217 lines
7.4 KiB
TypeScript
217 lines
7.4 KiB
TypeScript
import * as t from "@babel/types";
|
|
|
|
import {
|
|
unwrapJavaScriptExpression,
|
|
semanticStaticPropertyKey,
|
|
semanticFinalObjectStringProperty,
|
|
} from "./javascriptAstValues.js";
|
|
import { calleeName } from "./javascriptStaticAnalysisHelpers.js";
|
|
import type { JavaScriptBindingIndex } from "./javascriptSemanticState.js";
|
|
import { createJavaScriptGraphqlScan } from "./javascriptGraphqlOperations.js";
|
|
|
|
import type {
|
|
JavaScriptRpcEndpoint,
|
|
JavaScriptRpcScan,
|
|
} from "./javascriptRpcEndpoint.js";
|
|
|
|
// tRPC proxies preserve router and procedure names as property names, so they
|
|
// survive identifier minification. Method names follow @trpc/react-query and
|
|
// @trpc/tanstack-react-query 11.19 documentation; cache-key helpers such as
|
|
// queryKey are excluded because they never issue a request.
|
|
const TRPC_HOOK_METHODS: ReadonlySet<string> = new Set([
|
|
"useQuery",
|
|
"useSuspenseQuery",
|
|
"useInfiniteQuery",
|
|
"useSuspenseInfiniteQuery",
|
|
"usePrefetchQuery",
|
|
"usePrefetchInfiniteQuery",
|
|
"useMutation",
|
|
"useSubscription",
|
|
"queryOptions",
|
|
"infiniteQueryOptions",
|
|
"mutationOptions",
|
|
"subscriptionOptions",
|
|
]);
|
|
|
|
// @trpc/client 11.19 clientCallTypeMap methods; common outside tRPC (Apollo,
|
|
// database pools, stores), so they need corroborating evidence.
|
|
const TRPC_REQUEST_METHODS: ReadonlySet<string> = new Set(["query", "mutate"]);
|
|
// @trpc/client 11.19 TRPCSubscriptionObserver callbacks; RxJS observers use
|
|
// next/error/complete instead.
|
|
const TRPC_SUBSCRIPTION_CALLBACKS: ReadonlySet<string> = new Set([
|
|
"onStarted",
|
|
"onData",
|
|
"onError",
|
|
"onStopped",
|
|
"onComplete",
|
|
"onConnectionStateChange",
|
|
]);
|
|
// @trpc/client 11.19 client factories (createTRPCProxyClient is the
|
|
// deprecated alias of createTRPCClient).
|
|
const TRPC_CLIENT_FACTORIES: ReadonlySet<string> = new Set([
|
|
"createTRPCClient",
|
|
"createTRPCProxyClient",
|
|
]);
|
|
// Built-in HTTP links documented by tRPC 11.19 that carry a `url` option.
|
|
const TRPC_URL_LINKS: ReadonlySet<string> = new Set([
|
|
"httpLink",
|
|
"httpBatchLink",
|
|
"httpBatchStreamLink",
|
|
"httpSubscriptionLink",
|
|
]);
|
|
// Literals retained by @trpc/client 11.19 bundles (`this.name`, the default
|
|
// accept header key) or by the conventional transport path.
|
|
const TRPC_SOURCE_MARKERS = ["TRPCClientError", "trpc-accept", "/trpc"];
|
|
|
|
// Browser-owned roots whose methods share tRPC names, such as
|
|
// navigator.permissions.query.
|
|
const BROWSER_GLOBAL_NAMES: ReadonlySet<string> = new Set([
|
|
"window",
|
|
"self",
|
|
"globalThis",
|
|
"document",
|
|
"navigator",
|
|
"location",
|
|
"history",
|
|
"console",
|
|
"performance",
|
|
"screen",
|
|
"top",
|
|
"parent",
|
|
"frames",
|
|
]);
|
|
|
|
const PROCEDURE_SEGMENT = /^[A-Za-z_$][\w$]*$/u;
|
|
|
|
interface MemberPath {
|
|
readonly root: t.Node;
|
|
/** Non-computed property names from the root outward. */
|
|
readonly properties: readonly string[];
|
|
}
|
|
|
|
/** Read a static member path and the expression it starts from. */
|
|
const memberPath = (callee: t.Node): MemberPath | undefined => {
|
|
const reversed: string[] = [];
|
|
let current: t.Node = callee;
|
|
while (
|
|
t.isMemberExpression(current) ||
|
|
t.isOptionalMemberExpression(current)
|
|
) {
|
|
const property = semanticStaticPropertyKey(
|
|
current.property,
|
|
current.computed,
|
|
);
|
|
if (property === null || !PROCEDURE_SEGMENT.test(property))
|
|
return undefined;
|
|
reversed.push(property);
|
|
current = current.object;
|
|
}
|
|
return { root: current, properties: reversed.reverse() };
|
|
};
|
|
|
|
const objectKeys = (node: t.Node | undefined): readonly string[] =>
|
|
t.isObjectExpression(node)
|
|
? node.properties.flatMap((property) => {
|
|
if (!t.isObjectProperty(property) && !t.isObjectMethod(property))
|
|
return [];
|
|
const key = semanticStaticPropertyKey(property.key, property.computed);
|
|
return key === null ? [] : [key];
|
|
})
|
|
: [];
|
|
|
|
/** Bundlers use `(0, imported.factory)` to call an import without a receiver. */
|
|
const rpcFactoryName = (callee: t.Node): string => {
|
|
let current = unwrapJavaScriptExpression(callee).node;
|
|
while (t.isSequenceExpression(current)) {
|
|
const last = current.expressions.at(-1);
|
|
if (last === undefined) return "";
|
|
current = unwrapJavaScriptExpression(last).node;
|
|
}
|
|
return calleeName(current);
|
|
};
|
|
|
|
/** Whether an initializer has the syntax of a documented tRPC client factory. */
|
|
export const isJavaScriptRpcClientFactory = (node: t.Node): boolean => {
|
|
const call = unwrapJavaScriptExpression(node).node;
|
|
return (
|
|
(t.isCallExpression(call) || t.isOptionalCallExpression(call)) &&
|
|
TRPC_CLIENT_FACTORIES.has(
|
|
rpcFactoryName(call.callee).split(".").pop() ?? "",
|
|
)
|
|
);
|
|
};
|
|
|
|
/** Recover the literal `url` option passed to a documented tRPC HTTP link. */
|
|
const trpcLinkUrl = (
|
|
node: t.CallExpression | t.OptionalCallExpression,
|
|
): JavaScriptRpcEndpoint | undefined => {
|
|
const name = rpcFactoryName(node.callee);
|
|
const factory = name.slice(name.lastIndexOf(".") + 1);
|
|
if (!TRPC_URL_LINKS.has(factory)) return undefined;
|
|
const options = node.arguments[0];
|
|
if (!t.isObjectExpression(options)) return undefined;
|
|
const url = semanticFinalObjectStringProperty(options, "url");
|
|
return url === undefined
|
|
? undefined
|
|
: { value: url, mechanism: `trpc:link:${factory}` };
|
|
};
|
|
|
|
/**
|
|
* Create the per-source scan. Hook and option-factory methods are distinctive.
|
|
* `query`/`mutate` need a root bound to a tRPC client factory, or a tRPC source
|
|
* marker with a router path of at least two segments; `subscribe` needs the
|
|
* marker and a tRPC observer argument. Unshadowed browser globals and `this`
|
|
* roots are excluded; RTK Query `endpoints` exclusions apply only without a
|
|
* recognized client binding. Router segment spelling does not supply identity.
|
|
*/
|
|
export const createJavaScriptRpcScan = (
|
|
source: string,
|
|
clientRoots: ReadonlySet<t.Node>,
|
|
index: JavaScriptBindingIndex,
|
|
): JavaScriptRpcScan => {
|
|
let marker: boolean | undefined;
|
|
const hasMarker = (): boolean =>
|
|
(marker ??= TRPC_SOURCE_MARKERS.some((value) => source.includes(value)));
|
|
const graphql = createJavaScriptGraphqlScan(index);
|
|
|
|
const procedure = (
|
|
node: t.CallExpression | t.OptionalCallExpression,
|
|
): JavaScriptRpcEndpoint | undefined => {
|
|
const path = memberPath(node.callee);
|
|
if (path === undefined || t.isThisExpression(path.root)) return undefined;
|
|
const bound = clientRoots.has(path.root);
|
|
if (
|
|
t.isIdentifier(path.root) &&
|
|
BROWSER_GLOBAL_NAMES.has(path.root.name) &&
|
|
index.bindingForReferenceAt(path.root) === undefined
|
|
)
|
|
return undefined;
|
|
const method = path.properties[path.properties.length - 1];
|
|
const segments = path.properties.slice(0, -1);
|
|
const first = segments[0];
|
|
if (method === undefined || first === undefined) return undefined;
|
|
if (!bound && segments.includes("endpoints")) return undefined;
|
|
const recognized = TRPC_HOOK_METHODS.has(method)
|
|
? true
|
|
: TRPC_REQUEST_METHODS.has(method)
|
|
? bound || (segments.length >= 2 && hasMarker())
|
|
: method === "subscribe" &&
|
|
(bound || hasMarker()) &&
|
|
objectKeys(node.arguments[1]).some((key) =>
|
|
TRPC_SUBSCRIPTION_CALLBACKS.has(key),
|
|
);
|
|
return recognized
|
|
? { value: segments.join("."), mechanism: `trpc:procedure:${method}` }
|
|
: undefined;
|
|
};
|
|
|
|
return {
|
|
inspect: (node) => {
|
|
if (t.isCallExpression(node) || t.isOptionalCallExpression(node))
|
|
return trpcLinkUrl(node) ?? procedure(node);
|
|
return graphql.inspect(node);
|
|
},
|
|
finish: graphql.finish,
|
|
};
|
|
};
|