Skip to content

Commit 988d85b

Browse files
committed
use hmac function from wasm
1 parent 66299a3 commit 988d85b

File tree

8 files changed

+1816
-1767
lines changed

8 files changed

+1816
-1767
lines changed

packages/hub/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"test": "vitest run",
4343
"test:browser": "vitest run --browser.name=chrome --browser.headless --config vitest-browser.config.mts",
4444
"check": "tsc",
45-
"build:xet-wasm": "./scripts/build-xet-wasm.sh -t bundler --clean"
45+
"build:xet-wasm": "./scripts/build-xet-wasm.sh -t bundler --clean -b hmac-wasm"
4646
},
4747
"files": [
4848
"src",

packages/hub/src/utils/ChunkCache.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ export class ChunkCache {
5353
}
5454
}
5555

56-
getChunk(hash: string):
56+
getChunk(
57+
hash: string,
58+
hmacFunction: (hash: string, key: string) => string
59+
):
5760
| {
5861
xorbIndex: number;
5962
offset: number;
@@ -63,7 +66,7 @@ export class ChunkCache {
6366
let index = this.map.get(hash);
6467
if (index === undefined) {
6568
for (const hmac of this.hmacs) {
66-
index = this.map.get(hashHash(hash, hmac));
69+
index = this.map.get(hmacFunction(hash, hmac));
6770
if (index !== undefined) {
6871
break;
6972
}
@@ -79,8 +82,3 @@ export class ChunkCache {
7982
};
8083
}
8184
}
82-
83-
// Todo: use wasm to hash
84-
function hashHash(hash: string, hmac: string): string {
85-
return hash + hmac;
86-
}

packages/hub/src/utils/createXorbs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export async function* createXorbs(
135135
sourceChunks.splice(0, index);
136136
}
137137

138-
let cacheData = chunkCache.getChunk(chunk.hash);
138+
let cacheData = chunkCache.getChunk(chunk.hash, chunkModule.compute_hmac);
139139
if (cacheData === undefined && chunk.dedup && bytesSinceRemoteDedup >= INTERVAL_BETWEEN_REMOTE_DEDUP) {
140140
const token = await xetWriteToken(params);
141141
bytesSinceRemoteDedup = 0;
@@ -160,8 +160,8 @@ export async function* createXorbs(
160160
);
161161
}
162162
}
163+
cacheData = chunkCache.getChunk(chunk.hash, chunkModule.compute_hmac);
163164
}
164-
cacheData = chunkCache.getChunk(chunk.hash);
165165
}
166166
if (cacheData === undefined) {
167167
xorbOffset = writeChunk(xorb, xorbOffset, chunkToCopy);

packages/hub/src/vendor/xet-chunk/chunker_wasm.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ init();
5555

5656
export { init };
5757

58-
export { compute_xorb_hash, compute_file_hash, Chunker, compute_verification_hash } from "./chunker_wasm_bg.js";
58+
export {
59+
compute_xorb_hash,
60+
compute_file_hash,
61+
Chunker,
62+
compute_verification_hash,
63+
compute_hmac,
64+
} from "./chunker_wasm_bg.js";
5965

6066
// const exports = WebAssembly.Module.exports(wasmModule).map((item) => item.name);
6167

packages/hub/src/vendor/xet-chunk/chunker_wasm_bg.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
export function compute_xorb_hash(chunks_array: Array<{ hash: string; length: number }>): string;
44
export function compute_verification_hash(chunkHashes: string[]): string;
55
export function compute_file_hash(chunks_array: Array<{ hash: string; length: number }>): string;
6+
export function compute_hmac(hash: string, key: string): string;
67
export class Chunker {
78
free(): void;
89
constructor(target_chunk_size: number);

packages/hub/src/vendor/xet-chunk/chunker_wasm_bg.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,35 @@ export function compute_verification_hash(chunk_hashes) {
272272
}
273273
}
274274

275+
/**
276+
* takes a hash and HMAC key (both as hex strings) and returns the HMAC result as a hex string
277+
* @param {string} hash_hex
278+
* @param {string} hmac_key_hex
279+
* @returns {string}
280+
*/
281+
export function compute_hmac(hash_hex, hmac_key_hex) {
282+
let deferred4_0;
283+
let deferred4_1;
284+
try {
285+
const ptr0 = passStringToWasm0(hash_hex, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
286+
const len0 = WASM_VECTOR_LEN;
287+
const ptr1 = passStringToWasm0(hmac_key_hex, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
288+
const len1 = WASM_VECTOR_LEN;
289+
const ret = wasm.compute_hmac(ptr0, len0, ptr1, len1);
290+
var ptr3 = ret[0];
291+
var len3 = ret[1];
292+
if (ret[3]) {
293+
ptr3 = 0; len3 = 0;
294+
throw takeFromExternrefTable0(ret[2]);
295+
}
296+
deferred4_0 = ptr3;
297+
deferred4_1 = len3;
298+
return getStringFromWasm0(ptr3, len3);
299+
} finally {
300+
wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
301+
}
302+
}
303+
275304
const ChunkerFinalization = (typeof FinalizationRegistry === 'undefined')
276305
? { register: () => {}, unregister: () => {} }
277306
: new FinalizationRegistry(ptr => wasm.__wbg_chunker_free(ptr >>> 0, 1));

packages/hub/src/vendor/xet-chunk/chunker_wasm_bg.wasm.base64.ts

Lines changed: 1770 additions & 1756 deletions
Large diffs are not rendered by default.

packages/hub/src/vendor/xet-chunk/chunker_wasm_bg.wasm.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const chunker_finish: (a: number) => [number, number, number];
88
export const compute_xorb_hash: (a: any) => [number, number, number, number];
99
export const compute_file_hash: (a: any) => [number, number, number, number];
1010
export const compute_verification_hash: (a: number, b: number) => [number, number, number, number];
11+
export const compute_hmac: (a: number, b: number, c: number, d: number) => [number, number, number, number];
1112
export const __wbindgen_malloc: (a: number, b: number) => number;
1213
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
1314
export const __wbindgen_exn_store: (a: number) => void;

0 commit comments

Comments
 (0)