2026-07-22 23:07:51 +00:00
|
|
|
import canonicalize from "canonicalize";
|
|
|
|
|
import {
|
|
|
|
|
javaScriptSemanticGraphSchema,
|
|
|
|
|
type JavaScriptSemanticGraph,
|
|
|
|
|
} from "./javascriptSemanticGraph.js";
|
|
|
|
|
|
|
|
|
|
const canonicalJson = (value: unknown): string => {
|
|
|
|
|
const encoded = canonicalize(value);
|
|
|
|
|
if (encoded === undefined)
|
|
|
|
|
throw new TypeError(
|
|
|
|
|
"JavaScript semantic graph could not canonicalize data",
|
|
|
|
|
);
|
|
|
|
|
return encoded;
|
|
|
|
|
};
|
|
|
|
|
|
2026-09-29 07:08:46 +08:00
|
|
|
/** Parse a stored semantic graph and reject stale IDs. */
|
2026-07-22 23:07:51 +00:00
|
|
|
export const parseJavaScriptSemanticGraph = (
|
|
|
|
|
input: unknown,
|
2026-09-29 07:08:46 +08:00
|
|
|
): JavaScriptSemanticGraph => javaScriptSemanticGraphSchema.parse(input);
|
2026-07-22 23:07:51 +00:00
|
|
|
|
|
|
|
|
/** Serialize a verified semantic graph as canonical JSON. */
|
|
|
|
|
export const serializeJavaScriptSemanticGraph = (input: unknown): string =>
|
|
|
|
|
canonicalJson(parseJavaScriptSemanticGraph(input));
|