UNPKG

ipfs-http-client

Version:
30 lines 1.06 kB
import { configure } from '../lib/configure.js'; import { resolve } from '../lib/resolve.js'; import first from 'it-first'; import last from 'it-last'; import errCode from 'err-code'; import { createGet as createBlockGet } from '../block/get.js'; export const createGet = (codecs, options) => { const fn = configure((api, opts) => { const getBlock = createBlockGet(opts); const get = async (cid, options = {}) => { if (options.path) { const entry = options.localResolve ? await first(resolve(cid, options.path, codecs, getBlock, options)) : await last(resolve(cid, options.path, codecs, getBlock, options)); const result = entry; if (!result) { throw errCode(new Error('Not found'), 'ERR_NOT_FOUND'); } return result; } const codec = await codecs.getCodec(cid.code); const block = await getBlock(cid, options); const node = codec.decode(block); return { value: node, remainderPath: '' }; }; return get; }); return fn(options); };