Skip to content

Commit 5d62dfb

Browse files
authored
fix: relax unixfs blockstore input type (#509)
We only use the get/put/has methods from the blockstore interface so pick those methods to allow easy use from other codebases.
1 parent 58d7ddf commit 5d62dfb

20 files changed

+134
-125
lines changed

packages/unixfs/src/commands/add.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type ByteStream, type DirectoryCandidate, type FileCandidate, importBytes, importByteStream, type ImportCandidateStream, importDirectory, importer, type ImporterOptions, importFile, type ImportResult } from 'ipfs-unixfs-importer'
22
import { fixedSize } from 'ipfs-unixfs-importer/chunker'
33
import { balanced } from 'ipfs-unixfs-importer/layout'
4-
import type { Blockstore } from 'interface-blockstore'
4+
import type { PutStore } from '../unixfs.js'
55
import type { CID } from 'multiformats/cid'
66

77
/**
@@ -18,14 +18,14 @@ const defaultImporterSettings: ImporterOptions = {
1818
})
1919
}
2020

21-
export async function * addAll (source: ImportCandidateStream, blockstore: Blockstore, options: Partial<ImporterOptions> = {}): AsyncGenerator<ImportResult, void, unknown> {
21+
export async function * addAll (source: ImportCandidateStream, blockstore: PutStore, options: Partial<ImporterOptions> = {}): AsyncGenerator<ImportResult, void, unknown> {
2222
yield * importer(source, blockstore, {
2323
...defaultImporterSettings,
2424
...options
2525
})
2626
}
2727

28-
export async function addBytes (bytes: Uint8Array, blockstore: Blockstore, options: Partial<ImporterOptions> = {}): Promise<CID> {
28+
export async function addBytes (bytes: Uint8Array, blockstore: PutStore, options: Partial<ImporterOptions> = {}): Promise<CID> {
2929
const { cid } = await importBytes(bytes, blockstore, {
3030
...defaultImporterSettings,
3131
...options
@@ -34,7 +34,7 @@ export async function addBytes (bytes: Uint8Array, blockstore: Blockstore, optio
3434
return cid
3535
}
3636

37-
export async function addByteStream (bytes: ByteStream, blockstore: Blockstore, options: Partial<ImporterOptions> = {}): Promise<CID> {
37+
export async function addByteStream (bytes: ByteStream, blockstore: PutStore, options: Partial<ImporterOptions> = {}): Promise<CID> {
3838
const { cid } = await importByteStream(bytes, blockstore, {
3939
...defaultImporterSettings,
4040
...options
@@ -43,7 +43,7 @@ export async function addByteStream (bytes: ByteStream, blockstore: Blockstore,
4343
return cid
4444
}
4545

46-
export async function addFile (file: FileCandidate, blockstore: Blockstore, options: Partial<ImporterOptions> = {}): Promise<CID> {
46+
export async function addFile (file: FileCandidate, blockstore: PutStore, options: Partial<ImporterOptions> = {}): Promise<CID> {
4747
const { cid } = await importFile(file, blockstore, {
4848
...defaultImporterSettings,
4949
...options
@@ -52,7 +52,7 @@ export async function addFile (file: FileCandidate, blockstore: Blockstore, opti
5252
return cid
5353
}
5454

55-
export async function addDirectory (dir: Partial<DirectoryCandidate>, blockstore: Blockstore, options: Partial<ImporterOptions> = {}): Promise<CID> {
55+
export async function addDirectory (dir: Partial<DirectoryCandidate>, blockstore: PutStore, options: Partial<ImporterOptions> = {}): Promise<CID> {
5656
const { cid } = await importDirectory({
5757
...dir,
5858
path: dir.path ?? '-'

packages/unixfs/src/commands/cat.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import mergeOpts from 'merge-options'
33
import { NoContentError, NotAFileError } from '../errors.js'
44
import { resolve } from './utils/resolve.js'
55
import type { CatOptions } from '../index.js'
6-
import type { Blockstore } from 'interface-blockstore'
6+
import type { GetStore } from '../unixfs.js'
77
import type { CID } from 'multiformats/cid'
88

99
const mergeOptions = mergeOpts.bind({ ignoreUndefined: true })
@@ -12,7 +12,7 @@ const defaultOptions: CatOptions = {
1212

1313
}
1414

15-
export async function * cat (cid: CID, blockstore: Blockstore, options: Partial<CatOptions> = {}): AsyncIterable<Uint8Array> {
15+
export async function * cat (cid: CID, blockstore: GetStore, options: Partial<CatOptions> = {}): AsyncIterable<Uint8Array> {
1616
const opts: CatOptions = mergeOptions(defaultOptions, options)
1717
const resolved = await resolve(cid, opts.path, blockstore, opts)
1818
const result = await exporter(resolved.cid, blockstore, opts)

packages/unixfs/src/commands/chmod.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import { SHARD_SPLIT_THRESHOLD_BYTES } from './utils/constants.js'
1414
import { persist } from './utils/persist.js'
1515
import { resolve, updatePathCids } from './utils/resolve.js'
1616
import type { ChmodOptions } from '../index.js'
17+
import type { GetStore, PutStore } from '../unixfs.js'
1718
import type { PBNode, PBLink } from '@ipld/dag-pb'
18-
import type { Blockstore } from 'interface-blockstore'
1919

2020
const mergeOptions = mergeOpts.bind({ ignoreUndefined: true })
2121
const log = logger('helia:unixfs:chmod')
@@ -25,7 +25,7 @@ const defaultOptions: ChmodOptions = {
2525
shardSplitThresholdBytes: SHARD_SPLIT_THRESHOLD_BYTES
2626
}
2727

28-
export async function chmod (cid: CID, mode: number, blockstore: Blockstore, options: Partial<ChmodOptions> = {}): Promise<CID> {
28+
export async function chmod (cid: CID, mode: number, blockstore: PutStore & GetStore, options: Partial<ChmodOptions> = {}): Promise<CID> {
2929
const opts: ChmodOptions = mergeOptions(defaultOptions, options)
3030
const resolved = await resolve(cid, opts.path, blockstore, options)
3131

packages/unixfs/src/commands/cp.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { cidToDirectory } from './utils/cid-to-directory.js'
66
import { cidToPBLink } from './utils/cid-to-pblink.js'
77
import { SHARD_SPLIT_THRESHOLD_BYTES } from './utils/constants.js'
88
import type { CpOptions } from '../index.js'
9-
import type { Blockstore } from 'interface-blockstore'
9+
import type { GetStore, PutStore } from '../unixfs.js'
1010
import type { CID } from 'multiformats/cid'
1111

1212
const mergeOptions = mergeOpts.bind({ ignoreUndefined: true })
@@ -17,7 +17,7 @@ const defaultOptions: CpOptions = {
1717
shardSplitThresholdBytes: SHARD_SPLIT_THRESHOLD_BYTES
1818
}
1919

20-
export async function cp (source: CID, target: CID, name: string, blockstore: Blockstore, options: Partial<CpOptions> = {}): Promise<CID> {
20+
export async function cp (source: CID, target: CID, name: string, blockstore: GetStore & PutStore, options: Partial<CpOptions> = {}): Promise<CID> {
2121
const opts: CpOptions = mergeOptions(defaultOptions, options)
2222

2323
if (name.includes('/')) {

packages/unixfs/src/commands/ls.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import mergeOpts from 'merge-options'
33
import { NoContentError, NotADirectoryError } from '../errors.js'
44
import { resolve } from './utils/resolve.js'
55
import type { LsOptions } from '../index.js'
6-
import type { Blockstore } from 'interface-blockstore'
6+
import type { GetStore } from '../unixfs.js'
77
import type { CID } from 'multiformats/cid'
88

99
const mergeOptions = mergeOpts.bind({ ignoreUndefined: true })
@@ -12,7 +12,7 @@ const defaultOptions: LsOptions = {
1212

1313
}
1414

15-
export async function * ls (cid: CID, blockstore: Blockstore, options: Partial<LsOptions> = {}): AsyncIterable<UnixFSEntry> {
15+
export async function * ls (cid: CID, blockstore: GetStore, options: Partial<LsOptions> = {}): AsyncIterable<UnixFSEntry> {
1616
const opts: LsOptions = mergeOptions(defaultOptions, options)
1717
const resolved = await resolve(cid, opts.path, blockstore, opts)
1818
const result = await exporter(resolved.cid, blockstore)

packages/unixfs/src/commands/mkdir.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { cidToDirectory } from './utils/cid-to-directory.js'
1111
import { cidToPBLink } from './utils/cid-to-pblink.js'
1212
import { SHARD_SPLIT_THRESHOLD_BYTES } from './utils/constants.js'
1313
import type { MkdirOptions } from '../index.js'
14-
import type { Blockstore } from 'interface-blockstore'
14+
import type { GetStore, PutStore } from '../unixfs.js'
1515

1616
const mergeOptions = mergeOpts.bind({ ignoreUndefined: true })
1717
const log = logger('helia:unixfs:mkdir')
@@ -22,7 +22,7 @@ const defaultOptions: MkdirOptions = {
2222
shardSplitThresholdBytes: SHARD_SPLIT_THRESHOLD_BYTES
2323
}
2424

25-
export async function mkdir (parentCid: CID, dirname: string, blockstore: Blockstore, options: Partial<MkdirOptions> = {}): Promise<CID> {
25+
export async function mkdir (parentCid: CID, dirname: string, blockstore: GetStore & PutStore, options: Partial<MkdirOptions> = {}): Promise<CID> {
2626
const opts: MkdirOptions = mergeOptions(defaultOptions, options)
2727

2828
if (dirname.includes('/')) {

packages/unixfs/src/commands/rm.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { cidToDirectory } from './utils/cid-to-directory.js'
55
import { SHARD_SPLIT_THRESHOLD_BYTES } from './utils/constants.js'
66
import { removeLink } from './utils/remove-link.js'
77
import type { RmOptions } from '../index.js'
8-
import type { Blockstore } from 'interface-blockstore'
8+
import type { GetStore, PutStore } from '../unixfs.js'
99
import type { CID } from 'multiformats/cid'
1010

1111
const mergeOptions = mergeOpts.bind({ ignoreUndefined: true })
@@ -15,7 +15,7 @@ const defaultOptions: RmOptions = {
1515
shardSplitThresholdBytes: SHARD_SPLIT_THRESHOLD_BYTES
1616
}
1717

18-
export async function rm (target: CID, name: string, blockstore: Blockstore, options: Partial<RmOptions> = {}): Promise<CID> {
18+
export async function rm (target: CID, name: string, blockstore: GetStore & PutStore, options: Partial<RmOptions> = {}): Promise<CID> {
1919
const opts: RmOptions = mergeOptions(defaultOptions, options)
2020

2121
if (name.includes('/')) {

packages/unixfs/src/commands/stat.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import * as raw from 'multiformats/codecs/raw'
77
import { InvalidPBNodeError, NotUnixFSError, UnknownError } from '../errors.js'
88
import { resolve } from './utils/resolve.js'
99
import type { StatOptions, UnixFSStats } from '../index.js'
10+
import type { GetStore, HasStore } from '../unixfs.js'
1011
import type { AbortOptions } from '@libp2p/interface'
11-
import type { Blockstore } from 'interface-blockstore'
1212
import type { Mtime } from 'ipfs-unixfs'
1313
import type { CID } from 'multiformats/cid'
1414

@@ -19,7 +19,7 @@ const defaultOptions: StatOptions = {
1919

2020
}
2121

22-
export async function stat (cid: CID, blockstore: Blockstore, options: Partial<StatOptions> = {}): Promise<UnixFSStats> {
22+
export async function stat (cid: CID, blockstore: GetStore & HasStore, options: Partial<StatOptions> = {}): Promise<UnixFSStats> {
2323
const opts: StatOptions = mergeOptions(defaultOptions, options)
2424
const resolved = await resolve(cid, options.path, blockstore, opts)
2525

@@ -93,7 +93,7 @@ interface InspectDagResults {
9393
blocks: number
9494
}
9595

96-
async function inspectDag (cid: CID, blockstore: Blockstore, options: AbortOptions): Promise<InspectDagResults> {
96+
async function inspectDag (cid: CID, blockstore: GetStore & HasStore, options: AbortOptions): Promise<InspectDagResults> {
9797
const results = {
9898
localFileSize: 0,
9999
localDagSize: 0,

packages/unixfs/src/commands/touch.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import { SHARD_SPLIT_THRESHOLD_BYTES } from './utils/constants.js'
1414
import { persist } from './utils/persist.js'
1515
import { resolve, updatePathCids } from './utils/resolve.js'
1616
import type { TouchOptions } from '../index.js'
17+
import type { GetStore, PutStore } from '../unixfs.js'
1718
import type { PBNode, PBLink } from '@ipld/dag-pb'
18-
import type { Blockstore } from 'interface-blockstore'
1919

2020
const mergeOptions = mergeOpts.bind({ ignoreUndefined: true })
2121
const log = logger('helia:unixfs:touch')
@@ -25,7 +25,7 @@ const defaultOptions: TouchOptions = {
2525
shardSplitThresholdBytes: SHARD_SPLIT_THRESHOLD_BYTES
2626
}
2727

28-
export async function touch (cid: CID, blockstore: Blockstore, options: Partial<TouchOptions> = {}): Promise<CID> {
28+
export async function touch (cid: CID, blockstore: GetStore & PutStore, options: Partial<TouchOptions> = {}): Promise<CID> {
2929
const opts: TouchOptions = mergeOptions(defaultOptions, options)
3030
const resolved = await resolve(cid, opts.path, blockstore, opts)
3131
const mtime = opts.mtime ?? {

packages/unixfs/src/commands/utils/add-link.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import {
1717
} from './hamt-utils.js'
1818
import { isOverShardThreshold } from './is-over-shard-threshold.js'
1919
import type { Directory } from './cid-to-directory.js'
20+
import type { GetStore, PutStore } from '../../unixfs.js'
2021
import type { PBNode, PBLink } from '@ipld/dag-pb/interface'
2122
import type { AbortOptions } from '@libp2p/interface'
22-
import type { Blockstore } from 'interface-blockstore'
2323
import type { ImportResult } from 'ipfs-unixfs-importer'
2424

2525
const log = logger('helia:unixfs:components:utils:add-link')
@@ -35,7 +35,7 @@ export interface AddLinkOptions extends AbortOptions {
3535
cidVersion: Version
3636
}
3737

38-
export async function addLink (parent: Directory, child: Required<PBLink>, blockstore: Blockstore, options: AddLinkOptions): Promise<AddLinkResult> {
38+
export async function addLink (parent: Directory, child: Required<PBLink>, blockstore: GetStore & PutStore, options: AddLinkOptions): Promise<AddLinkResult> {
3939
if (parent.node.Data == null) {
4040
throw new InvalidParametersError('Invalid parent passed to addLink')
4141
}
@@ -63,7 +63,7 @@ export async function addLink (parent: Directory, child: Required<PBLink>, block
6363
return result
6464
}
6565

66-
const convertToShardedDirectory = async (parent: Directory, blockstore: Blockstore): Promise<ImportResult> => {
66+
const convertToShardedDirectory = async (parent: Directory, blockstore: PutStore): Promise<ImportResult> => {
6767
if (parent.node.Data == null) {
6868
throw new InvalidParametersError('Invalid parent passed to convertToShardedDirectory')
6969
}
@@ -85,7 +85,7 @@ const convertToShardedDirectory = async (parent: Directory, blockstore: Blocksto
8585
return result
8686
}
8787

88-
const addToDirectory = async (parent: Directory, child: PBLink, blockstore: Blockstore, options: AddLinkOptions): Promise<AddLinkResult> => {
88+
const addToDirectory = async (parent: Directory, child: PBLink, blockstore: PutStore, options: AddLinkOptions): Promise<AddLinkResult> => {
8989
// Remove existing link if it exists
9090
const parentLinks = parent.node.Links.filter((link) => {
9191
const matches = link.Name === child.Name
@@ -137,7 +137,7 @@ const addToDirectory = async (parent: Directory, child: PBLink, blockstore: Bloc
137137
}
138138
}
139139

140-
const addToShardedDirectory = async (parent: Directory, child: Required<PBLink>, blockstore: Blockstore, options: AddLinkOptions): Promise<AddLinkResult> => {
140+
const addToShardedDirectory = async (parent: Directory, child: Required<PBLink>, blockstore: GetStore & PutStore, options: AddLinkOptions): Promise<AddLinkResult> => {
141141
const { path, hash } = await recreateShardedDirectory(parent.cid, child.Name, blockstore, options)
142142
const finalSegment = path[path.length - 1]
143143

packages/unixfs/src/commands/utils/cid-to-directory.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { exporter, type ExporterOptions } from 'ipfs-unixfs-exporter'
22
import { NotADirectoryError } from '../../errors.js'
3+
import type { GetStore } from '../../unixfs.js'
34
import type { PBNode } from '@ipld/dag-pb'
4-
import type { Blockstore } from 'interface-blockstore'
55
import type { CID } from 'multiformats/cid'
66

77
export interface Directory {
88
cid: CID
99
node: PBNode
1010
}
1111

12-
export async function cidToDirectory (cid: CID, blockstore: Blockstore, options: ExporterOptions = {}): Promise<Directory> {
12+
export async function cidToDirectory (cid: CID, blockstore: GetStore, options: ExporterOptions = {}): Promise<Directory> {
1313
const entry = await exporter(cid, blockstore, options)
1414

1515
if (entry.type !== 'directory') {

packages/unixfs/src/commands/utils/cid-to-pblink.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as dagPb from '@ipld/dag-pb'
22
import { exporter, type ExporterOptions } from 'ipfs-unixfs-exporter'
33
import { NotUnixFSError } from '../../errors.js'
4+
import type { GetStore } from '../../unixfs.js'
45
import type { PBNode, PBLink } from '@ipld/dag-pb'
5-
import type { Blockstore } from 'interface-blockstore'
66
import type { CID } from 'multiformats/cid'
77

8-
export async function cidToPBLink (cid: CID, name: string, blockstore: Blockstore, options?: ExporterOptions): Promise<Required<PBLink>> {
8+
export async function cidToPBLink (cid: CID, name: string, blockstore: GetStore, options?: ExporterOptions): Promise<Required<PBLink>> {
99
const sourceEntry = await exporter(cid, blockstore, options)
1010

1111
if (sourceEntry.type !== 'directory' && sourceEntry.type !== 'file' && sourceEntry.type !== 'raw') {

packages/unixfs/src/commands/utils/dir-sharded.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
hamtHashFn
88
} from './hamt-constants.js'
99
import { persist, type PersistOptions } from './persist.js'
10-
import type { Blockstore } from 'interface-blockstore'
10+
import type { PutStore } from '../../unixfs.js'
1111
import type { Mtime } from 'ipfs-unixfs'
1212

1313
interface InProgressImportResult extends ImportResult {
@@ -69,7 +69,7 @@ abstract class Dir {
6969
abstract put (name: string, value: InProgressImportResult | Dir): Promise<void>
7070
abstract get (name: string): Promise<InProgressImportResult | Dir | undefined>
7171
abstract eachChildSeries (): AsyncIterable<{ key: string, child: InProgressImportResult | Dir }>
72-
abstract flush (blockstore: Blockstore): AsyncGenerator<ImportResult>
72+
abstract flush (blockstore: PutStore): AsyncGenerator<ImportResult>
7373
abstract estimateNodeSize (): number
7474
abstract childCount (): number
7575
}
@@ -129,7 +129,7 @@ export class DirSharded extends Dir {
129129
return this.nodeSize
130130
}
131131

132-
async * flush (blockstore: Blockstore): AsyncGenerator<ImportResult> {
132+
async * flush (blockstore: PutStore): AsyncGenerator<ImportResult> {
133133
for await (const entry of flush(this._bucket, blockstore, this, this.options)) {
134134
yield {
135135
...entry,
@@ -139,7 +139,7 @@ export class DirSharded extends Dir {
139139
}
140140
}
141141

142-
async function * flush (bucket: Bucket<Dir | InProgressImportResult>, blockstore: Blockstore, shardRoot: DirSharded | null, options: PersistOptions): AsyncIterable<ImportResult> {
142+
async function * flush (bucket: Bucket<Dir | InProgressImportResult>, blockstore: PutStore, shardRoot: DirSharded | null, options: PersistOptions): AsyncIterable<ImportResult> {
143143
const children = bucket._children
144144
const links: PBLink[] = []
145145
let childrenSize = 0n

packages/unixfs/src/commands/utils/hamt-utils.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from './hamt-constants.js'
1515
import { persist } from './persist.js'
1616
import type { PersistOptions } from './persist.js'
17+
import type { GetStore, PutStore } from '../../unixfs.js'
1718
import type { AbortOptions } from '@libp2p/interface'
1819
import type { Blockstore } from 'interface-blockstore'
1920
import type { Mtime } from 'ipfs-unixfs'
@@ -40,7 +41,7 @@ export interface CreateShardOptions {
4041
cidVersion: Version
4142
}
4243

43-
export const createShard = async (blockstore: Blockstore, contents: Array<{ name: string, size: bigint, cid: CID }>, options: CreateShardOptions): Promise<ImportResult> => {
44+
export const createShard = async (blockstore: PutStore, contents: Array<{ name: string, size: bigint, cid: CID }>, options: CreateShardOptions): Promise<ImportResult> => {
4445
const shard = new DirSharded({
4546
root: true,
4647
dir: true,
@@ -75,7 +76,7 @@ export interface HAMTPath {
7576
node: dagPB.PBNode
7677
}
7778

78-
export const updateShardedDirectory = async (path: HAMTPath[], blockstore: Blockstore, options: PersistOptions): Promise<{ cid: CID, node: dagPB.PBNode }> => {
79+
export const updateShardedDirectory = async (path: HAMTPath[], blockstore: GetStore & PutStore, options: PersistOptions): Promise<{ cid: CID, node: dagPB.PBNode }> => {
7980
// persist any metadata on the shard root
8081
const shardRoot = UnixFS.unmarshal(path[0].node.Data ?? new Uint8Array(0))
8182

@@ -142,7 +143,7 @@ export const updateShardedDirectory = async (path: HAMTPath[], blockstore: Block
142143
return { cid, node }
143144
}
144145

145-
export const recreateShardedDirectory = async (cid: CID, fileName: string, blockstore: Blockstore, options: AbortOptions): Promise<{ path: HAMTPath[], hash: InfiniteHash }> => {
146+
export const recreateShardedDirectory = async (cid: CID, fileName: string, blockstore: Pick<Blockstore, 'get'>, options: AbortOptions): Promise<{ path: HAMTPath[], hash: InfiniteHash }> => {
146147
const wrapped = wrapHash(hamtHashFn)
147148
const hash = wrapped(uint8ArrayFromString(fileName))
148149
const path: HAMTPath[] = []

0 commit comments

Comments
 (0)