kubo-rpc-client-esm-cjs
Version:
A client library for the Kubo RPC API
146 lines • 7.38 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 __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 __asyncDelegator = (this && this.__asyncDelegator) || function (o) {
var i, p;
return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; }
};
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");
const stat_js_1 = require("./files/stat.js");
exports.createLs = (0, configure_js_1.configure)((api, opts) => {
/**
* @type {import('./types').RootAPI["ls"]}
*/
function ls(path, options = {}) {
return __asyncGenerator(this, arguments, function* ls_1() {
var _a, e_1, _b, _c;
const pathStr = `${path instanceof Uint8Array ? cid_1.CID.decode(path) : path}`;
/**
* @param {*} link
*/
function mapLink(link) {
return __awaiter(this, void 0, void 0, function* () {
let hash = link.Hash;
if (hash.includes('/')) {
// the hash is a path, but we need the CID
const ipfsPath = hash.startsWith('/ipfs/') ? hash : `/ipfs/${hash}`;
const stats = yield (0, stat_js_1.createStat)(opts)(ipfsPath);
hash = stats.cid;
}
else {
hash = cid_1.CID.parse(hash);
}
/** @type {import('./types').IPFSEntry} */
const entry = {
name: link.Name,
path: pathStr + (link.Name ? `/${link.Name}` : ''),
size: link.Size,
cid: hash,
type: typeOf(link)
};
if (link.Mode) {
entry.mode = parseInt(link.Mode, 8);
}
if (link.Mtime !== undefined && link.Mtime !== null) {
entry.mtime = {
secs: link.Mtime
};
if (link.MtimeNsecs !== undefined && link.MtimeNsecs !== null) {
entry.mtime.nsecs = link.MtimeNsecs;
}
}
return entry;
});
}
const res = yield __await(api.post('ls', {
signal: options.signal,
searchParams: (0, to_url_search_params_js_1.toUrlSearchParams)(Object.assign({ arg: pathStr }, options)),
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 {
let result = _c;
result = result.Objects;
if (!result) {
throw new Error('expected .Objects in results');
}
result = result[0];
if (!result) {
throw new Error('expected one array in results.Objects');
}
const links = result.Links;
if (!Array.isArray(links)) {
throw new Error('expected one array in results.Objects[0].Links');
}
if (!links.length) {
// no links, this is a file, yield a single result
yield yield __await(mapLink(result));
return yield __await(void 0);
}
yield __await(yield* __asyncDelegator(__asyncValues(links.map(mapLink))));
}
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;
});
/**
* @param {any} link
*/
function typeOf(link) {
switch (link.Type) {
case 1:
case 5:
return 'dir';
case 2:
return 'file';
default:
return 'file';
}
}
//# sourceMappingURL=ls.js.map