@hugomrdias/commp-wasm
v0.1.5
Published
Fast CommP (Filecoin Piece Commitment) WASM implementation with inline base64
Maintainers
Readme
@hugomrdias/commp-wasm
Fast CommP (Filecoin Piece Commitment) in Rust/WASM with 4-lane SIMD SHA-256. The WASM is inlined as base64, so there is no async init and no bundler configuration.
Install
npm install @hugomrdias/commp-wasmUsage
import { create, digest, root } from '@hugomrdias/commp-wasm'
// One-shot: just the 32-byte root
const rootHash = root(data)
// One-shot: full multihash digest with metadata
const result = digest(data)
// Streaming: for large files
const hasher = create()
for (const chunk of chunks) {
hasher.write(chunk)
}
const streamResult = hasher.digest()
hasher.free() // Free WASM memory when doneContent-Security-Policy
In browsers, a page whose Content-Security-Policy sets script-src must allow 'wasm-unsafe-eval' for hashing to work:
Content-Security-Policy: script-src 'self' 'wasm-unsafe-eval'Without it, importing still works, but the first hash throws an error that names the missing directive, with the browser's CompileError as its cause.
Cloudflare Workers
workerd only compiles WebAssembly from bytes while a Worker starts up, so the inline base64 can't be used when the package is evaluated later (a dynamic import, a lazily loaded route chunk, or @cloudflare/vitest-plugin). For workerd the package exports an entry, under the workerd condition, that imports commp_bg.wasm instead. Wrangler, @cloudflare/vite-plugin and @cloudflare/vitest-plugin pick it up and compile the .wasm ahead of time, so no configuration is needed.
See the repository README for the full API and benchmarks.
License
MIT
