2023-03-31 16:36:53 +00:00
|
|
|
import { Client, factory } from "@lumeweb/libkernel-universal";
|
|
|
|
import defer from "p-defer";
|
2023-04-01 17:39:22 +00:00
|
|
|
import { CatOptions, LsOptions, StatOptions } from "@helia/unixfs";
|
2022-08-05 13:34:50 +00:00
|
|
|
|
2023-03-31 16:36:53 +00:00
|
|
|
interface AbortableGenerator {
|
|
|
|
abort: () => void;
|
2023-04-09 20:19:41 +00:00
|
|
|
iterable: () => AsyncIterable<Uint8Array>;
|
2022-08-05 13:34:50 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 16:36:53 +00:00
|
|
|
export class IPFSClient extends Client {
|
|
|
|
public async ready() {
|
|
|
|
return this.callModuleReturn("ready");
|
2022-08-05 13:34:50 +00:00
|
|
|
}
|
|
|
|
|
2023-04-01 17:39:22 +00:00
|
|
|
public async stat(cid: string, options?: Partial<StatOptions>) {
|
2023-04-09 18:58:46 +00:00
|
|
|
return this.callModuleReturn("stat", { cid, options });
|
2022-08-05 13:34:50 +00:00
|
|
|
}
|
|
|
|
|
2023-04-01 17:39:22 +00:00
|
|
|
public ls(cid: string, options?: Partial<LsOptions>): AbortableGenerator {
|
|
|
|
return this.connectModuleGenerator("ls", { cid, options });
|
2022-08-05 13:34:50 +00:00
|
|
|
}
|
|
|
|
|
2023-04-01 17:39:22 +00:00
|
|
|
public cat(cid: string, options?: Partial<CatOptions>): AbortableGenerator {
|
|
|
|
return this.connectModuleGenerator("cat", { cid, options });
|
2022-08-05 13:34:50 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 16:36:53 +00:00
|
|
|
public async ipns(cid: string): Promise<string> {
|
2023-04-09 18:53:44 +00:00
|
|
|
return this.callModuleReturn("ipnsResolve", { cid });
|
2022-08-05 13:34:50 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 16:36:53 +00:00
|
|
|
public async activePeers(): Promise<number> {
|
|
|
|
return this.callModuleReturn("getActivePeers");
|
2022-08-05 13:34:50 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 16:36:53 +00:00
|
|
|
private connectModuleGenerator(
|
|
|
|
method: string,
|
|
|
|
data: any
|
|
|
|
): AbortableGenerator {
|
2023-04-09 20:19:41 +00:00
|
|
|
let pipe = defer<Uint8Array>();
|
2023-03-31 16:36:53 +00:00
|
|
|
|
|
|
|
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() {
|
2023-04-09 20:19:41 +00:00
|
|
|
update("abort");
|
2023-03-31 16:36:53 +00:00
|
|
|
},
|
2023-04-09 20:19:41 +00:00
|
|
|
|
|
|
|
iterable(): AsyncIterable<Uint8Array> {
|
|
|
|
return {
|
|
|
|
[Symbol.asyncIterator]() {
|
|
|
|
return {
|
|
|
|
async next(): Promise<IteratorResult<Uint8Array>> {
|
2023-04-17 00:55:34 +00:00
|
|
|
if (done) {
|
|
|
|
return {
|
|
|
|
value: undefined,
|
|
|
|
done,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-09 20:19:41 +00:00
|
|
|
const chunk = await pipe.promise;
|
|
|
|
update("next");
|
|
|
|
|
|
|
|
pipe = defer();
|
|
|
|
|
|
|
|
return {
|
|
|
|
value: chunk,
|
2023-04-09 21:15:13 +00:00
|
|
|
done,
|
2023-04-09 20:19:41 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
2023-03-31 16:36:53 +00:00
|
|
|
},
|
|
|
|
};
|
2022-08-05 13:34:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 16:36:53 +00:00
|
|
|
export const createClient = factory<IPFSClient>(
|
|
|
|
IPFSClient,
|
2023-04-17 02:45:45 +00:00
|
|
|
"AAA0F0m8xP2YVcP0YZ-1QT8nLqYPZjgANotOQO3nGST1Bg"
|
2023-03-31 16:36:53 +00:00
|
|
|
);
|