2023-03-31 16:37:44 +00:00
|
|
|
import { Client, factory } from "@lumeweb/libkernel-universal";
|
|
|
|
import defer from "p-defer";
|
|
|
|
export class IPFSClient extends Client {
|
|
|
|
async ready() {
|
|
|
|
return this.callModuleReturn("ready");
|
|
|
|
}
|
2023-04-01 17:40:08 +00:00
|
|
|
async stat(cid, options) {
|
|
|
|
return this.callModuleReturn("stat", { options });
|
2023-03-31 16:37:44 +00:00
|
|
|
}
|
2023-04-01 17:40:08 +00:00
|
|
|
ls(cid, options) {
|
|
|
|
return this.connectModuleGenerator("ls", { cid, options });
|
2023-03-31 16:37:44 +00:00
|
|
|
}
|
2023-04-01 17:40:08 +00:00
|
|
|
cat(cid, options) {
|
|
|
|
return this.connectModuleGenerator("cat", { cid, options });
|
2023-03-31 16:37:44 +00:00
|
|
|
}
|
|
|
|
async ipns(cid) {
|
|
|
|
return this.callModuleReturn("ipnsResolve");
|
|
|
|
}
|
|
|
|
async activePeers() {
|
|
|
|
return this.callModuleReturn("getActivePeers");
|
|
|
|
}
|
|
|
|
connectModuleGenerator(method, data) {
|
|
|
|
const pipe = defer();
|
|
|
|
let done = false;
|
|
|
|
const [update, result] = this.connectModule(method, data, (item) => {
|
|
|
|
pipe.resolve(item);
|
|
|
|
});
|
|
|
|
(async () => {
|
|
|
|
const ret = await result;
|
|
|
|
done = true;
|
|
|
|
this.handleError(ret);
|
|
|
|
})();
|
|
|
|
return {
|
|
|
|
abort() {
|
|
|
|
update();
|
|
|
|
},
|
|
|
|
// @ts-ignore
|
|
|
|
iterable: async function* () {
|
|
|
|
// @ts-ignore
|
|
|
|
const iterator = (await pipe.promise)[Symbol.asyncIterator]();
|
|
|
|
for await (const value of iterator) {
|
|
|
|
yield value;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2023-04-04 11:26:42 +00:00
|
|
|
export const createClient = factory(IPFSClient, "AABspp2cAyOVG2ows3KA6J9-frjdfOmyvGR8Wc0nELi4eA");
|