kubo-rpc-client-esm-cjs
Version:
A client library for the Kubo RPC API
89 lines • 4.37 kB
JavaScript
;
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
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]); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createLs = void 0;
const cid_1 = require("multiformats/cid");
const configure_js_1 = require("../lib/configure.js");
const to_url_search_params_js_1 = require("../lib/to-url-search-params.js");
/**
* @param {string} type
* @param {string} cid
* @param {Record<string, string>} metadata
*/
function toPin(type, cid, metadata) {
/** @type {import('../types').LsResult} */
const pin = {
type,
cid: cid_1.CID.parse(cid)
};
if (metadata) {
pin.metadata = metadata;
}
return pin;
}
exports.createLs = (0, configure_js_1.configure)(api => {
/**
* @type {import('../types').PinAPI["ls"]}
*/
function ls(options = {}) {
return __asyncGenerator(this, arguments, function* ls_1() {
var _a, e_1, _b, _c;
/** @type {any[]} */
let paths = [];
if (options.paths) {
paths = Array.isArray(options.paths) ? options.paths : [options.paths];
}
const res = yield __await(api.post('pin/ls', {
signal: options.signal,
searchParams: (0, to_url_search_params_js_1.toUrlSearchParams)(Object.assign(Object.assign({}, options), { arg: paths.map(path => `${path}`), stream: true })),
headers: options.headers
}));
try {
for (var _d = true, _e = __asyncValues(res.ndjson()), _f; _f = yield __await(_e.next()), _a = _f.done, !_a;) {
_c = _f.value;
_d = false;
try {
const pin = _c;
if (pin.Keys) { // non-streaming response
for (const cid of Object.keys(pin.Keys)) {
yield yield __await(toPin(pin.Keys[cid].Type, cid, pin.Keys[cid].Metadata));
}
return yield __await(void 0);
}
yield yield __await(toPin(pin.Type, pin.Cid, pin.Metadata));
}
finally {
_d = true;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) yield __await(_b.call(_e));
}
finally { if (e_1) throw e_1.error; }
}
});
}
return ls;
});
//# sourceMappingURL=ls.js.map