Skip to content

Commit

Permalink
feat: use wasm piece hasher (#195)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Alan Shaw authored Jun 20, 2024
1 parent c3cda34 commit 8ddc4d2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import ora from 'ora'
import { CID } from 'multiformats/cid'
import { base64 } from 'multiformats/bases/base64'
import { identity } from 'multiformats/hashes/identity'
import * as Digest from 'multiformats/hashes/digest'
import * as DID from '@ipld/dag-ucan/did'
import * as dagJSON from '@ipld/dag-json'
import { CarWriter } from '@ipld/car'
import { filesFromPaths } from 'files-from-path'
import * as PieceHasher from 'fr32-sha2-256-trunc254-padded-binary-tree-multihash'
import * as Account from './account.js'

import { spaceAccess } from '@web3-storage/w3up-client/capability/access'
import { AgentData } from '@web3-storage/access'
import * as Space from './space.js'
Expand Down Expand Up @@ -157,6 +160,20 @@ export async function upload(firstPath, opts) {
: `Storing ${Math.min(Math.round((totalSent / totalSize) * 100), 100)}%`

const root = await uploadFn({
pieceHasher: {
code: PieceHasher.code,
name: 'fr32-sha2-256-trunc254-padded-binary-tree-multihash',
async digest (input) {
const hasher = PieceHasher.create()
hasher.write(input)

const bytes = new Uint8Array(hasher.multihashByteLength())
hasher.digestInto(bytes, 0, true)
hasher.free()

return Digest.decode(bytes)
}
},
onShardStored: ({ cid, size, piece }) => {
totalSent += size
if (opts?.verbose) {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"ansi-escapes": "^6.2.0",
"chalk": "^5.3.0",
"files-from-path": "^1.0.4",
"fr32-sha2-256-trunc254-padded-binary-tree-multihash": "^3.3.0",
"open": "^9.1.0",
"ora": "^7.0.1",
"pretty-tree": "^1.0.0",
Expand Down

0 comments on commit 8ddc4d2

Please sign in to comment.