refactor(browser): share canonical identity digest

This commit is contained in:
morluto
2026-09-29 19:08:30 +08:00
parent eec75ce918
commit a6d9e49ab4
3 changed files with 14 additions and 22 deletions
+10
View File
@@ -0,0 +1,10 @@
import { createHash } from "node:crypto";
import canonicalize from "canonicalize";
/** Hash a value using the canonical JSON representation. */
export const canonicalJsonDigest = (value: unknown): string => {
const encoded = canonicalize(value);
if (encoded === undefined) throw new TypeError("Expected canonical JSON");
return createHash("sha256").update(encoded).digest("hex");
};
+2 -11
View File
@@ -1,7 +1,3 @@
import { createHash } from "node:crypto";
import canonicalize from "canonicalize";
import type { ProgressReporter } from "../application/ProgressReporter.js"; import type { ProgressReporter } from "../application/ProgressReporter.js";
import type { import type {
ElectronPageInspection, ElectronPageInspection,
@@ -11,6 +7,7 @@ import { BrowserObservationError } from "../domain/errors.js";
import type { CdpConnection, CdpEvent } from "./CdpConnection.js"; import type { CdpConnection, CdpEvent } from "./CdpConnection.js";
import type { CdpEndpointDiscovery, CdpEndpointTarget } from "./CdpEndpoint.js"; import type { CdpEndpointDiscovery, CdpEndpointTarget } from "./CdpEndpoint.js";
import { CdpCaptureCompleteness } from "./CdpCaptureCompleteness.js"; import { CdpCaptureCompleteness } from "./CdpCaptureCompleteness.js";
import { canonicalJsonDigest } from "./CdpCanonicalJsonDigest.js";
import { import {
delayWithCancellation, delayWithCancellation,
numberValue, numberValue,
@@ -298,7 +295,7 @@ const captureResources = async (
? null ? null
: Math.max(0, numberValue(resource.contentSize) ?? 0), : Math.max(0, numberValue(resource.contentSize) ?? 0),
}; };
const resourceKey = `electron_resource_${digest(item)}`; const resourceKey = `electron_resource_${canonicalJsonDigest(item)}`;
if (seen.has(resourceKey)) continue; if (seen.has(resourceKey)) continue;
seen.add(resourceKey); seen.add(resourceKey);
resources.push({ resources.push({
@@ -381,12 +378,6 @@ const integer = (value: unknown, fallback: number): number => {
return number === undefined ? fallback : Math.trunc(number); return number === undefined ? fallback : Math.trunc(number);
}; };
const digest = (value: unknown): string => {
const encoded = canonicalize(value);
if (encoded === undefined) throw new TypeError("Expected canonical JSON");
return createHash("sha256").update(encoded).digest("hex");
};
const report = async ( const report = async (
progress: ProgressReporter | undefined, progress: ProgressReporter | undefined,
completed: number, completed: number,
+2 -11
View File
@@ -1,7 +1,3 @@
import { createHash } from "node:crypto";
import canonicalize from "canonicalize";
import type { import type {
ElectronPageInspection, ElectronPageInspection,
InspectElectronPageInput, InspectElectronPageInput,
@@ -9,6 +5,7 @@ import type {
import { createWebTextArtifact } from "../domain/webContentArtifact.js"; import { createWebTextArtifact } from "../domain/webContentArtifact.js";
import type { CdpConnection } from "./CdpConnection.js"; import type { CdpConnection } from "./CdpConnection.js";
import { CdpCaptureCompleteness } from "./CdpCaptureCompleteness.js"; import { CdpCaptureCompleteness } from "./CdpCaptureCompleteness.js";
import { canonicalJsonDigest } from "./CdpCanonicalJsonDigest.js";
import { requiredRecord, stringValue } from "./CdpCaptureValues.js"; import { requiredRecord, stringValue } from "./CdpCaptureValues.js";
import type { ElectronScriptDraft } from "./CdpElectronScriptEvents.js"; import type { ElectronScriptDraft } from "./CdpElectronScriptEvents.js";
import { authorizedElectronFile } from "./ElectronFileScope.js"; import { authorizedElectronFile } from "./ElectronFileScope.js";
@@ -52,7 +49,7 @@ export const captureElectronScripts = async (
is_module: script.isModule, is_module: script.isModule,
language: script.language, language: script.language,
}; };
const scriptKey = `electron_script_${digest(identity)}`; const scriptKey = `electron_script_${canonicalJsonDigest(identity)}`;
if (seen.has(scriptKey)) continue; if (seen.has(scriptKey)) continue;
seen.add(scriptKey); seen.add(scriptKey);
total += 1; total += 1;
@@ -105,9 +102,3 @@ const captureScriptSource = async (
}, },
}; };
}; };
const digest = (value: unknown): string => {
const encoded = canonicalize(value);
if (encoded === undefined) throw new TypeError("Expected canonical JSON");
return createHash("sha256").update(encoded).digest("hex");
};