Skip to content

Commit

Permalink
feat: byo piece hasher
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed Mar 4, 2024
1 parent d6978d7 commit 1275027
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/upload-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"@ucanto/interface": "^9.0.0",
"@ucanto/transport": "^9.1.0",
"@web3-storage/capabilities": "workspace:^",
"fr32-sha2-256-trunc254-padded-binary-tree-multihash": "^3.3.0",
"@web3-storage/data-segment": "^5.1.0",
"ipfs-utils": "^9.0.14",
"multiformats": "^12.1.2",
"p-retry": "^5.1.2",
Expand Down
5 changes: 3 additions & 2 deletions packages/upload-client/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Parallel } from 'parallel-transform-web'
import * as PieceHasher from 'fr32-sha2-256-trunc254-padded-binary-tree-multihash/async'
import * as PieceHasher from '@web3-storage/data-segment/multihash'
import * as Link from 'multiformats/link'
import * as raw from 'multiformats/codecs/raw'
import * as Store from './store.js'
Expand Down Expand Up @@ -123,6 +123,7 @@ async function uploadBlockStream(conf, blocks, options = {}) {
/** @type {import('./types.js').AnyLink?} */
let root = null
const concurrency = options.concurrentRequests ?? CONCURRENT_REQUESTS
const hasher = options.pieceHasher ?? PieceHasher
await blocks
.pipeThrough(new ShardingStream(options))
.pipeThrough(
Expand All @@ -131,7 +132,7 @@ async function uploadBlockStream(conf, blocks, options = {}) {
const [cid, piece] = await Promise.all([
Store.add(conf, bytes, options),
(async () => {
const multihashDigest = await PieceHasher.digest(bytes)
const multihashDigest = await hasher.digest(bytes)
return /** @type {import('@web3-storage/capabilities/types').PieceLink} */ (
Link.create(raw.code, multihashDigest)
)
Expand Down
4 changes: 3 additions & 1 deletion packages/upload-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
FetchOptions as IpfsUtilsFetchOptions,
ProgressStatus as XHRProgressStatus,
} from 'ipfs-utils/src/types.js'
import { Link, UnknownLink, Version } from 'multiformats/link'
import { Link, UnknownLink, Version, MultihashHasher } from 'multiformats'
import { Block } from '@ipld/unixfs'
import {
ServiceMethod,
Expand Down Expand Up @@ -44,6 +44,7 @@ import {
UsageReportSuccess,
UsageReportFailure,
} from '@web3-storage/capabilities/types'
import { code as pieceHashCode } from '@web3-storage/data-segment/multihash'

type Override<T, R> = Omit<T, keyof R> & R

Expand Down Expand Up @@ -271,6 +272,7 @@ export interface UploadOptions
ShardStoringOptions,
UploadProgressTrackable {
onShardStored?: (meta: CARMetadata) => void
pieceHasher?: MultihashHasher<typeof pieceHashCode>
}

export interface UploadDirectoryOptions
Expand Down
18 changes: 11 additions & 7 deletions pnpm-lock.yaml

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

0 comments on commit 1275027

Please sign in to comment.