kernel-ipfs-client/dist/index.js

63 lines
2.1 KiB
JavaScript
Raw Normal View History

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", { cid, 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) {
2023-04-09 18:54:00 +00:00
return this.callModuleReturn("ipnsResolve", { cid });
2023-03-31 16:37:44 +00:00
}
async activePeers() {
return this.callModuleReturn("getActivePeers");
}
connectModuleGenerator(method, data) {
2023-04-09 20:20:01 +00:00
let pipe = defer();
2023-03-31 16:37:44 +00:00
const [update, result] = this.connectModule(method, data, (item) => {
pipe.resolve(item);
});
(async () => {
const ret = await result;
this.handleError(ret);
2023-04-17 06:04:29 +00:00
pipe.resolve(undefined);
2023-03-31 16:37:44 +00:00
})();
return {
abort() {
2023-04-09 20:20:01 +00:00
update("abort");
2023-03-31 16:37:44 +00:00
},
2023-04-09 20:20:01 +00:00
iterable() {
return {
[Symbol.asyncIterator]() {
return {
async next() {
2023-04-17 06:04:29 +00:00
const chunk = await pipe.promise;
if (chunk === undefined) {
2023-04-17 00:55:54 +00:00
return {
2023-04-17 06:04:29 +00:00
value: new Uint8Array(),
done: true,
2023-04-17 00:55:54 +00:00
};
}
2023-04-09 20:20:01 +00:00
pipe = defer();
2023-04-17 06:10:57 +00:00
update("next");
2023-04-09 20:20:01 +00:00
return {
value: chunk,
2023-04-17 06:04:29 +00:00
done: false,
2023-04-09 20:20:01 +00:00
};
},
};
},
};
2023-03-31 16:37:44 +00:00
},
};
}
}
2023-04-17 02:45:58 +00:00
export const createClient = factory(IPFSClient, "AAA0F0m8xP2YVcP0YZ-1QT8nLqYPZjgANotOQO3nGST1Bg");