kernel-s5-client/src/index.ts

56 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-09-01 13:40:08 +00:00
import { factory, NetworkClient } from "@lumeweb/libkernel/module";
import type { SignedRegistryEntry } from "@lumeweb/libs5";
2023-11-17 14:07:32 +00:00
export const MODULE = "zrjLjKVByzt233rfcjWvTQXrMfGFa11oBLydPaUk7gwnC2d";
2023-09-01 13:40:08 +00:00
export interface RegistryEntry {
key: Uint8Array;
data: Uint8Array;
revision: number;
}
export class S5Client extends NetworkClient {
public async getRegistryEntry(pubkey: Uint8Array | Buffer | string) {
return this.callModuleReturn("getRegistryEntry", { pubkey });
}
public async setRegistryEntry({
key,
data,
revision,
}: RegistryEntry): Promise<SignedRegistryEntry> {
return this.callModuleReturn("setRegistryEntry", { key, data, revision });
}
public registrySubscription(
pubkey: Uint8Array,
cb: (sre: SignedRegistryEntry) => void,
): () => void {
let done = false;
const [end, ret] = this.connectModule(
"registrySubscription",
{ pubkey },
cb,
);
ret.then((ret) => {
this.handleError(ret);
});
return () => {
if (done) {
return;
}
done = true;
end();
};
}
2023-11-17 14:07:32 +00:00
public async cat(cid: string) {
return this.callModuleReturn("cat", { cid });
}
2023-09-01 13:40:08 +00:00
}
export const createClient = factory<S5Client>(S5Client, MODULE);