kubo-rpc-client-esm-cjs
Version:
A client library for the Kubo RPC API
81 lines • 4.07 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolve = void 0;
const cid_1 = require("multiformats/cid");
const err_code_1 = __importDefault(require("err-code"));
/**
* Retrieves IPLD Nodes along the `path` that is rooted at `cid`.
*
* @param {CID} cid - the CID where the resolving starts
* @param {string} path - the path that should be resolved
* @param {import('../types').Multicodecs} codecs
* @param {(cid: CID, options?: import('../types').AbortOptions) => Promise<Uint8Array>} getBlock
* @param {import('../types').AbortOptions} [options]
*/
function resolve(cid, path, codecs, getBlock, options) {
return __asyncGenerator(this, arguments, function* resolve_1() {
/**
* @param {CID} cid
*/
const load = (cid) => __awaiter(this, void 0, void 0, function* () {
const codec = yield codecs.getCodec(cid.code);
const block = yield getBlock(cid, options);
return codec.decode(block);
});
const parts = path.split('/').filter(Boolean);
let value = yield __await(load(cid));
let lastCid = cid;
// End iteration if there isn't a CID to follow any more
while (parts.length) {
const key = parts.shift();
if (!key) {
throw (0, err_code_1.default)(new Error(`Could not resolve path "${path}"`), 'ERR_INVALID_PATH');
}
if (Object.prototype.hasOwnProperty.call(value, key)) {
value = value[key];
yield yield __await({
value,
remainderPath: parts.join('/')
});
}
else {
throw (0, err_code_1.default)(new Error(`no link named "${key}" under ${lastCid}`), 'ERR_NO_LINK');
}
const cid = cid_1.CID.asCID(value);
if (cid) {
lastCid = cid;
value = yield __await(load(value));
}
}
yield yield __await({
value,
remainderPath: ''
});
});
}
exports.resolve = resolve;
//# sourceMappingURL=resolve.js.map