ipfs-http-client
Version:
A client library for the IPFS HTTP API
50 lines (43 loc) • 1.4 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var cid = require('multiformats/cid');
var errCode = require('err-code');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var errCode__default = /*#__PURE__*/_interopDefaultLegacy(errCode);
async function* resolve(cid$1, path, codecs, getBlock, options) {
const load = async cid => {
const codec = await codecs.getCodec(cid.code);
const block = await getBlock(cid, options);
return codec.decode(block);
};
const parts = path.split('/').filter(Boolean);
let value = await load(cid$1);
let lastCid = cid$1;
if (!parts.length) {
yield {
value,
remainderPath: ''
};
}
while (parts.length) {
const key = parts.shift();
if (!key) {
throw errCode__default["default"](new Error(`Could not resolve path "${ path }"`), 'ERR_INVALID_PATH');
}
if (Object.prototype.hasOwnProperty.call(value, key)) {
value = value[key];
yield {
value,
remainderPath: parts.join('/')
};
} else {
throw errCode__default["default"](new Error(`no link named "${ key }" under ${ lastCid }`), 'ERR_NO_LINK');
}
const cid$1 = cid.CID.asCID(value);
if (cid$1) {
lastCid = cid$1;
value = await load(value);
}
}
}
exports.resolve = resolve;