Files
reverse-engineering-anything/src/ghidra/protocol.ts
T
郭立 Guo Li 7590b50801 perf(schema): reduce retained integer validator allocations (#4360)
Construct integer formats directly while preserving bounds, refinements, metadata and current shared schema owners. Keep Incur-dependent CLI numeric coercion declarations unchanged.

Verify canonical advertisements, 126 numeric boundary cases, 974 actual CLI parser cases, broad source tests and real public-artifact SDK workflows. Record 72 normal tasks and 12 separate owned-inspector heap diagnostics with whole-family resource bounds; distinguish SDK spans and asymmetric audit overhead. Generate numerical report sections from receipts and reject stale tables through documentation conformance checks.

Closes #6236. Current-head CI and Greptile pass; actionable review findings are addressed. Codex review was unavailable due repository usage limits.
2026-10-22 05:11:08 +00:00

62 lines
1.7 KiB
TypeScript

import { z } from "zod";
import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
import { err, ok, type Result } from "../domain/result.js";
const responseSchema = z.discriminatedUnion("ok", [
z.strictObject({
id: z.int().positive(),
ok: z.literal(true),
result: jsonValueSchema,
}),
z.strictObject({
id: z.int().positive(),
ok: z.literal(false),
error: z
.object({
code: z.string().min(1),
message: z.string().min(1),
})
.strict(),
}),
]);
/** Validated response emitted by REA's packaged Ghidra script. */
export type GhidraBridgeResponse = z.infer<typeof responseSchema>;
/** Expected operation failure returned by the authenticated Java bridge. */
export class GhidraRemoteError extends Error {
constructor(
readonly code: string,
message: string,
) {
super(message);
this.name = "GhidraRemoteError";
}
}
/** Parse one complete Java-bridge response without accepting extra fields. */
export const parseGhidraResponseLine = (
line: string,
): Result<GhidraBridgeResponse, Error> => {
try {
const parsed: unknown = JSON.parse(line);
const response = responseSchema.safeParse(parsed);
return response.success
? ok(response.data)
: err(new Error("Ghidra bridge response has an invalid shape"));
} catch (cause: unknown) {
return err(
new Error("Ghidra bridge response is not valid JSON", { cause }),
);
}
};
/** Project one validated response into its result or remote failure. */
export const ghidraResponseResult = (
response: GhidraBridgeResponse,
): Result<JsonValue, GhidraRemoteError> =>
response.ok
? ok(response.result)
: err(new GhidraRemoteError(response.error.code, response.error.message));