Skip to content

Commit

Permalink
feat: export options to importer
Browse files Browse the repository at this point in the history
  • Loading branch information
clostao committed Sep 4, 2024
1 parent 3037bd3 commit e88be2b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
8 changes: 8 additions & 0 deletions packages/ipfs-unixfs-importer/src/dag-builder/dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ export interface DirBuilderOptions {
signal?: AbortSignal
}

export interface DirBuilder {
(
dir: Directory,
blockstore: WritableStorage,
options: DirBuilderOptions
): Promise<InProgressImportResult>
}

export const defaultDirBuilder = async (
dir: Directory,
blockstore: WritableStorage,
Expand Down
8 changes: 8 additions & 0 deletions packages/ipfs-unixfs-importer/src/dag-builder/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ const reduce = (
return reducer
}

export interface FileBuilder {
(
file: File,
blockstore: WritableStorage,
options: FileBuilderOptions
): Promise<InProgressImportResult>
}

export interface FileBuilderOptions
extends BuildFileBatchOptions,
ReduceOptions {
Expand Down
16 changes: 4 additions & 12 deletions packages/ipfs-unixfs-importer/src/dag-builder/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import errCode from 'err-code'
import { CustomProgressEvent } from 'progress-events'
import { defaultDirBuilder, type DirBuilderOptions } from './dir.js'
import { defaultFileBuilder, type FileBuilderOptions } from './file.js'
import { defaultDirBuilder, type DirBuilder, type DirBuilderOptions } from './dir.js'
import { defaultFileBuilder, type FileBuilder, type FileBuilderOptions } from './file.js'
import type { ChunkValidator } from './validate-chunks.js'
import type { Chunker } from '../chunker/index.js'
import type {
Expand Down Expand Up @@ -77,16 +77,8 @@ export interface DagBuilderOptions
chunker: Chunker
chunkValidator: ChunkValidator
wrapWithDirectory: boolean
dirBuilder?(
dir: Directory,
blockstore: WritableStorage,
options: DirBuilderOptions
): Promise<InProgressImportResult>
fileBuilder?(
file: File,
blockstore: WritableStorage,
options: FileBuilderOptions
): Promise<InProgressImportResult>
dirBuilder?: DirBuilder
fileBuilder?: FileBuilder
}

export type ImporterSourceStream =
Expand Down
23 changes: 20 additions & 3 deletions packages/ipfs-unixfs-importer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ import { type ChunkValidator, defaultChunkValidator } from './dag-builder/valida
import { balanced, type FileLayout } from './layout/index.js'
import { defaultTreeBuilder } from './tree-builder.js'
import type { Chunker } from './chunker/index.js'
import type { ReducerProgressEvents } from './dag-builder/file.js'
import type { DirBuilder } from './dag-builder/dir.js'
import type { FileBuilder, ReducerProgressEvents } from './dag-builder/file.js'
import type { Blockstore } from 'interface-blockstore'
import type { AwaitIterable } from 'interface-store'
import type { UnixFS, Mtime } from 'ipfs-unixfs'
Expand Down Expand Up @@ -270,6 +271,20 @@ export interface ImporterOptions extends ProgressOptions<ImporterProgressEvents>
* `Error`
*/
chunkValidator?: ChunkValidator

/**
* This option can be used to override how a directory IPLD node is built.
*
* This function takes a `Directory` object and returns a `Promise` that resolves to an `InProgressImportResult`.
*/
dirBuilder?: DirBuilder

/**
* This option can be used to override how a file IPLD node is built.
*
* This function takes a `File` object and returns a `Promise` that resolves to an `InProgressImportResult`.
*/
fileBuilder?: FileBuilder
}

export type ImportCandidateStream = AsyncIterable<FileCandidate | DirectoryCandidate> | Iterable<FileCandidate | DirectoryCandidate>
Expand Down Expand Up @@ -319,7 +334,7 @@ export async function * importer (source: ImportCandidateStream, blockstore: Wri
const fileImportConcurrency = options.fileImportConcurrency ?? 50
const blockWriteConcurrency = options.blockWriteConcurrency ?? 10
const reduceSingleLeafToSelf = options.reduceSingleLeafToSelf ?? true


const chunker = options.chunker ?? fixedSize()
const chunkValidator = options.chunkValidator ?? defaultChunkValidator()
Expand All @@ -337,7 +352,9 @@ export async function * importer (source: ImportCandidateStream, blockstore: Wri
blockWriteConcurrency,
reduceSingleLeafToSelf,
cidVersion,
onProgress: options.onProgress
onProgress: options.onProgress,
dirBuilder: options.dirBuilder,
fileBuilder: options.fileBuilder
})
const buildTree: TreeBuilder = options.treeBuilder ?? defaultTreeBuilder({
wrapWithDirectory,
Expand Down

0 comments on commit e88be2b

Please sign in to comment.