UNPKG

@helia/verified-fetch

Version:

A fetch-like API for obtaining verified & trustless IPFS content on the web

46 lines 1.59 kB
import { NotUnixFSError } from '@helia/unixfs/errors'; import { exporter, recursive } from 'ipfs-unixfs-exporter'; import map from 'it-map'; import { pipe } from 'it-pipe'; import { pack } from 'it-tar'; const EXPORTABLE = ['file', 'raw', 'directory']; function toHeader(file) { let mode; let mtime; if (file.type === 'file' || file.type === 'directory') { mode = file.unixfs.mode; mtime = file.unixfs.mtime != null ? new Date(Number(file.unixfs.mtime.secs * 1000n)) : undefined; } return { name: file.path, mode, mtime, size: Number(file.size), type: file.type === 'directory' ? 'directory' : 'file' }; } function toTarImportCandidate(entry) { if (!EXPORTABLE.includes(entry.type)) { throw new NotUnixFSError(`${entry.type} is not a UnixFS node`); } const candidate = { header: toHeader(entry) }; if (entry.type === 'file' || entry.type === 'raw') { candidate.body = entry.content(); } return candidate; } export async function* tarStream(ipfsPath, blockstore, options) { const file = await exporter(ipfsPath, blockstore, options); if (file.type === 'file' || file.type === 'raw') { yield* pipe([toTarImportCandidate(file)], pack()); return; } if (file.type === 'directory') { yield* pipe(recursive(ipfsPath, blockstore, options), (source) => map(source, (entry) => toTarImportCandidate(entry)), pack()); return; } throw new NotUnixFSError('Not a UnixFS node'); } //# sourceMappingURL=get-tar-stream.js.map