Last active
February 19, 2025 04:03
-
-
Save nichoth/4a94ba83ca05abfa7b5f6fa2e65ba00c to your computer and use it in GitHub Desktop.
Blake3 streaming + hashing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as blake3 from 'blake3'; | |
async function streamHash(stream:ReadableStream<Uint8Array>):Promise<string> { | |
const hasher = blake3.createHash(); | |
const reader = stream.getReader(); | |
while (true) { | |
const { done, value } = await reader.read(); | |
if (done) { | |
break; | |
} | |
hasher.update(value); | |
} | |
return hasher.digest('hex'); | |
} | |
// Example usage with a ReadableStream (e.g., from a fetch response): | |
async function hashFileFromUrl(url:string):Promise<string> { | |
const response = await fetch(url); | |
if (!response.body) { | |
throw new Error("Response body is null"); | |
} | |
return streamHash(response.body); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment