@helia/verified-fetch
Version:
A fetch-like API for obtaining verified & trustless IPFS content on the web
27 lines • 1.07 kB
JavaScript
import { CID } from 'multiformats/cid';
import { parseUrlString } from './parse-url-string.js';
/**
* Handles the different use cases for the `resource` argument.
* The resource can represent an IPFS path, IPNS path, or CID.
* If the resource represents an IPNS path, we need to resolve it to a CID.
*/
export async function parseResource(resource, { ipns, logger }, { withServerTiming = false, ...options } = { withServerTiming: false }) {
if (typeof resource === 'string') {
return parseUrlString({ urlString: resource, ipns, logger, withServerTiming }, options);
}
const cid = CID.asCID(resource);
if (cid != null) {
// an actual CID
return {
cid,
protocol: 'ipfs',
path: '',
query: {},
ipfsPath: `/ipfs/${cid.toString()}`,
ttl: 29030400, // 1 year for ipfs content
serverTimings: []
};
}
throw new TypeError(`Invalid resource. Cannot determine CID from resource: ${resource}`);
}
//# sourceMappingURL=parse-resource.js.map