Update hashing
This commit is contained in:
parent
65d75dec40
commit
1c6cd80b64
3 changed files with 9 additions and 14 deletions
|
|
@ -348,7 +348,7 @@ export class Syncer {
|
||||||
try {
|
try {
|
||||||
const contentBytes =
|
const contentBytes =
|
||||||
await this.operations.read(relativePath); // this can throw FileNotFoundError
|
await this.operations.read(relativePath); // this can throw FileNotFoundError
|
||||||
return hash(contentBytes);
|
return await hash(contentBytes);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (
|
if (
|
||||||
e instanceof Error &&
|
e instanceof Error &&
|
||||||
|
|
|
||||||
|
|
@ -101,7 +101,7 @@ export class UnrestrictedSyncer {
|
||||||
const contentBytes = await this.operations.read(
|
const contentBytes = await this.operations.read(
|
||||||
document.relativePath
|
document.relativePath
|
||||||
); // this can throw FileNotFoundError
|
); // this can throw FileNotFoundError
|
||||||
const contentHash = hash(contentBytes);
|
const contentHash = await hash(contentBytes);
|
||||||
|
|
||||||
let response: DocumentVersion | DocumentUpdateResponse | undefined =
|
let response: DocumentVersion | DocumentUpdateResponse | undefined =
|
||||||
undefined;
|
undefined;
|
||||||
|
|
@ -342,7 +342,7 @@ export class UnrestrictedSyncer {
|
||||||
{
|
{
|
||||||
documentId: remoteVersion.documentId,
|
documentId: remoteVersion.documentId,
|
||||||
parentVersionId: remoteVersion.vaultUpdateId,
|
parentVersionId: remoteVersion.vaultUpdateId,
|
||||||
hash: hash(contentBytes),
|
hash: await hash(contentBytes),
|
||||||
remoteRelativePath: remoteVersion.relativePath
|
remoteRelativePath: remoteVersion.relativePath
|
||||||
},
|
},
|
||||||
this.database.createNewPendingDocument(
|
this.database.createNewPendingDocument(
|
||||||
|
|
@ -513,7 +513,7 @@ export class UnrestrictedSyncer {
|
||||||
|
|
||||||
if (!("type" in response) || response.type === "MergingUpdate") {
|
if (!("type" in response) || response.type === "MergingUpdate") {
|
||||||
const responseBytes = base64ToBytes(response.contentBase64);
|
const responseBytes = base64ToBytes(response.contentBase64);
|
||||||
contentHash = hash(responseBytes);
|
contentHash = await hash(responseBytes);
|
||||||
|
|
||||||
this.database.updateDocumentMetadata(
|
this.database.updateDocumentMetadata(
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,7 @@
|
||||||
// https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript
|
export async function hash(content: Uint8Array): Promise<string> {
|
||||||
export function hash(content: Uint8Array): string {
|
const digest = await crypto.subtle.digest("SHA-256", content);
|
||||||
let result = 0;
|
const bytes = new Uint8Array(digest);
|
||||||
// eslint-disable-next-line @typescript-eslint/prefer-for-of
|
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
||||||
for (let i = 0; i < content.length; i++) {
|
|
||||||
result = (result << 5) - result + content[i];
|
|
||||||
result |= 0; // Convert to 32bit integer
|
|
||||||
}
|
|
||||||
return Math.abs(result).toString(16).padStart(8, "0");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const EMPTY_HASH = hash(new Uint8Array(0));
|
export const EMPTY_HASH = await hash(new Uint8Array(0));
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue