Package Exports
- ipfs-unixfs-importer
Readme
ipfs-unixfs-importer
JavaScript implementation of the UnixFs importer used by IPFS
Table of contents
Install
$ npm i ipfs-unixfs-importerLead Maintainer
Usage
Example
Let's create a little directory to import:
> cd /tmp
> mkdir foo
> echo 'hello' > foo/bar
> echo 'world' > foo/quuxAnd write the importing logic:
import { importer } from 'ipfs-unixfs-importer'
import { MemoryBlockstore } from 'blockstore-core/memory'
// Where the blocks will be stored
const blockstore = new MemoryBlockstore()
// Import path /tmp/foo/bar
const source = [{
path: '/tmp/foo/bar',
content: fs.createReadStream(file)
}, {
path: '/tmp/foo/quxx',
content: fs.createReadStream(file2)
}]
for await (const entry of importer(source, blockstore, options)) {
console.info(entry)
}When run, metadata about DAGNodes in the created tree is printed until the root:
{
cid: CID, // see https://github.com/multiformats/js-cid
path: 'tmp/foo/bar',
unixfs: UnixFS // see https://github.com/ipfs/js-ipfs-unixfs
}
{
cid: CID, // see https://github.com/multiformats/js-cid
path: 'tmp/foo/quxx',
unixfs: UnixFS // see https://github.com/ipfs/js-ipfs-unixfs
}
{
cid: CID, // see https://github.com/multiformats/js-cid
path: 'tmp/foo',
unixfs: UnixFS // see https://github.com/ipfs/js-ipfs-unixfs
}
{
cid: CID, // see https://github.com/multiformats/js-cid
path: 'tmp',
unixfs: UnixFS // see https://github.com/ipfs/js-ipfs-unixfs
}API
import { importer } from 'ipfs-unixfs-importer'const stream = importer(source, blockstore [, options])
The importer function returns an async iterator takes a source async iterator that yields objects of the form:
{
path: 'a name',
content: (Buffer or iterator emitting Buffers),
mtime: (Number representing seconds since (positive) or before (negative) the Unix Epoch),
mode: (Number representing ugo-rwx, setuid, setguid and sticky bit)
}stream will output file info objects as files get stored in IPFS. When stats on a node are emitted they are guaranteed to have been written.
blockstore is an instance of a blockstore
The input's file paths and directory structure will be preserved in the dag-pb created nodes.
options is an JavaScript option that might include the following keys:
wrapWithDirectory(boolean, defaults to false): if true, a wrapping node will be createdshardSplitThreshold(positive integer, defaults to 1000): the number of directory entries above which we decide to use a sharding directory builder (instead of the default flat one)chunker(string, defaults to"fixed"): the chunking strategy. Supports:fixedrabin
avgChunkSize(positive integer, defaults to262144): the average chunk size (rabin chunker only)minChunkSize(positive integer): the minimum chunk size (rabin chunker only)maxChunkSize(positive integer, defaults to262144): the maximum chunk sizestrategy(string, defaults to"balanced"): the DAG builder strategy name. Supports:flat: flat list of chunksbalanced: builds a balanced treetrickle: builds a trickle tree
maxChildrenPerNode(positive integer, defaults to174): the maximum children per node for thebalancedandtrickleDAG builder strategieslayerRepeat(positive integer, defaults to 4): (only applicable to thetrickleDAG builder strategy). The maximum repetition of parent nodes for each layer of the tree.reduceSingleLeafToSelf(boolean, defaults totrue): optimization for, when reducing a set of nodes with one node, reduce it to that node.hamtHashFn(async function(string) Buffer): a function that hashes file names to create HAMT shardshamtBucketBits(positive integer, defaults to8): the number of bits at each bucket of the HAMTprogress(function): a function that will be called with the byte length of chunks as a file is added to ipfs.onlyHash(boolean, defaults to false): Only chunk and hash - do not write to diskhashAlg(string): multihash hashing algorithm to usecidVersion(integer, default 0): the CID version to use when storing the data (storage keys are based on the CID, including it's version)rawLeaves(boolean, defaults to false): When a file would span multiple DAGNodes, if this is true the leaf nodes will not be wrapped inUnixFSprotobufs and will instead contain the raw file bytesleafType(string, defaults to'file') what type of UnixFS node leaves should be - can be'file'or'raw'(ignored whenrawLeavesistrue)blockWriteConcurrency(positive integer, defaults to 10) How many blocks to hash and write to the block store concurrently. For small numbers of large files this should be high (e.g. 50).fileImportConcurrency(number, defaults to 50) How many files to import concurrently. For large numbers of small files this should be high (e.g. 50).
Overriding internals
Several aspects of the importer are overridable by specifying functions as part of the options object with these keys:
chunkValidator(function): Optional function that supports the signatureasync function * (source, options)- This function takes input from the
contentfield of imported entries. It should transform them intoBuffers, throwing an error if it cannot. - It should yield
Bufferobjects constructed from thesourceor throw anError
- This function takes input from the
chunker(function): Optional function that supports the signatureasync function * (source, options)wheresourceis an async generator andoptionsis an options object- It should yield
Bufferobjects.
- It should yield
bufferImporter(function): Optional function that supports the signatureasync function * (entry, blockstore, options)- This function should read
Buffers fromsourceand persist them usingblockstore.putor similar entryis the{ path, content }entry, whereentry.contentis an async generator that yields Buffers- It should yield functions that return a Promise that resolves to an object with the properties
{ cid, unixfs, size }wherecidis a CID,unixfsis a UnixFS entry andsizeis aNumberthat represents the serialized size of the IPLD node that holds the buffer data. - Values will be pulled from this generator in parallel - the amount of parallelisation is controlled by the
blockWriteConcurrencyoption (default: 10)
- This function should read
dagBuilder(function): Optional function that supports the signatureasync function * (source, blockstore, options)- This function should read
{ path, content }entries fromsourceand turn them into DAGs - It should yield a
functionthat returns aPromisethat resolves to{ cid, path, unixfs, node }wherecidis aCID,pathis a string,unixfsis a UnixFS entry andnodeis aDAGNode. - Values will be pulled from this generator in parallel - the amount of parallelisation is controlled by the
fileImportConcurrencyoption (default: 50)
- This function should read
treeBuilder(function): Optional function that supports the signatureasync function * (source, blockstore, options)- This function should read
{ cid, path, unixfs, node }entries fromsourceand place them in a directory structure - It should yield an object with the properties
{ cid, path, unixfs, size }wherecidis aCID,pathis a string,unixfsis a UnixFS entry andsizeis aNumber.
- This function should read
Contribute
Feel free to join in. All welcome. Open an issue!
This repository falls under the IPFS Code of Conduct.
License
Licensed under either of
- Apache 2.0, (LICENSE-APACHE / http://www.apache.org/licenses/LICENSE-2.0)
- MIT (LICENSE-MIT / http://opensource.org/licenses/MIT)
Contribute
Contributions welcome! Please check out the issues.
Also see our contributing document for more information on how we work, and about contributing in general.
Please be aware that all interactions related to this repo are subject to the IPFS Code of Conduct.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
