Compare commits
No commits in common. "bda0af49faedced803df88be85b12bfbfbb20d51" and "db5f00241dfb0c7fa17461edff18d12c46329a53" have entirely different histories.
bda0af49fa
...
db5f00241d
|
@ -1,17 +1,7 @@
|
||||||
import { Client } from "@lumeweb/libkernel-universal";
|
import type { DataFn } from "libskynet";
|
||||||
interface AbortableGenerator {
|
export declare function refreshGatewayList(): Promise<any>;
|
||||||
abort: () => void;
|
export declare function fetchIpfs(hash: string, path: string | undefined, receiveUpdate: DataFn): Promise<any>;
|
||||||
iterable: AsyncGenerator<object>;
|
export declare function statIpfs(hash: string, path?: string): Promise<any>;
|
||||||
}
|
export declare function fetchIpns(hash: string, path: string | undefined, receiveUpdate: DataFn): Promise<any>;
|
||||||
export declare class IPFSClient extends Client {
|
export declare function statIpns(hash: string, path?: string): Promise<any>;
|
||||||
ready(): Promise<any>;
|
|
||||||
stat(cid: string): Promise<any>;
|
|
||||||
ls(cid: string): AbortableGenerator;
|
|
||||||
cat(cid: string): AbortableGenerator;
|
|
||||||
ipns(cid: string): Promise<string>;
|
|
||||||
activePeers(): Promise<number>;
|
|
||||||
private connectModuleGenerator;
|
|
||||||
}
|
|
||||||
export declare const createClient: (...args: any) => IPFSClient;
|
|
||||||
export {};
|
|
||||||
//# sourceMappingURL=index.d.ts.map
|
//# sourceMappingURL=index.d.ts.map
|
|
@ -1 +1 @@
|
||||||
{"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"}
|
{"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"}
|
|
@ -1,48 +1,66 @@
|
||||||
import { Client, factory } from "@lumeweb/libkernel-universal";
|
import { ipnsPath, ipfsPath } from "is-ipfs";
|
||||||
import defer from "p-defer";
|
const IPFS_MODULE = "AQDr2iGYEiMKIdb14w7dxwxFYBo3LaYc0mAuRKXsF2w9OQ";
|
||||||
export class IPFSClient extends Client {
|
let callModule, connectModule;
|
||||||
async ready() {
|
async function loadLibs() {
|
||||||
return this.callModuleReturn("ready");
|
if (callModule && connectModule) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
async stat(cid) {
|
if (typeof window !== "undefined" && window?.document) {
|
||||||
return this.callModuleReturn("stat");
|
const pkg = await import("libkernel");
|
||||||
|
callModule = pkg.callModule;
|
||||||
|
connectModule = pkg.connectModule;
|
||||||
}
|
}
|
||||||
ls(cid) {
|
else {
|
||||||
return this.connectModuleGenerator("ls", { cid });
|
const pkg = await import("libkmodule");
|
||||||
}
|
callModule = pkg.callModule;
|
||||||
cat(cid) {
|
connectModule = pkg.connectModule;
|
||||||
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 const createClient = factory(IPFSClient, "_AkimjN5qo5cYklxdBNszsxh6VXgypNZVq4zk_BIS3s76A");
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -4,14 +4,13 @@
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git",
|
"is-ipfs": "^6.0.2",
|
||||||
"libkernel": "^0.1.43",
|
"libkernel": "^0.1.43",
|
||||||
"libkmodule": "^0.2.44",
|
"libkmodule": "^0.2.44"
|
||||||
"p-defer": "^4.0.0"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^18.0.6",
|
"@types/node": "^18.0.6",
|
||||||
"prettier": "^2.7.1",
|
"libskynet": "^0.0.62",
|
||||||
"typescript": "^5.0.3"
|
"prettier": "^2.7.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
149
src/index.ts
149
src/index.ts
|
@ -1,71 +1,90 @@
|
||||||
import { Client, factory } from "@lumeweb/libkernel-universal";
|
import type { DataFn } from "libskynet";
|
||||||
import defer from "p-defer";
|
import { ipnsPath, ipfsPath } from "is-ipfs";
|
||||||
|
|
||||||
interface AbortableGenerator {
|
const IPFS_MODULE = "AQDr2iGYEiMKIdb14w7dxwxFYBo3LaYc0mAuRKXsF2w9OQ";
|
||||||
abort: () => void;
|
|
||||||
iterable: AsyncGenerator<object>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class IPFSClient extends Client {
|
let callModule: any, connectModule: any;
|
||||||
public async ready() {
|
|
||||||
return this.callModuleReturn("ready");
|
async function loadLibs() {
|
||||||
|
if (callModule && connectModule) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
if (typeof window !== "undefined" && window?.document) {
|
||||||
public async stat(cid: string) {
|
const pkg = await import("libkernel");
|
||||||
return this.callModuleReturn("stat");
|
callModule = pkg.callModule;
|
||||||
}
|
connectModule = pkg.connectModule;
|
||||||
|
} else {
|
||||||
public ls(cid: string): AbortableGenerator {
|
const pkg = await import("libkmodule");
|
||||||
return this.connectModuleGenerator("ls", { cid });
|
callModule = pkg.callModule;
|
||||||
}
|
connectModule = pkg.connectModule;
|
||||||
|
|
||||||
public cat(cid: string): AbortableGenerator {
|
|
||||||
return this.connectModuleGenerator("cat", { cid });
|
|
||||||
}
|
|
||||||
|
|
||||||
public async ipns(cid: string): Promise<string> {
|
|
||||||
return this.callModuleReturn("ipnsResolve");
|
|
||||||
}
|
|
||||||
|
|
||||||
public async activePeers(): Promise<number> {
|
|
||||||
return this.callModuleReturn("getActivePeers");
|
|
||||||
}
|
|
||||||
|
|
||||||
private connectModuleGenerator(
|
|
||||||
method: string,
|
|
||||||
data: any
|
|
||||||
): AbortableGenerator {
|
|
||||||
const pipe = defer();
|
|
||||||
|
|
||||||
let done = false;
|
|
||||||
|
|
||||||
const [update, result] = this.connectModule(method, data, (item: any) => {
|
|
||||||
pipe.resolve(item);
|
|
||||||
});
|
|
||||||
|
|
||||||
(async () => {
|
|
||||||
const ret = await result;
|
|
||||||
done = true;
|
|
||||||
this.handleError(ret);
|
|
||||||
})();
|
|
||||||
|
|
||||||
return {
|
|
||||||
abort() {
|
|
||||||
update();
|
|
||||||
},
|
|
||||||
// @ts-ignore
|
|
||||||
iterable: async function* (): AsyncGenerator<object> {
|
|
||||||
// @ts-ignore
|
|
||||||
const iterator = (await pipe.promise)[Symbol.asyncIterator]();
|
|
||||||
for await (const value of iterator) {
|
|
||||||
yield value as object;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createClient = factory<IPFSClient>(
|
export async function refreshGatewayList() {
|
||||||
IPFSClient,
|
const [resp, err] = await doCall("refreshGatewayList");
|
||||||
"_AkimjN5qo5cYklxdBNszsxh6VXgypNZVq4zk_BIS3s76A"
|
|
||||||
);
|
if (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function fetchIpfs(
|
||||||
|
hash: string,
|
||||||
|
path = "",
|
||||||
|
receiveUpdate: DataFn
|
||||||
|
) {
|
||||||
|
if (!ipfsPath(`/ipfs/${hash}`)) {
|
||||||
|
throw new Error("Invalid hash");
|
||||||
|
}
|
||||||
|
return doFetch("fetchIpfs", { hash, path }, receiveUpdate);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function statIpfs(hash: string, path = "") {
|
||||||
|
if (!ipfsPath(`/ipfs/${hash}`)) {
|
||||||
|
throw new Error("Invalid hash");
|
||||||
|
}
|
||||||
|
return doFetch("statIpfs", { hash, path });
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function fetchIpns(
|
||||||
|
hash: string,
|
||||||
|
path = "",
|
||||||
|
receiveUpdate: DataFn
|
||||||
|
) {
|
||||||
|
if (!ipnsPath(`/ipns/{${hash}`)) {
|
||||||
|
throw new Error("Invalid hash");
|
||||||
|
}
|
||||||
|
return doFetch("fetchIpns", { hash, path }, receiveUpdate);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function statIpns(hash: string, path = "") {
|
||||||
|
if (!ipnsPath(`/ipns/{${hash}`)) {
|
||||||
|
throw new Error("Invalid hash");
|
||||||
|
}
|
||||||
|
return doFetch("statIpns", { hash, path });
|
||||||
|
}
|
||||||
|
|
||||||
|
async function doFetch(method: string, data: any, receiveUpdate?: DataFn) {
|
||||||
|
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: string, data?: any, receiveUpdate?: DataFn) {
|
||||||
|
await loadLibs();
|
||||||
|
if (receiveUpdate) {
|
||||||
|
return connectModule(IPFS_MODULE, method, data, receiveUpdate);
|
||||||
|
}
|
||||||
|
|
||||||
|
return callModule(IPFS_MODULE, method, data);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue