From fc5847810efaa003ea4ed9bb2a218c2ccc886a58 Mon Sep 17 00:00:00 2001 From: Chuan-kai Lin Date: Fri, 5 Sep 2025 11:23:02 -0700 Subject: [PATCH] Overlay: clarify componentsJson computation This commit updates componentsJson computation to call JSON.stringify() without the replacer array and documents why the result is stable. --- src/overlay-database-utils.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/overlay-database-utils.ts b/src/overlay-database-utils.ts index 79cd7c788..b53f02f07 100644 --- a/src/overlay-database-utils.ts +++ b/src/overlay-database-utils.ts @@ -443,10 +443,15 @@ async function getCacheRestoreKey( * @returns A short SHA-256 hash (first 16 characters) of the components */ function createCacheKeyHash(components: Record): string { - const componentsJson = JSON.stringify( - components, - Object.keys(components).sort(), - ); + // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify + // + // "Properties are visited using the same algorithm as Object.keys(), which + // has a well-defined order and is stable across implementations. For example, + // JSON.stringify on the same object will always produce the same string, and + // JSON.parse(JSON.stringify(obj)) would produce an object with the same key + // ordering as the original (assuming the object is completely + // JSON-serializable)." + const componentsJson = JSON.stringify(components); return crypto .createHash("sha256") .update(componentsJson)