The UnixFS Exporter provides a means to read DAGs from a blockstore given a CID.
// import a file and export it againimport { importer } from 'ipfs-unixfs-importer'import { exporter } from 'ipfs-unixfs-exporter'import { MemoryBlockstore } from 'blockstore-core/memory'// Should contain the blocks we are trying to exportconst blockstore = new MemoryBlockstore()const files = []for await (const file of importer([{ path: '/foo/bar.txt', content: new Uint8Array([0, 1, 2, 3])}], blockstore)) { files.push(file)}console.info(files[0].cid) // Qmbazconst entry = await exporter(files[0].cid, blockstore)if (entry.type !== 'file') { throw new Error('Unexpected entry type')}console.info(entry.cid) // Qmquxconsole.info(entry.path) // Qmbaz/foo/bar.txtconsole.info(entry.name) // bar.txtconsole.info(entry.unixfs.fileSize()) // 4// stream content from unixfs nodeconst size = entry.unixfs.fileSize()const bytes = new Uint8Array(Number(size))let offset = 0for await (const buf of entry.content()) { bytes.set(buf, offset) offset += buf.byteLength}console.info(bytes) // 0, 1, 2, 3 Copy
// import a file and export it againimport { importer } from 'ipfs-unixfs-importer'import { exporter } from 'ipfs-unixfs-exporter'import { MemoryBlockstore } from 'blockstore-core/memory'// Should contain the blocks we are trying to exportconst blockstore = new MemoryBlockstore()const files = []for await (const file of importer([{ path: '/foo/bar.txt', content: new Uint8Array([0, 1, 2, 3])}], blockstore)) { files.push(file)}console.info(files[0].cid) // Qmbazconst entry = await exporter(files[0].cid, blockstore)if (entry.type !== 'file') { throw new Error('Unexpected entry type')}console.info(entry.cid) // Qmquxconsole.info(entry.path) // Qmbaz/foo/bar.txtconsole.info(entry.name) // bar.txtconsole.info(entry.unixfs.fileSize()) // 4// stream content from unixfs nodeconst size = entry.unixfs.fileSize()const bytes = new Uint8Array(Number(size))let offset = 0for await (const buf of entry.content()) { bytes.set(buf, offset) offset += buf.byteLength}console.info(bytes) // 0, 1, 2, 3
The UnixFS Exporter provides a means to read DAGs from a blockstore given a CID.
Example