refactor(domain): consolidate canonical ordering and digest helpers (#624)
* refactor(domain): consolidate canonical ordering and digest helpers Single owner for code-point ordering (domain/canonicalOrdering.ts) and canonical JSON digests (domain/comparisonSemantics.ts). Delete all local compareCodePoints/compareText/digestCanonical duplicates and migrate every importer. No re-export shims. * fix(ci): allow slower real browser startup
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { lstat, realpath } from "node:fs/promises";
|
||||
|
||||
import { canonicalDigest } from "../domain/comparisonSemantics.js";
|
||||
import { AsarArtifactReader } from "../artifacts/AsarArtifactReader.js";
|
||||
import {
|
||||
ArtifactPathRegistry,
|
||||
@@ -23,7 +24,6 @@ import {
|
||||
} from "../domain/artifactGraph.js";
|
||||
import type { BinaryTarget } from "../domain/binaryTarget.js";
|
||||
import { scanArtifactInventory } from "./ArtifactInventory.js";
|
||||
import { digestCanonical } from "./ArtifactGraphConstruction.js";
|
||||
|
||||
/** Local extraction input with the output root chosen by the adapter. */
|
||||
export interface ArtifactExtractionInput {
|
||||
@@ -188,14 +188,14 @@ const createExtractionResult = (
|
||||
selected_occurrence_ids: selected
|
||||
.map(({ occurrence }) => occurrence.occurrence_id)
|
||||
.sort((left, right) => left.localeCompare(right)),
|
||||
files_sha256: digestCanonical(extracted),
|
||||
files_sha256: canonicalDigest(extracted, "Artifact"),
|
||||
output_root_alias: "$OUTPUT_ROOT" as const,
|
||||
};
|
||||
return artifactExtractionResultSchema.parse({
|
||||
manifest: inventory.manifest,
|
||||
extraction_manifest: {
|
||||
...extractionSemantic,
|
||||
extraction_id: `aex_${digestCanonical(extractionSemantic)}`,
|
||||
extraction_id: `aex_${canonicalDigest(extractionSemantic, "Artifact")}`,
|
||||
},
|
||||
output_root: input.outputRoot,
|
||||
artifacts: extracted,
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import canonicalize from "canonicalize";
|
||||
|
||||
import { canonicalDigest } from "../domain/comparisonSemantics.js";
|
||||
import type { ArtifactEntry } from "../artifacts/ArtifactReader.js";
|
||||
import type {
|
||||
ArtifactCommand,
|
||||
@@ -44,7 +41,7 @@ export const createOccurrence = (
|
||||
path: string,
|
||||
parent: string | null,
|
||||
): MutableOccurrence => ({
|
||||
occurrence_id: `occ_${digestCanonical({ path, kind: entry.kind })}`,
|
||||
occurrence_id: `occ_${canonicalDigest({ path, kind: entry.kind }, "Artifact")}`,
|
||||
artifact_id: null,
|
||||
parent_occurrence_id: parent,
|
||||
logical_path: path,
|
||||
@@ -99,7 +96,7 @@ export const materializeDirectoryNodes = (
|
||||
}))
|
||||
.sort((left, right) => compareDirectoryChildNames(left.name, right.name));
|
||||
const node = createArtifactNode({
|
||||
sha256: digestCanonical({ kind: "directory", children }),
|
||||
sha256: canonicalDigest({ kind: "directory", children }, "Artifact"),
|
||||
size: 0,
|
||||
kind: directory.logical_path.toLowerCase().endsWith(".framework")
|
||||
? "framework"
|
||||
@@ -128,13 +125,16 @@ export const createRootNode = (input: {
|
||||
createArtifactNode({
|
||||
sha256:
|
||||
input.digest?.sha256 ??
|
||||
digestCanonical({
|
||||
kind: "directory-root",
|
||||
children: input.occurrences.map(({ logical_path, artifact_id }) => ({
|
||||
logical_path,
|
||||
artifact_id,
|
||||
})),
|
||||
}),
|
||||
canonicalDigest(
|
||||
{
|
||||
kind: "directory-root",
|
||||
children: input.occurrences.map(({ logical_path, artifact_id }) => ({
|
||||
logical_path,
|
||||
artifact_id,
|
||||
})),
|
||||
},
|
||||
"Artifact",
|
||||
),
|
||||
size: input.digest?.bytes ?? 0,
|
||||
kind:
|
||||
input.directory ||
|
||||
@@ -159,7 +159,7 @@ export const createArtifactNode = (input: {
|
||||
readonly contentState: ArtifactNode["content_state"];
|
||||
readonly limitations?: readonly string[];
|
||||
}): ArtifactNode => ({
|
||||
artifact_id: `art_${digestCanonical({ sha256: input.sha256 })}`,
|
||||
artifact_id: `art_${canonicalDigest({ sha256: input.sha256 }, "Artifact")}`,
|
||||
kind: input.kind,
|
||||
format: input.format,
|
||||
sha256: input.sha256,
|
||||
@@ -175,7 +175,7 @@ export const rootOccurrenceFor = (
|
||||
node: ArtifactNode,
|
||||
declaredSize: number,
|
||||
): MutableOccurrence => ({
|
||||
occurrence_id: `occ_${digestCanonical({ root: node.artifact_id })}`,
|
||||
occurrence_id: `occ_${canonicalDigest({ root: node.artifact_id }, "Artifact")}`,
|
||||
artifact_id: node.artifact_id,
|
||||
parent_occurrence_id: null,
|
||||
logical_path: ".",
|
||||
@@ -197,11 +197,14 @@ export const rekeyOccurrences = (
|
||||
for (const occurrence of occurrences)
|
||||
replacements.set(
|
||||
occurrence.occurrence_id,
|
||||
`occ_${digestCanonical({
|
||||
root_artifact_id: rootArtifactId,
|
||||
logical_path: occurrence.logical_path,
|
||||
entry_kind: occurrence.entry_kind,
|
||||
})}`,
|
||||
`occ_${canonicalDigest(
|
||||
{
|
||||
root_artifact_id: rootArtifactId,
|
||||
logical_path: occurrence.logical_path,
|
||||
entry_kind: occurrence.entry_kind,
|
||||
},
|
||||
"Artifact",
|
||||
)}`,
|
||||
);
|
||||
for (const occurrence of occurrences) {
|
||||
occurrence.occurrence_id =
|
||||
@@ -245,7 +248,7 @@ export const createArtifactEdges = (
|
||||
logical_path: occurrence.logical_path,
|
||||
};
|
||||
edges.push({
|
||||
edge_id: `edge_${digestCanonical(semantic)}`,
|
||||
edge_id: `edge_${canonicalDigest(semantic, "Artifact")}`,
|
||||
...semantic,
|
||||
producer: occurrence.entry_kind === "slice" ? (producer ?? null) : null,
|
||||
ordinal: edges.length,
|
||||
@@ -356,13 +359,6 @@ export const classifyArtifactContent = (
|
||||
return byPath;
|
||||
};
|
||||
|
||||
export const digestCanonical = (value: unknown): string => {
|
||||
const encoded = canonicalize(value);
|
||||
if (encoded === undefined)
|
||||
throw new TypeError("Artifact value is not canonical JSON");
|
||||
return createHash("sha256").update(encoded).digest("hex");
|
||||
};
|
||||
|
||||
const relationFor = (path: string): ArtifactEdge["relation"] => {
|
||||
const lower = path.toLowerCase();
|
||||
if (lower.endsWith(".map")) return "maps-source";
|
||||
|
||||
@@ -15,13 +15,13 @@ import {
|
||||
import {
|
||||
createArtifactEdges,
|
||||
createRootNode,
|
||||
digestCanonical,
|
||||
indexOccurrencesByPath,
|
||||
materializeDirectoryNodes,
|
||||
rekeyOccurrences,
|
||||
rootOccurrenceFor,
|
||||
type MutableOccurrence,
|
||||
} from "../ArtifactGraphConstruction.js";
|
||||
import { canonicalDigest } from "../../domain/comparisonSemantics.js";
|
||||
import { classifyRoot } from "./classify.js";
|
||||
import { hashReadable, type HashResult } from "./hash.js";
|
||||
import { createReader, inventoryLimitations } from "./reader.js";
|
||||
@@ -125,12 +125,15 @@ const buildInventorySnapshot = async (
|
||||
|
||||
await verifyRootDigest(path, rootDigest, signal);
|
||||
|
||||
const graphSha256 = digestCanonical({
|
||||
nodes: orderedNodes,
|
||||
occurrences: orderedOccurrences,
|
||||
edges: orderedEdges,
|
||||
integrity_contradictions: integrityContradictions,
|
||||
});
|
||||
const graphSha256 = canonicalDigest(
|
||||
{
|
||||
nodes: orderedNodes,
|
||||
occurrences: orderedOccurrences,
|
||||
edges: orderedEdges,
|
||||
integrity_contradictions: integrityContradictions,
|
||||
},
|
||||
"Artifact",
|
||||
);
|
||||
const manifest = buildManifest({
|
||||
rootNode,
|
||||
graphSha256,
|
||||
@@ -168,12 +171,15 @@ const buildIntegrityContradictions = (
|
||||
: occurrenceById.get(occurrence.parent_occurrence_id);
|
||||
const parentArtifactId = parent?.artifact_id ?? rootNode.artifact_id;
|
||||
return {
|
||||
contradiction_id: `ic_${digestCanonical({
|
||||
root_artifact_id: rootNode.artifact_id,
|
||||
logical_path: contradiction.logicalPath,
|
||||
declared_sha256: contradiction.declaredSha256,
|
||||
observed_sha256: contradiction.observedSha256,
|
||||
})}`,
|
||||
contradiction_id: `ic_${canonicalDigest(
|
||||
{
|
||||
root_artifact_id: rootNode.artifact_id,
|
||||
logical_path: contradiction.logicalPath,
|
||||
declared_sha256: contradiction.declaredSha256,
|
||||
observed_sha256: contradiction.observedSha256,
|
||||
},
|
||||
"Artifact",
|
||||
)}`,
|
||||
occurrence_id: occurrence.occurrence_id,
|
||||
parent_artifact_id: parentArtifactId,
|
||||
logical_path: contradiction.logicalPath,
|
||||
@@ -235,10 +241,13 @@ const buildManifest = ({
|
||||
readonly orderedEdges: readonly { edge_id: string }[];
|
||||
}): ArtifactInventoryResult["manifest"] =>
|
||||
artifactInventoryResultSchema.shape.manifest.parse({
|
||||
manifest_id: `agm_${digestCanonical({
|
||||
root_artifact_id: rootNode.artifact_id,
|
||||
graph_sha256: graphSha256,
|
||||
})}`,
|
||||
manifest_id: `agm_${canonicalDigest(
|
||||
{
|
||||
root_artifact_id: rootNode.artifact_id,
|
||||
graph_sha256: graphSha256,
|
||||
},
|
||||
"Artifact",
|
||||
)}`,
|
||||
root_artifact_id: rootNode.artifact_id,
|
||||
root_sha256: rootNode.sha256,
|
||||
root_format: rootNode.format,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { canonicalDigest } from "../domain/comparisonSemantics.js";
|
||||
import type { ProviderIdentity } from "./AnalysisProvider.js";
|
||||
import type { BrowserScenario } from "../domain/browserScenario.js";
|
||||
import type { BrowserScenarioCapture } from "../domain/browserScenarioCapture.js";
|
||||
@@ -8,12 +9,11 @@ import {
|
||||
type EvidenceObservation,
|
||||
} from "../domain/evidence.js";
|
||||
import { jsonValueSchema } from "../domain/jsonValue.js";
|
||||
import { digestCanonicalJson } from "../domain/browserScenarioNormalization.js";
|
||||
|
||||
const browserScenarioParameters = (
|
||||
scenario: BrowserScenario,
|
||||
): EvidenceObservation["parameters"] => ({
|
||||
scenario_sha256: digestCanonicalJson(scenario),
|
||||
scenario_sha256: canonicalDigest(scenario, "Browser scenario"),
|
||||
browser_mode: scenario.browser.mode,
|
||||
...(scenario.browser.mode === "launch"
|
||||
? { browser_headless: scenario.browser.headless }
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { uniqueSorted } from "../domain/canonicalOrdering.js";
|
||||
import type {
|
||||
ClientRegistrationStatus,
|
||||
UnhealthyClientRegistrationStatus,
|
||||
@@ -175,8 +176,5 @@ const selectedSkillCheck = (
|
||||
: { remediation: "Run rea setup to update the installed REA skill." }),
|
||||
};
|
||||
|
||||
const uniqueSorted = (values: readonly string[]): readonly string[] =>
|
||||
[...new Set(values)].sort((left, right) => left.localeCompare(right));
|
||||
|
||||
const doctorProviderCheckName = (providerId: string, name: string): boolean =>
|
||||
name === providerId || name.startsWith(`${providerId}-`);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { canonicalDigest } from "../domain/comparisonSemantics.js";
|
||||
import type { ProviderIdentity } from "./AnalysisProvider.js";
|
||||
import type { Evidence, EvidenceObservation } from "../domain/evidence.js";
|
||||
import { createEvidence } from "../domain/evidence.js";
|
||||
@@ -6,7 +7,6 @@ import type {
|
||||
ElectronActiveObservationResult,
|
||||
} from "../domain/electronActiveObservation.js";
|
||||
import { jsonValueSchema } from "../domain/jsonValue.js";
|
||||
import { digestCanonicalJson } from "../domain/browserScenarioNormalization.js";
|
||||
|
||||
type CanonicalElectronActiveObservationInput =
|
||||
ElectronActiveObservationInput & {
|
||||
@@ -52,5 +52,8 @@ const parameters = (
|
||||
input: CanonicalElectronActiveObservationInput,
|
||||
): EvidenceObservation["parameters"] => ({
|
||||
...scenarioProjection(input),
|
||||
scenario_sha256: digestCanonicalJson(scenarioProjection(input)),
|
||||
scenario_sha256: canonicalDigest(
|
||||
scenarioProjection(input),
|
||||
"Browser scenario",
|
||||
),
|
||||
});
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { compareCodePoints } from "../domain/canonicalOrdering.js";
|
||||
import type { ElectronIpcFinding } from "../domain/electronStaticAnalysisTypes.js";
|
||||
import type { ElectronBoundarySummary } from "../domain/javascriptApplicationAnalysis.js";
|
||||
import type { JavaScriptArtifactAnalysis } from "./JavaScriptArtifactAnalysisTypes.js";
|
||||
@@ -221,6 +222,3 @@ const resolvesToFile = (input: ResolveToFileInput): boolean => {
|
||||
|
||||
const ipcRecordKey = (path: string, finding: ElectronIpcFinding): string =>
|
||||
`${path}\0${String(finding.location.start.line)}:${String(finding.location.start.column)}\0${finding.side}\0${finding.operation}`;
|
||||
|
||||
const compareCodePoints = (left: string, right: string): number =>
|
||||
left < right ? -1 : left > right ? 1 : 0;
|
||||
|
||||
@@ -2,6 +2,7 @@ import { createHash } from "node:crypto";
|
||||
import { isAbsolute } from "node:path";
|
||||
import type { Readable } from "node:stream";
|
||||
|
||||
import { compareCodePoints } from "../domain/canonicalOrdering.js";
|
||||
import { AsarArtifactReader } from "../artifacts/AsarArtifactReader.js";
|
||||
import {
|
||||
ArtifactPathRegistry,
|
||||
@@ -331,6 +332,3 @@ const abortIfNeeded = (signal?: AbortSignal): void => {
|
||||
"JavaScript artifact reconstruction cancelled",
|
||||
);
|
||||
};
|
||||
|
||||
const compareCodePoints = (left: string, right: string): number =>
|
||||
left < right ? -1 : left > right ? 1 : 0;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { compareCodePoints } from "../domain/canonicalOrdering.js";
|
||||
import type { JavaScriptJsonModuleObservation } from "./JavaScriptArtifactAnalysisTypes.js";
|
||||
import type { JavaScriptArtifactFile } from "./JavaScriptArtifactFiles.js";
|
||||
|
||||
@@ -41,6 +42,3 @@ export const analyzeJavaScriptJsonModule = (
|
||||
limitation: null,
|
||||
};
|
||||
};
|
||||
|
||||
const compareCodePoints = (left: string, right: string): number =>
|
||||
left < right ? -1 : left > right ? 1 : 0;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { posix } from "node:path";
|
||||
|
||||
import { compareCodePoints } from "../domain/canonicalOrdering.js";
|
||||
import type { ApplicationNode } from "../domain/javascriptApplicationGraph.js";
|
||||
import {
|
||||
completeApplicationCoverage,
|
||||
@@ -409,6 +410,3 @@ const isExportLink = (link: JavaScriptSemanticModuleLink): boolean =>
|
||||
link.kind === "export" ||
|
||||
link.kind === "re-export" ||
|
||||
link.kind === "commonjs-export";
|
||||
|
||||
const compareCodePoints = (left: string, right: string): number =>
|
||||
left < right ? -1 : left > right ? 1 : 0;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { compareCodePoints } from "../domain/javascriptApplicationGraph.js";
|
||||
import { compareCodePoints } from "../domain/canonicalOrdering.js";
|
||||
import type { JavaScriptSemanticGraphNode } from "../domain/javascriptSemanticGraph.js";
|
||||
import { JAVASCRIPT_SEMANTIC_RELATION_FAMILIES } from "../domain/javascriptSemanticGraphSchemas.js";
|
||||
import type { JavaScriptSemanticIr } from "../domain/javascriptSemanticIr.js";
|
||||
|
||||
Reference in New Issue
Block a user