From bda0af49faedced803df88be85b12bfbfbb20d51 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 31 Mar 2023 12:37:44 -0400 Subject: [PATCH] *Update dist --- dist/index.d.ts | 22 ++++++--- dist/index.d.ts.map | 2 +- dist/index.js | 106 ++++++++++++++++++-------------------------- 3 files changed, 61 insertions(+), 69 deletions(-) diff --git a/dist/index.d.ts b/dist/index.d.ts index 00971c6..2b12473 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,7 +1,17 @@ -import type { DataFn } from "libskynet"; -export declare function refreshGatewayList(): Promise; -export declare function fetchIpfs(hash: string, path: string | undefined, receiveUpdate: DataFn): Promise; -export declare function statIpfs(hash: string, path?: string): Promise; -export declare function fetchIpns(hash: string, path: string | undefined, receiveUpdate: DataFn): Promise; -export declare function statIpns(hash: string, path?: string): Promise; +import { Client } from "@lumeweb/libkernel-universal"; +interface AbortableGenerator { + abort: () => void; + iterable: AsyncGenerator; +} +export declare class IPFSClient extends Client { + ready(): Promise; + stat(cid: string): Promise; + ls(cid: string): AbortableGenerator; + cat(cid: string): AbortableGenerator; + ipns(cid: string): Promise; + activePeers(): Promise; + private connectModuleGenerator; +} +export declare const createClient: (...args: any) => IPFSClient; +export {}; //# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/dist/index.d.ts.map b/dist/index.d.ts.map index 04787a3..4aa0f40 100644 --- a/dist/index.d.ts.map +++ b/dist/index.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAsBxC,wBAAsB,kBAAkB,iBAQvC;AAED,wBAAsB,SAAS,CAC7B,IAAI,EAAE,MAAM,EACZ,IAAI,oBAAK,EACT,aAAa,EAAE,MAAM,gBAMtB;AAED,wBAAsB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,SAAK,gBAKrD;AAED,wBAAsB,SAAS,CAC7B,IAAI,EAAE,MAAM,EACZ,IAAI,oBAAK,EACT,aAAa,EAAE,MAAM,gBAMtB;AAED,wBAAsB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,SAAK,gBAKrD"} \ No newline at end of file +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAW,MAAM,8BAA8B,CAAC;AAG/D,UAAU,kBAAkB;IAC1B,KAAK,EAAE,MAAM,IAAI,CAAC;IAClB,QAAQ,EAAE,cAAc,CAAC,MAAM,CAAC,CAAC;CAClC;AAED,qBAAa,UAAW,SAAQ,MAAM;IACvB,KAAK;IAIL,IAAI,CAAC,GAAG,EAAE,MAAM;IAItB,EAAE,CAAC,GAAG,EAAE,MAAM,GAAG,kBAAkB;IAInC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,kBAAkB;IAI9B,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAIlC,WAAW,IAAI,OAAO,CAAC,MAAM,CAAC;IAI3C,OAAO,CAAC,sBAAsB;CAgC/B;AAED,eAAO,MAAM,YAAY,8BAGxB,CAAC"} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index a05ec9c..5c4763b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,66 +1,48 @@ -import { ipnsPath, ipfsPath } from "is-ipfs"; -const IPFS_MODULE = "AQDr2iGYEiMKIdb14w7dxwxFYBo3LaYc0mAuRKXsF2w9OQ"; -let callModule, connectModule; -async function loadLibs() { - if (callModule && connectModule) { - return; +import { Client, factory } from "@lumeweb/libkernel-universal"; +import defer from "p-defer"; +export class IPFSClient extends Client { + async ready() { + return this.callModuleReturn("ready"); } - if (typeof window !== "undefined" && window?.document) { - const pkg = await import("libkernel"); - callModule = pkg.callModule; - connectModule = pkg.connectModule; + async stat(cid) { + return this.callModuleReturn("stat"); } - else { - const pkg = await import("libkmodule"); - callModule = pkg.callModule; - connectModule = pkg.connectModule; + ls(cid) { + return this.connectModuleGenerator("ls", { cid }); + } + cat(cid) { + return this.connectModuleGenerator("cat", { cid }); + } + 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; + } + }, + }; } } -export async function refreshGatewayList() { - const [resp, err] = await doCall("refreshGatewayList"); - if (err) { - throw new Error(err); - } - return resp; -} -export async function fetchIpfs(hash, path = "", receiveUpdate) { - if (!ipfsPath(`/ipfs/${hash}`)) { - throw new Error("Invalid hash"); - } - return doFetch("fetchIpfs", { hash, path }, receiveUpdate); -} -export async function statIpfs(hash, path = "") { - if (!ipfsPath(`/ipfs/${hash}`)) { - throw new Error("Invalid hash"); - } - return doFetch("statIpfs", { hash, path }); -} -export async function fetchIpns(hash, path = "", receiveUpdate) { - if (!ipnsPath(`/ipns/{${hash}`)) { - throw new Error("Invalid hash"); - } - return doFetch("fetchIpns", { hash, path }, receiveUpdate); -} -export async function statIpns(hash, path = "") { - if (!ipnsPath(`/ipns/{${hash}`)) { - throw new Error("Invalid hash"); - } - return doFetch("statIpns", { hash, path }); -} -async function doFetch(method, data, receiveUpdate) { - let [resp, err] = await doCall(method, data, receiveUpdate); - if (typeof err?.then === "function") { - [resp, err] = await err; - } - if (err) { - throw new Error(err); - } - return resp; -} -async function doCall(method, data, receiveUpdate) { - await loadLibs(); - if (receiveUpdate) { - return connectModule(IPFS_MODULE, method, data, receiveUpdate); - } - return callModule(IPFS_MODULE, method, data); -} +export const createClient = factory(IPFSClient, "_AkimjN5qo5cYklxdBNszsxh6VXgypNZVq4zk_BIS3s76A");