diff --git a/src/domain/webInventory.ts b/src/domain/webInventory.ts index cf9b24ba753..c28cca8d5aa 100644 --- a/src/domain/webInventory.ts +++ b/src/domain/webInventory.ts @@ -46,19 +46,19 @@ export const webResourceComparisonIdentity = ( "Web resource comparison identity", )}`; +const resourceKey = (resource: WebInventoryResourceInput): string => + `res_${digestCanonicalValue(resource, "Web inventory")}`; + /** Assign deterministic identities to a bounded resource inventory. */ export const stableWebResources = ( resources: readonly WebInventoryResourceInput[], ): StableWebResource[] => { const keyed = resources.map((resource) => ({ ...resource, - resource_key: `res_${digestCanonicalValue(resource, "Web inventory")}`, + resource_key: resourceKey(resource), })); - return [ - ...new Map( - keyed.map((resource) => [resource.resource_key, resource]), - ).values(), - ].sort( + const byKey = new Map(keyed.map((row) => [row.resource_key, row])); + return [...byKey.values()].sort( (left, right) => compareUnicodeCodePoints(left.resource_key, right.resource_key) || compareUnicodeCodePoints(left.url, right.url), @@ -93,9 +93,7 @@ export const createCapturedWebScriptReconciler = ( const resource = rawByUrl.get(script.rawUrl); if (resource !== undefined && resource !== null) { const { rawUrl: _rawUrl, ...publicResource } = resource; - const stable = stableWebResources([publicResource])[0]; - if (stable !== undefined) - return { status: "exact", resource_key: stable.resource_key }; + return { status: "exact", resource_key: resourceKey(publicResource) }; } const candidates = keysByUrl.get(script.url); if (candidates !== undefined)