mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 22:41:36 +00:00
* refactor(api-contract): share parser heap admission * refactor(errors): centralize measured heap refusals * test(api-contract): isolate parser heap regression
148 lines
5.2 KiB
TypeScript
148 lines
5.2 KiB
TypeScript
import { decodeStrictUtf8 } from "../../domain/strictUtf8.js";
|
|
import { version } from "graphql";
|
|
import type { ArtifactResourceScope } from "../../resources/index.js";
|
|
import {
|
|
primaryAnalysisError,
|
|
type AnalysisError,
|
|
} from "../../domain/analysisErrorBase.js";
|
|
import { AnalysisInputError } from "../../domain/analysisErrorCore.js";
|
|
import { fileSnapshotPartialObservationSchema } from "../../domain/fileSnapshotObservation.js";
|
|
import {
|
|
API_CONTRACT_OPERATION as operation,
|
|
type ApiContractArtifact,
|
|
} from "../../domain/apiContract/apiContract.js";
|
|
import type { InspectApiContractInput } from "../../domain/apiContract/apiContractQuery.js";
|
|
import { graphqlContractSchema } from "../../domain/apiContract/graphqlContract.js";
|
|
import { GraphqlInventory } from "../../domain/apiContract/GraphqlInventory.js";
|
|
import { parseGraphqlContractSteps } from "../../domain/apiContract/parseGraphqlContract.js";
|
|
import { err, ok, type Result } from "../../domain/result.js";
|
|
import { completeProviderEvidence } from "../ProviderOperationExecution.js";
|
|
import { completeApiContractSteps } from "./ApiContractTraversal.js";
|
|
import { apiContractEvidence } from "./ApiContractEvidence.js";
|
|
import {
|
|
apiContractParseMemoryFailure,
|
|
readApiContract,
|
|
} from "./ApiContractRead.js";
|
|
|
|
type GraphqlInput = Extract<InspectApiContractInput, { format: "graphql-sdl" }>;
|
|
const inputFailure = (parameter: string, message: string, cause?: unknown) =>
|
|
new AnalysisInputError(operation, { cause }, [
|
|
{ path: [parameter], reason: "invalid_format", message },
|
|
]);
|
|
|
|
const decode = (
|
|
bytes: Buffer,
|
|
parameter: string,
|
|
): Result<string, AnalysisError> => {
|
|
const memoryFailure = apiContractParseMemoryFailure(
|
|
bytes.length,
|
|
"Insufficient parser headroom for GraphQL text and AST expansion.",
|
|
"Select a smaller complete document or free parser memory; reply paging does not reduce parsing work.",
|
|
);
|
|
if (memoryFailure !== undefined) return err(memoryFailure);
|
|
try {
|
|
return ok(decodeStrictUtf8(bytes, "preserve"));
|
|
} catch (cause) {
|
|
if (!(cause instanceof TypeError)) throw cause;
|
|
return err(
|
|
inputFailure(
|
|
parameter,
|
|
"The selected GraphQL document is not valid UTF-8.",
|
|
cause,
|
|
),
|
|
);
|
|
}
|
|
};
|
|
|
|
/** Partial facts and independently verified identities survive parsing and descriptor cleanup. */
|
|
const retainFailure = (
|
|
failure: AnalysisError,
|
|
input: GraphqlInput,
|
|
artifact: ApiContractArtifact,
|
|
operationArtifact: ApiContractArtifact | null,
|
|
) => {
|
|
const prior =
|
|
failure.partialObservation ??
|
|
primaryAnalysisError(failure).partialObservation;
|
|
const parsed = graphqlContractSchema.safeParse(prior);
|
|
const snapshot = fileSnapshotPartialObservationSchema.safeParse(prior);
|
|
const secondary =
|
|
snapshot.success &&
|
|
snapshot.data.kind === "file-snapshot-hash" &&
|
|
snapshot.data.sha256 !== null &&
|
|
snapshot.data.path !== null &&
|
|
snapshot.data.path === input.operations_path
|
|
? {
|
|
path: snapshot.data.path,
|
|
sha256: snapshot.data.sha256,
|
|
bytes: snapshot.data.bytes,
|
|
}
|
|
: operationArtifact;
|
|
const report = parsed.success
|
|
? parsed.data
|
|
: new GraphqlInventory(
|
|
artifact,
|
|
secondary,
|
|
version,
|
|
input.operations_path !== undefined,
|
|
).finish(true);
|
|
const projected = apiContractEvidence(input, report);
|
|
let current: AnalysisError | undefined = failure;
|
|
while (current !== undefined) {
|
|
if (
|
|
current.partialObservation === prior ||
|
|
current.partialObservation === undefined
|
|
)
|
|
current.partialObservation = projected;
|
|
current = current.cleanupPrimary;
|
|
}
|
|
return err(failure);
|
|
};
|
|
|
|
/** Loaded only for SDL; both adapters use this verified local-file interpretation. */
|
|
export const importGraphqlContract = async (
|
|
input: GraphqlInput,
|
|
resources: ArtifactResourceScope,
|
|
signal?: AbortSignal,
|
|
) => {
|
|
const loaded = await readApiContract(input, resources, signal);
|
|
if (!loaded.ok) return loaded;
|
|
const { artifact, bytes: schemaBytes } = loaded.value;
|
|
const schemaText = decode(schemaBytes, "path");
|
|
if (!schemaText.ok)
|
|
return retainFailure(schemaText.error, input, artifact, null);
|
|
let operations: { artifact: ApiContractArtifact; text: string } | undefined;
|
|
if (input.operations_path !== undefined) {
|
|
const second = await readApiContract(
|
|
{
|
|
path: input.operations_path,
|
|
max_file_bytes: input.max_file_bytes,
|
|
parameter: "operations_path",
|
|
},
|
|
resources,
|
|
signal,
|
|
);
|
|
if (!second.ok) return retainFailure(second.error, input, artifact, null);
|
|
const { artifact: secondArtifact, bytes: operationsBytes } = second.value;
|
|
const text = decode(operationsBytes, "operations_path");
|
|
if (!text.ok)
|
|
return retainFailure(text.error, input, artifact, secondArtifact);
|
|
operations = { artifact: secondArtifact, text: text.value };
|
|
}
|
|
const parsed = await completeApiContractSteps(
|
|
parseGraphqlContractSteps({ artifact, text: schemaText.value }, operations),
|
|
signal,
|
|
);
|
|
if (!parsed.ok)
|
|
return retainFailure(
|
|
parsed.error,
|
|
input,
|
|
artifact,
|
|
operations?.artifact ?? null,
|
|
);
|
|
return completeProviderEvidence(
|
|
apiContractEvidence(input, parsed.value),
|
|
signal,
|
|
);
|
|
};
|