Skip to content

Commit

Permalink
fix: add methods to import data (#3)
Browse files Browse the repository at this point in the history
In order to have a more consistent developer experience, wrap the `ipfs-unixfs-importer` methods to allow adding data from the `UnixFS` interface.
  • Loading branch information
achingbrain authored Feb 24, 2023
1 parent 08eccf9 commit 917a564
Show file tree
Hide file tree
Showing 3 changed files with 525 additions and 3 deletions.
34 changes: 34 additions & 0 deletions src/commands/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { CID } from 'multiformats/cid'
import type { Blockstore } from 'interface-blockstore'
import { ByteStream, DirectoryCandidate, FileCandidate, importBytes, importByteStream, ImportCandidateStream, importDirectory, importer, ImporterOptions, importFile, ImportResult } from 'ipfs-unixfs-importer'

export async function * addAll (source: ImportCandidateStream, blockstore: Blockstore, options: Partial<ImporterOptions> = {}): AsyncGenerator<ImportResult, void, unknown> {
yield * importer(source, blockstore, options)
}

export async function addBytes (bytes: Uint8Array, blockstore: Blockstore, options: Partial<ImporterOptions> = {}): Promise<CID> {
const { cid } = await importBytes(bytes, blockstore, options)

return cid
}

export async function addByteStream (bytes: ByteStream, blockstore: Blockstore, options: Partial<ImporterOptions> = {}): Promise<CID> {
const { cid } = await importByteStream(bytes, blockstore, options)

return cid
}

export async function addFile (file: FileCandidate, blockstore: Blockstore, options: Partial<ImporterOptions> = {}): Promise<CID> {
const { cid } = await importFile(file, blockstore, options)

return cid
}

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

return cid
}
Loading

0 comments on commit 917a564

Please sign in to comment.