JavaScript implementation of the UnixFs importer used by IPFS
The npm package "ipfs-unixfs-importer" is a critical tool within the InterPlanetary File System (IPFS) ecosystem, designed specifically to handle the importing of files and directory structures into the IPFS network using the UnixFS (Unix File System) format. This JavaScript implementation plays a pivotal role in ensuring that data added to IPFS maintains its structure and metadata effectively, mimicking traditional UNIX file systems. One of the primary benefits of using the "ipfs-unixfs-importer" is its ability to break down large files into smaller, manageable blocks, which are then hashed and stored in a decentralized manner. This not only enhances data retrieval speeds across the network but also boosts data redundancy and resilience, crucial for maintaining data integrity in a distributed system.
For developers looking to integrate this functionality into their projects, the process is straightforward. Simply run the command `npm install ipfs-unixfs-importer` in your project's root directory to add the package. This installation adds robust file handling capabilities to any application, enabling it to leverage the decentralized file storage benefits of IPFS. Once installed, developers can programmatically import files to IPFS, maintaining all the necessary attributes like file hierarchies and permissions which are essential for complex systems. This functionality is particularly beneficial for applications that need to handle large volumes of data or require a high degree of data availability and integrity.
The "ipfs-unixfs-importer" is not only about importing files efficiently but also about enhancing the scalability and performance of applications. By utilizing this package, developers can significantly reduce the overhead associated with traditional file uploads and storage, since IPFS nodes can cache files and serve them closer to the user, reducing latency and server load. This makes the "ipfs-unixfs-importer" an invaluable tool for building modern, efficient web applications that need to handle large datasets or provide fast, reliable access to a broad user base across different geographies.
Core dependencies of this npm package and its dev dependencies.
aegir, npm-run-all
A README file for the ipfs-unixfs-importer code repository. View Code
JavaScript implementation of the UnixFs importer used by IPFS
Let's create a little directory to import:
> cd /tmp
> mkdir foo
> echo 'hello' > foo/bar
> echo 'world' > foo/quux
And write the importing logic:
import { importer } from 'ipfs-unixfs-importer'
import { MemoryBlockstore } from 'blockstore-core/memory'
import * as fs from 'node:fs'
// Where the blocks will be stored
const blockstore = new MemoryBlockstore()
// Import path /tmp/foo/
const source = [{
path: '/tmp/foo/bar',
content: fs.createReadStream('/tmp/foo/bar')
}, {
path: '/tmp/foo/quxx',
content: fs.createReadStream('/tmp/foo/quux')
}]
for await (const entry of importer(source, blockstore)) {
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
}
$ npm i ipfs-unixfs-importer
<script>
tagLoading this module through a script tag will make it's exports available as IpfsUnixfsImporter
in the global namespace.
<script src="https://unpkg.com/ipfs-unixfs-importer/dist/index.min.js"></script>
Licensed under either of
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.