@helia/verified-fetch
Version:
A fetch-like API for obtaining verified & trustless IPFS content on the web
45 lines • 1.71 kB
JavaScript
import { DoesNotExistError } from '@helia/unixfs/errors';
import { AbortError } from '@libp2p/interface';
import { walkPath as exporterWalk } from 'ipfs-unixfs-exporter';
import { badGatewayResponse, notFoundResponse } from './responses.js';
async function walkPath(blockstore, path, options) {
const ipfsRoots = [];
let terminalElement;
for await (const entry of exporterWalk(path, blockstore, options)) {
ipfsRoots.push(entry.cid);
terminalElement = entry;
}
if (terminalElement == null) {
throw new DoesNotExistError('No terminal element found');
}
return {
ipfsRoots,
terminalElement
};
}
export function isObjectNode(node) {
return node.type === 'object';
}
/**
* Attempts to walk the path in the blockstore, returning ipfsRoots needed to resolve the path, and the terminal element.
* If the signal is aborted, the function will throw an AbortError
* If a terminal element is not found, a notFoundResponse is returned
* If another unknown error occurs, a badGatewayResponse is returned
*
*/
export async function handlePathWalking({ cid, path, resource, options, blockstore, log }) {
try {
return await walkPath(blockstore, `${cid.toString()}/${path}`, options);
}
catch (err) {
if (options?.signal?.aborted) {
throw new AbortError(options?.signal?.reason);
}
if (['ERR_NO_PROP', 'ERR_NO_TERMINAL_ELEMENT', 'ERR_NOT_FOUND'].includes(err.code)) {
return notFoundResponse(resource);
}
log.error('error walking path %s', path, err);
return badGatewayResponse(resource, 'Error walking path');
}
}
//# sourceMappingURL=walk-path.js.map