2023-07-23 17:41:18 +00:00
|
|
|
import { Client, factory, NetworkClient } from "@lumeweb/libkernel/module";
|
2023-03-31 16:36:53 +00:00
|
|
|
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-07-14 08:58:45 +00:00
|
|
|
export const MODULE =
|
2023-09-02 17:17:11 +00:00
|
|
|
"z3o47iXr8DSRHot9iFsqoWRQjXkGbDuFVt1LqMYWpcvJMqq3t2qkePnYG8MM";
|
2023-07-14 08:58:45 +00:00
|
|
|
|
2023-07-23 17:41:18 +00:00
|
|
|
export class IPFSClient extends NetworkClient {
|
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,
|
2023-07-14 08:55:44 +00:00
|
|
|
data: any,
|
2023-03-31 16:36:53 +00:00
|
|
|
): AbortableGenerator {
|
2023-04-09 20:19:41 +00:00
|
|
|
let pipe = defer<Uint8Array>();
|
2023-03-31 16:36:53 +00:00
|
|
|
|
|
|
|
const [update, result] = this.connectModule(method, data, (item: any) => {
|
|
|
|
pipe.resolve(item);
|
|
|
|
});
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
const ret = await result;
|
|
|
|
this.handleError(ret);
|
2023-04-17 06:04:10 +00:00
|
|
|
pipe.resolve(undefined);
|
2023-03-31 16:36:53 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
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 06:04:10 +00:00
|
|
|
const chunk = await pipe.promise;
|
|
|
|
if (chunk === undefined) {
|
2023-04-17 00:55:34 +00:00
|
|
|
return {
|
2023-04-17 06:04:10 +00:00
|
|
|
value: new Uint8Array(),
|
|
|
|
done: true,
|
2023-04-17 00:55:34 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-09 20:19:41 +00:00
|
|
|
pipe = defer();
|
|
|
|
|
2023-04-17 06:10:43 +00:00
|
|
|
update("next");
|
|
|
|
|
2023-04-09 20:19:41 +00:00
|
|
|
return {
|
|
|
|
value: chunk,
|
2023-04-17 06:04:10 +00:00
|
|
|
done: false,
|
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-07-14 08:58:45 +00:00
|
|
|
export const createClient = factory<IPFSClient>(IPFSClient, MODULE);
|