Skip to content

Instantly share code, notes, and snippets.

@nichoth
Last active February 19, 2025 04:03
Show Gist options
  • Save nichoth/4a94ba83ca05abfa7b5f6fa2e65ba00c to your computer and use it in GitHub Desktop.
Save nichoth/4a94ba83ca05abfa7b5f6fa2e65ba00c to your computer and use it in GitHub Desktop.
Blake3 streaming + hashing
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