Skip to content

Commit 8ddc4d2

Browse files
author
Alan Shaw
authored
feat: use wasm piece hasher (storacha#195)
We removed this from the client library (and use the JS native version) since there's a miriad of environments that JS runs in that have differing ways to load WASM. However in Node.js it's a captive environment so should load fine and give us a little speedup.
1 parent c3cda34 commit 8ddc4d2

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import ora from 'ora'
55
import { CID } from 'multiformats/cid'
66
import { base64 } from 'multiformats/bases/base64'
77
import { identity } from 'multiformats/hashes/identity'
8+
import * as Digest from 'multiformats/hashes/digest'
89
import * as DID from '@ipld/dag-ucan/did'
910
import * as dagJSON from '@ipld/dag-json'
1011
import { CarWriter } from '@ipld/car'
1112
import { filesFromPaths } from 'files-from-path'
13+
import * as PieceHasher from 'fr32-sha2-256-trunc254-padded-binary-tree-multihash'
1214
import * as Account from './account.js'
15+
1316
import { spaceAccess } from '@web3-storage/w3up-client/capability/access'
1417
import { AgentData } from '@web3-storage/access'
1518
import * as Space from './space.js'
@@ -157,6 +160,20 @@ export async function upload(firstPath, opts) {
157160
: `Storing ${Math.min(Math.round((totalSent / totalSize) * 100), 100)}%`
158161

159162
const root = await uploadFn({
163+
pieceHasher: {
164+
code: PieceHasher.code,
165+
name: 'fr32-sha2-256-trunc254-padded-binary-tree-multihash',
166+
async digest (input) {
167+
const hasher = PieceHasher.create()
168+
hasher.write(input)
169+
170+
const bytes = new Uint8Array(hasher.multihashByteLength())
171+
hasher.digestInto(bytes, 0, true)
172+
hasher.free()
173+
174+
return Digest.decode(bytes)
175+
}
176+
},
160177
onShardStored: ({ cid, size, piece }) => {
161178
totalSent += size
162179
if (opts?.verbose) {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"ansi-escapes": "^6.2.0",
6363
"chalk": "^5.3.0",
6464
"files-from-path": "^1.0.4",
65+
"fr32-sha2-256-trunc254-padded-binary-tree-multihash": "^3.3.0",
6566
"open": "^9.1.0",
6667
"ora": "^7.0.1",
6768
"pretty-tree": "^1.0.0",

0 commit comments

Comments
 (0)