@@ -16,13 +16,21 @@ import type { AwaitIterable } from 'blockstore-core/base'
1616export type ByteStream = AwaitIterable < Uint8Array >
1717export type ImportContent = ByteStream | Uint8Array
1818
19- export interface ImportCandidate {
19+ export interface FileCandidate {
2020 path ?: string
21- content ? : ImportContent
21+ content : ImportContent
2222 mtime ?: Mtime
2323 mode ?: number
2424}
2525
26+ export interface DirectoryCandidate {
27+ path : string
28+ mtime ?: Mtime
29+ mode ?: number
30+ }
31+
32+ export type ImportCandidate = FileCandidate | DirectoryCandidate
33+
2634export interface File {
2735 content : AsyncIterable < Uint8Array >
2836 path ?: string
@@ -180,7 +188,7 @@ export interface ImporterOptions {
180188 chunkValidator ?: ChunkValidator
181189}
182190
183- export type ImportCandidateStream = AsyncIterable < ImportCandidate > | Iterable < ImportCandidate >
191+ export type ImportCandidateStream = AsyncIterable < FileCandidate | DirectoryCandidate > | Iterable < FileCandidate | DirectoryCandidate >
184192
185193/**
186194 * The importer creates UnixFS DAGs and stores the blocks that make
@@ -210,7 +218,7 @@ export type ImportCandidateStream = AsyncIterable<ImportCandidate> | Iterable<Im
210218 * ```
211219 */
212220export async function * importer ( source : ImportCandidateStream , blockstore : Blockstore , options : ImporterOptions = { } ) : AsyncGenerator < ImportResult , void , unknown > {
213- let candidates : AsyncIterable < ImportCandidate > | Iterable < ImportCandidate >
221+ let candidates : AsyncIterable < FileCandidate | DirectoryCandidate > | Iterable < FileCandidate | DirectoryCandidate >
214222
215223 if ( Symbol . asyncIterator in source || Symbol . iterator in source ) {
216224 candidates = source
@@ -261,28 +269,59 @@ export async function * importer (source: ImportCandidateStream, blockstore: Blo
261269}
262270
263271/**
264- * `importContent ` is similar to `importer` except it accepts a single
265- * `ImportCandidate ` and returns a promise of a single `ImportResult`
272+ * `importFile ` is similar to `importer` except it accepts a single
273+ * `FileCandidate ` and returns a promise of a single `ImportResult`
266274 * instead of a stream of results.
267275 *
268276 * @example
269277 *
270278 * ```typescript
271- * import { importOne } from 'ipfs-unixfs-importer'
279+ * import { importFile } from 'ipfs-unixfs-importer'
272280 * import { MemoryBlockstore } from 'blockstore-core'
273281 *
274282 * // store blocks in memory, other blockstores are available
275283 * const blockstore = new MemoryBlockstore()
276284 *
277- * const input = {
285+ * const input: FileCandidate = {
278286 * path: './foo.txt',
279287 * content: Uint8Array.from([0, 1, 2, 3, 4])
280288 * }
281289 *
282- * const entry = await importContent(input, blockstore)
290+ * const entry = await importFile(input, blockstore)
291+ * ```
292+ */
293+ export async function importFile ( content : FileCandidate , blockstore : Blockstore , options : ImporterOptions = { } ) : Promise < ImportResult > {
294+ const result = await first ( importer ( [ content ] , blockstore , options ) )
295+
296+ if ( result == null ) {
297+ throw errcode ( new Error ( 'Nothing imported' ) , 'ERR_INVALID_PARAMS' )
298+ }
299+
300+ return result
301+ }
302+
303+ /**
304+ * `importDir` is similar to `importer` except it accepts a single
305+ * `DirectoryCandidate` and returns a promise of a single `ImportResult`
306+ * instead of a stream of results.
307+ *
308+ * @example
309+ *
310+ * ```typescript
311+ * import { importDirectory } from 'ipfs-unixfs-importer'
312+ * import { MemoryBlockstore } from 'blockstore-core'
313+ *
314+ * // store blocks in memory, other blockstores are available
315+ * const blockstore = new MemoryBlockstore()
316+ *
317+ * const input: DirectoryCandidate = {
318+ * path: './foo.txt'
319+ * }
320+ *
321+ * const entry = await importDirectory(input, blockstore)
283322 * ```
284323 */
285- export async function importContent ( content : ImportCandidate , blockstore : Blockstore , options : ImporterOptions = { } ) : Promise < ImportResult > {
324+ export async function importDirectory ( content : DirectoryCandidate , blockstore : Blockstore , options : ImporterOptions = { } ) : Promise < ImportResult > {
286325 const result = await first ( importer ( [ content ] , blockstore , options ) )
287326
288327 if ( result == null ) {
@@ -299,7 +338,7 @@ export async function importContent (content: ImportCandidate, blockstore: Block
299338 * @example
300339 *
301340 * ```typescript
302- * import { importOne } from 'ipfs-unixfs-importer'
341+ * import { importBytes } from 'ipfs-unixfs-importer'
303342 * import { MemoryBlockstore } from 'blockstore-core'
304343 *
305344 * // store blocks in memory, other blockstores are available
@@ -311,7 +350,7 @@ export async function importContent (content: ImportCandidate, blockstore: Block
311350 * ```
312351 */
313352export async function importBytes ( buf : ImportContent , blockstore : Blockstore , options : ImporterOptions = { } ) : Promise < ImportResult > {
314- return await importContent ( {
353+ return await importFile ( {
315354 content : buf
316355 } , blockstore , options )
317356}
@@ -323,7 +362,7 @@ export async function importBytes (buf: ImportContent, blockstore: Blockstore, o
323362 * @example
324363 *
325364 * ```typescript
326- * import { importOne } from 'ipfs-unixfs-importer'
365+ * import { importByteStream } from 'ipfs-unixfs-importer'
327366 * import { MemoryBlockstore } from 'blockstore-core'
328367 *
329368 * // store blocks in memory, other blockstores are available
@@ -338,7 +377,7 @@ export async function importBytes (buf: ImportContent, blockstore: Blockstore, o
338377 * ```
339378 */
340379export async function importByteStream ( bufs : ByteStream , blockstore : Blockstore , options : ImporterOptions = { } ) : Promise < ImportResult > {
341- return await importContent ( {
380+ return await importFile ( {
342381 content : bufs
343382 } , blockstore , options )
344383}
0 commit comments