From e88be2be38771ed050e7046ccd7d47a84f8fd57e Mon Sep 17 00:00:00 2001 From: Carlos Lostao Date: Wed, 4 Sep 2024 23:28:36 +0200 Subject: [PATCH] feat: export options to importer --- .../src/dag-builder/dir.ts | 8 +++++++ .../src/dag-builder/file.ts | 8 +++++++ .../src/dag-builder/index.ts | 16 ++++--------- packages/ipfs-unixfs-importer/src/index.ts | 23 ++++++++++++++++--- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/packages/ipfs-unixfs-importer/src/dag-builder/dir.ts b/packages/ipfs-unixfs-importer/src/dag-builder/dir.ts index eda5c9dd..593f41a3 100644 --- a/packages/ipfs-unixfs-importer/src/dag-builder/dir.ts +++ b/packages/ipfs-unixfs-importer/src/dag-builder/dir.ts @@ -13,6 +13,14 @@ export interface DirBuilderOptions { signal?: AbortSignal } +export interface DirBuilder { + ( + dir: Directory, + blockstore: WritableStorage, + options: DirBuilderOptions + ): Promise +} + export const defaultDirBuilder = async ( dir: Directory, blockstore: WritableStorage, diff --git a/packages/ipfs-unixfs-importer/src/dag-builder/file.ts b/packages/ipfs-unixfs-importer/src/dag-builder/file.ts index e100229e..5117db01 100644 --- a/packages/ipfs-unixfs-importer/src/dag-builder/file.ts +++ b/packages/ipfs-unixfs-importer/src/dag-builder/file.ts @@ -232,6 +232,14 @@ const reduce = ( return reducer } +export interface FileBuilder { + ( + file: File, + blockstore: WritableStorage, + options: FileBuilderOptions + ): Promise +} + export interface FileBuilderOptions extends BuildFileBatchOptions, ReduceOptions { diff --git a/packages/ipfs-unixfs-importer/src/dag-builder/index.ts b/packages/ipfs-unixfs-importer/src/dag-builder/index.ts index 94f722e2..2531eff0 100644 --- a/packages/ipfs-unixfs-importer/src/dag-builder/index.ts +++ b/packages/ipfs-unixfs-importer/src/dag-builder/index.ts @@ -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 { @@ -77,16 +77,8 @@ export interface DagBuilderOptions chunker: Chunker chunkValidator: ChunkValidator wrapWithDirectory: boolean - dirBuilder?( - dir: Directory, - blockstore: WritableStorage, - options: DirBuilderOptions - ): Promise - fileBuilder?( - file: File, - blockstore: WritableStorage, - options: FileBuilderOptions - ): Promise + dirBuilder?: DirBuilder + fileBuilder?: FileBuilder } export type ImporterSourceStream = diff --git a/packages/ipfs-unixfs-importer/src/index.ts b/packages/ipfs-unixfs-importer/src/index.ts index 39dcd5bb..4ea3740e 100644 --- a/packages/ipfs-unixfs-importer/src/index.ts +++ b/packages/ipfs-unixfs-importer/src/index.ts @@ -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' @@ -270,6 +271,20 @@ export interface ImporterOptions extends ProgressOptions * `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 | Iterable @@ -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() @@ -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,