@@ -16,13 +16,21 @@ import type { AwaitIterable } from 'blockstore-core/base'
16
16
export type ByteStream = AwaitIterable < Uint8Array >
17
17
export type ImportContent = ByteStream | Uint8Array
18
18
19
- export interface ImportCandidate {
19
+ export interface FileCandidate {
20
20
path ?: string
21
- content ? : ImportContent
21
+ content : ImportContent
22
22
mtime ?: Mtime
23
23
mode ?: number
24
24
}
25
25
26
+ export interface DirectoryCandidate {
27
+ path : string
28
+ mtime ?: Mtime
29
+ mode ?: number
30
+ }
31
+
32
+ export type ImportCandidate = FileCandidate | DirectoryCandidate
33
+
26
34
export interface File {
27
35
content : AsyncIterable < Uint8Array >
28
36
path ?: string
@@ -180,7 +188,7 @@ export interface ImporterOptions {
180
188
chunkValidator ?: ChunkValidator
181
189
}
182
190
183
- export type ImportCandidateStream = AsyncIterable < ImportCandidate > | Iterable < ImportCandidate >
191
+ export type ImportCandidateStream = AsyncIterable < FileCandidate | DirectoryCandidate > | Iterable < FileCandidate | DirectoryCandidate >
184
192
185
193
/**
186
194
* The importer creates UnixFS DAGs and stores the blocks that make
@@ -210,7 +218,7 @@ export type ImportCandidateStream = AsyncIterable<ImportCandidate> | Iterable<Im
210
218
* ```
211
219
*/
212
220
export 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 >
214
222
215
223
if ( Symbol . asyncIterator in source || Symbol . iterator in source ) {
216
224
candidates = source
@@ -261,28 +269,59 @@ export async function * importer (source: ImportCandidateStream, blockstore: Blo
261
269
}
262
270
263
271
/**
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`
266
274
* instead of a stream of results.
267
275
*
268
276
* @example
269
277
*
270
278
* ```typescript
271
- * import { importOne } from 'ipfs-unixfs-importer'
279
+ * import { importFile } from 'ipfs-unixfs-importer'
272
280
* import { MemoryBlockstore } from 'blockstore-core'
273
281
*
274
282
* // store blocks in memory, other blockstores are available
275
283
* const blockstore = new MemoryBlockstore()
276
284
*
277
- * const input = {
285
+ * const input: FileCandidate = {
278
286
* path: './foo.txt',
279
287
* content: Uint8Array.from([0, 1, 2, 3, 4])
280
288
* }
281
289
*
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)
283
322
* ```
284
323
*/
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 > {
286
325
const result = await first ( importer ( [ content ] , blockstore , options ) )
287
326
288
327
if ( result == null ) {
@@ -299,7 +338,7 @@ export async function importContent (content: ImportCandidate, blockstore: Block
299
338
* @example
300
339
*
301
340
* ```typescript
302
- * import { importOne } from 'ipfs-unixfs-importer'
341
+ * import { importBytes } from 'ipfs-unixfs-importer'
303
342
* import { MemoryBlockstore } from 'blockstore-core'
304
343
*
305
344
* // store blocks in memory, other blockstores are available
@@ -311,7 +350,7 @@ export async function importContent (content: ImportCandidate, blockstore: Block
311
350
* ```
312
351
*/
313
352
export async function importBytes ( buf : ImportContent , blockstore : Blockstore , options : ImporterOptions = { } ) : Promise < ImportResult > {
314
- return await importContent ( {
353
+ return await importFile ( {
315
354
content : buf
316
355
} , blockstore , options )
317
356
}
@@ -323,7 +362,7 @@ export async function importBytes (buf: ImportContent, blockstore: Blockstore, o
323
362
* @example
324
363
*
325
364
* ```typescript
326
- * import { importOne } from 'ipfs-unixfs-importer'
365
+ * import { importByteStream } from 'ipfs-unixfs-importer'
327
366
* import { MemoryBlockstore } from 'blockstore-core'
328
367
*
329
368
* // store blocks in memory, other blockstores are available
@@ -338,7 +377,7 @@ export async function importBytes (buf: ImportContent, blockstore: Blockstore, o
338
377
* ```
339
378
*/
340
379
export async function importByteStream ( bufs : ByteStream , blockstore : Blockstore , options : ImporterOptions = { } ) : Promise < ImportResult > {
341
- return await importContent ( {
380
+ return await importFile ( {
342
381
content : bufs
343
382
} , blockstore , options )
344
383
}
0 commit comments