-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add methods to import data (#3)
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
1 parent
08eccf9
commit 917a564
Showing
3 changed files
with
525 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.