Skip to content

Commit fc58478

Browse files
committed
Overlay: clarify componentsJson computation
This commit updates componentsJson computation to call JSON.stringify() without the replacer array and documents why the result is stable.
1 parent 4c82ae2 commit fc58478

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/overlay-database-utils.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,15 @@ async function getCacheRestoreKey(
443443
* @returns A short SHA-256 hash (first 16 characters) of the components
444444
*/
445445
function createCacheKeyHash(components: Record<string, any>): string {
446-
const componentsJson = JSON.stringify(
447-
components,
448-
Object.keys(components).sort(),
449-
);
446+
// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
447+
//
448+
// "Properties are visited using the same algorithm as Object.keys(), which
449+
// has a well-defined order and is stable across implementations. For example,
450+
// JSON.stringify on the same object will always produce the same string, and
451+
// JSON.parse(JSON.stringify(obj)) would produce an object with the same key
452+
// ordering as the original (assuming the object is completely
453+
// JSON-serializable)."
454+
const componentsJson = JSON.stringify(components);
450455
return crypto
451456
.createHash("sha256")
452457
.update(componentsJson)

0 commit comments

Comments
 (0)