Skip to content

Commit 4b83a19

Browse files
authored
fix: export importFile and importDirectory function (#284)
These have slightly more obvious names.
1 parent f74a850 commit 4b83a19

File tree

3 files changed

+67
-19
lines changed

3 files changed

+67
-19
lines changed

packages/ipfs-unixfs-importer/README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
- [Example](#example)
1515
- [API](#api)
1616
- [const stream = importer(source, blockstore \[, options\])](#const-stream--importersource-blockstore--options)
17-
- [const result = await importContent(content, blockstore \[, options\])](#const-result--await-importcontentcontent-blockstore--options)
17+
- [const result = await importFile(content, blockstore \[, options\])](#const-result--await-importfilecontent-blockstore--options)
18+
- [const result = await importDirectory(content, blockstore \[, options\])](#const-result--await-importdirectorycontent-blockstore--options)
1819
- [const result = await importBytes(buf, blockstore \[, options\])](#const-result--await-importbytesbuf-blockstore--options)
1920
- [const result = await importByteStream(source, blockstore \[, options\])](#const-result--await-importbytestreamsource-blockstore--options)
2021
- [API Docs](#api-docs)
@@ -97,7 +98,7 @@ When run, metadata about DAGNodes in the created tree is printed until the root:
9798
## API
9899

99100
```js
100-
import { importer, importContent, importBytes } from 'ipfs-unixfs-importer'
101+
import { importer, importFile, importDir, importBytes, importByteStream } from 'ipfs-unixfs-importer'
101102
```
102103

103104
### const stream = importer(source, blockstore \[, options])
@@ -119,10 +120,14 @@ The `importer` function returns an async iterator takes a source async iterator
119120

120121
The input's file paths and directory structure will be preserved in the [`dag-pb`](https://github.com/ipld/js-dag-pb) created nodes.
121122

122-
### const result = await importContent(content, blockstore \[, options])
123+
### const result = await importFile(content, blockstore \[, options])
123124

124125
A convenience function for importing a single file or directory.
125126

127+
### const result = await importDirectory(content, blockstore \[, options])
128+
129+
A convenience function for importing a directory - note this is non-recursive, to import recursively use the [importer](#const-stream--importersource-blockstore--options) function.
130+
126131
### const result = await importBytes(buf, blockstore \[, options])
127132

128133
A convenience function for importing a single Uint8Array.

packages/ipfs-unixfs-importer/src/dag-builder/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { dirBuilder, DirBuilderOptions } from './dir.js'
22
import { fileBuilder, FileBuilderOptions } from './file.js'
33
import errCode from 'err-code'
4-
import type { Directory, File, ImportCandidate, InProgressImportResult } from '../index.js'
4+
import type { Directory, File, FileCandidate, ImportCandidate, InProgressImportResult } from '../index.js'
55
import type { Blockstore } from 'interface-blockstore'
66
import type { ChunkValidator } from './validate-chunks.js'
77
import type { Chunker } from '../chunker/index.js'
@@ -59,7 +59,7 @@ export function defaultDagBuilder (options: DagBuilderOptions): DAGBuilder {
5959
.join('/')
6060
}
6161

62-
if (entry.content != null) {
62+
if (isFileCandidate(entry)) {
6363
const file: File = {
6464
path: entry.path,
6565
mtime: entry.mtime,
@@ -84,3 +84,7 @@ export function defaultDagBuilder (options: DagBuilderOptions): DAGBuilder {
8484
}
8585
}
8686
}
87+
88+
function isFileCandidate (entry: any): entry is FileCandidate {
89+
return entry.content != null
90+
}

packages/ipfs-unixfs-importer/src/index.ts

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,21 @@ import type { AwaitIterable } from 'blockstore-core/base'
1616
export type ByteStream = AwaitIterable<Uint8Array>
1717
export 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+
2634
export 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
*/
212220
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>
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
*/
313352
export 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
*/
340379
export 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

Comments
 (0)