From 0897f7a545906e64e5bc734faf91d7a26b79cfdb Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Wed, 1 Apr 2026 22:29:57 +0100 Subject: [PATCH] Make hash async --- frontend/sync-client/src/utils/find-matching-file.ts | 12 ++++++------ frontend/sync-client/src/utils/hash.ts | 8 ++++++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/frontend/sync-client/src/utils/find-matching-file.ts b/frontend/sync-client/src/utils/find-matching-file.ts index c3d323d3..67b89876 100644 --- a/frontend/sync-client/src/utils/find-matching-file.ts +++ b/frontend/sync-client/src/utils/find-matching-file.ts @@ -1,14 +1,14 @@ -import type { DocumentRecord } from "../persistence/database"; +import type { DocumentRecord, RelativePath } from "../sync-operations/types"; import { EMPTY_HASH } from "./hash"; // TODO: make this smarter so that offline files can be renamed & edited at the same time -export function findMatchingFile( +export async function findMatchingFile( contentHash: string, - candidates: DocumentRecord[] -): DocumentRecord | undefined { - if (contentHash === EMPTY_HASH) { + candidates: { path: RelativePath; record: DocumentRecord }[] +): Promise<{ path: RelativePath; record: DocumentRecord } | undefined> { + if (contentHash === await EMPTY_HASH) { return undefined; } - return candidates.find(({ metadata }) => metadata?.hash === contentHash); + return candidates.find(({ record }) => record.hash === contentHash); } diff --git a/frontend/sync-client/src/utils/hash.ts b/frontend/sync-client/src/utils/hash.ts index 814faefa..933929c5 100644 --- a/frontend/sync-client/src/utils/hash.ts +++ b/frontend/sync-client/src/utils/hash.ts @@ -1,7 +1,11 @@ export async function hash(content: Uint8Array): Promise { - const digest = await crypto.subtle.digest("SHA-256", content); + const digest = await crypto.subtle.digest( + "SHA-256", + content as Uint8Array + ); const bytes = new Uint8Array(digest); return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join(""); } -export const EMPTY_HASH = await hash(new Uint8Array(0)); +// SHA-256 of empty content, computed once at import time +export const EMPTY_HASH: Promise = hash(new Uint8Array());